1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'helpers/container_test_helper'
7 require 'helpers/docker_migration_helper'
8 require 'arvados/collection'
10 class ContainerRequestTest < ActiveSupport::TestCase
11 include DockerMigrationHelper
13 include ContainerTestHelper
15 def with_container_auth(ctr)
16 auth_was = Thread.current[:api_client_authorization]
17 client_was = Thread.current[:api_client]
18 token_was = Thread.current[:token]
19 user_was = Thread.current[:user]
20 auth = ApiClientAuthorization.find_by_uuid(ctr.auth_uuid)
21 Thread.current[:api_client_authorization] = auth
22 Thread.current[:api_client] = auth.api_client
23 Thread.current[:token] = auth.token
24 Thread.current[:user] = auth.user
28 Thread.current[:api_client_authorization] = auth_was
29 Thread.current[:api_client] = client_was
30 Thread.current[:token] = token_was
31 Thread.current[:user] = user_was
37 ctr.update_attributes!(state: Container::Locked)
38 ctr.update_attributes!(state: Container::Running)
42 def create_minimal_req! attrs={}
44 command: ["echo", "foo"],
45 container_image: links(:docker_image_collection_tag).name,
48 mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
50 runtime_constraints: {"vcpus" => 1, "ram" => 2},
54 cr = ContainerRequest.create!(defaults.merge(attrs))
59 def check_bogus_states cr
60 [nil, "Flubber"].each do |state|
61 assert_raises(ActiveRecord::RecordInvalid) do
69 def configure_preemptible_instance_type
70 Rails.configuration.InstanceTypes = ConfigLoader.to_OrderedOptions({
72 "Preemptible" => true,
74 "ProviderType" => "a1.small",
81 test "Container request create" do
82 set_user_from_auth :active
83 cr = create_minimal_req!
85 assert_nil cr.container_uuid
86 assert_equal 0, cr.priority
90 # Ensure we can modify all attributes
91 cr.command = ["echo", "foo3"]
92 cr.container_image = "img3"
94 cr.environment = {"BUP" => "BOP"}
95 cr.mounts = {"BAR" => {"kind" => "BAZ"}}
96 cr.output_path = "/tmp4"
98 cr.runtime_constraints = {"vcpus" => 4}
100 cr.description = "bar3"
103 assert_nil cr.container_uuid
107 {"runtime_constraints" => {"vcpus" => 1}},
108 {"runtime_constraints" => {"vcpus" => 1, "ram" => nil}},
109 {"runtime_constraints" => {"vcpus" => 0, "ram" => 123}},
110 {"runtime_constraints" => {"vcpus" => "1", "ram" => -1}},
111 {"mounts" => {"FOO" => "BAR"}},
112 {"mounts" => {"FOO" => {}}},
113 {"mounts" => {"FOO" => {"kind" => "tmp", "capacity" => 42.222}}},
114 {"command" => ["echo", 55]},
115 {"environment" => {"FOO" => 55}}
117 test "Create with invalid #{value}" do
118 set_user_from_auth :active
119 assert_raises(ActiveRecord::RecordInvalid) do
120 cr = create_minimal_req!({state: "Committed",
121 priority: 1}.merge(value))
126 test "Update with invalid #{value}" do
127 set_user_from_auth :active
128 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
130 assert_raises(ActiveRecord::RecordInvalid) do
131 cr = ContainerRequest.find_by_uuid cr.uuid
132 cr.update_attributes!({state: "Committed",
133 priority: 1}.merge(value))
138 test "Update from fixture" do
139 set_user_from_auth :active
140 cr = ContainerRequest.find_by_uuid(container_requests(:running).uuid)
141 cr.update_attributes!(description: "New description")
142 assert_equal "New description", cr.description
145 test "Update with valid runtime constraints" do
146 set_user_from_auth :active
147 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
149 cr = ContainerRequest.find_by_uuid cr.uuid
150 cr.update_attributes!(state: "Committed",
151 runtime_constraints: {"vcpus" => 1, "ram" => 23})
152 assert_not_nil cr.container_uuid
155 test "Container request priority must be non-nil" do
156 set_user_from_auth :active
157 cr = create_minimal_req!
159 cr.state = "Committed"
160 assert_raises(ActiveRecord::RecordInvalid) do
165 test "Container request commit" do
166 set_user_from_auth :active
167 cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 300000000})
169 assert_nil cr.container_uuid
172 cr.state = "Committed"
178 assert_empty({"vcpus" => 2, "ram" => 300000000}.to_a - cr.runtime_constraints.to_a)
180 assert_equal 0, Rails.configuration.Containers.DefaultKeepCacheRAM
182 assert_not_nil cr.container_uuid
183 c = Container.find_by_uuid cr.container_uuid
185 assert_equal ["echo", "foo"], c.command
186 assert_equal collections(:docker_image).portable_data_hash, c.container_image
187 assert_equal "/tmp", c.cwd
188 assert_equal({}, c.environment)
189 assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
190 assert_equal "/out", c.output_path
191 assert ({"keep_cache_disk" => 2<<30, "keep_cache_ram" => 0, "vcpus" => 2, "ram" => 300000000}.to_a - c.runtime_constraints.to_a).empty?
192 assert_operator 0, :<, c.priority
194 assert_raises(ActiveRecord::RecordInvalid) do
204 assert_equal 0, cr.priority
205 assert_equal 0, c.priority
208 test "Independent container requests" do
209 set_user_from_auth :active
210 cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
211 cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
213 c1 = Container.find_by_uuid cr1.container_uuid
214 assert_operator 0, :<, c1.priority
216 c2 = Container.find_by_uuid cr2.container_uuid
217 assert_operator c1.priority, :<, c2.priority
218 c2priority_was = c2.priority
220 cr1.update_attributes!(priority: 0)
223 assert_equal 0, c1.priority
226 assert_equal c2priority_was, c2.priority
229 test "Request is finalized when its container is cancelled" do
230 set_user_from_auth :active
231 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1)
232 assert_equal users(:active).uuid, cr.modified_by_user_uuid
234 act_as_system_user do
235 Container.find_by_uuid(cr.container_uuid).
236 update_attributes!(state: Container::Cancelled, cost: 1.25)
240 assert_equal "Final", cr.state
241 assert_equal 1.25, cr.cumulative_cost
242 assert_equal users(:active).uuid, cr.modified_by_user_uuid
245 test "Request is finalized when its container is completed" do
246 set_user_from_auth :active
247 project = groups(:private)
248 cr = create_minimal_req!(owner_uuid: project.uuid,
251 assert_equal users(:active).uuid, cr.modified_by_user_uuid
253 c = act_as_system_user do
254 c = Container.find_by_uuid(cr.container_uuid)
255 c.update_attributes!(state: Container::Locked)
256 c.update_attributes!(state: Container::Running)
261 assert_equal "Committed", cr.state
263 output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
264 log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
265 act_as_system_user do
266 c.update_attributes!(state: Container::Complete,
273 assert_equal "Final", cr.state
274 assert_equal 1.25, cr.cumulative_cost
275 assert_equal users(:active).uuid, cr.modified_by_user_uuid
277 assert_not_nil cr.output_uuid
278 assert_not_nil cr.log_uuid
279 output = Collection.find_by_uuid cr.output_uuid
280 assert_equal output_pdh, output.portable_data_hash
281 assert_equal output.owner_uuid, project.uuid, "Container output should be copied to #{project.uuid}"
282 assert_not_nil output.modified_at
284 log = Collection.find_by_uuid cr.log_uuid
285 assert_equal log.manifest_text, ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar
286 ./log\\040for\\040container\\040#{cr.container_uuid} 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"
288 assert_equal log.owner_uuid, project.uuid, "Container log should be copied to #{project.uuid}"
291 # This tests bug report #16144
292 test "Request is finalized when its container is completed even when log & output don't exist" do
293 set_user_from_auth :active
294 project = groups(:private)
295 cr = create_minimal_req!(owner_uuid: project.uuid,
298 assert_equal users(:active).uuid, cr.modified_by_user_uuid
300 output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
301 log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
303 c = act_as_system_user do
304 c = Container.find_by_uuid(cr.container_uuid)
305 c.update_attributes!(state: Container::Locked)
306 c.update_attributes!(state: Container::Running,
313 assert_equal "Committed", cr.state
315 act_as_system_user do
316 Collection.where(portable_data_hash: output_pdh).delete_all
317 Collection.where(portable_data_hash: log_pdh).delete_all
318 c.update_attributes!(state: Container::Complete)
322 assert_equal "Final", cr.state
325 # This tests bug report #16144
326 test "Can destroy CR even if its container doesn't exist" do
327 set_user_from_auth :active
328 project = groups(:private)
329 cr = create_minimal_req!(owner_uuid: project.uuid,
332 assert_equal users(:active).uuid, cr.modified_by_user_uuid
334 c = act_as_system_user do
335 c = Container.find_by_uuid(cr.container_uuid)
336 c.update_attributes!(state: Container::Locked)
337 c.update_attributes!(state: Container::Running)
342 assert_equal "Committed", cr.state
345 act_as_system_user do
346 Container.find_by_uuid(cr.container_uuid).destroy
349 assert_nil ContainerRequest.find_by_uuid(cr_uuid)
352 test "Container makes container request, then is cancelled" do
353 set_user_from_auth :active
354 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
356 c = Container.find_by_uuid cr.container_uuid
357 assert_operator 0, :<, c.priority
360 cr2 = with_container_auth(c) do
361 create_minimal_req!(priority: 10, state: "Committed", container_count_max: 1, command: ["echo", "foo2"])
363 assert_equal c.uuid, cr2.requesting_container_uuid
364 assert_equal users(:active).uuid, cr2.modified_by_user_uuid
366 c2 = Container.find_by_uuid cr2.container_uuid
367 assert_operator 0, :<, c2.priority
369 act_as_system_user do
370 c.state = "Cancelled"
375 assert_equal "Final", cr.state
378 assert_equal 0, cr2.priority
379 assert_equal users(:active).uuid, cr2.modified_by_user_uuid
382 assert_equal 0, c2.priority
385 test "child container priority follows same ordering as corresponding top-level ancestors" do
386 findctr = lambda { |cr| Container.find_by_uuid(cr.container_uuid) }
388 set_user_from_auth :active
391 create_minimal_req!(priority: 5, state: "Committed", environment: {"workflow" => "0"}),
392 create_minimal_req!(priority: 5, state: "Committed", environment: {"workflow" => "1"}),
393 create_minimal_req!(priority: 5, state: "Committed", environment: {"workflow" => "2"}),
395 parents = toplevel_crs.map(&findctr)
397 children = parents.map do |parent|
399 with_container_auth(parent) do
400 create_minimal_req!(state: "Committed",
402 environment: {"child" => parent.environment["workflow"]})
406 grandchildren = children.reverse.map do |child|
408 with_container_auth(child) do
409 create_minimal_req!(state: "Committed",
411 environment: {"grandchild" => child.environment["child"]})
413 end.reverse.map(&findctr)
415 shared_grandchildren = children.map do |child|
416 with_container_auth(child) do
417 create_minimal_req!(state: "Committed",
419 environment: {"grandchild" => "shared"})
423 assert_equal shared_grandchildren[0].uuid, shared_grandchildren[1].uuid
424 assert_equal shared_grandchildren[0].uuid, shared_grandchildren[2].uuid
425 shared_grandchild = shared_grandchildren[0]
427 set_user_from_auth :active
429 # parents should be prioritized by submit time.
430 assert_operator parents[0].priority, :>, parents[1].priority
431 assert_operator parents[1].priority, :>, parents[2].priority
433 # children should be prioritized in same order as their respective
435 assert_operator children[0].priority, :>, children[1].priority
436 assert_operator children[1].priority, :>, children[2].priority
438 # grandchildren should also be prioritized in the same order,
439 # despite having been submitted in the opposite order.
440 assert_operator grandchildren[0].priority, :>, grandchildren[1].priority
441 assert_operator grandchildren[1].priority, :>, grandchildren[2].priority
443 # shared grandchild container should be prioritized above
444 # everything that isn't needed by parents[0], but not above
445 # earlier-submitted descendants of parents[0]
446 assert_operator shared_grandchild.priority, :>, grandchildren[1].priority
447 assert_operator shared_grandchild.priority, :>, children[1].priority
448 assert_operator shared_grandchild.priority, :>, parents[1].priority
449 assert_operator shared_grandchild.priority, :<=, grandchildren[0].priority
450 assert_operator shared_grandchild.priority, :<=, children[0].priority
451 assert_operator shared_grandchild.priority, :<=, parents[0].priority
453 # increasing priority of the most recent toplevel container should
454 # reprioritize all of its descendants (including the shared
455 # grandchild) above everything else.
456 toplevel_crs[2].update_attributes!(priority: 72)
457 (parents + children + grandchildren + [shared_grandchild]).map(&:reload)
458 assert_operator shared_grandchild.priority, :>, grandchildren[0].priority
459 assert_operator shared_grandchild.priority, :>, children[0].priority
460 assert_operator shared_grandchild.priority, :>, parents[0].priority
461 assert_operator shared_grandchild.priority, :>, grandchildren[1].priority
462 assert_operator shared_grandchild.priority, :>, children[1].priority
463 assert_operator shared_grandchild.priority, :>, parents[1].priority
464 # ...but the shared container should not have higher priority than
465 # the earlier-submitted descendants of the high-priority workflow.
466 assert_operator shared_grandchild.priority, :<=, grandchildren[2].priority
467 assert_operator shared_grandchild.priority, :<=, children[2].priority
468 assert_operator shared_grandchild.priority, :<=, parents[2].priority
472 ['running_container_auth', 'zzzzz-dz642-runningcontainr', 501],
473 ['active_no_prefs', nil, 0]
474 ].each do |token, expected, expected_priority|
475 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
476 set_user_from_auth token
477 cr = create_minimal_req!
478 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
479 assert_equal expected, cr.requesting_container_uuid
480 assert_equal expected_priority, cr.priority
485 ['running_container_auth', 'zzzzz-dz642-runningcontainr', 501],
486 ].each do |token, expected, expected_priority|
487 test "create as #{token} with requesting_container_uuid set and expect output to be intermediate" do
488 set_user_from_auth token
489 cr = create_minimal_req!
490 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
491 assert_equal expected, cr.requesting_container_uuid
492 assert_equal expected_priority, cr.priority
494 cr.state = ContainerRequest::Committed
499 output = Collection.find_by_uuid(cr.output_uuid)
500 props = {"type": "intermediate", "container_request": cr.uuid}
501 assert_equal props.symbolize_keys, output.properties.symbolize_keys
505 test "create as container_runtime_token and expect requesting_container_uuid to be zzzzz-dz642-20isqbkl8xwnsao" do
506 set_user_from_auth :container_runtime_token
507 Thread.current[:token] = "#{Thread.current[:token]}/zzzzz-dz642-20isqbkl8xwnsao"
508 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
509 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
510 assert_equal 'zzzzz-dz642-20isqbkl8xwnsao', cr.requesting_container_uuid
511 assert_equal 1, cr.priority
514 [[{"vcpus" => [2, nil]},
515 lambda { |resolved| resolved["vcpus"] == 2 }],
516 [{"vcpus" => [3, 7]},
517 lambda { |resolved| resolved["vcpus"] == 3 }],
519 lambda { |resolved| resolved["vcpus"] == 4 }],
520 [{"ram" => [1000000000, 2000000000]},
521 lambda { |resolved| resolved["ram"] == 1000000000 }],
522 [{"ram" => [1234234234]},
523 lambda { |resolved| resolved["ram"] == 1234234234 }],
524 ].each do |rc, okfunc|
525 test "resolve runtime constraint range #{rc} to values" do
526 resolved = Container.resolve_runtime_constraints(rc)
527 assert(okfunc.call(resolved),
528 "container runtime_constraints was #{resolved.inspect}")
533 "kind" => "collection",
534 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
537 resolved["/out"] == {
538 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
539 "kind" => "collection",
544 "kind" => "collection",
545 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
546 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
549 resolved["/out"] == {
550 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
551 "kind" => "collection",
556 "kind" => "collection",
557 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
560 resolved["/out"] == {
561 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
562 "kind" => "collection",
568 "kind" => "collection",
571 resolved["/out"] == {
572 "kind" => "collection",
576 ].each do |mounts, okfunc|
577 test "resolve mounts #{mounts.inspect} to values" do
578 set_user_from_auth :active
579 resolved = Container.resolve_mounts(mounts)
580 assert(okfunc.call(resolved),
581 "Container.resolve_mounts returned #{resolved.inspect}")
585 test 'mount unreadable collection' do
586 set_user_from_auth :spectator
589 "kind" => "collection",
590 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
594 assert_raises(ArvadosModel::UnresolvableContainerError) do
595 Container.resolve_mounts(m)
599 test 'mount collection with mismatched UUID and PDH' do
600 set_user_from_auth :active
603 "kind" => "collection",
604 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
605 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
609 resolved_mounts = Container.resolve_mounts(m)
610 assert_equal m['portable_data_hash'], resolved_mounts['portable_data_hash']
613 ['arvados/apitestfixture:latest',
614 'arvados/apitestfixture',
615 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
617 test "Container.resolve_container_image(#{tag.inspect})" do
618 set_user_from_auth :active
619 resolved = Container.resolve_container_image(tag)
620 assert_equal resolved, collections(:docker_image).portable_data_hash
624 test "Container.resolve_container_image(pdh)" do
625 set_user_from_auth :active
626 [[:docker_image, 'v1'], [:docker_image_1_12, 'v2']].each do |coll, ver|
627 Rails.configuration.Containers.SupportedDockerImageFormats = ConfigLoader.to_OrderedOptions({ver=>{}})
628 pdh = collections(coll).portable_data_hash
629 resolved = Container.resolve_container_image(pdh)
630 assert_equal resolved, pdh
634 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
636 'arvados/apitestfixture:ENOEXIST',
638 test "container_image_for_container(#{img.inspect}) => 422" do
639 set_user_from_auth :active
640 assert_raises(ArvadosModel::UnresolvableContainerError) do
641 Container.resolve_container_image(img)
646 test "allow unrecognized container when there are remote_hosts" do
647 set_user_from_auth :active
648 Rails.configuration.RemoteClusters = Rails.configuration.RemoteClusters.merge({foooo: ActiveSupport::InheritableOptions.new({Host: "bar.com"})})
649 Container.resolve_container_image('acbd18db4cc2f85cedef654fccc4a4d8+3')
652 test "migrated docker image" do
653 Rails.configuration.Containers.SupportedDockerImageFormats = ConfigLoader.to_OrderedOptions({'v2'=>{}})
654 add_docker19_migration_link
656 # Test that it returns only v2 images even though request is for v1 image.
658 set_user_from_auth :active
659 cr = create_minimal_req!(command: ["true", "1"],
660 container_image: collections(:docker_image).portable_data_hash)
661 assert_equal(Container.resolve_container_image(cr.container_image),
662 collections(:docker_image_1_12).portable_data_hash)
664 cr = create_minimal_req!(command: ["true", "2"],
665 container_image: links(:docker_image_collection_tag).name)
666 assert_equal(Container.resolve_container_image(cr.container_image),
667 collections(:docker_image_1_12).portable_data_hash)
670 test "use unmigrated docker image" do
671 Rails.configuration.Containers.SupportedDockerImageFormats = ConfigLoader.to_OrderedOptions({'v1'=>{}})
672 add_docker19_migration_link
674 # Test that it returns only supported v1 images even though there is a
677 set_user_from_auth :active
678 cr = create_minimal_req!(command: ["true", "1"],
679 container_image: collections(:docker_image).portable_data_hash)
680 assert_equal(Container.resolve_container_image(cr.container_image),
681 collections(:docker_image).portable_data_hash)
683 cr = create_minimal_req!(command: ["true", "2"],
684 container_image: links(:docker_image_collection_tag).name)
685 assert_equal(Container.resolve_container_image(cr.container_image),
686 collections(:docker_image).portable_data_hash)
689 test "incompatible docker image v1" do
690 Rails.configuration.Containers.SupportedDockerImageFormats = ConfigLoader.to_OrderedOptions({'v1'=>{}})
691 add_docker19_migration_link
693 # Don't return unsupported v2 image even if we ask for it directly.
694 set_user_from_auth :active
695 cr = create_minimal_req!(command: ["true", "1"],
696 container_image: collections(:docker_image_1_12).portable_data_hash)
697 assert_raises(ArvadosModel::UnresolvableContainerError) do
698 Container.resolve_container_image(cr.container_image)
702 test "incompatible docker image v2" do
703 Rails.configuration.Containers.SupportedDockerImageFormats = ConfigLoader.to_OrderedOptions({'v2'=>{}})
704 # No migration link, don't return unsupported v1 image,
706 set_user_from_auth :active
707 cr = create_minimal_req!(command: ["true", "1"],
708 container_image: collections(:docker_image).portable_data_hash)
709 assert_raises(ArvadosModel::UnresolvableContainerError) do
710 Container.resolve_container_image(cr.container_image)
712 cr = create_minimal_req!(command: ["true", "2"],
713 container_image: links(:docker_image_collection_tag).name)
714 assert_raises(ArvadosModel::UnresolvableContainerError) do
715 Container.resolve_container_image(cr.container_image)
719 test "requestor can retrieve container owned by dispatch" do
720 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
721 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
722 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
726 [{"var" => "value1"}, {"var" => "value1"}, nil],
727 [{"var" => "value1"}, {"var" => "value1"}, true],
728 [{"var" => "value1"}, {"var" => "value1"}, false],
729 [{"var" => "value1"}, {"var" => "value2"}, nil],
730 ].each do |env1, env2, use_existing|
731 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
732 common_attrs = {cwd: "test",
734 command: ["echo", "hello"],
736 runtime_constraints: {"vcpus" => 4,
737 "ram" => 12000000000},
738 mounts: {"test" => {"kind" => "json"}}}
739 set_user_from_auth :active
740 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
745 # Testing with use_existing default value
746 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
750 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
752 use_existing: use_existing}))
754 assert_not_nil cr1.container_uuid
755 assert_nil cr2.container_uuid
757 # Update cr2 to commited state and check for container equality on different cases:
758 # * When env1 and env2 are equal and use_existing is true, the same container
759 # should be assigned.
760 # * When use_existing is false, a different container should be assigned.
761 # * When env1 and env2 are different, a different container should be assigned.
762 cr2.update_attributes!({state: ContainerRequest::Committed})
763 assert_equal (cr2.use_existing == true and (env1 == env2)),
764 (cr1.container_uuid == cr2.container_uuid)
768 test "requesting_container_uuid at create is not allowed" do
769 set_user_from_auth :active
770 assert_raises(ActiveRecord::RecordInvalid) do
771 create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
775 test "Retry on container cancelled" do
776 set_user_from_auth :active
777 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
778 cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
779 prev_container_uuid = cr.container_uuid
781 c = act_as_system_user do
782 c = Container.find_by_uuid(cr.container_uuid)
783 c.update_attributes!(state: Container::Locked)
784 c.update_attributes!(state: Container::Running)
790 assert_equal "Committed", cr.state
791 assert_equal prev_container_uuid, cr.container_uuid
792 assert_not_equal cr2.container_uuid, cr.container_uuid
793 prev_container_uuid = cr.container_uuid
795 act_as_system_user do
796 c.update_attributes!(cost: 0.5, subrequests_cost: 1.25)
797 c.update_attributes!(state: Container::Cancelled)
802 assert_equal "Committed", cr.state
803 assert_not_equal prev_container_uuid, cr.container_uuid
804 assert_not_equal cr2.container_uuid, cr.container_uuid
805 prev_container_uuid = cr.container_uuid
807 c = act_as_system_user do
808 c = Container.find_by_uuid(cr.container_uuid)
809 c.update_attributes!(state: Container::Locked)
810 c.update_attributes!(state: Container::Running)
811 c.update_attributes!(cost: 0.125)
812 c.update_attributes!(state: Container::Cancelled)
818 assert_equal "Final", cr.state
819 assert_equal prev_container_uuid, cr.container_uuid
820 assert_not_equal cr2.container_uuid, cr.container_uuid
821 assert_equal 1.875, cr.cumulative_cost
824 test "Retry on container cancelled with runtime_token" do
825 set_user_from_auth :spectator
826 spec = api_client_authorizations(:active)
827 cr = create_minimal_req!(priority: 1, state: "Committed",
828 runtime_token: spec.token,
829 container_count_max: 2)
830 prev_container_uuid = cr.container_uuid
832 c = act_as_system_user do
833 c = Container.find_by_uuid(cr.container_uuid)
834 assert_equal spec.token, c.runtime_token
835 c.update_attributes!(state: Container::Locked)
836 c.update_attributes!(state: Container::Running)
841 assert_equal "Committed", cr.state
842 assert_equal prev_container_uuid, cr.container_uuid
843 prev_container_uuid = cr.container_uuid
845 act_as_system_user do
846 c.update_attributes!(state: Container::Cancelled)
850 assert_equal "Committed", cr.state
851 assert_not_equal prev_container_uuid, cr.container_uuid
852 prev_container_uuid = cr.container_uuid
854 c = act_as_system_user do
855 c = Container.find_by_uuid(cr.container_uuid)
856 assert_equal spec.token, c.runtime_token
857 c.update_attributes!(state: Container::Cancelled)
862 assert_equal "Final", cr.state
863 assert_equal prev_container_uuid, cr.container_uuid
867 test "Retry saves logs from previous attempts" do
868 set_user_from_auth :active
869 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 3)
871 c = act_as_system_user do
872 c = Container.find_by_uuid(cr.container_uuid)
873 c.update_attributes!(state: Container::Locked)
874 c.update_attributes!(state: Container::Running)
882 assert_equal "Committed", cr.state
883 container_uuids << cr.container_uuid
885 c = act_as_system_user do
886 logc = Collection.new(manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n")
888 c = Container.find_by_uuid(cr.container_uuid)
889 c.update_attributes!(state: Container::Cancelled, log: logc.portable_data_hash)
894 container_uuids.sort!
897 assert_equal "Final", cr.state
898 assert_equal 3, cr.container_count
899 assert_equal ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar
900 ./log\\040for\\040container\\040#{container_uuids[0]} 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar
901 ./log\\040for\\040container\\040#{container_uuids[1]} 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar
902 ./log\\040for\\040container\\040#{container_uuids[2]} 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar
903 " , Collection.find_by_uuid(cr.log_uuid).manifest_text
907 test "Output collection name setting using output_name with name collision resolution" do
908 set_user_from_auth :active
909 output_name = 'unimaginative name'
910 Collection.create!(name: output_name)
912 cr = create_minimal_req!(priority: 1,
913 state: ContainerRequest::Committed,
914 output_name: output_name)
917 assert_equal ContainerRequest::Final, cr.state
918 output_coll = Collection.find_by_uuid(cr.output_uuid)
919 # Make sure the resulting output collection name include the original name
921 assert_not_equal output_name, output_coll.name,
922 "more than one collection with the same owner and name"
923 assert output_coll.name.include?(output_name),
924 "New name should include original name"
925 assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/, output_coll.name,
926 "New name should include ISO8601 date"
929 [[0, :check_output_ttl_0],
930 [1, :check_output_ttl_1s],
931 [365*86400, :check_output_ttl_1y],
932 ].each do |ttl, checker|
933 test "output_ttl=#{ttl}" do
934 act_as_user users(:active) do
935 cr = create_minimal_req!(priority: 1,
936 state: ContainerRequest::Committed,
941 output = Collection.find_by_uuid(cr.output_uuid)
942 send(checker, db_current_time, output.trash_at, output.delete_at)
947 def check_output_ttl_0(now, trash, delete)
952 def check_output_ttl_1s(now, trash, delete)
953 assert_not_nil(trash)
954 assert_not_nil(delete)
955 assert_in_delta(trash, now + 1.second, 10)
956 assert_in_delta(delete, now + Rails.configuration.Collections.BlobSigningTTL, 10)
959 def check_output_ttl_1y(now, trash, delete)
960 year = (86400*365).second
961 assert_not_nil(trash)
962 assert_not_nil(delete)
963 assert_in_delta(trash, now + year, 10)
964 assert_in_delta(delete, now + year, 10)
967 def run_container(cr)
968 act_as_system_user do
969 logc = Collection.new(owner_uuid: system_user_uuid,
970 manifest_text: ". ef772b2f28e2c8ca84de45466ed19ee9+7815 0:0:arv-mount.txt\n")
973 c = Container.find_by_uuid(cr.container_uuid)
974 c.update_attributes!(state: Container::Locked)
975 c.update_attributes!(state: Container::Running)
976 c.update_attributes!(state: Container::Complete,
978 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
979 log: logc.portable_data_hash)
985 test "Finalize committed request when reusing a finished container" do
986 set_user_from_auth :active
987 cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
989 assert_equal ContainerRequest::Committed, cr.state
992 assert_equal ContainerRequest::Final, cr.state
994 cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
995 assert_equal cr.container_uuid, cr2.container_uuid
996 assert_equal ContainerRequest::Final, cr2.state
998 cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
999 assert_equal ContainerRequest::Uncommitted, cr3.state
1000 cr3.update_attributes!(state: ContainerRequest::Committed)
1001 assert_equal cr.container_uuid, cr3.container_uuid
1002 assert_equal ContainerRequest::Final, cr3.state
1006 # client requests preemptible, but types are not configured
1007 [false, false, false, true, ActiveRecord::RecordInvalid],
1008 [true, false, false, true, ActiveRecord::RecordInvalid],
1009 # client requests preemptible, types are configured
1010 [false, true, false, true, true],
1011 [true, true, false, true, true],
1012 # client requests non-preemptible for top-level container
1013 [false, false, false, false, false],
1014 [true, false, false, false, false],
1015 [false, true, false, false, false],
1016 [true, true, false, false, false],
1017 # client requests non-preemptible for child container, preemptible
1018 # is enabled anyway if AlwaysUsePreemptibleInstances and instance types
1020 [false, false, true, false, false],
1021 [true, false, true, false, false],
1022 [false, true, true, false, false],
1023 [true, true, true, false, true],
1024 ].each do |use_preemptible, have_preemptible, is_child, ask, expect|
1025 test "with AlwaysUsePreemptibleInstances=#{use_preemptible} and preemptible types #{have_preemptible ? '' : 'not '}configured, create #{is_child ? 'child' : 'top-level'} container request with preemptible=#{ask} and expect #{expect}" do
1026 Rails.configuration.Containers.AlwaysUsePreemptibleInstances = use_preemptible
1028 configure_preemptible_instance_type
1033 command: ["echo", "hello"],
1034 output_path: "test",
1035 scheduling_parameters: {"preemptible" => ask},
1036 mounts: {"test" => {"kind" => "json"}},
1038 set_user_from_auth :active
1041 cr = with_container_auth(containers(:running)) do
1042 create_minimal_req!(common_attrs)
1045 cr = create_minimal_req!(common_attrs)
1049 cr.state = ContainerRequest::Committed
1051 if expect == true || expect == false
1053 assert_equal expect, cr.scheduling_parameters["preemptible"]
1055 assert_raises(expect) do
1062 test "config update does not flip preemptible flag on already-committed container requests" do
1063 parent = containers(:running_container_with_logs)
1065 scheduling_parameters: {"preemptible" => true},
1066 "state" => "Committed",
1070 scheduling_parameters: {"preemptible" => false},
1071 "state" => "Committed",
1074 expect = {false => [], true => []}
1076 with_container_auth(parent) do
1077 configure_preemptible_instance_type
1078 Rails.configuration.Containers.AlwaysUsePreemptibleInstances = false
1080 expect[true].push create_minimal_req!(attrs_p)
1081 expect[false].push create_minimal_req!(attrs_nonp)
1083 Rails.configuration.Containers.AlwaysUsePreemptibleInstances = true
1085 expect[true].push create_minimal_req!(attrs_p)
1086 expect[true].push create_minimal_req!(attrs_nonp)
1087 commit_later = create_minimal_req!()
1089 Rails.configuration.InstanceTypes = ConfigLoader.to_OrderedOptions({})
1091 expect[false].push create_minimal_req!(attrs_nonp)
1093 # Even though preemptible is not allowed, we should be able to
1094 # commit a CR that was created earlier when preemptible was the
1096 commit_later.update_attributes!(priority: 1, state: "Committed")
1097 expect[false].push commit_later
1100 set_user_from_auth :active
1101 [false, true].each do |pflag|
1102 expect[pflag].each do |cr|
1104 assert_equal pflag, cr.scheduling_parameters['preemptible']
1108 act_as_system_user do
1109 # Cancelling the parent used to fail while updating the child
1110 # containers' priority, because the child containers' unchanged
1111 # preemptible fields caused validation to fail.
1112 parent.update_attributes!(state: 'Cancelled')
1114 [false, true].each do |pflag|
1115 expect[pflag].each do |cr|
1117 assert_equal 0, cr.priority, "unexpected non-zero priority #{cr.priority} for #{cr.uuid}"
1124 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
1125 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Uncommitted],
1126 [{"partitions" => "fastcpu"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
1127 [{"partitions" => "fastcpu"}, ContainerRequest::Uncommitted],
1128 [{"partitions" => ["fastcpu","vfastcpu"]}, ContainerRequest::Committed],
1129 [{"max_run_time" => "one day"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
1130 [{"max_run_time" => "one day"}, ContainerRequest::Uncommitted],
1131 [{"max_run_time" => -1}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
1132 [{"max_run_time" => -1}, ContainerRequest::Uncommitted],
1133 [{"max_run_time" => 86400}, ContainerRequest::Committed],
1134 ].each do |sp, state, expected|
1135 test "create container request with scheduling_parameters #{sp} in state #{state} and verify #{expected}" do
1136 common_attrs = {cwd: "test",
1138 command: ["echo", "hello"],
1139 output_path: "test",
1140 scheduling_parameters: sp,
1141 mounts: {"test" => {"kind" => "json"}}}
1142 set_user_from_auth :active
1144 if expected == ActiveRecord::RecordInvalid
1145 assert_raises(ActiveRecord::RecordInvalid) do
1146 create_minimal_req!(common_attrs.merge({state: state}))
1149 cr = create_minimal_req!(common_attrs.merge({state: state}))
1150 assert (sp.to_a - cr.scheduling_parameters.to_a).empty?
1152 if state == ContainerRequest::Committed
1153 c = Container.find_by_uuid(cr.container_uuid)
1154 assert (sp.to_a - c.scheduling_parameters.to_a).empty?
1160 test "AlwaysUsePreemptibleInstances makes child containers preemptible" do
1161 Rails.configuration.Containers.AlwaysUsePreemptibleInstances = true
1162 common_attrs = {cwd: "test",
1164 command: ["echo", "hello"],
1165 output_path: "test",
1166 state: ContainerRequest::Committed,
1167 mounts: {"test" => {"kind" => "json"}}}
1168 set_user_from_auth :active
1169 configure_preemptible_instance_type
1171 cr = with_container_auth(Container.find_by_uuid 'zzzzz-dz642-runningcontainr') do
1172 create_minimal_req!(common_attrs)
1174 assert_equal 'zzzzz-dz642-runningcontainr', cr.requesting_container_uuid
1175 assert_equal true, cr.scheduling_parameters["preemptible"]
1177 c = Container.find_by_uuid(cr.container_uuid)
1178 assert_equal true, c.scheduling_parameters["preemptible"]
1181 [['Committed', true, {name: "foobar", priority: 123}],
1182 ['Committed', false, {container_count: 2}],
1183 ['Committed', false, {container_count: 0}],
1184 ['Committed', false, {container_count: nil}],
1185 ['Committed', true, {priority: 0, mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}}}],
1186 ['Committed', true, {priority: 0, mounts: {"/out" => {"capacity" => 1000000, "kind" => "tmp"}}}],
1187 # Addition of default values for mounts / runtime_constraints /
1188 # scheduling_parameters, as happens in a round-trip through
1189 # controller, does not have any real effect and should be
1190 # accepted/ignored rather than causing an error when the CR state
1191 # dictates those attributes are not allowed to change.
1192 ['Committed', true, {priority: 0, mounts: {"/out" => {"capacity" => 0, "kind" => "tmp"}}}, {mounts: {"/out" => {"kind" => "tmp"}}}],
1193 ['Committed', true, {priority: 0, mounts: {"/out" => {"capacity" => 1000000, "kind" => "tmp", "exclude_from_output": false}}}],
1194 ['Committed', true, {priority: 0, mounts: {"/out" => {"capacity" => 1000000, "kind" => "tmp", "repository_name": ""}}}],
1195 ['Committed', true, {priority: 0, mounts: {"/out" => {"capacity" => 1000000, "kind" => "tmp", "content": nil}}}],
1196 ['Committed', false, {priority: 0, mounts: {"/out" => {"capacity" => 1000000, "kind" => "tmp", "content": {}}}}],
1197 ['Committed', false, {priority: 0, mounts: {"/out" => {"capacity" => 1000000, "kind" => "tmp", "repository_name": "foo"}}}],
1198 ['Committed', false, {priority: 0, mounts: {"/out" => {"kind" => "tmp", "capacity" => 1234567}}}],
1199 ['Committed', false, {priority: 0, mounts: {}}],
1200 ['Committed', true, {priority: 0, runtime_constraints: {"vcpus" => 1, "ram" => 2}}],
1201 ['Committed', true, {priority: 0, runtime_constraints: {"vcpus" => 1, "ram" => 2, "keep_cache_ram" => 0}}],
1202 ['Committed', true, {priority: 0, runtime_constraints: {"vcpus" => 1, "ram" => 2, "API" => false}}],
1203 ['Committed', false, {priority: 0, runtime_constraints: {"vcpus" => 1, "ram" => 2, "keep_cache_ram" => 1}}],
1204 ['Committed', false, {priority: 0, runtime_constraints: {"vcpus" => 1, "ram" => 2, "API" => true}}],
1205 ['Committed', true, {priority: 0, scheduling_parameters: {"preemptible" => false}}],
1206 ['Committed', true, {priority: 0, scheduling_parameters: {"partitions" => []}}],
1207 ['Committed', true, {priority: 0, scheduling_parameters: {"max_run_time" => 0}}],
1208 ['Committed', false, {priority: 0, scheduling_parameters: {"preemptible" => true}}],
1209 ['Committed', false, {priority: 0, scheduling_parameters: {"partitions" => ["foo"]}}],
1210 ['Committed', false, {priority: 0, scheduling_parameters: {"max_run_time" => 1}}],
1211 ['Final', false, {state: ContainerRequest::Committed, name: "foobar"}],
1212 ['Final', false, {name: "foobar", priority: 123}],
1213 ['Final', false, {name: "foobar", output_uuid: "zzzzz-4zz18-znfnqtbbv4spc3w"}],
1214 ['Final', false, {name: "foobar", log_uuid: "zzzzz-4zz18-znfnqtbbv4spc3w"}],
1215 ['Final', false, {log_uuid: "zzzzz-4zz18-znfnqtbbv4spc3w"}],
1216 ['Final', false, {priority: 123}],
1217 ['Final', false, {mounts: {}}],
1218 ['Final', false, {container_count: 2}],
1219 ['Final', true, {name: "foobar"}],
1220 ['Final', true, {name: "foobar", description: "baz"}],
1221 ].each do |state, permitted, updates, create_attrs|
1222 test "state=#{state} can#{'not' if !permitted} update #{updates.inspect}" do
1223 act_as_user users(:active) do
1227 container_count_max: 1
1229 if !create_attrs.nil?
1230 attrs.merge!(create_attrs)
1232 cr = create_minimal_req!(attrs)
1237 act_as_system_user do
1238 Container.find_by_uuid(cr.container_uuid).
1239 update_attributes!(state: Container::Cancelled)
1243 raise 'broken test case'
1245 assert_equal state, cr.state
1247 assert cr.update_attributes!(updates)
1249 assert_raises(ActiveRecord::RecordInvalid) do
1250 cr.update_attributes!(updates)
1257 test "delete container_request and check its container's priority" do
1258 act_as_user users(:active) do
1259 cr = ContainerRequest.find_by_uuid container_requests(:running_to_be_deleted).uuid
1261 # initially the cr's container has priority > 0
1262 c = Container.find_by_uuid(cr.container_uuid)
1263 assert_equal 1, c.priority
1267 # the cr's container now has priority of 0
1268 c = Container.find_by_uuid(cr.container_uuid)
1269 assert_equal 0, c.priority
1273 test "delete container_request in final state and expect no error due to before_destroy callback" do
1274 act_as_user users(:active) do
1275 cr = ContainerRequest.find_by_uuid container_requests(:completed).uuid
1276 assert_nothing_raised {cr.destroy}
1280 test "Container request valid priority" do
1281 set_user_from_auth :active
1282 cr = create_minimal_req!
1284 assert_raises(ActiveRecord::RecordInvalid) do
1304 assert_raises(ActiveRecord::RecordInvalid) do
1310 # Note: some of these tests might look redundant because they test
1311 # that out-of-order spellings of hashes are still considered equal
1312 # regardless of whether the existing (container) or new (container
1313 # request) hash needs to be re-ordered.
1314 secrets = {"/foo" => {"kind" => "text", "content" => "xyzzy"}}
1315 same_secrets = {"/foo" => {"content" => "xyzzy", "kind" => "text"}}
1316 different_secrets = {"/foo" => {"kind" => "text", "content" => "something completely different"}}
1322 [true, secrets, same_secrets],
1323 [true, same_secrets, secrets],
1324 [false, nil, secrets],
1325 [false, {}, secrets],
1326 [false, secrets, {}],
1327 [false, secrets, nil],
1328 [false, secrets, different_secrets],
1329 ].each do |expect_reuse, sm1, sm2|
1330 test "container reuse secret_mounts #{sm1.inspect}, #{sm2.inspect}" do
1331 set_user_from_auth :active
1332 cr1 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm1)
1333 cr2 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm2)
1334 assert_not_nil cr1.container_uuid
1335 assert_not_nil cr2.container_uuid
1337 assert_equal cr1.container_uuid, cr2.container_uuid
1339 assert_not_equal cr1.container_uuid, cr2.container_uuid
1344 test "scrub secret_mounts but reuse container for request with identical secret_mounts" do
1345 set_user_from_auth :active
1346 sm = {'/secret/foo' => {'kind' => 'text', 'content' => secret_string}}
1347 cr1 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm.dup)
1351 # secret_mounts scrubbed from db
1352 c = Container.where(uuid: cr1.container_uuid).first
1353 assert_equal({}, c.secret_mounts)
1354 assert_equal({}, cr1.secret_mounts)
1356 # can reuse container if secret_mounts match
1357 cr2 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm.dup)
1358 assert_equal cr1.container_uuid, cr2.container_uuid
1360 # don't reuse container if secret_mounts don't match
1361 cr3 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: {})
1362 assert_not_equal cr1.container_uuid, cr3.container_uuid
1364 assert_no_secrets_logged
1367 test "conflicting key in mounts and secret_mounts" do
1368 sm = {'/secret/foo' => {'kind' => 'text', 'content' => secret_string}}
1369 set_user_from_auth :active
1370 cr = create_minimal_req!
1371 assert_equal false, cr.update_attributes(state: "Committed",
1373 mounts: cr.mounts.merge(sm),
1375 assert_equal [:secret_mounts], cr.errors.messages.keys
1378 test "using runtime_token" do
1379 set_user_from_auth :spectator
1380 spec = api_client_authorizations(:active)
1381 cr = create_minimal_req!(state: "Committed", runtime_token: spec.token, priority: 1)
1383 c = Container.find_by_uuid cr.container_uuid
1385 assert_nil c.auth_uuid
1386 assert_equal c.runtime_token, spec.token
1388 assert_not_nil ApiClientAuthorization.find_by_uuid(spec.uuid)
1390 act_as_system_user do
1391 c.update_attributes!(state: Container::Complete,
1393 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
1394 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
1399 assert_nil cr.runtime_token
1400 assert_nil c.runtime_token
1403 test "invalid runtime_token" do
1404 set_user_from_auth :active
1405 spec = api_client_authorizations(:spectator)
1406 assert_raises(ArgumentError) do
1407 cr = create_minimal_req!(state: "Committed", runtime_token: "#{spec.token}xx")
1412 test "default output_storage_classes" do
1413 saved = Rails.configuration.DefaultStorageClasses
1414 Rails.configuration.DefaultStorageClasses = ["foo"]
1416 act_as_user users(:active) do
1417 cr = create_minimal_req!(priority: 1,
1418 state: ContainerRequest::Committed,
1422 output = Collection.find_by_uuid(cr.output_uuid)
1423 assert_equal ["foo"], output.storage_classes_desired
1426 Rails.configuration.DefaultStorageClasses = saved
1430 test "setting output_storage_classes" do
1431 act_as_user users(:active) do
1432 cr = create_minimal_req!(priority: 1,
1433 state: ContainerRequest::Committed,
1435 output_storage_classes: ["foo_storage_class", "bar_storage_class"])
1438 output = Collection.find_by_uuid(cr.output_uuid)
1439 assert_equal ["foo_storage_class", "bar_storage_class"], output.storage_classes_desired
1440 log = Collection.find_by_uuid(cr.log_uuid)
1441 assert_equal ["foo_storage_class", "bar_storage_class"], log.storage_classes_desired
1445 test "reusing container with different container_request.output_storage_classes" do
1446 common_attrs = {cwd: "test",
1448 command: ["echo", "hello"],
1449 output_path: "test",
1450 runtime_constraints: {"vcpus" => 4,
1451 "ram" => 12000000000},
1452 mounts: {"test" => {"kind" => "json"}},
1453 environment: {"var" => "value1"},
1454 output_storage_classes: ["foo_storage_class"]}
1455 set_user_from_auth :active
1456 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed}))
1457 cont1 = run_container(cr1)
1460 output1 = Collection.find_by_uuid(cr1.output_uuid)
1462 # Testing with use_existing default value
1463 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
1464 output_storage_classes: ["bar_storage_class"]}))
1466 assert_not_nil cr1.container_uuid
1467 assert_nil cr2.container_uuid
1469 # Update cr2 to commited state, check for reuse, then run it
1470 cr2.update_attributes!({state: ContainerRequest::Committed})
1471 assert_equal cr1.container_uuid, cr2.container_uuid
1474 output2 = Collection.find_by_uuid(cr2.output_uuid)
1476 # the original CR output has the original storage class,
1477 # but the second CR output has the new storage class.
1478 assert_equal ["foo_storage_class"], cont1.output_storage_classes
1479 assert_equal ["foo_storage_class"], output1.storage_classes_desired
1480 assert_equal ["bar_storage_class"], output2.storage_classes_desired
1484 [{}, {}, {"type": "output"}],
1485 [{"a1": "b1"}, {}, {"type": "output", "a1": "b1"}],
1486 [{}, {"a1": "b1"}, {"type": "output", "a1": "b1"}],
1487 [{"a1": "b1"}, {"a1": "c1"}, {"type": "output", "a1": "b1"}],
1488 [{"a1": "b1"}, {"a2": "c2"}, {"type": "output", "a1": "b1", "a2": "c2"}],
1489 [{"type": "blah"}, {}, {"type": "blah"}],
1490 ].each do |cr_prop, container_prop, expect_prop|
1491 test "setting output_properties #{cr_prop} #{container_prop} on current container" do
1492 act_as_user users(:active) do
1493 cr = create_minimal_req!(priority: 1,
1494 state: ContainerRequest::Committed,
1496 output_properties: cr_prop)
1498 act_as_system_user do
1499 logc = Collection.new(owner_uuid: system_user_uuid,
1500 manifest_text: ". ef772b2f28e2c8ca84de45466ed19ee9+7815 0:0:arv-mount.txt\n")
1503 c = Container.find_by_uuid(cr.container_uuid)
1504 c.update_attributes!(state: Container::Locked)
1505 c.update_attributes!(state: Container::Running)
1507 c.update_attributes!(output_properties: container_prop)
1509 c.update_attributes!(state: Container::Complete,
1511 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
1512 log: logc.portable_data_hash)
1517 expect_prop["container_request"] = cr.uuid
1518 output = Collection.find_by_uuid(cr.output_uuid)
1519 assert_equal expect_prop.symbolize_keys, output.properties.symbolize_keys
1524 test "Cumulative cost includes retried attempts but not reused containers" do
1525 set_user_from_auth :active
1526 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 3)
1527 c = Container.find_by_uuid cr.container_uuid
1528 act_as_system_user do
1529 c.update_attributes!(state: Container::Locked)
1530 c.update_attributes!(state: Container::Running)
1531 c.update_attributes!(state: Container::Cancelled, cost: 3)
1534 assert_equal 3, cr.cumulative_cost
1536 c = Container.find_by_uuid cr.container_uuid
1539 assert_equal 0, c.subrequests_cost
1541 # cr2 is a child/subrequest
1542 cr2 = with_container_auth(c) do
1543 create_minimal_req!(priority: 10, state: "Committed", container_count_max: 1, command: ["echo", "foo2"])
1545 assert_equal c.uuid, cr2.requesting_container_uuid
1546 c2 = Container.find_by_uuid cr2.container_uuid
1547 act_as_system_user do
1548 c2.update_attributes!(state: Container::Locked)
1549 c2.update_attributes!(state: Container::Running)
1550 logc = Collection.new(owner_uuid: system_user_uuid,
1551 manifest_text: ". ef772b2f28e2c8ca84de45466ed19ee9+7815 0:0:arv-mount.txt\n")
1553 c2.update_attributes!(state: Container::Complete,
1555 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
1556 log: logc.portable_data_hash,
1560 assert_equal 7, c.subrequests_cost
1562 # cr3 is an identical child/subrequest, will reuse c2
1563 cr3 = with_container_auth(c) do
1564 create_minimal_req!(priority: 10, state: "Committed", container_count_max: 1, command: ["echo", "foo2"])
1566 assert_equal c.uuid, cr3.requesting_container_uuid
1567 c3 = Container.find_by_uuid cr3.container_uuid
1568 assert_equal c2.uuid, c3.uuid
1569 assert_equal Container::Complete, c3.state
1571 assert_equal 7, c.subrequests_cost
1573 act_as_system_user do
1574 c.update_attributes!(state: Container::Complete, exit_code: 0, cost: 9)
1578 assert_equal 7, c.subrequests_cost
1580 assert_equal 3+7+9, cr.cumulative_cost