2 require 'helpers/docker_migration_helper'
4 class ContainerRequestTest < ActiveSupport::TestCase
5 include DockerMigrationHelper
7 def create_minimal_req! attrs={}
9 command: ["echo", "foo"],
10 container_image: links(:docker_image_collection_tag).name,
13 mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
15 runtime_constraints: {"vcpus" => 1, "ram" => 2},
19 cr = ContainerRequest.create!(defaults.merge(attrs))
24 def check_bogus_states cr
25 [nil, "Flubber"].each do |state|
26 assert_raises(ActiveRecord::RecordInvalid) do
34 test "Container request create" do
35 set_user_from_auth :active
36 cr = create_minimal_req!
38 assert_nil cr.container_uuid
39 assert_nil cr.priority
43 # Ensure we can modify all attributes
44 cr.command = ["echo", "foo3"]
45 cr.container_image = "img3"
47 cr.environment = {"BUP" => "BOP"}
48 cr.mounts = {"BAR" => "BAZ"}
49 cr.output_path = "/tmp4"
51 cr.runtime_constraints = {"vcpus" => 4}
53 cr.description = "bar3"
56 assert_nil cr.container_uuid
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",
70 runtime_constraints: invalid_constraints)
75 test "Update with #{invalid_constraints}" do
76 set_user_from_auth :active
77 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
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)
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
94 test "Update with valid runtime constraints" do
95 set_user_from_auth :active
96 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
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
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
113 test "Container request commit" do
114 set_user_from_auth :active
115 cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 30})
117 assert_nil cr.container_uuid
120 cr.state = "Committed"
126 assert_equal({"vcpus" => 2, "ram" => 30}, cr.runtime_constraints)
128 assert_not_nil cr.container_uuid
129 c = Container.find_by_uuid cr.container_uuid
131 assert_equal ["echo", "foo"], c.command
132 assert_equal collections(:docker_image).portable_data_hash, c.container_image
133 assert_equal "/tmp", c.cwd
134 assert_equal({}, c.environment)
135 assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
136 assert_equal "/out", c.output_path
137 assert_equal({"keep_cache_ram"=>268435456, "vcpus" => 2, "ram" => 30}, c.runtime_constraints)
138 assert_equal 1, c.priority
140 assert_raises(ActiveRecord::RecordInvalid) do
150 assert_equal 0, cr.priority
151 assert_equal 0, c.priority
155 test "Container request max priority" do
156 set_user_from_auth :active
157 cr = create_minimal_req!(priority: 5, state: "Committed")
159 c = Container.find_by_uuid cr.container_uuid
160 assert_equal 5, c.priority
162 cr2 = create_minimal_req!
164 cr2.state = "Committed"
165 cr2.container_uuid = cr.container_uuid
166 act_as_system_user do
170 # cr and cr2 have priority 5 and 10, and are being satisfied by
171 # the same container c, so c's priority should be
174 assert_equal 10, c.priority
176 cr2.update_attributes!(priority: 0)
179 assert_equal 5, c.priority
181 cr.update_attributes!(priority: 0)
184 assert_equal 0, c.priority
188 test "Independent container requests" do
189 set_user_from_auth :active
190 cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
191 cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
193 c1 = Container.find_by_uuid cr1.container_uuid
194 assert_equal 5, c1.priority
196 c2 = Container.find_by_uuid cr2.container_uuid
197 assert_equal 10, c2.priority
199 cr1.update_attributes!(priority: 0)
202 assert_equal 0, c1.priority
205 assert_equal 10, c2.priority
208 test "Request is finalized when its container is cancelled" do
209 set_user_from_auth :active
210 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1)
212 act_as_system_user do
213 Container.find_by_uuid(cr.container_uuid).
214 update_attributes!(state: Container::Cancelled)
218 assert_equal "Final", cr.state
221 test "Request is finalized when its container is completed" do
222 set_user_from_auth :active
223 project = groups(:private)
224 cr = create_minimal_req!(owner_uuid: project.uuid,
228 c = act_as_system_user do
229 c = Container.find_by_uuid(cr.container_uuid)
230 c.update_attributes!(state: Container::Locked)
231 c.update_attributes!(state: Container::Running)
236 assert_equal "Committed", cr.state
238 output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
239 log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
240 act_as_system_user do
241 c.update_attributes!(state: Container::Complete,
247 assert_equal "Final", cr.state
248 ['output', 'log'].each do |out_type|
249 pdh = Container.find_by_uuid(cr.container_uuid).send(out_type)
250 assert_equal(1, Collection.where(portable_data_hash: pdh,
251 owner_uuid: project.uuid).count,
252 "Container #{out_type} should be copied to #{project.uuid}")
254 assert_not_nil cr.output_uuid
255 assert_not_nil cr.log_uuid
256 output = Collection.find_by_uuid cr.output_uuid
257 assert_equal output_pdh, output.portable_data_hash
258 log = Collection.find_by_uuid cr.log_uuid
259 assert_equal log_pdh, log.portable_data_hash
262 test "Container makes container request, then is cancelled" do
263 set_user_from_auth :active
264 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
266 c = Container.find_by_uuid cr.container_uuid
267 assert_equal 5, c.priority
269 cr2 = create_minimal_req!
270 cr2.update_attributes!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid, command: ["echo", "foo2"], container_count_max: 1)
273 c2 = Container.find_by_uuid cr2.container_uuid
274 assert_equal 10, c2.priority
276 act_as_system_user do
277 c.state = "Cancelled"
282 assert_equal "Final", cr.state
285 assert_equal 0, cr2.priority
288 assert_equal 0, c2.priority
292 ['running_container_auth', 'zzzzz-dz642-runningcontainr'],
293 ['active_no_prefs', nil],
294 ].each do |token, expected|
295 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
296 set_user_from_auth token
297 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
298 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
299 assert_equal expected, cr.requesting_container_uuid
303 [[{"vcpus" => [2, nil]},
304 lambda { |resolved| resolved["vcpus"] == 2 }],
305 [{"vcpus" => [3, 7]},
306 lambda { |resolved| resolved["vcpus"] == 3 }],
308 lambda { |resolved| resolved["vcpus"] == 4 }],
309 [{"ram" => [1000000000, 2000000000]},
310 lambda { |resolved| resolved["ram"] == 1000000000 }],
311 [{"ram" => [1234234234]},
312 lambda { |resolved| resolved["ram"] == 1234234234 }],
313 ].each do |rc, okfunc|
314 test "resolve runtime constraint range #{rc} to values" do
315 resolved = Container.resolve_runtime_constraints(rc)
316 assert(okfunc.call(resolved),
317 "container runtime_constraints was #{resolved.inspect}")
322 "kind" => "collection",
323 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
326 resolved["/out"] == {
327 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
328 "kind" => "collection",
333 "kind" => "collection",
334 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
335 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
338 resolved["/out"] == {
339 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
340 "kind" => "collection",
344 ].each do |mounts, okfunc|
345 test "resolve mounts #{mounts.inspect} to values" do
346 set_user_from_auth :active
347 resolved = Container.resolve_mounts(mounts)
348 assert(okfunc.call(resolved),
349 "Container.resolve_mounts returned #{resolved.inspect}")
353 test 'mount unreadable collection' do
354 set_user_from_auth :spectator
357 "kind" => "collection",
358 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
362 assert_raises(ArvadosModel::UnresolvableContainerError) do
363 Container.resolve_mounts(m)
367 test 'mount collection with mismatched UUID and PDH' do
368 set_user_from_auth :active
371 "kind" => "collection",
372 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
373 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
377 assert_raises(ArgumentError) do
378 Container.resolve_mounts(m)
382 ['arvados/apitestfixture:latest',
383 'arvados/apitestfixture',
384 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
386 test "Container.resolve_container_image(#{tag.inspect})" do
387 set_user_from_auth :active
388 resolved = Container.resolve_container_image(tag)
389 assert_equal resolved, collections(:docker_image).portable_data_hash
393 test "Container.resolve_container_image(pdh)" do
394 set_user_from_auth :active
395 [[:docker_image, 'v1'], [:docker_image_1_12, 'v2']].each do |coll, ver|
396 Rails.configuration.docker_image_formats = [ver]
397 pdh = collections(coll).portable_data_hash
398 resolved = Container.resolve_container_image(pdh)
399 assert_equal resolved, pdh
403 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
405 'arvados/apitestfixture:ENOEXIST',
407 test "container_image_for_container(#{img.inspect}) => 422" do
408 set_user_from_auth :active
409 assert_raises(ArvadosModel::UnresolvableContainerError) do
410 Container.resolve_container_image(img)
415 test "migrated docker image" do
416 Rails.configuration.docker_image_formats = ['v2']
417 add_docker19_migration_link
419 # Test that it returns only v2 images even though request is for v1 image.
421 set_user_from_auth :active
422 cr = create_minimal_req!(command: ["true", "1"],
423 container_image: collections(:docker_image).portable_data_hash)
424 assert_equal(Container.resolve_container_image(cr.container_image),
425 collections(:docker_image_1_12).portable_data_hash)
427 cr = create_minimal_req!(command: ["true", "2"],
428 container_image: links(:docker_image_collection_tag).name)
429 assert_equal(Container.resolve_container_image(cr.container_image),
430 collections(:docker_image_1_12).portable_data_hash)
433 test "use unmigrated docker image" do
434 Rails.configuration.docker_image_formats = ['v1']
435 add_docker19_migration_link
437 # Test that it returns only supported v1 images even though there is a
440 set_user_from_auth :active
441 cr = create_minimal_req!(command: ["true", "1"],
442 container_image: collections(:docker_image).portable_data_hash)
443 assert_equal(Container.resolve_container_image(cr.container_image),
444 collections(:docker_image).portable_data_hash)
446 cr = create_minimal_req!(command: ["true", "2"],
447 container_image: links(:docker_image_collection_tag).name)
448 assert_equal(Container.resolve_container_image(cr.container_image),
449 collections(:docker_image).portable_data_hash)
452 test "incompatible docker image v1" do
453 Rails.configuration.docker_image_formats = ['v1']
454 add_docker19_migration_link
456 # Don't return unsupported v2 image even if we ask for it directly.
457 set_user_from_auth :active
458 cr = create_minimal_req!(command: ["true", "1"],
459 container_image: collections(:docker_image_1_12).portable_data_hash)
460 assert_raises(ArvadosModel::UnresolvableContainerError) do
461 Container.resolve_container_image(cr.container_image)
465 test "incompatible docker image v2" do
466 Rails.configuration.docker_image_formats = ['v2']
467 # No migration link, don't return unsupported v1 image,
469 set_user_from_auth :active
470 cr = create_minimal_req!(command: ["true", "1"],
471 container_image: collections(:docker_image).portable_data_hash)
472 assert_raises(ArvadosModel::UnresolvableContainerError) do
473 Container.resolve_container_image(cr.container_image)
475 cr = create_minimal_req!(command: ["true", "2"],
476 container_image: links(:docker_image_collection_tag).name)
477 assert_raises(ArvadosModel::UnresolvableContainerError) do
478 Container.resolve_container_image(cr.container_image)
482 test "requestor can retrieve container owned by dispatch" do
483 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
484 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
485 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
489 [{"var" => "value1"}, {"var" => "value1"}, nil],
490 [{"var" => "value1"}, {"var" => "value1"}, true],
491 [{"var" => "value1"}, {"var" => "value1"}, false],
492 [{"var" => "value1"}, {"var" => "value2"}, nil],
493 ].each do |env1, env2, use_existing|
494 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
495 common_attrs = {cwd: "test",
497 command: ["echo", "hello"],
499 runtime_constraints: {"vcpus" => 4,
500 "ram" => 12000000000},
501 mounts: {"test" => {"kind" => "json"}}}
502 set_user_from_auth :active
503 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
506 # Testing with use_existing default value
507 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
511 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
513 use_existing: use_existing}))
515 assert_not_nil cr1.container_uuid
516 assert_nil cr2.container_uuid
518 # Update cr2 to commited state and check for container equality on different cases:
519 # * When env1 and env2 are equal and use_existing is true, the same container
520 # should be assigned.
521 # * When use_existing is false, a different container should be assigned.
522 # * When env1 and env2 are different, a different container should be assigned.
523 cr2.update_attributes!({state: ContainerRequest::Committed})
524 assert_equal (cr2.use_existing == true and (env1 == env2)),
525 (cr1.container_uuid == cr2.container_uuid)
529 test "requesting_container_uuid at create is not allowed" do
530 set_user_from_auth :active
531 assert_raises(ActiveRecord::RecordNotSaved) do
532 create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
536 test "Retry on container cancelled" do
537 set_user_from_auth :active
538 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
539 cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
540 prev_container_uuid = cr.container_uuid
542 c = act_as_system_user do
543 c = Container.find_by_uuid(cr.container_uuid)
544 c.update_attributes!(state: Container::Locked)
545 c.update_attributes!(state: Container::Running)
551 assert_equal "Committed", cr.state
552 assert_equal prev_container_uuid, cr.container_uuid
553 assert_not_equal cr2.container_uuid, cr.container_uuid
554 prev_container_uuid = cr.container_uuid
556 act_as_system_user do
557 c.update_attributes!(state: Container::Cancelled)
562 assert_equal "Committed", cr.state
563 assert_not_equal prev_container_uuid, cr.container_uuid
564 assert_not_equal cr2.container_uuid, cr.container_uuid
565 prev_container_uuid = cr.container_uuid
567 c = act_as_system_user do
568 c = Container.find_by_uuid(cr.container_uuid)
569 c.update_attributes!(state: Container::Cancelled)
575 assert_equal "Final", cr.state
576 assert_equal prev_container_uuid, cr.container_uuid
577 assert_not_equal cr2.container_uuid, cr.container_uuid
580 test "Output collection name setting using output_name with name collision resolution" do
581 set_user_from_auth :active
582 output_name = collections(:foo_file).name
584 cr = create_minimal_req!(priority: 1,
585 state: ContainerRequest::Committed,
586 output_name: output_name)
587 act_as_system_user do
588 c = Container.find_by_uuid(cr.container_uuid)
589 c.update_attributes!(state: Container::Locked)
590 c.update_attributes!(state: Container::Running)
591 c.update_attributes!(state: Container::Complete,
593 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
594 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
597 assert_equal ContainerRequest::Final, cr.state
598 output_coll = Collection.find_by_uuid(cr.output_uuid)
599 # Make sure the resulting output collection name include the original name
601 assert_not_equal output_name, output_coll.name,
602 "It shouldn't exist more than one collection with the same owner and name '${output_name}'"
603 assert output_coll.name.include?(output_name),
604 "New name should include original name"
605 assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/, output_coll.name,
606 "New name should include ISO8601 date"
609 test "Finalize committed request when reusing a finished container" do
610 set_user_from_auth :active
611 cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
613 assert_equal ContainerRequest::Committed, cr.state
614 act_as_system_user do
615 c = Container.find_by_uuid(cr.container_uuid)
616 c.update_attributes!(state: Container::Locked)
617 c.update_attributes!(state: Container::Running)
618 c.update_attributes!(state: Container::Complete,
620 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
621 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
624 assert_equal ContainerRequest::Final, cr.state
626 cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
627 assert_equal cr.container_uuid, cr2.container_uuid
628 assert_equal ContainerRequest::Final, cr2.state
630 cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
631 assert_equal ContainerRequest::Uncommitted, cr3.state
632 cr3.update_attributes!(state: ContainerRequest::Committed)
633 assert_equal cr.container_uuid, cr3.container_uuid
634 assert_equal ContainerRequest::Final, cr3.state
638 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
639 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Uncommitted],
640 [{"partitions" => "fastcpu"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
641 [{"partitions" => "fastcpu"}, ContainerRequest::Uncommitted],
642 [{"partitions" => ["fastcpu","vfastcpu"]}, ContainerRequest::Committed],
643 ].each do |sp, state, expected|
644 test "create container request with scheduling_parameters #{sp} in state #{state} and verify #{expected}" do
645 common_attrs = {cwd: "test",
647 command: ["echo", "hello"],
649 scheduling_parameters: sp,
650 mounts: {"test" => {"kind" => "json"}}}
651 set_user_from_auth :active
653 if expected == ActiveRecord::RecordInvalid
654 assert_raises(ActiveRecord::RecordInvalid) do
655 create_minimal_req!(common_attrs.merge({state: state}))
658 cr = create_minimal_req!(common_attrs.merge({state: state}))
659 assert_equal sp, cr.scheduling_parameters
661 if state == ContainerRequest::Committed
662 c = Container.find_by_uuid(cr.container_uuid)
663 assert_equal sp, c.scheduling_parameters