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']
83 test "ping adds node stats to info" do
88 ping_secret: node.info['ping_secret'],
91 total_scratch_mb: 2048
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)
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"])
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")
116 [:admin, :spectator].each do |user|
117 test "select param does not break node list for #{user}" do
119 get :index, {select: ['domain']}
120 assert_response :success
121 assert_operator 0, :<, json_response['items_available']
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
130 id: changed_node.uuid,
131 node: {job_uuid: assigned_job.uuid},
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")
140 test "non-admin can't associate a job with a node" do
141 authorize_with :active
143 id: nodes(:idle).uuid,
144 node: {job_uuid: jobs(:queued).uuid},
149 test "admin can unassign a job from a node" do
150 changed_node = nodes(:busy)
151 authorize_with :admin
153 id: changed_node.uuid,
154 node: {job_uuid: nil},
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")
163 test "non-admin can't unassign a job from a node" do
164 authorize_with :project_viewer
166 id: nodes(:busy).uuid,
167 node: {job_uuid: nil},
172 test "job readable after updating other attributes" do
173 authorize_with :admin
175 id: nodes(:busy).uuid,
176 node: {last_ping_at: 1.second.ago},
178 assert_response :success
179 assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
180 "mismatched job UUID after ping update")
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"
186 id: nodes(:new_with_no_hostname).uuid,
187 ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
192 test "first ping should set ip addr using local_ipv4 when provided" do
194 id: 'zzzzz-7ekkf-nodenoipaddryet',
195 instance_id: 'i-0000000',
196 local_ipv4: '172.17.2.172',
197 ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
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']
205 test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
207 id: 'zzzzz-7ekkf-nodenoipaddryet',
208 instance_id: 'i-0000000',
209 ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
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']
217 test "future pings should not change previous ip address" do
219 id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
220 instance_id: 'i-0000000',
221 local_ipv4: '172.17.2.175',
222 ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
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