6304: Avoid possible dns update race condition by writing the temp file with unique...
[arvados.git] / services / api / test / unit / node_test.rb
1 require 'test_helper'
2
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))
9     node
10   end
11
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'])
16   end
17
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'])
22   end
23
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"
28   end
29
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))
37     File.unlink conffile
38   end
39
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
48   end
49
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'
54   end
55
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)
63     File.unlink testfile
64   end
65
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'
69   end
70
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'
77   end
78
79   test "dns update with dir configured but no command configured" do
80     Rails.configuration.dns_server_update_command = false
81     Rails.configuration.dns_server_conf_dir = Rails.root.join 'tmp'
82     conffile = Rails.root.join 'tmp', 'compute65535.conf'
83     assert Node.dns_server_update 'compute65535', '127.0.0.127'
84     refute File.exist? conffile
85   end
86
87   test "ping new node with no hostname and default config" do
88     node = ping_node(:new_with_no_hostname, {})
89     slot_number = node.slot_number
90     refute_nil slot_number
91     assert_equal("compute#{slot_number}", node.hostname)
92   end
93
94   test "ping new node with no hostname and no config" do
95     Rails.configuration.assign_node_hostname = false
96     node = ping_node(:new_with_no_hostname, {})
97     refute_nil node.slot_number
98     assert_nil node.hostname
99   end
100
101   test "ping new node with zero padding config" do
102     Rails.configuration.assign_node_hostname = 'compute%<slot_number>04d'
103     node = ping_node(:new_with_no_hostname, {})
104     slot_number = node.slot_number
105     refute_nil slot_number
106     assert_equal("compute000#{slot_number}", node.hostname)
107   end
108
109   test "ping node with hostname and config and expect hostname unchanged" do
110     node = ping_node(:new_with_custom_hostname, {})
111     assert_equal(23, node.slot_number)
112     assert_equal("custom1", node.hostname)
113   end
114
115   test "ping node with hostname and no config and expect hostname unchanged" do
116     Rails.configuration.assign_node_hostname = false
117     node = ping_node(:new_with_custom_hostname, {})
118     assert_equal(23, node.slot_number)
119     assert_equal("custom1", node.hostname)
120   end
121
122   # Ping two nodes: one without a hostname and the other with a hostname.
123   # Verify that the first one gets a hostname and second one is unchanged.
124   test "ping two nodes one with no hostname and one with hostname and check hostnames" do
125     # ping node with no hostname and expect it set with config format
126     node = ping_node(:new_with_no_hostname, {})
127     slot_number = node.slot_number
128     refute_nil node.slot_number
129     assert_equal "compute#{slot_number}", node.hostname
130
131     # ping node with a hostname and expect it to be unchanged
132     node2 = ping_node(:new_with_custom_hostname, {})
133     refute_nil node2.slot_number
134     assert_equal "custom1", node2.hostname
135   end
136
137   test "update dns when nodemanager clears hostname and ip_address" do
138     act_as_system_user do
139       node = ping_node(:new_with_custom_hostname, {})
140       Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
141       node.update_attributes(hostname: nil, ip_address: nil)
142     end
143   end
144
145   test "update dns when hostname changes" do
146     act_as_system_user do
147       node = ping_node(:new_with_custom_hostname, {})
148
149       Node.expects(:dns_server_update).with(node.hostname, Node::UNUSED_NODE_IP)
150       Node.expects(:dns_server_update).with('foo0', node.ip_address)
151       node.update_attributes!(hostname: 'foo0')
152
153       Node.expects(:dns_server_update).with('foo0', Node::UNUSED_NODE_IP)
154       node.update_attributes!(hostname: nil, ip_address: nil)
155
156       Node.expects(:dns_server_update).with('foo0', '10.11.12.13')
157       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.13')
158
159       Node.expects(:dns_server_update).with('foo0', '10.11.12.14')
160       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.14')
161     end
162   end
163
164   test 'newest ping wins IP address conflict' do
165     act_as_system_user do
166       n1, n2 = Node.create!, Node.create!
167
168       n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
169       n1.reload
170
171       Node.expects(:dns_server_update).with(n1.hostname, Node::UNUSED_NODE_IP)
172       Node.expects(:dns_server_update).with(Not(equals(n1.hostname)), '10.5.5.5')
173       n2.ping(ip: '10.5.5.5', ping_secret: n2.info['ping_secret'])
174
175       n1.reload
176       n2.reload
177       assert_nil n1.ip_address
178       assert_equal '10.5.5.5', n2.ip_address
179
180       Node.expects(:dns_server_update).with(n2.hostname, Node::UNUSED_NODE_IP)
181       Node.expects(:dns_server_update).with(n1.hostname, '10.5.5.5')
182       n1.ping(ip: '10.5.5.5', ping_secret: n1.info['ping_secret'])
183
184       n1.reload
185       n2.reload
186       assert_nil n2.ip_address
187       assert_equal '10.5.5.5', n1.ip_address
188     end
189   end
190 end