Merge branch 'master' into 3198-writable-fuse
[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 end