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, {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, {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']
97 test "update node and assign slot" do
99 node = nodes(:new_with_no_hostname)
100 post :update, {id: node.uuid, node: {}, assign_slot: true}
101 assert_response :success
102 assert_operator 0, :<, json_response['slot_number']
103 n = json_response['slot_number']
104 assert_equal "compute#{n}", json_response['hostname']
107 test "update node and assign slot, don't clobber hostname" do
108 authorize_with :admin
109 node = nodes(:new_with_custom_hostname)
110 post :update, {id: node.uuid, node: {}, assign_slot: true}
111 assert_response :success
112 assert_operator 0, :<, json_response['slot_number']
113 n = json_response['slot_number']
114 assert_equal "custom1", json_response['hostname']
117 test "ping adds node stats to info" do
118 authorize_with :admin
122 ping_secret: node.info['ping_secret'],
125 total_scratch_mb: 2048
127 assert_response :success
128 info = JSON.parse(@response.body)['info']
129 properties = JSON.parse(@response.body)['properties']
130 assert_equal(node.info['ping_secret'], info['ping_secret'])
131 assert_equal(32, properties['total_cpu_cores'].to_i)
132 assert_equal(1024, properties['total_ram_mb'].to_i)
133 assert_equal(2048, properties['total_scratch_mb'].to_i)
136 test "active user can see their assigned job" do
137 authorize_with :active
138 get :show, {id: nodes(:busy).uuid}
139 assert_response :success
140 assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"])
143 test "user without job read permission can't see job" do
144 authorize_with :spectator
145 get :show, {id: nodes(:busy).uuid}
146 assert_response :success
147 assert_nil(json_response["job"], "spectator can see node's assigned job")
150 [:admin, :spectator].each do |user|
151 test "select param does not break node list for #{user}" do
153 get :index, {select: ['domain']}
154 assert_response :success
155 assert_operator 0, :<, json_response['items_available']
159 test "admin can associate a job with a node" do
160 changed_node = nodes(:idle)
161 assigned_job = jobs(:queued)
162 authorize_with :admin
164 id: changed_node.uuid,
165 node: {job_uuid: assigned_job.uuid},
167 assert_response :success
168 assert_equal(changed_node.hostname, json_response["hostname"],
169 "hostname mismatch after defining job")
170 assert_equal(assigned_job.uuid, json_response["job_uuid"],
171 "mismatch in node's assigned job UUID")
174 test "non-admin can't associate a job with a node" do
175 authorize_with :active
177 id: nodes(:idle).uuid,
178 node: {job_uuid: jobs(:queued).uuid},
183 test "admin can unassign a job from a node" do
184 changed_node = nodes(:busy)
185 authorize_with :admin
187 id: changed_node.uuid,
188 node: {job_uuid: nil},
190 assert_response :success
191 assert_equal(changed_node.hostname, json_response["hostname"],
192 "hostname mismatch after defining job")
193 assert_nil(json_response["job_uuid"],
194 "node still has job assignment after update")
197 test "non-admin can't unassign a job from a node" do
198 authorize_with :project_viewer
200 id: nodes(:busy).uuid,
201 node: {job_uuid: nil},
206 test "job readable after updating other attributes" do
207 authorize_with :admin
209 id: nodes(:busy).uuid,
210 node: {last_ping_at: 1.second.ago},
212 assert_response :success
213 assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
214 "mismatched job UUID after ping update")
217 test "node should fail ping with invalid hostname config format" do
218 Rails.configuration.assign_node_hostname = 'compute%<slot_number>04' # should end with "04d"
220 id: nodes(:new_with_no_hostname).uuid,
221 ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
226 test "first ping should set ip addr using local_ipv4 when provided" do
228 id: 'zzzzz-7ekkf-nodenoipaddryet',
229 instance_id: 'i-0000000',
230 local_ipv4: '172.17.2.172',
231 ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
233 assert_response :success
234 response = JSON.parse(@response.body)
235 assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
236 assert_equal '172.17.2.172', response['ip_address']
239 test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
241 id: 'zzzzz-7ekkf-nodenoipaddryet',
242 instance_id: 'i-0000000',
243 ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
245 assert_response :success
246 response = JSON.parse(@response.body)
247 assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
248 assert_equal request.remote_ip, response['ip_address']
251 test "future pings should not change previous ip address" do
253 id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
254 instance_id: 'i-0000000',
255 local_ipv4: '172.17.2.175',
256 ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
258 assert_response :success
259 response = JSON.parse(@response.body)
260 assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
261 assert_equal '172.17.2.174', response['ip_address'] # original ip address is not overwritten