c198c4c8ee9874cc1769b11428c11f44290fcf85
[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     assert_nil json_response['slot_number']
82     assert_nil json_response['hostname']
83   end
84
85   test "create node and assign slot" do
86     authorize_with :admin
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']
95   end
96
97   test "update node and assign slot" do
98     authorize_with :admin
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']
105   end
106
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']
115   end
116
117   test "ping adds node stats to info" do
118     authorize_with :admin
119     node = nodes(:idle)
120     post :ping, {
121       id: node.uuid,
122       ping_secret: node.info['ping_secret'],
123       total_cpu_cores: 32,
124       total_ram_mb: 1024,
125       total_scratch_mb: 2048
126     }
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)
134   end
135
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"])
141   end
142
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")
148   end
149
150   [:admin, :spectator].each do |user|
151     test "select param does not break node list for #{user}" do
152       authorize_with user
153       get :index, {select: ['domain']}
154       assert_response :success
155       assert_operator 0, :<, json_response['items_available']
156     end
157   end
158
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
163     post :update, {
164       id: changed_node.uuid,
165       node: {job_uuid: assigned_job.uuid},
166     }
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")
172   end
173
174   test "non-admin can't associate a job with a node" do
175     authorize_with :active
176     post :update, {
177       id: nodes(:idle).uuid,
178       node: {job_uuid: jobs(:queued).uuid},
179     }
180     assert_response 403
181   end
182
183   test "admin can unassign a job from a node" do
184     changed_node = nodes(:busy)
185     authorize_with :admin
186     post :update, {
187       id: changed_node.uuid,
188       node: {job_uuid: nil},
189     }
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")
195   end
196
197   test "non-admin can't unassign a job from a node" do
198     authorize_with :project_viewer
199     post :update, {
200       id: nodes(:busy).uuid,
201       node: {job_uuid: nil},
202     }
203     assert_response 403
204   end
205
206   test "job readable after updating other attributes" do
207     authorize_with :admin
208     post :update, {
209       id: nodes(:busy).uuid,
210       node: {last_ping_at: 1.second.ago},
211     }
212     assert_response :success
213     assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
214                  "mismatched job UUID after ping update")
215   end
216
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"
219     post :ping, {
220       id: nodes(:new_with_no_hostname).uuid,
221       ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
222     }
223     assert_response 422
224   end
225
226   test "first ping should set ip addr using local_ipv4 when provided" do
227     post :ping, {
228       id: 'zzzzz-7ekkf-nodenoipaddryet',
229       instance_id: 'i-0000000',
230       local_ipv4: '172.17.2.172',
231       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
232     }
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']
237   end
238
239   test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
240     post :ping, {
241       id: 'zzzzz-7ekkf-nodenoipaddryet',
242       instance_id: 'i-0000000',
243       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
244     }
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']
249   end
250
251   test "future pings should not change previous ip address" do
252     post :ping, {
253       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
254       instance_id: 'i-0000000',
255       local_ipv4: '172.17.2.175',
256       ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
257     }
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
262   end
263 end