3 class NodeTest < ActiveSupport::TestCase
4 def ping_node(node_name, ping_data)
5 set_user_from_auth :admin
6 node = nodes(node_name)
7 node.ping({ping_secret: node.info['ping_secret'],
8 ip: node.ip_address}.merge(ping_data))
12 test "pinging a node can add and update stats" do
13 node = ping_node(:idle, {total_cpu_cores: '12', total_ram_mb: '512'})
14 assert_equal(12, node.properties['total_cpu_cores'])
15 assert_equal(512, node.properties['total_ram_mb'])
18 test "stats disappear if not in a ping" do
19 node = ping_node(:idle, {total_ram_mb: '256'})
20 refute_includes(node.properties, 'total_cpu_cores')
21 assert_equal(256, node.properties['total_ram_mb'])
24 test "worker state is down for node with no slot" do
25 node = nodes(:was_idle_now_down)
26 assert_nil node.slot_number, "fixture is not what I expected"
27 assert_equal 'down', node.crunch_worker_state, "wrong worker state"
30 test "dns_server_conf_template" do
31 Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp'
32 Rails.configuration.dns_server_conf_template = Rails.root.join 'config', 'unbound.template'
33 conffile = Rails.root.join 'tmp', 'compute65535.conf'
34 File.unlink conffile rescue nil
35 assert Node.dns_server_update 'compute65535', '127.0.0.1'
36 assert_match /\"1\.0\.0\.127\.in-addr\.arpa\. IN PTR compute65535\.zzzzz\.arvadosapi\.com\"/, IO.read(conffile)
40 test "dns_server_restart_command" do
41 Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp'
42 Rails.configuration.dns_server_reload_command = 'foobar'
43 restartfile = Rails.root.join 'tmp', 'restart.txt'
44 File.unlink restartfile rescue nil
45 assert Node.dns_server_update 'compute65535', '127.0.0.127'
46 assert_equal "foobar\n", IO.read(restartfile)
47 File.unlink restartfile
50 test "dns_server_restart_command fail" do
51 Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp', 'bogusdir'
52 Rails.configuration.dns_server_reload_command = 'foobar'
53 refute Node.dns_server_update 'compute65535', '127.0.0.127'
56 test "dns_server_update_command with valid command" do
57 testfile = Rails.root.join('tmp', 'node_test_dns_server_update_command.txt')
58 Rails.configuration.dns_server_update_command =
59 ('echo -n "%{hostname} == %{ip_address}" >' +
60 testfile.to_s.shellescape)
61 assert Node.dns_server_update 'compute65535', '127.0.0.1'
62 assert_equal 'compute65535 == 127.0.0.1', IO.read(testfile)
66 test "dns_server_update_command with failing command" do
67 Rails.configuration.dns_server_update_command = 'false %{hostname}'
68 refute Node.dns_server_update 'compute65535', '127.0.0.1'
71 test "dns update with no commands/dirs configured" do
72 Rails.configuration.dns_server_update_command = false
73 Rails.configuration.dns_server_conf_dir = false
74 Rails.configuration.dns_server_conf_template = 'ignored!'
75 Rails.configuration.dns_server_reload_command = 'ignored!'
76 assert Node.dns_server_update 'compute65535', '127.0.0.127'
79 test "ping new node with no hostname and default config" do
80 node = ping_node(:new_with_no_hostname, {})
81 slot_number = node.slot_number
82 refute_nil slot_number
83 assert_equal("compute#{slot_number}", node.hostname)
86 test "ping new node with no hostname and no config" do
87 Rails.configuration.assign_node_hostname = false
88 node = ping_node(:new_with_no_hostname, {})
89 refute_nil node.slot_number
90 assert_nil node.hostname
93 test "ping new node with zero padding config" do
94 Rails.configuration.assign_node_hostname = 'compute%<slot_number>04d'
95 node = ping_node(:new_with_no_hostname, {})
96 slot_number = node.slot_number
97 refute_nil slot_number
98 assert_equal("compute000#{slot_number}", node.hostname)
101 test "ping node with hostname and config and expect hostname unchanged" do
102 node = ping_node(:new_with_custom_hostname, {})
103 assert_equal(23, node.slot_number)
104 assert_equal("custom1", node.hostname)
107 test "ping node with hostname and no config and expect hostname unchanged" do
108 Rails.configuration.assign_node_hostname = false
109 node = ping_node(:new_with_custom_hostname, {})
110 assert_equal(23, node.slot_number)
111 assert_equal("custom1", node.hostname)
114 # Ping two nodes: one without a hostname and the other with a hostname.
115 # Verify that the first one gets a hostname and second one is unchanged.
116 test "ping two nodes one with no hostname and one with hostname and check hostnames" do
117 # ping node with no hostname and expect it set with config format
118 node = ping_node(:new_with_no_hostname, {})
119 slot_number = node.slot_number
120 refute_nil node.slot_number
121 assert_equal "compute#{slot_number}", node.hostname
123 # ping node with a hostname and expect it to be unchanged
124 node2 = ping_node(:new_with_custom_hostname, {})
125 refute_nil node2.slot_number
126 assert_equal "custom1", node2.hostname