Merge branch '9446-refactor-keep-parallel-write-strategy'
[arvados.git] / services / api / test / unit / container_request_test.rb
1 require 'test_helper'
2
3 class ContainerRequestTest < ActiveSupport::TestCase
4   def create_minimal_req! attrs={}
5     defaults = {
6       command: ["echo", "foo"],
7       container_image: links(:docker_image_collection_tag).name,
8       cwd: "/tmp",
9       environment: {},
10       mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
11       output_path: "/out",
12       runtime_constraints: {},
13       name: "foo",
14       description: "bar",
15     }
16     cr = ContainerRequest.create!(defaults.merge(attrs))
17     cr.reload
18     return cr
19   end
20
21   def check_bogus_states cr
22     [nil, "Flubber"].each do |state|
23       assert_raises(ActiveRecord::RecordInvalid) do
24         cr.state = state
25         cr.save!
26       end
27       cr.reload
28     end
29   end
30
31   test "Container request create" do
32     set_user_from_auth :active
33     cr = create_minimal_req!
34
35     assert_nil cr.container_uuid
36     assert_nil cr.priority
37
38     check_bogus_states cr
39
40     # Ensure we can modify all attributes
41     cr.command = ["echo", "foo3"]
42     cr.container_image = "img3"
43     cr.cwd = "/tmp3"
44     cr.environment = {"BUP" => "BOP"}
45     cr.mounts = {"BAR" => "BAZ"}
46     cr.output_path = "/tmp4"
47     cr.priority = 2
48     cr.runtime_constraints = {"vcpus" => 4}
49     cr.name = "foo3"
50     cr.description = "bar3"
51     cr.save!
52
53     assert_nil cr.container_uuid
54   end
55
56   test "Container request priority must be non-nil" do
57     set_user_from_auth :active
58     cr = create_minimal_req!(priority: nil)
59     cr.state = "Committed"
60     assert_raises(ActiveRecord::RecordInvalid) do
61       cr.save!
62     end
63   end
64
65   test "Container request commit" do
66     set_user_from_auth :active
67     cr = create_minimal_req!(runtime_constraints: {"vcpus" => [2,3]})
68
69     assert_nil cr.container_uuid
70
71     cr.reload
72     cr.state = "Committed"
73     cr.priority = 1
74     cr.save!
75
76     cr.reload
77
78     assert_not_nil cr.container_uuid
79     c = Container.find_by_uuid cr.container_uuid
80     assert_not_nil c
81     assert_equal ["echo", "foo"], c.command
82     assert_equal collections(:docker_image).portable_data_hash, c.container_image
83     assert_equal "/tmp", c.cwd
84     assert_equal({}, c.environment)
85     assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
86     assert_equal "/out", c.output_path
87     assert_equal({"vcpus" => 2}, c.runtime_constraints)
88     assert_equal 1, c.priority
89
90     assert_raises(ActiveRecord::RecordInvalid) do
91       cr.priority = nil
92       cr.save!
93     end
94
95     cr.priority = 0
96     cr.save!
97
98     cr.reload
99     c.reload
100     assert_equal 0, cr.priority
101     assert_equal 0, c.priority
102   end
103
104
105   test "Container request max priority" do
106     set_user_from_auth :active
107     cr = create_minimal_req!(priority: 5, state: "Committed")
108
109     c = Container.find_by_uuid cr.container_uuid
110     assert_equal 5, c.priority
111
112     cr2 = create_minimal_req!
113     cr2.priority = 10
114     cr2.state = "Committed"
115     cr2.container_uuid = cr.container_uuid
116     act_as_system_user do
117       cr2.save!
118     end
119
120     # cr and cr2 have priority 5 and 10, and are being satisfied by
121     # the same container c, so c's priority should be
122     # max(priority)=10.
123     c.reload
124     assert_equal 10, c.priority
125
126     cr2.update_attributes!(priority: 0)
127
128     c.reload
129     assert_equal 5, c.priority
130
131     cr.update_attributes!(priority: 0)
132
133     c.reload
134     assert_equal 0, c.priority
135   end
136
137
138   test "Independent container requests" do
139     set_user_from_auth :active
140     cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
141     cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
142
143     c1 = Container.find_by_uuid cr1.container_uuid
144     assert_equal 5, c1.priority
145
146     c2 = Container.find_by_uuid cr2.container_uuid
147     assert_equal 10, c2.priority
148
149     cr1.update_attributes!(priority: 0)
150
151     c1.reload
152     assert_equal 0, c1.priority
153
154     c2.reload
155     assert_equal 10, c2.priority
156   end
157
158   test "Request is finalized when its container is cancelled" do
159     set_user_from_auth :active
160     cr = create_minimal_req!(priority: 1, state: "Committed")
161
162     act_as_system_user do
163       Container.find_by_uuid(cr.container_uuid).
164         update_attributes!(state: Container::Cancelled)
165     end
166
167     cr.reload
168     assert_equal "Final", cr.state
169   end
170
171   test "Request is finalized when its container is completed" do
172     set_user_from_auth :active
173     cr = create_minimal_req!(priority: 1, state: "Committed")
174
175     c = act_as_system_user do
176       c = Container.find_by_uuid(cr.container_uuid)
177       c.update_attributes!(state: Container::Locked)
178       c.update_attributes!(state: Container::Running)
179       c
180     end
181
182     cr.reload
183     assert_equal "Committed", cr.state
184
185     act_as_system_user do
186       c.update_attributes!(state: Container::Complete)
187     end
188
189     cr.reload
190     assert_equal "Final", cr.state
191   end
192
193   test "Container makes container request, then is cancelled" do
194     set_user_from_auth :active
195     cr = create_minimal_req!(priority: 5, state: "Committed")
196
197     c = Container.find_by_uuid cr.container_uuid
198     assert_equal 5, c.priority
199
200     cr2 = create_minimal_req!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid)
201
202     c2 = Container.find_by_uuid cr2.container_uuid
203     assert_equal 10, c2.priority
204
205     act_as_system_user do
206       c.state = "Cancelled"
207       c.save!
208     end
209
210     cr.reload
211     assert_equal "Final", cr.state
212
213     cr2.reload
214     assert_equal 0, cr2.priority
215
216     c2.reload
217     assert_equal 0, c2.priority
218   end
219
220   [
221     ['active', 'zzzzz-dz642-runningcontainr'],
222     ['active_no_prefs', nil],
223   ].each do |token, expected|
224     test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
225       set_user_from_auth token
226       cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
227       assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
228       assert_equal expected, cr.requesting_container_uuid
229     end
230   end
231
232   [[{"vcpus" => [2, nil]},
233     lambda { |resolved| resolved["vcpus"] == 2 }],
234    [{"vcpus" => [3, 7]},
235     lambda { |resolved| resolved["vcpus"] == 3 }],
236    [{"vcpus" => 4},
237     lambda { |resolved| resolved["vcpus"] == 4 }],
238    [{"ram" => [1000000000, 2000000000]},
239     lambda { |resolved| resolved["ram"] == 1000000000 }],
240    [{"ram" => [1234234234]},
241     lambda { |resolved| resolved["ram"] == 1234234234 }],
242   ].each do |rc, okfunc|
243     test "resolve runtime constraint range #{rc} to values" do
244       cr = ContainerRequest.new(runtime_constraints: rc)
245       resolved = cr.send :runtime_constraints_for_container
246       assert(okfunc.call(resolved),
247              "container runtime_constraints was #{resolved.inspect}")
248     end
249   end
250
251   [[{"/out" => {
252         "kind" => "collection",
253         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
254         "path" => "/foo"}},
255     lambda do |resolved|
256       resolved["/out"] == {
257         "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
258         "kind" => "collection",
259         "path" => "/foo",
260       }
261     end],
262    [{"/out" => {
263         "kind" => "collection",
264         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
265         "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
266         "path" => "/foo"}},
267     lambda do |resolved|
268       resolved["/out"] == {
269         "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
270         "kind" => "collection",
271         "path" => "/foo",
272       }
273     end],
274   ].each do |mounts, okfunc|
275     test "resolve mounts #{mounts.inspect} to values" do
276       set_user_from_auth :active
277       cr = ContainerRequest.new(mounts: mounts)
278       resolved = cr.send :mounts_for_container
279       assert(okfunc.call(resolved),
280              "mounts_for_container returned #{resolved.inspect}")
281     end
282   end
283
284   test 'mount unreadable collection' do
285     set_user_from_auth :spectator
286     m = {
287       "/foo" => {
288         "kind" => "collection",
289         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
290         "path" => "/foo",
291       },
292     }
293     cr = ContainerRequest.new(mounts: m)
294     assert_raises(ActiveRecord::RecordNotFound) do
295       cr.send :mounts_for_container
296     end
297   end
298
299   test 'mount collection with mismatched UUID and PDH' do
300     set_user_from_auth :active
301     m = {
302       "/foo" => {
303         "kind" => "collection",
304         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
305         "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
306         "path" => "/foo",
307       },
308     }
309     cr = ContainerRequest.new(mounts: m)
310     assert_raises(ArgumentError) do
311       cr.send :mounts_for_container
312     end
313   end
314
315   ['arvados/apitestfixture:latest',
316    'arvados/apitestfixture',
317    'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
318   ].each do |tag|
319     test "container_image_for_container(#{tag.inspect})" do
320       set_user_from_auth :active
321       cr = ContainerRequest.new(container_image: tag)
322       resolved = cr.send :container_image_for_container
323       assert_equal resolved, collections(:docker_image).portable_data_hash
324     end
325   end
326
327   test "container_image_for_container(pdh)" do
328     set_user_from_auth :active
329     pdh = collections(:docker_image).portable_data_hash
330     cr = ContainerRequest.new(container_image: pdh)
331     resolved = cr.send :container_image_for_container
332     assert_equal resolved, pdh
333   end
334
335   ['acbd18db4cc2f85cedef654fccc4a4d8+3',
336    'ENOEXIST',
337    'arvados/apitestfixture:ENOEXIST',
338   ].each do |img|
339     test "container_image_for_container(#{img.inspect}) => 404" do
340       set_user_from_auth :active
341       cr = ContainerRequest.new(container_image: img)
342       assert_raises(ActiveRecord::RecordNotFound) do
343         cr.send :container_image_for_container
344       end
345     end
346   end
347 end