11305: added kernel and module dependency for docker migration in doc
[arvados.git] / services / api / test / unit / container_request_test.rb
1 require 'test_helper'
2 require 'helpers/docker_migration_helper'
3
4 class ContainerRequestTest < ActiveSupport::TestCase
5   include DockerMigrationHelper
6
7   def create_minimal_req! attrs={}
8     defaults = {
9       command: ["echo", "foo"],
10       container_image: links(:docker_image_collection_tag).name,
11       cwd: "/tmp",
12       environment: {},
13       mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
14       output_path: "/out",
15       runtime_constraints: {"vcpus" => 1, "ram" => 2},
16       name: "foo",
17       description: "bar",
18     }
19     cr = ContainerRequest.create!(defaults.merge(attrs))
20     cr.reload
21     return cr
22   end
23
24   def check_bogus_states cr
25     [nil, "Flubber"].each do |state|
26       assert_raises(ActiveRecord::RecordInvalid) do
27         cr.state = state
28         cr.save!
29       end
30       cr.reload
31     end
32   end
33
34   test "Container request create" do
35     set_user_from_auth :active
36     cr = create_minimal_req!
37
38     assert_nil cr.container_uuid
39     assert_nil cr.priority
40
41     check_bogus_states cr
42
43     # Ensure we can modify all attributes
44     cr.command = ["echo", "foo3"]
45     cr.container_image = "img3"
46     cr.cwd = "/tmp3"
47     cr.environment = {"BUP" => "BOP"}
48     cr.mounts = {"BAR" => "BAZ"}
49     cr.output_path = "/tmp4"
50     cr.priority = 2
51     cr.runtime_constraints = {"vcpus" => 4}
52     cr.name = "foo3"
53     cr.description = "bar3"
54     cr.save!
55
56     assert_nil cr.container_uuid
57   end
58
59   [
60     {"vcpus" => 1},
61     {"vcpus" => 1, "ram" => nil},
62     {"vcpus" => 0, "ram" => 123},
63     {"vcpus" => "1", "ram" => "123"}
64   ].each do |invalid_constraints|
65     test "Create with #{invalid_constraints}" do
66       set_user_from_auth :active
67       assert_raises(ActiveRecord::RecordInvalid) do
68         cr = create_minimal_req!(state: "Committed",
69                                  priority: 1,
70                                  runtime_constraints: invalid_constraints)
71         cr.save!
72       end
73     end
74
75     test "Update with #{invalid_constraints}" do
76       set_user_from_auth :active
77       cr = create_minimal_req!(state: "Uncommitted", priority: 1)
78       cr.save!
79       assert_raises(ActiveRecord::RecordInvalid) do
80         cr = ContainerRequest.find_by_uuid cr.uuid
81         cr.update_attributes!(state: "Committed",
82                               runtime_constraints: invalid_constraints)
83       end
84     end
85   end
86
87   test "Update from fixture" do
88     set_user_from_auth :active
89     cr = ContainerRequest.find_by_uuid(container_requests(:running).uuid)
90     cr.update_attributes!(description: "New description")
91     assert_equal "New description", cr.description
92   end
93
94   test "Update with valid runtime constraints" do
95       set_user_from_auth :active
96       cr = create_minimal_req!(state: "Uncommitted", priority: 1)
97       cr.save!
98       cr = ContainerRequest.find_by_uuid cr.uuid
99       cr.update_attributes!(state: "Committed",
100                             runtime_constraints: {"vcpus" => 1, "ram" => 23})
101       assert_not_nil cr.container_uuid
102   end
103
104   test "Container request priority must be non-nil" do
105     set_user_from_auth :active
106     cr = create_minimal_req!(priority: nil)
107     cr.state = "Committed"
108     assert_raises(ActiveRecord::RecordInvalid) do
109       cr.save!
110     end
111   end
112
113   test "Container request commit" do
114     set_user_from_auth :active
115     cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 30})
116
117     assert_nil cr.container_uuid
118
119     cr.reload
120     cr.state = "Committed"
121     cr.priority = 1
122     cr.save!
123
124     cr.reload
125
126     assert_not_nil cr.container_uuid
127     c = Container.find_by_uuid cr.container_uuid
128     assert_not_nil c
129     assert_equal ["echo", "foo"], c.command
130     assert_equal collections(:docker_image).portable_data_hash, c.container_image
131     assert_equal "/tmp", c.cwd
132     assert_equal({}, c.environment)
133     assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
134     assert_equal "/out", c.output_path
135     assert_equal({"keep_cache_ram"=>268435456, "vcpus" => 2, "ram" => 30}, c.runtime_constraints)
136     assert_equal 1, c.priority
137
138     assert_raises(ActiveRecord::RecordInvalid) do
139       cr.priority = nil
140       cr.save!
141     end
142
143     cr.priority = 0
144     cr.save!
145
146     cr.reload
147     c.reload
148     assert_equal 0, cr.priority
149     assert_equal 0, c.priority
150   end
151
152
153   test "Container request max priority" do
154     set_user_from_auth :active
155     cr = create_minimal_req!(priority: 5, state: "Committed")
156
157     c = Container.find_by_uuid cr.container_uuid
158     assert_equal 5, c.priority
159
160     cr2 = create_minimal_req!
161     cr2.priority = 10
162     cr2.state = "Committed"
163     cr2.container_uuid = cr.container_uuid
164     act_as_system_user do
165       cr2.save!
166     end
167
168     # cr and cr2 have priority 5 and 10, and are being satisfied by
169     # the same container c, so c's priority should be
170     # max(priority)=10.
171     c.reload
172     assert_equal 10, c.priority
173
174     cr2.update_attributes!(priority: 0)
175
176     c.reload
177     assert_equal 5, c.priority
178
179     cr.update_attributes!(priority: 0)
180
181     c.reload
182     assert_equal 0, c.priority
183   end
184
185
186   test "Independent container requests" do
187     set_user_from_auth :active
188     cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
189     cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
190
191     c1 = Container.find_by_uuid cr1.container_uuid
192     assert_equal 5, c1.priority
193
194     c2 = Container.find_by_uuid cr2.container_uuid
195     assert_equal 10, c2.priority
196
197     cr1.update_attributes!(priority: 0)
198
199     c1.reload
200     assert_equal 0, c1.priority
201
202     c2.reload
203     assert_equal 10, c2.priority
204   end
205
206   test "Request is finalized when its container is cancelled" do
207     set_user_from_auth :active
208     cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1)
209
210     act_as_system_user do
211       Container.find_by_uuid(cr.container_uuid).
212         update_attributes!(state: Container::Cancelled)
213     end
214
215     cr.reload
216     assert_equal "Final", cr.state
217   end
218
219   test "Request is finalized when its container is completed" do
220     set_user_from_auth :active
221     project = groups(:private)
222     cr = create_minimal_req!(owner_uuid: project.uuid,
223                              priority: 1,
224                              state: "Committed")
225
226     c = act_as_system_user do
227       c = Container.find_by_uuid(cr.container_uuid)
228       c.update_attributes!(state: Container::Locked)
229       c.update_attributes!(state: Container::Running)
230       c
231     end
232
233     cr.reload
234     assert_equal "Committed", cr.state
235
236     output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
237     log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
238     act_as_system_user do
239       c.update_attributes!(state: Container::Complete,
240                            output: output_pdh,
241                            log: log_pdh)
242     end
243
244     cr.reload
245     assert_equal "Final", cr.state
246     ['output', 'log'].each do |out_type|
247       pdh = Container.find_by_uuid(cr.container_uuid).send(out_type)
248       assert_equal(1, Collection.where(portable_data_hash: pdh,
249                                        owner_uuid: project.uuid).count,
250                    "Container #{out_type} should be copied to #{project.uuid}")
251     end
252     assert_not_nil cr.output_uuid
253     assert_not_nil cr.log_uuid
254     output = Collection.find_by_uuid cr.output_uuid
255     assert_equal output_pdh, output.portable_data_hash
256     log = Collection.find_by_uuid cr.log_uuid
257     assert_equal log_pdh, log.portable_data_hash
258   end
259
260   test "Container makes container request, then is cancelled" do
261     set_user_from_auth :active
262     cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
263
264     c = Container.find_by_uuid cr.container_uuid
265     assert_equal 5, c.priority
266
267     cr2 = create_minimal_req!
268     cr2.update_attributes!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid, command: ["echo", "foo2"], container_count_max: 1)
269     cr2.reload
270
271     c2 = Container.find_by_uuid cr2.container_uuid
272     assert_equal 10, c2.priority
273
274     act_as_system_user do
275       c.state = "Cancelled"
276       c.save!
277     end
278
279     cr.reload
280     assert_equal "Final", cr.state
281
282     cr2.reload
283     assert_equal 0, cr2.priority
284
285     c2.reload
286     assert_equal 0, c2.priority
287   end
288
289   [
290     ['running_container_auth', 'zzzzz-dz642-runningcontainr'],
291     ['active_no_prefs', nil],
292   ].each do |token, expected|
293     test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
294       set_user_from_auth token
295       cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
296       assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
297       assert_equal expected, cr.requesting_container_uuid
298     end
299   end
300
301   [[{"vcpus" => [2, nil]},
302     lambda { |resolved| resolved["vcpus"] == 2 }],
303    [{"vcpus" => [3, 7]},
304     lambda { |resolved| resolved["vcpus"] == 3 }],
305    [{"vcpus" => 4},
306     lambda { |resolved| resolved["vcpus"] == 4 }],
307    [{"ram" => [1000000000, 2000000000]},
308     lambda { |resolved| resolved["ram"] == 1000000000 }],
309    [{"ram" => [1234234234]},
310     lambda { |resolved| resolved["ram"] == 1234234234 }],
311   ].each do |rc, okfunc|
312     test "resolve runtime constraint range #{rc} to values" do
313       cr = ContainerRequest.new(runtime_constraints: rc)
314       resolved = cr.send :runtime_constraints_for_container
315       assert(okfunc.call(resolved),
316              "container runtime_constraints was #{resolved.inspect}")
317     end
318   end
319
320   [[{"/out" => {
321         "kind" => "collection",
322         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
323         "path" => "/foo"}},
324     lambda do |resolved|
325       resolved["/out"] == {
326         "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
327         "kind" => "collection",
328         "path" => "/foo",
329       }
330     end],
331    [{"/out" => {
332         "kind" => "collection",
333         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
334         "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
335         "path" => "/foo"}},
336     lambda do |resolved|
337       resolved["/out"] == {
338         "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
339         "kind" => "collection",
340         "path" => "/foo",
341       }
342     end],
343   ].each do |mounts, okfunc|
344     test "resolve mounts #{mounts.inspect} to values" do
345       set_user_from_auth :active
346       cr = ContainerRequest.new(mounts: mounts)
347       resolved = cr.send :mounts_for_container
348       assert(okfunc.call(resolved),
349              "mounts_for_container returned #{resolved.inspect}")
350     end
351   end
352
353   test 'mount unreadable collection' do
354     set_user_from_auth :spectator
355     m = {
356       "/foo" => {
357         "kind" => "collection",
358         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
359         "path" => "/foo",
360       },
361     }
362     cr = ContainerRequest.new(mounts: m)
363     assert_raises(ArvadosModel::UnresolvableContainerError) do
364       cr.send :mounts_for_container
365     end
366   end
367
368   test 'mount collection with mismatched UUID and PDH' do
369     set_user_from_auth :active
370     m = {
371       "/foo" => {
372         "kind" => "collection",
373         "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
374         "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
375         "path" => "/foo",
376       },
377     }
378     cr = ContainerRequest.new(mounts: m)
379     assert_raises(ArgumentError) do
380       cr.send :mounts_for_container
381     end
382   end
383
384   ['arvados/apitestfixture:latest',
385    'arvados/apitestfixture',
386    'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
387   ].each do |tag|
388     test "container_image_for_container(#{tag.inspect})" do
389       set_user_from_auth :active
390       cr = ContainerRequest.new(container_image: tag)
391       resolved = cr.send :container_image_for_container
392       assert_equal resolved, collections(:docker_image).portable_data_hash
393     end
394   end
395
396   test "container_image_for_container(pdh)" do
397     set_user_from_auth :active
398     [[:docker_image, 'v1'], [:docker_image_1_12, 'v2']].each do |coll, ver|
399       Rails.configuration.docker_image_formats = [ver]
400       pdh = collections(coll).portable_data_hash
401       cr = ContainerRequest.new(container_image: pdh)
402       resolved = cr.send :container_image_for_container
403       assert_equal resolved, pdh
404     end
405   end
406
407   ['acbd18db4cc2f85cedef654fccc4a4d8+3',
408    'ENOEXIST',
409    'arvados/apitestfixture:ENOEXIST',
410   ].each do |img|
411     test "container_image_for_container(#{img.inspect}) => 422" do
412       set_user_from_auth :active
413       cr = ContainerRequest.new(container_image: img)
414       assert_raises(ArvadosModel::UnresolvableContainerError) do
415         cr.send :container_image_for_container
416       end
417     end
418   end
419
420   test "migrated docker image" do
421     Rails.configuration.docker_image_formats = ['v2']
422     add_docker19_migration_link
423
424     # Test that it returns only v2 images even though request is for v1 image.
425
426     set_user_from_auth :active
427     cr = create_minimal_req!(command: ["true", "1"],
428                              container_image: collections(:docker_image).portable_data_hash)
429     assert_equal(cr.send(:container_image_for_container),
430                  collections(:docker_image_1_12).portable_data_hash)
431
432     cr = create_minimal_req!(command: ["true", "2"],
433                              container_image: links(:docker_image_collection_tag).name)
434     assert_equal(cr.send(:container_image_for_container),
435                  collections(:docker_image_1_12).portable_data_hash)
436   end
437
438   test "use unmigrated docker image" do
439     Rails.configuration.docker_image_formats = ['v1']
440     add_docker19_migration_link
441
442     # Test that it returns only supported v1 images even though there is a
443     # migration link.
444
445     set_user_from_auth :active
446     cr = create_minimal_req!(command: ["true", "1"],
447                              container_image: collections(:docker_image).portable_data_hash)
448     assert_equal(cr.send(:container_image_for_container),
449                  collections(:docker_image).portable_data_hash)
450
451     cr = create_minimal_req!(command: ["true", "2"],
452                              container_image: links(:docker_image_collection_tag).name)
453     assert_equal(cr.send(:container_image_for_container),
454                  collections(:docker_image).portable_data_hash)
455   end
456
457   test "incompatible docker image v1" do
458     Rails.configuration.docker_image_formats = ['v1']
459     add_docker19_migration_link
460
461     # Don't return unsupported v2 image even if we ask for it directly.
462     set_user_from_auth :active
463     cr = create_minimal_req!(command: ["true", "1"],
464                              container_image: collections(:docker_image_1_12).portable_data_hash)
465     assert_raises(ArvadosModel::UnresolvableContainerError) do
466       cr.send(:container_image_for_container)
467     end
468   end
469
470   test "incompatible docker image v2" do
471     Rails.configuration.docker_image_formats = ['v2']
472     # No migration link, don't return unsupported v1 image,
473
474     set_user_from_auth :active
475     cr = create_minimal_req!(command: ["true", "1"],
476                              container_image: collections(:docker_image).portable_data_hash)
477     assert_raises(ArvadosModel::UnresolvableContainerError) do
478       cr.send(:container_image_for_container)
479     end
480     cr = create_minimal_req!(command: ["true", "2"],
481                              container_image: links(:docker_image_collection_tag).name)
482     assert_raises(ArvadosModel::UnresolvableContainerError) do
483       cr.send(:container_image_for_container)
484     end
485   end
486
487   test "requestor can retrieve container owned by dispatch" do
488     assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
489     assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
490     assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
491   end
492
493   [
494     [{"var" => "value1"}, {"var" => "value1"}, nil],
495     [{"var" => "value1"}, {"var" => "value1"}, true],
496     [{"var" => "value1"}, {"var" => "value1"}, false],
497     [{"var" => "value1"}, {"var" => "value2"}, nil],
498   ].each do |env1, env2, use_existing|
499     test "Container request #{((env1 == env2) and (use_existing.nil? or use_existing == true)) ? 'does' : 'does not'} reuse container when committed#{use_existing.nil? ? '' : use_existing ? ' and use_existing == true' : ' and use_existing == false'}" do
500       common_attrs = {cwd: "test",
501                       priority: 1,
502                       command: ["echo", "hello"],
503                       output_path: "test",
504                       runtime_constraints: {"vcpus" => 4,
505                                             "ram" => 12000000000,
506                                             "keep_cache_ram" => 268435456},
507                       mounts: {"test" => {"kind" => "json"}}}
508       set_user_from_auth :active
509       cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
510                                                     environment: env1}))
511       if use_existing.nil?
512         # Testing with use_existing default value
513         cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
514                                                       environment: env2}))
515       else
516
517         cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
518                                                       environment: env2,
519                                                       use_existing: use_existing}))
520       end
521       assert_not_nil cr1.container_uuid
522       assert_nil cr2.container_uuid
523
524       # Update cr2 to commited state and check for container equality on different cases:
525       # * When env1 and env2 are equal and use_existing is true, the same container
526       #   should be assigned.
527       # * When use_existing is false, a different container should be assigned.
528       # * When env1 and env2 are different, a different container should be assigned.
529       cr2.update_attributes!({state: ContainerRequest::Committed})
530       assert_equal (cr2.use_existing == true and (env1 == env2)),
531                    (cr1.container_uuid == cr2.container_uuid)
532     end
533   end
534
535   test "requesting_container_uuid at create is not allowed" do
536     set_user_from_auth :active
537     assert_raises(ActiveRecord::RecordNotSaved) do
538       create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
539     end
540   end
541
542   test "Retry on container cancelled" do
543     set_user_from_auth :active
544     cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
545     cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
546     prev_container_uuid = cr.container_uuid
547
548     c = act_as_system_user do
549       c = Container.find_by_uuid(cr.container_uuid)
550       c.update_attributes!(state: Container::Locked)
551       c.update_attributes!(state: Container::Running)
552       c
553     end
554
555     cr.reload
556     cr2.reload
557     assert_equal "Committed", cr.state
558     assert_equal prev_container_uuid, cr.container_uuid
559     assert_not_equal cr2.container_uuid, cr.container_uuid
560     prev_container_uuid = cr.container_uuid
561
562     act_as_system_user do
563       c.update_attributes!(state: Container::Cancelled)
564     end
565
566     cr.reload
567     cr2.reload
568     assert_equal "Committed", cr.state
569     assert_not_equal prev_container_uuid, cr.container_uuid
570     assert_not_equal cr2.container_uuid, cr.container_uuid
571     prev_container_uuid = cr.container_uuid
572
573     c = act_as_system_user do
574       c = Container.find_by_uuid(cr.container_uuid)
575       c.update_attributes!(state: Container::Cancelled)
576       c
577     end
578
579     cr.reload
580     cr2.reload
581     assert_equal "Final", cr.state
582     assert_equal prev_container_uuid, cr.container_uuid
583     assert_not_equal cr2.container_uuid, cr.container_uuid
584   end
585
586   test "Output collection name setting using output_name with name collision resolution" do
587     set_user_from_auth :active
588     output_name = collections(:foo_file).name
589
590     cr = create_minimal_req!(priority: 1,
591                              state: ContainerRequest::Committed,
592                              output_name: output_name)
593     act_as_system_user do
594       c = Container.find_by_uuid(cr.container_uuid)
595       c.update_attributes!(state: Container::Locked)
596       c.update_attributes!(state: Container::Running)
597       c.update_attributes!(state: Container::Complete,
598                            exit_code: 0,
599                            output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
600                            log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
601     end
602     cr.save
603     assert_equal ContainerRequest::Final, cr.state
604     output_coll = Collection.find_by_uuid(cr.output_uuid)
605     # Make sure the resulting output collection name include the original name
606     # plus the date
607     assert_not_equal output_name, output_coll.name,
608                      "It shouldn't exist more than one collection with the same owner and name '${output_name}'"
609     assert output_coll.name.include?(output_name),
610            "New name should include original name"
611     assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/, output_coll.name,
612                  "New name should include ISO8601 date"
613   end
614
615   test "Finalize committed request when reusing a finished container" do
616     set_user_from_auth :active
617     cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
618     cr.reload
619     assert_equal ContainerRequest::Committed, cr.state
620     act_as_system_user do
621       c = Container.find_by_uuid(cr.container_uuid)
622       c.update_attributes!(state: Container::Locked)
623       c.update_attributes!(state: Container::Running)
624       c.update_attributes!(state: Container::Complete,
625                            exit_code: 0,
626                            output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
627                            log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
628     end
629     cr.reload
630     assert_equal ContainerRequest::Final, cr.state
631
632     cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
633     assert_equal cr.container_uuid, cr2.container_uuid
634     assert_equal ContainerRequest::Final, cr2.state
635
636     cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
637     assert_equal ContainerRequest::Uncommitted, cr3.state
638     cr3.update_attributes!(state: ContainerRequest::Committed)
639     assert_equal cr.container_uuid, cr3.container_uuid
640     assert_equal ContainerRequest::Final, cr3.state
641   end
642
643   [
644     [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => 100}, ContainerRequest::Committed, 100],
645     [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Uncommitted],
646     [{"vcpus" => 1, "ram" => 123}, ContainerRequest::Committed],
647     [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => -1}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
648     [{"vcpus" => 1, "ram" => 123, "keep_cache_ram" => '123'}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
649   ].each do |rc, state, expected|
650     test "create container request with #{rc} in state #{state} and verify keep_cache_ram #{expected}" do
651       common_attrs = {cwd: "test",
652                       priority: 1,
653                       command: ["echo", "hello"],
654                       output_path: "test",
655                       runtime_constraints: rc,
656                       mounts: {"test" => {"kind" => "json"}}}
657       set_user_from_auth :active
658
659       if expected == ActiveRecord::RecordInvalid
660         assert_raises(ActiveRecord::RecordInvalid) do
661           create_minimal_req!(common_attrs.merge({state: state}))
662         end
663       else
664         cr = create_minimal_req!(common_attrs.merge({state: state}))
665         expected = Rails.configuration.container_default_keep_cache_ram if state == ContainerRequest::Committed and expected.nil?
666         assert_equal expected, cr.runtime_constraints['keep_cache_ram']
667       end
668     end
669   end
670
671   [
672     [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
673     [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Uncommitted],
674     [{"partitions" => "fastcpu"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
675     [{"partitions" => "fastcpu"}, ContainerRequest::Uncommitted],
676     [{"partitions" => ["fastcpu","vfastcpu"]}, ContainerRequest::Committed],
677   ].each do |sp, state, expected|
678     test "create container request with scheduling_parameters #{sp} in state #{state} and verify #{expected}" do
679       common_attrs = {cwd: "test",
680                       priority: 1,
681                       command: ["echo", "hello"],
682                       output_path: "test",
683                       scheduling_parameters: sp,
684                       mounts: {"test" => {"kind" => "json"}}}
685       set_user_from_auth :active
686
687       if expected == ActiveRecord::RecordInvalid
688         assert_raises(ActiveRecord::RecordInvalid) do
689           create_minimal_req!(common_attrs.merge({state: state}))
690         end
691       else
692         cr = create_minimal_req!(common_attrs.merge({state: state}))
693         assert_equal sp, cr.scheduling_parameters
694
695         if state == ContainerRequest::Committed
696           c = Container.find_by_uuid(cr.container_uuid)
697           assert_equal sp, c.scheduling_parameters
698         end
699       end
700     end
701   end
702 end