Merge branch '4295-efficient-websockets' closes #4295
[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   test "admin can associate a job with a node" do
112     changed_node = nodes(:idle)
113     assigned_job = jobs(:queued)
114     authorize_with :admin
115     post :update, {
116       id: changed_node.uuid,
117       node: {job_uuid: assigned_job.uuid},
118     }
119     assert_response :success
120     assert_equal(changed_node.hostname, json_response["hostname"],
121                  "hostname mismatch after defining job")
122     assert_equal(assigned_job.uuid, json_response["job_uuid"],
123                  "mismatch in node's assigned job UUID")
124   end
125
126   test "non-admin can't associate a job with a node" do
127     authorize_with :active
128     post :update, {
129       id: nodes(:idle).uuid,
130       node: {job_uuid: jobs(:queued).uuid},
131     }
132     assert_response 403
133   end
134
135   test "admin can unassign a job from a node" do
136     changed_node = nodes(:busy)
137     authorize_with :admin
138     post :update, {
139       id: changed_node.uuid,
140       node: {job_uuid: nil},
141     }
142     assert_response :success
143     assert_equal(changed_node.hostname, json_response["hostname"],
144                  "hostname mismatch after defining job")
145     assert_nil(json_response["job_uuid"],
146                "node still has job assignment after update")
147   end
148
149   test "non-admin can't unassign a job from a node" do
150     authorize_with :project_viewer
151     post :update, {
152       id: nodes(:busy).uuid,
153       node: {job_uuid: nil},
154     }
155     assert_response 403
156   end
157
158   test "job readable after updating other attributes" do
159     authorize_with :admin
160     post :update, {
161       id: nodes(:busy).uuid,
162       node: {last_ping_at: 1.second.ago},
163     }
164     assert_response :success
165     assert_equal(jobs(:nearly_finished_job).uuid, json_response["job_uuid"],
166                  "mismatched job UUID after ping update")
167   end
168 end