1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::NodesControllerTest < ActionController::TestCase
9 test "should get index with ping_secret" do
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']
19 # inactive user does not see any nodes
20 test "inactive user should get empty index" do
21 authorize_with :inactive
23 assert_response :success
24 assert_equal 0, json_response['items'].size
25 assert_equal 0, json_response['items_available']
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
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']
45 assert_equal true, found_busy_node
48 test "node should ping with ping_secret and no token" do
50 id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
51 instance_id: 'i-0000000',
52 local_ipv4: '172.17.2.174',
53 ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
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'
64 test "node should fail ping with invalid ping_secret" do
66 id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
67 instance_id: 'i-0000000',
68 local_ipv4: '172.17.2.174',
69 ping_secret: 'dricrha4lcpi23pd69e44soanc069udawxvn3zzj45hs8bumvn'
76 post :create, params: {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 assert_nil json_response['slot_number']
82 assert_nil json_response['hostname']
85 test "create node and assign slot" do
87 post :create, params: {node: {}, assign_slot: true}
88 assert_response :success
89 assert_not_nil json_response['uuid']
90 assert_not_nil json_response['info'].is_a? Hash
91 assert_not_nil json_response['info']['ping_secret']
92 assert_operator 0, :<, json_response['slot_number']
93 n = json_response['slot_number']
94 assert_equal "compute#{n}", json_response['hostname']
96 node = Node.where(uuid: json_response['uuid']).first
97 assert_equal n, node.slot_number
98 assert_equal "compute#{n}", node.hostname
101 test "update node and assign slot" do
102 authorize_with :admin
103 node = nodes(:new_with_no_hostname)
104 post :update, params: {id: node.uuid, node: {}, assign_slot: true}
105 assert_response :success
106 assert_operator 0, :<, json_response['slot_number']
107 n = json_response['slot_number']
108 assert_equal "compute#{n}", json_response['hostname']
111 assert_equal n, node.slot_number
112 assert_equal "compute#{n}", node.hostname
115 test "update node and assign slot, don't clobber hostname" do
116 authorize_with :admin
117 node = nodes(:new_with_custom_hostname)
118 post :update, params: {id: node.uuid, node: {}, assign_slot: true}
119 assert_response :success
120 assert_operator 0, :<, json_response['slot_number']
121 n = json_response['slot_number']
122 assert_equal "custom1", json_response['hostname']
125 test "ping adds node stats to info" do
126 authorize_with :admin
128 post :ping, params: {
130 ping_secret: node.info['ping_secret'],
133 total_scratch_mb: 2048
135 assert_response :success
136 info = JSON.parse(@response.body)['info']
137 properties = JSON.parse(@response.body)['properties']
138 assert_equal(node.info['ping_secret'], info['ping_secret'])
139 assert_equal(32, properties['total_cpu_cores'].to_i)
140 assert_equal(1024, properties['total_ram_mb'].to_i)
141 assert_equal(2048, properties['total_scratch_mb'].to_i)
144 test "active user can see their assigned job" do
145 authorize_with :active
146 get :show, params: {id: nodes(:busy).uuid}
147 assert_response :success
148 assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"])
151 test "user without job read permission can't see job" do
152 authorize_with :spectator
153 get :show, params: {id: nodes(:busy).uuid}
154 assert_response :success
155 assert_nil(json_response["job"], "spectator can see node's assigned job")
158 [:admin, :spectator].each do |user|
159 test "select param does not break node list for #{user}" do
161 get :index, params: {select: ['domain']}
162 assert_response :success
163 assert_operator 0, :<, json_response['items_available']
167 test "admin can associate a job with a node" do
168 changed_node = nodes(:idle)
169 assigned_job = jobs(:queued)
170 authorize_with :admin
171 post :update, params: {
172 id: changed_node.uuid,
173 node: {job_uuid: assigned_job.uuid},
175 assert_response :success
176 assert_equal(changed_node.hostname, json_response["hostname"],
177 "hostname mismatch after defining job")
178 assert_equal(assigned_job.uuid, json_response["job_uuid"],
179 "mismatch in node's assigned job UUID")
182 test "non-admin can't associate a job with a node" do
183 authorize_with :active
184 post :update, params: {
185 id: nodes(:idle).uuid,
186 node: {job_uuid: jobs(:queued).uuid},
191 test "admin can unassign a job from a node" do
192 changed_node = nodes(:busy)
193 authorize_with :admin
194 post :update, params: {
195 id: changed_node.uuid,
196 node: {job_uuid: nil},
198 assert_response :success
199 assert_equal(changed_node.hostname, json_response["hostname"],
200 "hostname mismatch after defining job")
201 assert_nil(json_response["job_uuid"],
202 "node still has job assignment after update")
205 test "non-admin can't unassign a job from a node" do
206 authorize_with :project_viewer
207 post :update, params: {
208 id: nodes(:busy).uuid,
209 node: {job_uuid: nil},
214 test "job readable after updating other attributes" do
215 authorize_with :admin
216 post :update, params: {
217 id: nodes(:busy).uuid,
218 node: {last_ping_at: 1.second.ago},
220 assert_response :success
221 assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
222 "mismatched job UUID after ping update")
225 test "node should fail ping with invalid hostname config format" do
226 Rails.configuration.Containers.SLURM.Managed.AssignNodeHostname = 'compute%<slot_number>04' # should end with "04d"
227 post :ping, params: {
228 id: nodes(:new_with_no_hostname).uuid,
229 ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
234 test "first ping should set ip addr using local_ipv4 when provided" do
235 post :ping, params: {
236 id: 'zzzzz-7ekkf-nodenoipaddryet',
237 instance_id: 'i-0000000',
238 local_ipv4: '172.17.2.172',
239 ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
241 assert_response :success
242 response = JSON.parse(@response.body)
243 assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
244 assert_equal '172.17.2.172', response['ip_address']
247 test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
248 post :ping, params: {
249 id: 'zzzzz-7ekkf-nodenoipaddryet',
250 instance_id: 'i-0000000',
251 ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
253 assert_response :success
254 response = JSON.parse(@response.body)
255 assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
256 assert_equal request.remote_ip, response['ip_address']
259 test "future pings should not change previous ip address" do
260 post :ping, params: {
261 id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
262 instance_id: 'i-0000000',
263 local_ipv4: '172.17.2.175',
264 ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
266 assert_response :success
267 response = JSON.parse(@response.body)
268 assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
269 assert_equal '172.17.2.174', response['ip_address'] # original ip address is not overwritten