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
129 test "update dns when nodemanager clears hostname and ip_address" do
130 act_as_system_user do
131 node = ping_node(:new_with_custom_hostname, {})
132 Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
133 node.update_attributes(hostname: nil, ip_address: nil)
137 test "update dns when hostname changes" do
138 act_as_system_user do
139 node = ping_node(:new_with_custom_hostname, {})
141 Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
142 Node.expects(:dns_server_update).with('foo0', node.ip_address)
143 node.update_attributes!(hostname: 'foo0')
145 Node.expects(:dns_server_update).with('foo0', Node::UNUSED_NODE_IP)
146 node.update_attributes!(hostname: nil, ip_address: nil)
148 Node.expects(:dns_server_update).with('foo0', '10.11.12.13')
149 node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.13')
151 Node.expects(:dns_server_update).with('foo0', '10.11.12.14')
152 node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.14')
156 test 'newest ping wins IP address conflict' do
157 act_as_system_user do
158 n1, n2 = Node.create!, Node.create!
160 n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
163 Node.expects(:dns_server_update).with(n1.hostname, Node::UNUSED_NODE_IP)
164 Node.expects(:dns_server_update).with(Not(equals(n1.hostname)), '10.5.5.5')
165 n2.ping(ip: '10.5.5.5', ping_secret: n2.info['ping_secret'])
169 assert_nil n1.ip_address
170 assert_equal '10.5.5.5', n2.ip_address
172 Node.expects(:dns_server_update).with(n2.hostname, Node::UNUSED_NODE_IP)
173 Node.expects(:dns_server_update).with(n1.hostname, '10.5.5.5')
174 n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
178 assert_nil n2.ip_address
179 assert_equal '10.5.5.5', n1.ip_address