Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / functional / arvados / v1 / nodes_controller_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
7 class Arvados::V1::NodesControllerTest < ActionController::TestCase
8
9   test "should get index with ping_secret" do
10     authorize_with :admin
11     get :index
12     assert_response :success
13     assert_not_nil assigns(:objects)
14     node_items = JSON.parse(@response.body)['items']
15     assert_not_equal 0, node_items.size
16     assert_not_nil node_items[0]['info'].andand['ping_secret']
17   end
18
19   # inactive user does not see any nodes
20   test "inactive user should get empty index" do
21     authorize_with :inactive
22     get :index
23     assert_response :success
24     assert_equal 0, json_response['items'].size
25     assert_equal 0, json_response['items_available']
26   end
27
28   # active user sees non-secret attributes of up and recently-up nodes
29   test "active user should get non-empty index with no ping_secret" do
30     authorize_with :active
31     get :index
32     assert_response :success
33     assert_operator 0, :<, json_response['items_available']
34     node_items = json_response['items']
35     assert_operator 0, :<, node_items.size
36     found_busy_node = false
37     node_items.each do |node|
38       assert_nil node['info'].andand['ping_secret']
39       assert_not_nil node['crunch_worker_state']
40       if node['uuid'] == nodes(:busy).uuid
41         found_busy_node = true
42         assert_equal 'busy', node['crunch_worker_state']
43       end
44     end
45     assert_equal true, found_busy_node
46   end
47
48   test "node should ping with ping_secret and no token" do
49     post :ping, {
50       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
51       instance_id: 'i-0000000',
52       local_ipv4: '172.17.2.174',
53       ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
54     }
55     assert_response :success
56     response = JSON.parse(@response.body)
57     assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
58     # Ensure we are getting the "superuser" attributes, too
59     assert_not_nil response['first_ping_at'], '"first_ping_at" attr missing'
60     assert_not_nil response['info'], '"info" attr missing'
61     assert_not_nil response['nameservers'], '"nameservers" attr missing'
62   end
63
64   test "node should fail ping with invalid ping_secret" do
65     post :ping, {
66       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
67       instance_id: 'i-0000000',
68       local_ipv4: '172.17.2.174',
69       ping_secret: 'dricrha4lcpi23pd69e44soanc069udawxvn3zzj45hs8bumvn'
70     }
71     assert_response 401
72   end
73
74   test "create node" do
75     authorize_with :admin
76     post :create, {node: {}}
77     assert_response :success
78     assert_not_nil json_response['uuid']
79     assert_not_nil json_response['info'].is_a? Hash
80     assert_not_nil json_response['info']['ping_secret']
81   end
82
83   test "ping adds node stats to info" do
84     authorize_with :admin
85     node = nodes(:idle)
86     post :ping, {
87       id: node.uuid,
88       ping_secret: node.info['ping_secret'],
89       total_cpu_cores: 32,
90       total_ram_mb: 1024,
91       total_scratch_mb: 2048
92     }
93     assert_response :success
94     info = JSON.parse(@response.body)['info']
95     properties = JSON.parse(@response.body)['properties']
96     assert_equal(node.info['ping_secret'], info['ping_secret'])
97     assert_equal(32, properties['total_cpu_cores'].to_i)
98     assert_equal(1024, properties['total_ram_mb'].to_i)
99     assert_equal(2048, properties['total_scratch_mb'].to_i)
100   end
101
102   test "active user can see their assigned job" do
103     authorize_with :active
104     get :show, {id: nodes(:busy).uuid}
105     assert_response :success
106     assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"])
107   end
108
109   test "user without job read permission can't see job" do
110     authorize_with :spectator
111     get :show, {id: nodes(:busy).uuid}
112     assert_response :success
113     assert_nil(json_response["job"], "spectator can see node's assigned job")
114   end
115
116   [:admin, :spectator].each do |user|
117     test "select param does not break node list for #{user}" do
118       authorize_with user
119       get :index, {select: ['domain']}
120       assert_response :success
121       assert_operator 0, :<, json_response['items_available']
122     end
123   end
124
125   test "admin can associate a job with a node" do
126     changed_node = nodes(:idle)
127     assigned_job = jobs(:queued)
128     authorize_with :admin
129     post :update, {
130       id: changed_node.uuid,
131       node: {job_uuid: assigned_job.uuid},
132     }
133     assert_response :success
134     assert_equal(changed_node.hostname, json_response["hostname"],
135                  "hostname mismatch after defining job")
136     assert_equal(assigned_job.uuid, json_response["job_uuid"],
137                  "mismatch in node's assigned job UUID")
138   end
139
140   test "non-admin can't associate a job with a node" do
141     authorize_with :active
142     post :update, {
143       id: nodes(:idle).uuid,
144       node: {job_uuid: jobs(:queued).uuid},
145     }
146     assert_response 403
147   end
148
149   test "admin can unassign a job from a node" do
150     changed_node = nodes(:busy)
151     authorize_with :admin
152     post :update, {
153       id: changed_node.uuid,
154       node: {job_uuid: nil},
155     }
156     assert_response :success
157     assert_equal(changed_node.hostname, json_response["hostname"],
158                  "hostname mismatch after defining job")
159     assert_nil(json_response["job_uuid"],
160                "node still has job assignment after update")
161   end
162
163   test "non-admin can't unassign a job from a node" do
164     authorize_with :project_viewer
165     post :update, {
166       id: nodes(:busy).uuid,
167       node: {job_uuid: nil},
168     }
169     assert_response 403
170   end
171
172   test "job readable after updating other attributes" do
173     authorize_with :admin
174     post :update, {
175       id: nodes(:busy).uuid,
176       node: {last_ping_at: 1.second.ago},
177     }
178     assert_response :success
179     assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
180                  "mismatched job UUID after ping update")
181   end
182
183   test "node should fail ping with invalid hostname config format" do
184     Rails.configuration.assign_node_hostname = 'compute%<slot_number>04'  # should end with "04d"
185     post :ping, {
186       id: nodes(:new_with_no_hostname).uuid,
187       ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
188     }
189     assert_response 422
190   end
191
192   test "first ping should set ip addr using local_ipv4 when provided" do
193     post :ping, {
194       id: 'zzzzz-7ekkf-nodenoipaddryet',
195       instance_id: 'i-0000000',
196       local_ipv4: '172.17.2.172',
197       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
198     }
199     assert_response :success
200     response = JSON.parse(@response.body)
201     assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
202     assert_equal '172.17.2.172', response['ip_address']
203   end
204
205   test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
206     post :ping, {
207       id: 'zzzzz-7ekkf-nodenoipaddryet',
208       instance_id: 'i-0000000',
209       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
210     }
211     assert_response :success
212     response = JSON.parse(@response.body)
213     assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
214     assert_equal request.remote_ip, response['ip_address']
215   end
216
217   test "future pings should not change previous ip address" do
218     post :ping, {
219       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
220       instance_id: 'i-0000000',
221       local_ipv4: '172.17.2.175',
222       ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
223     }
224     assert_response :success
225     response = JSON.parse(@response.body)
226     assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
227     assert_equal '172.17.2.174', response['ip_address']   # original ip address is not overwritten
228   end
229 end