21700: Install Bundler system-wide in Rails postinst
[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, params: {
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, params: {
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, 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']
83   end
84
85   test "create node and assign slot" do
86     authorize_with :admin
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']
95
96     node = Node.where(uuid: json_response['uuid']).first
97     assert_equal n, node.slot_number
98     assert_equal "compute#{n}", node.hostname
99   end
100
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']
109
110     node.reload
111     assert_equal n, node.slot_number
112     assert_equal "compute#{n}", node.hostname
113   end
114
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']
123   end
124
125   test "ping adds node stats to info" do
126     authorize_with :admin
127     node = nodes(:idle)
128     post :ping, params: {
129       id: node.uuid,
130       ping_secret: node.info['ping_secret'],
131       total_cpu_cores: 32,
132       total_ram_mb: 1024,
133       total_scratch_mb: 2048
134     }
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)
142   end
143
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"])
149   end
150
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")
156   end
157
158   [:admin, :spectator].each do |user|
159     test "select param does not break node list for #{user}" do
160       authorize_with user
161       get :index, params: {select: ['domain']}
162       assert_response :success
163       assert_operator 0, :<, json_response['items_available']
164     end
165   end
166
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},
174     }
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")
180   end
181
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},
187     }
188     assert_response 403
189   end
190
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},
197     }
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")
203   end
204
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},
210     }
211     assert_response 403
212   end
213
214   test "node should fail ping with invalid hostname config format" do
215     Rails.configuration.Containers.SLURM.Managed.AssignNodeHostname = 'compute%<slot_number>04'  # should end with "04d"
216     post :ping, params: {
217       id: nodes(:new_with_no_hostname).uuid,
218       ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
219     }
220     assert_response 422
221   end
222
223   test "first ping should set ip addr using local_ipv4 when provided" do
224     post :ping, params: {
225       id: 'zzzzz-7ekkf-nodenoipaddryet',
226       instance_id: 'i-0000000',
227       local_ipv4: '172.17.2.172',
228       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
229     }
230     assert_response :success
231     response = JSON.parse(@response.body)
232     assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
233     assert_equal '172.17.2.172', response['ip_address']
234   end
235
236   test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
237     post :ping, params: {
238       id: 'zzzzz-7ekkf-nodenoipaddryet',
239       instance_id: 'i-0000000',
240       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
241     }
242     assert_response :success
243     response = JSON.parse(@response.body)
244     assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
245     assert_equal request.remote_ip, response['ip_address']
246   end
247
248   test "future pings should not change previous ip address" do
249     post :ping, params: {
250       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
251       instance_id: 'i-0000000',
252       local_ipv4: '172.17.2.175',
253       ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
254     }
255     assert_response :success
256     response = JSON.parse(@response.body)
257     assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
258     assert_equal '172.17.2.174', response['ip_address']   # original ip address is not overwritten
259   end
260 end