Merge branch '8784-dir-listings'
[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, {
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, 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, 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, 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 in container response' do
49     authorize_with :dispatch1
50     c = containers(:queued)
51     assert c.lock, show_errors(c)
52     get :show, id: c.uuid
53     assert_response :success
54     assert_nil json_response['auth']
55   end
56
57   test "lock container" do
58     authorize_with :dispatch1
59     uuid = containers(:queued).uuid
60     post :lock, {id: uuid}
61     assert_response :success
62     assert_nil json_response['mounts']
63     assert_nil json_response['command']
64     assert_not_nil json_response['auth_uuid']
65     assert_not_nil json_response['locked_by_uuid']
66     assert_equal containers(:queued).uuid, json_response['uuid']
67     assert_equal 'Locked', json_response['state']
68     assert_equal containers(:queued).priority, json_response['priority']
69
70     container = Container.where(uuid: uuid).first
71     assert_equal 'Locked', container.state
72     assert_not_nil container.locked_by_uuid
73     assert_not_nil container.auth_uuid
74   end
75
76   test "unlock container" do
77     authorize_with :dispatch1
78     uuid = containers(:locked).uuid
79     post :unlock, {id: uuid}
80     assert_response :success
81     assert_nil json_response['mounts']
82     assert_nil json_response['command']
83     assert_nil json_response['auth_uuid']
84     assert_nil json_response['locked_by_uuid']
85     assert_equal containers(:locked).uuid, json_response['uuid']
86     assert_equal 'Queued', json_response['state']
87     assert_equal containers(:locked).priority, json_response['priority']
88
89     container = Container.where(uuid: uuid).first
90     assert_equal 'Queued', container.state
91     assert_nil container.locked_by_uuid
92     assert_nil container.auth_uuid
93   end
94
95   test "unlock container locked by different dispatcher" do
96     authorize_with :dispatch2
97     uuid = containers(:locked).uuid
98     post :unlock, {id: uuid}
99     assert_response 422
100   end
101
102   [
103     [:queued, :lock, :success, 'Locked'],
104     [:queued, :unlock, 422, 'Queued'],
105     [:locked, :lock, 422, 'Locked'],
106     [:running, :lock, 422, 'Running'],
107     [:running, :unlock, 422, 'Running'],
108   ].each do |fixture, action, response, state|
109     test "state transitions from #{fixture } to #{action}" do
110       authorize_with :dispatch1
111       uuid = containers(fixture).uuid
112       post action, {id: uuid}
113       assert_response response
114       assert_equal state, Container.where(uuid: uuid).first.state
115     end
116   end
117
118   test 'get current container for token' do
119     authorize_with :running_container_auth
120     get :current
121     assert_response :success
122     assert_equal containers(:running).uuid, json_response['uuid']
123   end
124
125   test 'no container associated with token' do
126     authorize_with :dispatch1
127     get :current
128     assert_response 404
129   end
130
131   test 'try get current container, no token' do
132     get :current
133     assert_response 401
134   end
135
136 end