8099: 7263: Merge branch 'hgi/7263-even-better-busy-behavior' of github.com:wtsi...
[arvados.git] / services / api / test / functional / arvados / v1 / nodes_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::NodesControllerTest < ActionController::TestCase
4
5   test "should get index with ping_secret" do
6     authorize_with :admin
7     get :index
8     assert_response :success
9     assert_not_nil assigns(:objects)
10     node_items = JSON.parse(@response.body)['items']
11     assert_not_equal 0, node_items.size
12     assert_not_nil node_items[0]['info'].andand['ping_secret']
13   end
14
15   # inactive user does not see any nodes
16   test "inactive user should get empty index" do
17     authorize_with :inactive
18     get :index
19     assert_response :success
20     node_items = JSON.parse(@response.body)['items']
21     assert_equal 0, node_items.size
22   end
23
24   # active user sees non-secret attributes of up and recently-up nodes
25   test "active user should get non-empty index with no ping_secret" do
26     authorize_with :active
27     get :index
28     assert_response :success
29     node_items = JSON.parse(@response.body)['items']
30     assert_not_equal 0, node_items.size
31     found_busy_node = false
32     node_items.each do |node|
33       assert_nil node['info'].andand['ping_secret']
34       assert_not_nil node['crunch_worker_state']
35       if node['uuid'] == nodes(:busy).uuid
36         found_busy_node = true
37         assert_equal 'busy', node['crunch_worker_state']
38       end
39     end
40     assert_equal true, found_busy_node
41   end
42
43   test "node should ping with ping_secret and no token" do
44     post :ping, {
45       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
46       instance_id: 'i-0000000',
47       local_ipv4: '172.17.2.174',
48       ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
49     }
50     assert_response :success
51     response = JSON.parse(@response.body)
52     assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
53     # Ensure we are getting the "superuser" attributes, too
54     assert_not_nil response['first_ping_at'], '"first_ping_at" attr missing'
55     assert_not_nil response['info'], '"info" attr missing'
56     assert_not_nil response['nameservers'], '"nameservers" attr missing'
57   end
58
59   test "node should fail ping with invalid ping_secret" do
60     post :ping, {
61       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
62       instance_id: 'i-0000000',
63       local_ipv4: '172.17.2.174',
64       ping_secret: 'dricrha4lcpi23pd69e44soanc069udawxvn3zzj45hs8bumvn'
65     }
66     assert_response 401
67   end
68
69   test "create node" do
70     authorize_with :admin
71     post :create, {node: {}}
72     assert_response :success
73     assert_not_nil json_response['uuid']
74     assert_not_nil json_response['info'].is_a? Hash
75     assert_not_nil json_response['info']['ping_secret']
76   end
77
78   test "ping adds node stats to info" do
79     authorize_with :admin
80     node = nodes(:idle)
81     post :ping, {
82       id: node.uuid,
83       ping_secret: node.info['ping_secret'],
84       total_cpu_cores: 32,
85       total_ram_mb: 1024,
86       total_scratch_mb: 2048
87     }
88     assert_response :success
89     info = JSON.parse(@response.body)['info']
90     properties = JSON.parse(@response.body)['properties']
91     assert_equal(node.info['ping_secret'], info['ping_secret'])
92     assert_equal(32, properties['total_cpu_cores'].to_i)
93     assert_equal(1024, properties['total_ram_mb'].to_i)
94     assert_equal(2048, properties['total_scratch_mb'].to_i)
95   end
96
97   test "active user can see their assigned job" do
98     authorize_with :active
99     get :show, {id: nodes(:busy).uuid}
100     assert_response :success
101     assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"])
102   end
103
104   test "user without job read permission can't see job" do
105     authorize_with :spectator
106     get :show, {id: nodes(:busy).uuid}
107     assert_response :success
108     assert_nil(json_response["job"], "spectator can see node's assigned job")
109   end
110
111   [:admin, :spectator].each do |user|
112     test "select param does not break node list for #{user}" do
113       authorize_with user
114       get :index, {select: ['domain']}
115       assert_response :success
116     end
117   end
118
119   test "admin can associate a job with a node" do
120     changed_node = nodes(:idle)
121     assigned_job = jobs(:queued)
122     authorize_with :admin
123     post :update, {
124       id: changed_node.uuid,
125       node: {job_uuid: assigned_job.uuid},
126     }
127     assert_response :success
128     assert_equal(changed_node.hostname, json_response["hostname"],
129                  "hostname mismatch after defining job")
130     assert_equal(assigned_job.uuid, json_response["job_uuid"],
131                  "mismatch in node's assigned job UUID")
132   end
133
134   test "non-admin can't associate a job with a node" do
135     authorize_with :active
136     post :update, {
137       id: nodes(:idle).uuid,
138       node: {job_uuid: jobs(:queued).uuid},
139     }
140     assert_response 403
141   end
142
143   test "admin can unassign a job from a node" do
144     changed_node = nodes(:busy)
145     authorize_with :admin
146     post :update, {
147       id: changed_node.uuid,
148       node: {job_uuid: nil},
149     }
150     assert_response :success
151     assert_equal(changed_node.hostname, json_response["hostname"],
152                  "hostname mismatch after defining job")
153     assert_nil(json_response["job_uuid"],
154                "node still has job assignment after update")
155   end
156
157   test "non-admin can't unassign a job from a node" do
158     authorize_with :project_viewer
159     post :update, {
160       id: nodes(:busy).uuid,
161       node: {job_uuid: nil},
162     }
163     assert_response 403
164   end
165
166   test "job readable after updating other attributes" do
167     authorize_with :admin
168     post :update, {
169       id: nodes(:busy).uuid,
170       node: {last_ping_at: 1.second.ago},
171     }
172     assert_response :success
173     assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
174                  "mismatched job UUID after ping update")
175   end
176
177   test "node should fail ping with invalid hostname config format" do
178     Rails.configuration.assign_node_hostname = 'compute%<slot_number>04'  # should end with "04d"
179     post :ping, {
180       id: nodes(:new_with_no_hostname).uuid,
181       ping_secret: nodes(:new_with_no_hostname).info['ping_secret'],
182     }
183     assert_response 422
184   end
185
186   test "first ping should set ip addr using local_ipv4 when provided" do
187     post :ping, {
188       id: 'zzzzz-7ekkf-nodenoipaddryet',
189       instance_id: 'i-0000000',
190       local_ipv4: '172.17.2.172',
191       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
192     }
193     assert_response :success
194     response = JSON.parse(@response.body)
195     assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
196     assert_equal '172.17.2.172', response['ip_address']
197   end
198
199   test "first ping should set ip addr using remote_ip when local_ipv4 is not provided" do
200     post :ping, {
201       id: 'zzzzz-7ekkf-nodenoipaddryet',
202       instance_id: 'i-0000000',
203       ping_secret: 'abcdyefg4lb5q4gzqqtrnq30oyj08r8dtdimmanbqw49z1anz2'
204     }
205     assert_response :success
206     response = JSON.parse(@response.body)
207     assert_equal 'zzzzz-7ekkf-nodenoipaddryet', response['uuid']
208     assert_equal request.remote_ip, response['ip_address']
209   end
210
211   test "future pings should not change previous ip address" do
212     post :ping, {
213       id: 'zzzzz-7ekkf-2z3mc76g2q73aio',
214       instance_id: 'i-0000000',
215       local_ipv4: '172.17.2.175',
216       ping_secret: '69udawxvn3zzj45hs8bumvndricrha4lcpi23pd69e44soanc0'
217     }
218     assert_response :success
219     response = JSON.parse(@response.body)
220     assert_equal 'zzzzz-7ekkf-2z3mc76g2q73aio', response['uuid']
221     assert_equal '172.17.2.174', response['ip_address']   # original ip address is not overwritten
222   end
223 end