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