1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::ContainersControllerTest < ActionController::TestCase
10 @dispatch2 = ApiClientAuthorization.create!(user_id: system_user.id)
15 authorize_with :system_user
16 post :create, params: {
18 command: ['echo', 'hello'],
19 container_image: 'test',
23 assert_response :success
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}
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)
39 authorize_with :system_user
40 get :auth, params: {id: c.uuid}
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']
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']
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']
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
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']
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
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}
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
125 test 'get current container for token' do
126 authorize_with :running_container_auth
128 assert_response :success
129 assert_equal containers(:running).uuid, json_response['uuid']
132 test 'no container associated with token' do
133 authorize_with :system_user
138 test 'try get current container, no token' do
144 [true, :running_container_auth],
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
155 get :secret_mounts, params: {id: containers(:running).uuid}
157 assert_response :success
158 assert_equal "42\n", json_response["secret_mounts"]["/secret/6x9"]["content"]
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']
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
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
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
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