21910: Fix test fixtures.
[arvados.git] / services / api / test / functional / arvados / v1 / containers_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::ContainersControllerTest < ActionController::TestCase
8   setup do
9     act_as_system_user do
10       @dispatch2 = ApiClientAuthorization.create!(user_id: system_user.id)
11     end
12   end
13
14   test 'create' do
15     authorize_with :system_user
16     post :create, params: {
17       container: {
18         command: ['echo', 'hello'],
19         container_image: 'test',
20         output_path: 'test',
21       },
22     }
23     assert_response :success
24   end
25
26   [Container::Queued, Container::Complete].each do |state|
27     test "cannot get auth in #{state} state" do
28       authorize_with :system_user
29       get :auth, params: {id: containers(:queued).uuid}
30       assert_response 403
31     end
32   end
33
34   test 'cannot get auth with wrong token' do
35     authorize_with_token @dispatch2.token
36     c = containers(:queued)
37     assert c.lock, show_errors(c)
38
39     authorize_with :system_user
40     get :auth, params: {id: c.uuid}
41     assert_response 403
42   end
43
44   test 'get auth' do
45     authorize_with :system_user
46     c = containers(:queued)
47     assert c.lock, show_errors(c)
48     get :auth, params: {id: c.uuid}
49     assert_response :success
50     assert_operator 32, :<, json_response['api_token'].length
51     assert_equal 'arvados#apiClientAuthorization', json_response['kind']
52   end
53
54   test 'no auth or secret_mounts in container response' do
55     authorize_with :system_user
56     c = containers(:queued)
57     assert c.lock, show_errors(c)
58     get :show, params: {id: c.uuid}
59     assert_response :success
60     assert_nil json_response['auth']
61     assert_nil json_response['secret_mounts']
62   end
63
64   test "lock container" do
65     authorize_with :system_user
66     uuid = containers(:queued).uuid
67     post :lock, params: {id: uuid}
68     assert_response :success
69     assert_nil json_response['mounts']
70     assert_nil json_response['command']
71     assert_not_nil json_response['auth_uuid']
72     assert_not_nil json_response['locked_by_uuid']
73     assert_equal containers(:queued).uuid, json_response['uuid']
74     assert_equal 'Locked', json_response['state']
75     assert_equal containers(:queued).priority, json_response['priority']
76
77     container = Container.where(uuid: uuid).first
78     assert_equal 'Locked', container.state
79     assert_not_nil container.locked_by_uuid
80     assert_not_nil container.auth_uuid
81   end
82
83   test "unlock container" do
84     authorize_with :system_user
85     uuid = containers(:locked).uuid
86     post :unlock, params: {id: uuid}
87     assert_response :success
88     assert_nil json_response['mounts']
89     assert_nil json_response['command']
90     assert_nil json_response['auth_uuid']
91     assert_nil json_response['locked_by_uuid']
92     assert_equal containers(:locked).uuid, json_response['uuid']
93     assert_equal 'Queued', json_response['state']
94     assert_equal containers(:locked).priority, json_response['priority']
95
96     container = Container.where(uuid: uuid).first
97     assert_equal 'Queued', container.state
98     assert_nil container.locked_by_uuid
99     assert_nil container.auth_uuid
100   end
101
102   test "unlock container locked by different dispatcher" do
103     authorize_with_token @dispatch2.token
104     uuid = containers(:locked).uuid
105     post :unlock, params: {id: uuid}
106     assert_response 403
107   end
108
109   [
110     [:queued, :lock, :success, 'Locked'],
111     [:queued, :unlock, 422, 'Queued'],
112     [:locked, :lock, 422, 'Locked'],
113     [:running, :lock, 422, 'Running'],
114     [:running, :unlock, 422, 'Running'],
115   ].each do |fixture, action, response, state|
116     test "state transitions from #{fixture} to #{action}" do
117       authorize_with :system_user
118       uuid = containers(fixture).uuid
119       post action, params: {id: uuid}
120       assert_response response
121       assert_equal state, Container.where(uuid: uuid).first.state
122     end
123   end
124
125   test 'get current container for token' do
126     authorize_with :running_container_auth
127     get :current
128     assert_response :success
129     assert_equal containers(:running).uuid, json_response['uuid']
130   end
131
132   test 'no container associated with token' do
133     authorize_with :system_user
134     get :current
135     assert_response 404
136   end
137
138   test 'try get current container, no token' do
139     get :current
140     assert_response 401
141   end
142
143   [
144     [true, :running_container_auth],
145     [false, :dispatch2],
146     [false, :admin],
147     [false, :active],
148   ].each do |expect_success, auth|
149     test "get secret_mounts with #{auth} token" do
150       if auth == :dispatch2
151         authorize_with_token @dispatch2.token
152       else
153         authorize_with auth
154       end
155       get :secret_mounts, params: {id: containers(:running).uuid}
156       if expect_success
157         assert_response :success
158         assert_equal "42\n", json_response["secret_mounts"]["/secret/6x9"]["content"]
159       else
160         assert_response 403
161       end
162     end
163   end
164
165   test 'get runtime_token auth' do
166     authorize_with :admin
167     c = containers(:runtime_token)
168     get :auth, params: {id: c.uuid}
169     assert_response :success
170     assert_equal "v2/#{json_response['uuid']}/#{json_response['api_token']}", api_client_authorizations(:container_runtime_token).token
171     assert_equal 'arvados#apiClientAuthorization', json_response['kind']
172   end
173
174   test 'update_priority' do
175     ActiveRecord::Base.connection.execute "update containers set priority=0 where uuid='#{containers(:running).uuid}'"
176     authorize_with :admin
177     post :update_priority, params: {id: containers(:running).uuid}
178     assert_response :success
179     assert_not_equal 0, Container.find_by_uuid(containers(:running).uuid).priority
180   end
181
182   test 'update runtime_status, runtime_status is toplevel key' do
183     authorize_with :system_user
184     c = containers(:running)
185     patch :update, params: {id: containers(:running).uuid, runtime_status: {activity: "foo", activityDetail: "bar"}}
186     assert_response :success
187   end
188
189   test 'update runtime_status, container is toplevel key' do
190     authorize_with :system_user
191     c = containers(:running)
192     patch :update, params: {id: containers(:running).uuid, container: {runtime_status: {activity: "foo", activityDetail: "bar"}}}
193     assert_response :success
194   end
195
196   test 'update state, state is toplevel key' do
197     authorize_with :system_user
198     c = containers(:running)
199     patch :update, params: {id: containers(:running).uuid, state: "Complete", runtime_status: {activity: "finishing"}}
200     assert_response :success
201   end
202 end