10649: Make errors emitted by squeue and scancel show up in logs.
[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 "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)
84   end
85
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
91   end
92
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)
99   end
100
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)
105   end
106
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)
112   end
113
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
122
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
127   end
128
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)
134     end
135   end
136
137   test "update dns when hostname changes" do
138     act_as_system_user do
139       node = ping_node(:new_with_custom_hostname, {})
140
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')
144
145       Node.expects(:dns_server_update).with('foo0', Node::UNUSED_NODE_IP)
146       node.update_attributes!(hostname: nil, ip_address: nil)
147
148       Node.expects(:dns_server_update).with('foo0', '10.11.12.13')
149       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.13')
150
151       Node.expects(:dns_server_update).with('foo0', '10.11.12.14')
152       node.update_attributes!(hostname: 'foo0', ip_address: '10.11.12.14')
153     end
154   end
155 end