10315: Merge branch 'master' into 10315-new-arv-put-performance
[arvados.git] / services / api / test / functional / arvados / v1 / containers_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::ContainersControllerTest < ActionController::TestCase
4   test 'create' do
5     authorize_with :system_user
6     post :create, {
7       container: {
8         command: ['echo', 'hello'],
9         container_image: 'test',
10         output_path: 'test',
11       },
12     }
13     assert_response :success
14   end
15
16   [Container::Queued, Container::Complete].each do |state|
17     test "cannot get auth in #{state} state" do
18       authorize_with :dispatch1
19       get :auth, id: containers(:queued).uuid
20       assert_response 403
21     end
22   end
23
24   test 'cannot get auth with wrong token' do
25     authorize_with :dispatch1
26     c = containers(:queued)
27     assert c.lock, show_errors(c)
28
29     authorize_with :system_user
30     get :auth, id: c.uuid
31     assert_response 403
32   end
33
34   test 'get auth' do
35     authorize_with :dispatch1
36     c = containers(:queued)
37     assert c.lock, show_errors(c)
38     get :auth, id: c.uuid
39     assert_response :success
40     assert_operator 32, :<, json_response['api_token'].length
41     assert_equal 'arvados#apiClientAuthorization', json_response['kind']
42   end
43
44   test 'no auth in container response' do
45     authorize_with :dispatch1
46     c = containers(:queued)
47     assert c.lock, show_errors(c)
48     get :show, id: c.uuid
49     assert_response :success
50     assert_nil json_response['auth']
51   end
52
53   test "lock container" do
54     authorize_with :dispatch1
55     uuid = containers(:queued).uuid
56     post :lock, {id: uuid}
57     assert_response :success
58     container = Container.where(uuid: uuid).first
59     assert_equal 'Locked', container.state
60     assert_not_nil container.locked_by_uuid
61     assert_not_nil container.auth_uuid
62   end
63
64   test "unlock container" do
65     authorize_with :dispatch1
66     uuid = containers(:locked).uuid
67     post :unlock, {id: uuid}
68     assert_response :success
69     container = Container.where(uuid: uuid).first
70     assert_equal 'Queued', container.state
71     assert_nil container.locked_by_uuid
72     assert_nil container.auth_uuid
73   end
74
75   [
76     [:queued, :lock, :success, 'Locked'],
77     [:queued, :unlock, 422, 'Queued'],
78     [:locked, :lock, 422, 'Locked'],
79     [:running, :lock, 422, 'Running'],
80     [:running, :unlock, 422, 'Running'],
81   ].each do |fixture, action, response, state|
82     test "state transitions from #{fixture } to #{action}" do
83       authorize_with :dispatch1
84       uuid = containers(fixture).uuid
85       post action, {id: uuid}
86       assert_response response
87       assert_equal state, Container.where(uuid: uuid).first.state
88     end
89   end
90
91   test 'get current container for token' do
92     authorize_with :running_container_auth
93     get :current
94     assert_response :success
95     assert_equal containers(:running).uuid, json_response['uuid']
96   end
97
98   test 'no container associated with token' do
99     authorize_with :dispatch1
100     get :current
101     assert_response 404
102   end
103
104   test 'try get current container, no token' do
105     get :current
106     assert_response 401
107   end
108
109 end