21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / api / test / functional / arvados / v1 / container_requests_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::ContainerRequestsControllerTest < ActionController::TestCase
8   def minimal_cr
9     {
10       command: ['echo', 'hello'],
11       container_image: 'arvados/apitestfixture:latest',
12       output_path: 'test',
13       runtime_constraints: {vcpus: 1, ram: 1}
14     }
15   end
16
17   test 'create with scheduling parameters' do
18     authorize_with :active
19
20     sp = {'partitions' => ['test1', 'test2']}
21     post :create, params: {
22            container_request: minimal_cr.merge(scheduling_parameters: sp.dup, state: "Committed")
23          }
24     assert_response :success
25
26     cr = JSON.parse(@response.body)
27     assert_not_nil cr, 'Expected container request'
28     assert_equal sp['partitions'], cr['scheduling_parameters']['partitions']
29     assert_equal false, cr['scheduling_parameters']['preemptible']
30     assert_equal false, cr['scheduling_parameters']['supervisor']
31   end
32
33   test 'create a-c-r should be supervisor' do
34     authorize_with :active
35
36     post :create, params: {
37            container_request: minimal_cr.merge(command: ["arvados-cwl-runner", "my-workflow.cwl"], state: "Committed")
38          }
39     assert_response :success
40
41     cr = JSON.parse(@response.body)
42     assert_not_nil cr, 'Expected container request'
43     assert_equal true, cr['scheduling_parameters']['supervisor']
44   end
45
46   test "secret_mounts not in #create responses" do
47     authorize_with :active
48
49     post :create, params: {
50            container_request: minimal_cr.merge(
51              secret_mounts: {'/foo' => {'kind' => 'json', 'content' => 'bar'}}),
52          }
53     assert_response :success
54
55     resp = JSON.parse(@response.body)
56     refute resp.has_key?('secret_mounts')
57
58     req = ContainerRequest.where(uuid: resp['uuid']).first
59     assert_equal 'bar', req.secret_mounts['/foo']['content']
60   end
61
62   test "update with secret_mounts" do
63     authorize_with :active
64     req = container_requests(:uncommitted)
65
66     patch :update, params: {
67             id: req.uuid,
68             container_request: {
69               secret_mounts: {'/foo' => {'kind' => 'json', 'content' => 'bar'}},
70             },
71           }
72     assert_response :success
73
74     resp = JSON.parse(@response.body)
75     refute resp.has_key?('secret_mounts')
76
77     req.reload
78     assert_equal 'bar', req.secret_mounts['/foo']['content']
79   end
80
81   test "cancel with runtime_constraints and scheduling_params with default values" do
82     authorize_with :active
83     req = container_requests(:queued)
84
85     patch :update, params: {
86       id: req.uuid,
87       container_request: {
88         state: 'Final',
89         priority: 0,
90         runtime_constraints: {
91           'vcpus' => 1,
92           'ram' => 123,
93           'keep_cache_ram' => 0,
94         },
95         scheduling_parameters: {
96           "preemptible"=>false
97         }
98       },
99     }
100     assert_response :success
101   end
102
103   test "update without deleting secret_mounts" do
104     authorize_with :active
105     req = container_requests(:uncommitted)
106     req.update!(secret_mounts: {'/foo' => {'kind' => 'json', 'content' => 'bar'}})
107
108     patch :update, params: {
109             id: req.uuid,
110             container_request: {
111               command: ['echo', 'test'],
112             },
113           }
114     assert_response :success
115
116     resp = JSON.parse(@response.body)
117     refute resp.has_key?('secret_mounts')
118
119     req.reload
120     assert_equal 'bar', req.secret_mounts['/foo']['content']
121   end
122
123   test "runtime_token not in #create responses" do
124     authorize_with :active
125
126     post :create, params: {
127            container_request: minimal_cr.merge(
128              runtime_token: api_client_authorizations(:spectator).token)
129          }
130     assert_response :success
131
132     resp = JSON.parse(@response.body)
133     refute resp.has_key?('runtime_token')
134
135     req = ContainerRequest.where(uuid: resp['uuid']).first
136     assert_equal api_client_authorizations(:spectator).token, req.runtime_token
137   end
138
139   %w(Running Complete).each do |state|
140     test "filter on container.state = #{state}" do
141       authorize_with :active
142       get :index, params: {
143             filters: [['container.state', '=', state]],
144           }
145       assert_response :success
146       assert_operator json_response['items'].length, :>, 0
147       json_response['items'].each do |cr|
148         assert_equal state, Container.find_by_uuid(cr['container_uuid']).state
149       end
150     end
151   end
152
153   test "filter on container success" do
154     authorize_with :active
155     get :index, params: {
156           filters: [
157             ['container.state', '=', 'Complete'],
158             ['container.exit_code', '=', '0'],
159           ],
160         }
161     assert_response :success
162     assert_operator json_response['items'].length, :>, 0
163     json_response['items'].each do |cr|
164       assert_equal 'Complete', Container.find_by_uuid(cr['container_uuid']).state
165       assert_equal 0, Container.find_by_uuid(cr['container_uuid']).exit_code
166     end
167   end
168
169   test "filter on container subproperty runtime_status[foo] = bar" do
170     ctr = containers(:running)
171     act_as_system_user do
172       ctr.update!(runtime_status: {foo: 'bar'})
173     end
174     authorize_with :active
175     get :index, params: {
176           filters: [
177             ['container.runtime_status.foo', '=', 'bar'],
178           ],
179         }
180     assert_response :success
181     assert_equal [ctr.uuid], json_response['items'].collect { |cr| cr['container_uuid'] }.uniq
182   end
183 end