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