6304: Corrected test setup to make it pass.
[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 Dir.entries(tmpdir).select{|f| File.file? f}.empty?
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     slot_number = node.slot_number
132     refute_nil node.slot_number
133     assert_equal "compute#{slot_number}", node.hostname
134
135     # ping node with a hostname and expect it to be unchanged
136     node2 = ping_node(:new_with_custom_hostname, {})
137     refute_nil node2.slot_number
138     assert_equal "custom1", node2.hostname
139   end
140
141   test "update dns when nodemanager clears hostname and ip_address" do
142     act_as_system_user do
143       node = ping_node(:new_with_custom_hostname, {})
144       Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
145       node.update_attributes(hostname: nil, ip_address: nil)
146     end
147   end
148
149   test "update dns when hostname changes" do
150     act_as_system_user do
151       node = ping_node(:new_with_custom_hostname, {})
152
153       Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
154       Node.expects(:dns_server_update).with('foo0', node.ip_address)
155       node.update_attributes!(hostname: 'foo0')
156
157       Node.expects(:dns_server_update).with('foo0', Node::UNUSED_NODE_IP)
158       node.update_attributes!(hostname: nil, ip_address: nil)
159
160       Node.expects(:dns_server_update).with('foo0', '10.11.12.13')
161       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.13')
162
163       Node.expects(:dns_server_update).with('foo0', '10.11.12.14')
164       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.14')
165     end
166   end
167
168   test 'newest ping wins IP address conflict' do
169     act_as_system_user do
170       n1, n2 = Node.create!, Node.create!
171
172       n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
173       n1.reload
174
175       Node.expects(:dns_server_update).with(n1.hostname, Node::UNUSED_NODE_IP)
176       Node.expects(:dns_server_update).with(Not(equals(n1.hostname)), '10.5.5.5')
177       n2.ping(ip: '10.5.5.5', ping_secret: n2.info['ping_secret'])
178
179       n1.reload
180       n2.reload
181       assert_nil n1.ip_address
182       assert_equal '10.5.5.5', n2.ip_address
183
184       Node.expects(:dns_server_update).with(n2.hostname, Node::UNUSED_NODE_IP)
185       Node.expects(:dns_server_update).with(n1.hostname, '10.5.5.5')
186       n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
187
188       n1.reload
189       n2.reload
190       assert_nil n2.ip_address
191       assert_equal '10.5.5.5', n1.ip_address
192     end
193   end
194 end