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