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