8784: Fix test for latest firefox.
[arvados.git] / services / api / test / unit / node_test.rb
1 require 'test_helper'
2 require 'tmpdir'
3 require 'tempfile'
4
5 class NodeTest < ActiveSupport::TestCase
6   def ping_node(node_name, ping_data)
7     set_user_from_auth :admin
8     node = nodes(node_name)
9     node.ping({ping_secret: node.info['ping_secret'],
10                 ip: node.ip_address}.merge(ping_data))
11     node
12   end
13
14   test "pinging a node can add and update stats" do
15     node = ping_node(:idle, {total_cpu_cores: '12', total_ram_mb: '512'})
16     assert_equal(12, node.properties['total_cpu_cores'])
17     assert_equal(512, node.properties['total_ram_mb'])
18   end
19
20   test "stats disappear if not in a ping" do
21     node = ping_node(:idle, {total_ram_mb: '256'})
22     refute_includes(node.properties, 'total_cpu_cores')
23     assert_equal(256, node.properties['total_ram_mb'])
24   end
25
26   test "worker state is down for node with no slot" do
27     node = nodes(:was_idle_now_down)
28     assert_nil node.slot_number, "fixture is not what I expected"
29     assert_equal 'down', node.crunch_worker_state, "wrong worker state"
30   end
31
32   test "dns_server_conf_template" do
33     Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp'
34     Rails.configuration.dns_server_conf_template = Rails.root.join 'config', 'unbound.template'
35     conffile = Rails.root.join 'tmp', 'compute65535.conf'
36     File.unlink conffile rescue nil
37     assert Node.dns_server_update 'compute65535', '127.0.0.1'
38     assert_match(/\"1\.0\.0\.127\.in-addr\.arpa\. IN PTR compute65535\.zzzzz\.arvadosapi\.com\"/, IO.read(conffile))
39     File.unlink conffile
40   end
41
42   test "dns_server_restart_command" do
43     Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp'
44     Rails.configuration.dns_server_reload_command = 'foobar'
45     restartfile = Rails.root.join 'tmp', 'restart.txt'
46     File.unlink restartfile rescue nil
47     assert Node.dns_server_update 'compute65535', '127.0.0.127'
48     assert_equal "foobar\n", IO.read(restartfile)
49     File.unlink restartfile
50   end
51
52   test "dns_server_restart_command fail" do
53     Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp', 'bogusdir'
54     Rails.configuration.dns_server_reload_command = 'foobar'
55     refute Node.dns_server_update 'compute65535', '127.0.0.127'
56   end
57
58   test "dns_server_update_command with valid command" do
59     testfile = Rails.root.join('tmp', 'node_test_dns_server_update_command.txt')
60     Rails.configuration.dns_server_update_command =
61       ('echo -n "%{hostname} == %{ip_address}" >' +
62        testfile.to_s.shellescape)
63     assert Node.dns_server_update 'compute65535', '127.0.0.1'
64     assert_equal 'compute65535 == 127.0.0.1', IO.read(testfile)
65     File.unlink testfile
66   end
67
68   test "dns_server_update_command with failing command" do
69     Rails.configuration.dns_server_update_command = 'false %{hostname}'
70     refute Node.dns_server_update 'compute65535', '127.0.0.1'
71   end
72
73   test "dns update with no commands/dirs configured" do
74     Rails.configuration.dns_server_update_command = false
75     Rails.configuration.dns_server_conf_dir = false
76     Rails.configuration.dns_server_conf_template = 'ignored!'
77     Rails.configuration.dns_server_reload_command = 'ignored!'
78     assert Node.dns_server_update 'compute65535', '127.0.0.127'
79   end
80
81   test "don't leave temp files behind if there's an error writing them" do
82     Rails.configuration.dns_server_conf_template = Rails.root.join 'config', 'unbound.template'
83     Tempfile.any_instance.stubs(:puts).raises(IOError)
84     Dir.mktmpdir do |tmpdir|
85       Rails.configuration.dns_server_conf_dir = tmpdir
86       refute Node.dns_server_update 'compute65535', '127.0.0.127'
87       assert_empty Dir.entries(tmpdir).select{|f| File.file? f}
88     end
89   end
90
91   test "ping new node with no hostname and default config" do
92     node = ping_node(:new_with_no_hostname, {})
93     slot_number = node.slot_number
94     refute_nil slot_number
95     assert_equal("compute#{slot_number}", node.hostname)
96   end
97
98   test "ping new node with no hostname and no config" do
99     Rails.configuration.assign_node_hostname = false
100     node = ping_node(:new_with_no_hostname, {})
101     refute_nil node.slot_number
102     assert_nil node.hostname
103   end
104
105   test "ping new node with zero padding config" do
106     Rails.configuration.assign_node_hostname = 'compute%<slot_number>04d'
107     node = ping_node(:new_with_no_hostname, {})
108     slot_number = node.slot_number
109     refute_nil slot_number
110     assert_equal("compute000#{slot_number}", node.hostname)
111   end
112
113   test "ping node with hostname and config and expect hostname unchanged" do
114     node = ping_node(:new_with_custom_hostname, {})
115     assert_equal(23, node.slot_number)
116     assert_equal("custom1", node.hostname)
117   end
118
119   test "ping node with hostname and no config and expect hostname unchanged" do
120     Rails.configuration.assign_node_hostname = false
121     node = ping_node(:new_with_custom_hostname, {})
122     assert_equal(23, node.slot_number)
123     assert_equal("custom1", node.hostname)
124   end
125
126   # Ping two nodes: one without a hostname and the other with a hostname.
127   # Verify that the first one gets a hostname and second one is unchanged.
128   test "ping two nodes one with no hostname and one with hostname and check hostnames" do
129     # ping node with no hostname and expect it set with config format
130     node = ping_node(:new_with_no_hostname, {})
131     refute_nil node.slot_number
132     assert_equal "compute#{node.slot_number}", node.hostname
133
134     # ping node with a hostname and expect it to be unchanged
135     node2 = ping_node(:new_with_custom_hostname, {})
136     refute_nil node2.slot_number
137     assert_equal "custom1", node2.hostname
138   end
139
140   test "update dns when nodemanager clears hostname and ip_address" do
141     act_as_system_user do
142       node = ping_node(:new_with_custom_hostname, {})
143       Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
144       node.update_attributes(hostname: nil, ip_address: nil)
145     end
146   end
147
148   test "update dns when hostname changes" do
149     act_as_system_user do
150       node = ping_node(:new_with_custom_hostname, {})
151
152       Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
153       Node.expects(:dns_server_update).with('foo0', node.ip_address)
154       node.update_attributes!(hostname: 'foo0')
155
156       Node.expects(:dns_server_update).with('foo0', Node::UNUSED_NODE_IP)
157       node.update_attributes!(hostname: nil, ip_address: nil)
158
159       Node.expects(:dns_server_update).with('foo0', '10.11.12.13')
160       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.13')
161
162       Node.expects(:dns_server_update).with('foo0', '10.11.12.14')
163       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.14')
164     end
165   end
166
167   test 'newest ping wins IP address conflict' do
168     act_as_system_user do
169       n1, n2 = Node.create!, Node.create!
170
171       n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
172       n1.reload
173
174       Node.expects(:dns_server_update).with(n1.hostname, Node::UNUSED_NODE_IP)
175       Node.expects(:dns_server_update).with(Not(equals(n1.hostname)), '10.5.5.5')
176       n2.ping(ip: '10.5.5.5', ping_secret: n2.info['ping_secret'])
177
178       n1.reload
179       n2.reload
180       assert_nil n1.ip_address
181       assert_equal '10.5.5.5', n2.ip_address
182
183       Node.expects(:dns_server_update).with(n2.hostname, Node::UNUSED_NODE_IP)
184       Node.expects(:dns_server_update).with(n1.hostname, '10.5.5.5')
185       n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
186
187       n1.reload
188       n2.reload
189       assert_nil n2.ip_address
190       assert_equal '10.5.5.5', n1.ip_address
191     end
192   end
193
194   test 'run out of slots' do
195     Rails.configuration.max_compute_nodes = 3
196     act_as_system_user do
197       Node.destroy_all
198       (1..4).each do |i|
199         n = Node.create!
200         args = { ip: "10.0.0.#{i}", ping_secret: n.info['ping_secret'] }
201         if i <= Rails.configuration.max_compute_nodes
202           n.ping(args)
203         else
204           assert_raises do
205             n.ping(args)
206           end
207         end
208       end
209     end
210   end
211 end