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'
9 class ContainerRequestTest < ActiveSupport::TestCase
10 include DockerMigrationHelper
12 include ContainerTestHelper
14 def with_container_auth(ctr)
15 auth_was = Thread.current[:api_client_authorization]
16 Thread.current[:api_client_authorization] = ApiClientAuthorization.find_by_uuid(ctr.auth_uuid)
20 Thread.current[:api_client_authorization] = auth_was
26 ctr.update_attributes!(state: Container::Locked)
27 ctr.update_attributes!(state: Container::Running)
31 def create_minimal_req! attrs={}
33 command: ["echo", "foo"],
34 container_image: links(:docker_image_collection_tag).name,
37 mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
39 runtime_constraints: {"vcpus" => 1, "ram" => 2},
43 cr = ContainerRequest.create!(defaults.merge(attrs))
48 def check_bogus_states cr
49 [nil, "Flubber"].each do |state|
50 assert_raises(ActiveRecord::RecordInvalid) do
58 test "Container request create" do
59 set_user_from_auth :active
60 cr = create_minimal_req!
62 assert_nil cr.container_uuid
63 assert_equal 0, cr.priority
67 # Ensure we can modify all attributes
68 cr.command = ["echo", "foo3"]
69 cr.container_image = "img3"
71 cr.environment = {"BUP" => "BOP"}
72 cr.mounts = {"BAR" => "BAZ"}
73 cr.output_path = "/tmp4"
75 cr.runtime_constraints = {"vcpus" => 4}
77 cr.description = "bar3"
80 assert_nil cr.container_uuid
85 {"vcpus" => 1, "ram" => nil},
86 {"vcpus" => 0, "ram" => 123},
87 {"vcpus" => "1", "ram" => "123"}
88 ].each do |invalid_constraints|
89 test "Create with #{invalid_constraints}" do
90 set_user_from_auth :active
91 assert_raises(ActiveRecord::RecordInvalid) do
92 cr = create_minimal_req!(state: "Committed",
94 runtime_constraints: invalid_constraints)
99 test "Update with #{invalid_constraints}" do
100 set_user_from_auth :active
101 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
103 assert_raises(ActiveRecord::RecordInvalid) do
104 cr = ContainerRequest.find_by_uuid cr.uuid
105 cr.update_attributes!(state: "Committed",
106 runtime_constraints: invalid_constraints)
111 test "Update from fixture" do
112 set_user_from_auth :active
113 cr = ContainerRequest.find_by_uuid(container_requests(:running).uuid)
114 cr.update_attributes!(description: "New description")
115 assert_equal "New description", cr.description
118 test "Update with valid runtime constraints" do
119 set_user_from_auth :active
120 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
122 cr = ContainerRequest.find_by_uuid cr.uuid
123 cr.update_attributes!(state: "Committed",
124 runtime_constraints: {"vcpus" => 1, "ram" => 23})
125 assert_not_nil cr.container_uuid
128 test "Container request priority must be non-nil" do
129 set_user_from_auth :active
130 cr = create_minimal_req!
132 cr.state = "Committed"
133 assert_raises(ActiveRecord::RecordInvalid) do
138 test "Container request commit" do
139 set_user_from_auth :active
140 cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 30})
142 assert_nil cr.container_uuid
145 cr.state = "Committed"
151 assert_equal({"vcpus" => 2, "ram" => 30}, cr.runtime_constraints)
153 assert_not_nil cr.container_uuid
154 c = Container.find_by_uuid cr.container_uuid
156 assert_equal ["echo", "foo"], c.command
157 assert_equal collections(:docker_image).portable_data_hash, c.container_image
158 assert_equal "/tmp", c.cwd
159 assert_equal({}, c.environment)
160 assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
161 assert_equal "/out", c.output_path
162 assert_equal({"keep_cache_ram"=>268435456, "vcpus" => 2, "ram" => 30}, c.runtime_constraints)
163 assert_operator 0, :<, c.priority
165 assert_raises(ActiveRecord::RecordInvalid) do
175 assert_equal 0, cr.priority
176 assert_equal 0, c.priority
179 test "Independent container requests" do
180 set_user_from_auth :active
181 cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
182 cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
184 c1 = Container.find_by_uuid cr1.container_uuid
185 assert_operator 0, :<, c1.priority
187 c2 = Container.find_by_uuid cr2.container_uuid
188 assert_operator c1.priority, :<, c2.priority
189 c2priority_was = c2.priority
191 cr1.update_attributes!(priority: 0)
194 assert_equal 0, c1.priority
197 assert_equal c2priority_was, c2.priority
200 test "Request is finalized when its container is cancelled" do
201 set_user_from_auth :active
202 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 1)
203 assert_equal users(:active).uuid, cr.modified_by_user_uuid
205 act_as_system_user do
206 Container.find_by_uuid(cr.container_uuid).
207 update_attributes!(state: Container::Cancelled)
211 assert_equal "Final", cr.state
212 assert_equal users(:active).uuid, cr.modified_by_user_uuid
215 test "Request is finalized when its container is completed" do
216 set_user_from_auth :active
217 project = groups(:private)
218 cr = create_minimal_req!(owner_uuid: project.uuid,
221 assert_equal users(:active).uuid, cr.modified_by_user_uuid
223 c = act_as_system_user do
224 c = Container.find_by_uuid(cr.container_uuid)
225 c.update_attributes!(state: Container::Locked)
226 c.update_attributes!(state: Container::Running)
231 assert_equal "Committed", cr.state
233 output_pdh = '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
234 log_pdh = 'fa7aeb5140e2848d39b416daeef4ffc5+45'
235 act_as_system_user do
236 c.update_attributes!(state: Container::Complete,
242 assert_equal "Final", cr.state
243 assert_equal users(:active).uuid, cr.modified_by_user_uuid
244 ['output', 'log'].each do |out_type|
245 pdh = Container.find_by_uuid(cr.container_uuid).send(out_type)
246 assert_equal(1, Collection.where(portable_data_hash: pdh,
247 owner_uuid: project.uuid).count,
248 "Container #{out_type} should be copied to #{project.uuid}")
250 assert_not_nil cr.output_uuid
251 assert_not_nil cr.log_uuid
252 output = Collection.find_by_uuid cr.output_uuid
253 assert_equal output_pdh, output.portable_data_hash
254 log = Collection.find_by_uuid cr.log_uuid
255 assert_equal log_pdh, log.portable_data_hash
258 test "Container makes container request, then is cancelled" do
259 set_user_from_auth :active
260 cr = create_minimal_req!(priority: 5, state: "Committed", container_count_max: 1)
262 c = Container.find_by_uuid cr.container_uuid
263 assert_operator 0, :<, c.priority
266 cr2 = with_container_auth(c) do
267 create_minimal_req!(priority: 10, state: "Committed", container_count_max: 1, command: ["echo", "foo2"])
269 assert_not_nil cr2.requesting_container_uuid
270 assert_equal users(:active).uuid, cr2.modified_by_user_uuid
272 c2 = Container.find_by_uuid cr2.container_uuid
273 assert_operator 0, :<, c2.priority
275 act_as_system_user do
276 c.state = "Cancelled"
281 assert_equal "Final", cr.state
284 assert_equal 0, cr2.priority
285 assert_equal users(:active).uuid, cr2.modified_by_user_uuid
288 assert_equal 0, c2.priority
291 test "child container priority follows same ordering as corresponding top-level ancestors" do
292 findctr = lambda { |cr| Container.find_by_uuid(cr.container_uuid) }
294 set_user_from_auth :active
297 create_minimal_req!(priority: 5, state: "Committed", environment: {"workflow" => "0"}),
298 create_minimal_req!(priority: 5, state: "Committed", environment: {"workflow" => "1"}),
299 create_minimal_req!(priority: 5, state: "Committed", environment: {"workflow" => "2"}),
301 parents = toplevel_crs.map(&findctr)
303 children = parents.map do |parent|
305 with_container_auth(parent) do
306 create_minimal_req!(state: "Committed",
308 environment: {"child" => parent.environment["workflow"]})
312 grandchildren = children.reverse.map do |child|
314 with_container_auth(child) do
315 create_minimal_req!(state: "Committed",
317 environment: {"grandchild" => child.environment["child"]})
319 end.reverse.map(&findctr)
321 shared_grandchildren = children.map do |child|
322 with_container_auth(child) do
323 create_minimal_req!(state: "Committed",
325 environment: {"grandchild" => "shared"})
329 assert_equal shared_grandchildren[0].uuid, shared_grandchildren[1].uuid
330 assert_equal shared_grandchildren[0].uuid, shared_grandchildren[2].uuid
331 shared_grandchild = shared_grandchildren[0]
333 set_user_from_auth :active
335 # parents should be prioritized by submit time.
336 assert_operator parents[0].priority, :>, parents[1].priority
337 assert_operator parents[1].priority, :>, parents[2].priority
339 # children should be prioritized in same order as their respective
341 assert_operator children[0].priority, :>, children[1].priority
342 assert_operator children[1].priority, :>, children[2].priority
344 # grandchildren should also be prioritized in the same order,
345 # despite having been submitted in the opposite order.
346 assert_operator grandchildren[0].priority, :>, grandchildren[1].priority
347 assert_operator grandchildren[1].priority, :>, grandchildren[2].priority
349 # shared grandchild container should be prioritized above
350 # everything that isn't needed by parents[0], but not above
351 # earlier-submitted descendants of parents[0]
352 assert_operator shared_grandchild.priority, :>, grandchildren[1].priority
353 assert_operator shared_grandchild.priority, :>, children[1].priority
354 assert_operator shared_grandchild.priority, :>, parents[1].priority
355 assert_operator shared_grandchild.priority, :<=, grandchildren[0].priority
356 assert_operator shared_grandchild.priority, :<=, children[0].priority
357 assert_operator shared_grandchild.priority, :<=, parents[0].priority
359 # increasing priority of the most recent toplevel container should
360 # reprioritize all of its descendants (including the shared
361 # grandchild) above everything else.
362 toplevel_crs[2].update_attributes!(priority: 72)
363 (parents + children + grandchildren + [shared_grandchild]).map(&:reload)
364 assert_operator shared_grandchild.priority, :>, grandchildren[0].priority
365 assert_operator shared_grandchild.priority, :>, children[0].priority
366 assert_operator shared_grandchild.priority, :>, parents[0].priority
367 assert_operator shared_grandchild.priority, :>, grandchildren[1].priority
368 assert_operator shared_grandchild.priority, :>, children[1].priority
369 assert_operator shared_grandchild.priority, :>, parents[1].priority
370 # ...but the shared container should not have higher priority than
371 # the earlier-submitted descendants of the high-priority workflow.
372 assert_operator shared_grandchild.priority, :<=, grandchildren[2].priority
373 assert_operator shared_grandchild.priority, :<=, children[2].priority
374 assert_operator shared_grandchild.priority, :<=, parents[2].priority
378 ['running_container_auth', 'zzzzz-dz642-runningcontainr', 1],
379 ['active_no_prefs', nil, 0],
380 ].each do |token, expected, expected_priority|
381 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
382 set_user_from_auth token
383 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
384 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
385 assert_equal expected, cr.requesting_container_uuid
386 assert_equal expected_priority, cr.priority
390 [[{"vcpus" => [2, nil]},
391 lambda { |resolved| resolved["vcpus"] == 2 }],
392 [{"vcpus" => [3, 7]},
393 lambda { |resolved| resolved["vcpus"] == 3 }],
395 lambda { |resolved| resolved["vcpus"] == 4 }],
396 [{"ram" => [1000000000, 2000000000]},
397 lambda { |resolved| resolved["ram"] == 1000000000 }],
398 [{"ram" => [1234234234]},
399 lambda { |resolved| resolved["ram"] == 1234234234 }],
400 ].each do |rc, okfunc|
401 test "resolve runtime constraint range #{rc} to values" do
402 resolved = Container.resolve_runtime_constraints(rc)
403 assert(okfunc.call(resolved),
404 "container runtime_constraints was #{resolved.inspect}")
409 "kind" => "collection",
410 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
413 resolved["/out"] == {
414 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
415 "kind" => "collection",
420 "kind" => "collection",
421 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
422 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
425 resolved["/out"] == {
426 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
427 "kind" => "collection",
431 ].each do |mounts, okfunc|
432 test "resolve mounts #{mounts.inspect} to values" do
433 set_user_from_auth :active
434 resolved = Container.resolve_mounts(mounts)
435 assert(okfunc.call(resolved),
436 "Container.resolve_mounts returned #{resolved.inspect}")
440 test 'mount unreadable collection' do
441 set_user_from_auth :spectator
444 "kind" => "collection",
445 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
449 assert_raises(ArvadosModel::UnresolvableContainerError) do
450 Container.resolve_mounts(m)
454 test 'mount collection with mismatched UUID and PDH' do
455 set_user_from_auth :active
458 "kind" => "collection",
459 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
460 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
464 assert_raises(ArgumentError) do
465 Container.resolve_mounts(m)
469 ['arvados/apitestfixture:latest',
470 'arvados/apitestfixture',
471 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
473 test "Container.resolve_container_image(#{tag.inspect})" do
474 set_user_from_auth :active
475 resolved = Container.resolve_container_image(tag)
476 assert_equal resolved, collections(:docker_image).portable_data_hash
480 test "Container.resolve_container_image(pdh)" do
481 set_user_from_auth :active
482 [[:docker_image, 'v1'], [:docker_image_1_12, 'v2']].each do |coll, ver|
483 Rails.configuration.docker_image_formats = [ver]
484 pdh = collections(coll).portable_data_hash
485 resolved = Container.resolve_container_image(pdh)
486 assert_equal resolved, pdh
490 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
492 'arvados/apitestfixture:ENOEXIST',
494 test "container_image_for_container(#{img.inspect}) => 422" do
495 set_user_from_auth :active
496 assert_raises(ArvadosModel::UnresolvableContainerError) do
497 Container.resolve_container_image(img)
502 test "migrated docker image" do
503 Rails.configuration.docker_image_formats = ['v2']
504 add_docker19_migration_link
506 # Test that it returns only v2 images even though request is for v1 image.
508 set_user_from_auth :active
509 cr = create_minimal_req!(command: ["true", "1"],
510 container_image: collections(:docker_image).portable_data_hash)
511 assert_equal(Container.resolve_container_image(cr.container_image),
512 collections(:docker_image_1_12).portable_data_hash)
514 cr = create_minimal_req!(command: ["true", "2"],
515 container_image: links(:docker_image_collection_tag).name)
516 assert_equal(Container.resolve_container_image(cr.container_image),
517 collections(:docker_image_1_12).portable_data_hash)
520 test "use unmigrated docker image" do
521 Rails.configuration.docker_image_formats = ['v1']
522 add_docker19_migration_link
524 # Test that it returns only supported v1 images even though there is a
527 set_user_from_auth :active
528 cr = create_minimal_req!(command: ["true", "1"],
529 container_image: collections(:docker_image).portable_data_hash)
530 assert_equal(Container.resolve_container_image(cr.container_image),
531 collections(:docker_image).portable_data_hash)
533 cr = create_minimal_req!(command: ["true", "2"],
534 container_image: links(:docker_image_collection_tag).name)
535 assert_equal(Container.resolve_container_image(cr.container_image),
536 collections(:docker_image).portable_data_hash)
539 test "incompatible docker image v1" do
540 Rails.configuration.docker_image_formats = ['v1']
541 add_docker19_migration_link
543 # Don't return unsupported v2 image even if we ask for it directly.
544 set_user_from_auth :active
545 cr = create_minimal_req!(command: ["true", "1"],
546 container_image: collections(:docker_image_1_12).portable_data_hash)
547 assert_raises(ArvadosModel::UnresolvableContainerError) do
548 Container.resolve_container_image(cr.container_image)
552 test "incompatible docker image v2" do
553 Rails.configuration.docker_image_formats = ['v2']
554 # No migration link, don't return unsupported v1 image,
556 set_user_from_auth :active
557 cr = create_minimal_req!(command: ["true", "1"],
558 container_image: collections(:docker_image).portable_data_hash)
559 assert_raises(ArvadosModel::UnresolvableContainerError) do
560 Container.resolve_container_image(cr.container_image)
562 cr = create_minimal_req!(command: ["true", "2"],
563 container_image: links(:docker_image_collection_tag).name)
564 assert_raises(ArvadosModel::UnresolvableContainerError) do
565 Container.resolve_container_image(cr.container_image)
569 test "requestor can retrieve container owned by dispatch" do
570 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
571 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
572 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)
576 [{"var" => "value1"}, {"var" => "value1"}, nil],
577 [{"var" => "value1"}, {"var" => "value1"}, true],
578 [{"var" => "value1"}, {"var" => "value1"}, false],
579 [{"var" => "value1"}, {"var" => "value2"}, nil],
580 ].each do |env1, env2, use_existing|
581 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
582 common_attrs = {cwd: "test",
584 command: ["echo", "hello"],
586 runtime_constraints: {"vcpus" => 4,
587 "ram" => 12000000000},
588 mounts: {"test" => {"kind" => "json"}}}
589 set_user_from_auth :active
590 cr1 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Committed,
593 # Testing with use_existing default value
594 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
598 cr2 = create_minimal_req!(common_attrs.merge({state: ContainerRequest::Uncommitted,
600 use_existing: use_existing}))
602 assert_not_nil cr1.container_uuid
603 assert_nil cr2.container_uuid
605 # Update cr2 to commited state and check for container equality on different cases:
606 # * When env1 and env2 are equal and use_existing is true, the same container
607 # should be assigned.
608 # * When use_existing is false, a different container should be assigned.
609 # * When env1 and env2 are different, a different container should be assigned.
610 cr2.update_attributes!({state: ContainerRequest::Committed})
611 assert_equal (cr2.use_existing == true and (env1 == env2)),
612 (cr1.container_uuid == cr2.container_uuid)
616 test "requesting_container_uuid at create is not allowed" do
617 set_user_from_auth :active
618 assert_raises(ActiveRecord::RecordInvalid) do
619 create_minimal_req!(state: "Uncommitted", priority: 1, requesting_container_uuid: 'youcantdothat')
623 test "Retry on container cancelled" do
624 set_user_from_auth :active
625 cr = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2)
626 cr2 = create_minimal_req!(priority: 1, state: "Committed", container_count_max: 2, command: ["echo", "baz"])
627 prev_container_uuid = cr.container_uuid
629 c = act_as_system_user do
630 c = Container.find_by_uuid(cr.container_uuid)
631 c.update_attributes!(state: Container::Locked)
632 c.update_attributes!(state: Container::Running)
638 assert_equal "Committed", cr.state
639 assert_equal prev_container_uuid, cr.container_uuid
640 assert_not_equal cr2.container_uuid, cr.container_uuid
641 prev_container_uuid = cr.container_uuid
643 act_as_system_user do
644 c.update_attributes!(state: Container::Cancelled)
649 assert_equal "Committed", cr.state
650 assert_not_equal prev_container_uuid, cr.container_uuid
651 assert_not_equal cr2.container_uuid, cr.container_uuid
652 prev_container_uuid = cr.container_uuid
654 c = act_as_system_user do
655 c = Container.find_by_uuid(cr.container_uuid)
656 c.update_attributes!(state: Container::Cancelled)
662 assert_equal "Final", cr.state
663 assert_equal prev_container_uuid, cr.container_uuid
664 assert_not_equal cr2.container_uuid, cr.container_uuid
667 test "Output collection name setting using output_name with name collision resolution" do
668 set_user_from_auth :active
669 output_name = 'unimaginative name'
670 Collection.create!(name: output_name)
672 cr = create_minimal_req!(priority: 1,
673 state: ContainerRequest::Committed,
674 output_name: output_name)
677 assert_equal ContainerRequest::Final, cr.state
678 output_coll = Collection.find_by_uuid(cr.output_uuid)
679 # Make sure the resulting output collection name include the original name
681 assert_not_equal output_name, output_coll.name,
682 "more than one collection with the same owner and name"
683 assert output_coll.name.include?(output_name),
684 "New name should include original name"
685 assert_match /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/, output_coll.name,
686 "New name should include ISO8601 date"
689 [[0, :check_output_ttl_0],
690 [1, :check_output_ttl_1s],
691 [365*86400, :check_output_ttl_1y],
692 ].each do |ttl, checker|
693 test "output_ttl=#{ttl}" do
694 act_as_user users(:active) do
695 cr = create_minimal_req!(priority: 1,
696 state: ContainerRequest::Committed,
701 output = Collection.find_by_uuid(cr.output_uuid)
702 send(checker, db_current_time, output.trash_at, output.delete_at)
707 def check_output_ttl_0(now, trash, delete)
712 def check_output_ttl_1s(now, trash, delete)
713 assert_not_nil(trash)
714 assert_not_nil(delete)
715 assert_in_delta(trash, now + 1.second, 10)
716 assert_in_delta(delete, now + Rails.configuration.blob_signature_ttl.second, 10)
719 def check_output_ttl_1y(now, trash, delete)
720 year = (86400*365).second
721 assert_not_nil(trash)
722 assert_not_nil(delete)
723 assert_in_delta(trash, now + year, 10)
724 assert_in_delta(delete, now + year, 10)
727 def run_container(cr)
728 act_as_system_user do
729 c = Container.find_by_uuid(cr.container_uuid)
730 c.update_attributes!(state: Container::Locked)
731 c.update_attributes!(state: Container::Running)
732 c.update_attributes!(state: Container::Complete,
734 output: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45',
735 log: 'fa7aeb5140e2848d39b416daeef4ffc5+45')
740 test "Finalize committed request when reusing a finished container" do
741 set_user_from_auth :active
742 cr = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
744 assert_equal ContainerRequest::Committed, cr.state
747 assert_equal ContainerRequest::Final, cr.state
749 cr2 = create_minimal_req!(priority: 1, state: ContainerRequest::Committed)
750 assert_equal cr.container_uuid, cr2.container_uuid
751 assert_equal ContainerRequest::Final, cr2.state
753 cr3 = create_minimal_req!(priority: 1, state: ContainerRequest::Uncommitted)
754 assert_equal ContainerRequest::Uncommitted, cr3.state
755 cr3.update_attributes!(state: ContainerRequest::Committed)
756 assert_equal cr.container_uuid, cr3.container_uuid
757 assert_equal ContainerRequest::Final, cr3.state
761 [false, ActiveRecord::RecordInvalid],
763 ].each do |preemptible_conf, expected|
764 test "having Rails.configuration.preemptible_instances=#{preemptible_conf}, create preemptible container request and verify #{expected}" do
765 sp = {"preemptible" => true}
766 common_attrs = {cwd: "test",
768 command: ["echo", "hello"],
770 scheduling_parameters: sp,
771 mounts: {"test" => {"kind" => "json"}}}
772 Rails.configuration.preemptible_instances = preemptible_conf
773 set_user_from_auth :active
775 cr = create_minimal_req!(common_attrs)
776 cr.state = ContainerRequest::Committed
779 assert_raises(expected) do
784 assert_equal sp, cr.scheduling_parameters
790 'zzzzz-dz642-runningcontainr',
792 ].each do |requesting_c|
793 test "having preemptible instances active on the API server, a committed #{requesting_c.nil? ? 'non-':''}child CR should not ask for preemptible instance if parameter already set to false" do
794 common_attrs = {cwd: "test",
796 command: ["echo", "hello"],
798 scheduling_parameters: {"preemptible" => false},
799 mounts: {"test" => {"kind" => "json"}}}
801 Rails.configuration.preemptible_instances = true
802 set_user_from_auth :active
805 cr = with_container_auth(Container.find_by_uuid requesting_c) do
806 create_minimal_req!(common_attrs)
808 assert_not_nil cr.requesting_container_uuid
810 cr = create_minimal_req!(common_attrs)
813 cr.state = ContainerRequest::Committed
816 assert_equal false, cr.scheduling_parameters['preemptible']
821 [true, 'zzzzz-dz642-runningcontainr', true],
823 [false, 'zzzzz-dz642-runningcontainr', nil],
825 ].each do |preemptible_conf, requesting_c, schedule_preemptible|
826 test "having Rails.configuration.preemptible_instances=#{preemptible_conf}, #{requesting_c.nil? ? 'non-':''}child CR should #{schedule_preemptible ? '':'not'} ask for preemptible instance by default" do
827 common_attrs = {cwd: "test",
829 command: ["echo", "hello"],
831 mounts: {"test" => {"kind" => "json"}}}
833 Rails.configuration.preemptible_instances = preemptible_conf
834 set_user_from_auth :active
837 cr = with_container_auth(Container.find_by_uuid requesting_c) do
838 create_minimal_req!(common_attrs)
840 assert_not_nil cr.requesting_container_uuid
842 cr = create_minimal_req!(common_attrs)
845 cr.state = ContainerRequest::Committed
848 assert_equal schedule_preemptible, cr.scheduling_parameters['preemptible']
853 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
854 [{"partitions" => ["fastcpu","vfastcpu", 100]}, ContainerRequest::Uncommitted],
855 [{"partitions" => "fastcpu"}, ContainerRequest::Committed, ActiveRecord::RecordInvalid],
856 [{"partitions" => "fastcpu"}, ContainerRequest::Uncommitted],
857 [{"partitions" => ["fastcpu","vfastcpu"]}, ContainerRequest::Committed],
858 ].each do |sp, state, expected|
859 test "create container request with scheduling_parameters #{sp} in state #{state} and verify #{expected}" do
860 common_attrs = {cwd: "test",
862 command: ["echo", "hello"],
864 scheduling_parameters: sp,
865 mounts: {"test" => {"kind" => "json"}}}
866 set_user_from_auth :active
868 if expected == ActiveRecord::RecordInvalid
869 assert_raises(ActiveRecord::RecordInvalid) do
870 create_minimal_req!(common_attrs.merge({state: state}))
873 cr = create_minimal_req!(common_attrs.merge({state: state}))
874 assert_equal sp, cr.scheduling_parameters
876 if state == ContainerRequest::Committed
877 c = Container.find_by_uuid(cr.container_uuid)
878 assert_equal sp, c.scheduling_parameters
884 [['Committed', true, {name: "foobar", priority: 123}],
885 ['Committed', false, {container_count: 2}],
886 ['Committed', false, {container_count: 0}],
887 ['Committed', false, {container_count: nil}],
888 ['Final', false, {state: ContainerRequest::Committed, name: "foobar"}],
889 ['Final', false, {name: "foobar", priority: 123}],
890 ['Final', false, {name: "foobar", output_uuid: "zzzzz-4zz18-znfnqtbbv4spc3w"}],
891 ['Final', false, {name: "foobar", log_uuid: "zzzzz-4zz18-znfnqtbbv4spc3w"}],
892 ['Final', false, {log_uuid: "zzzzz-4zz18-znfnqtbbv4spc3w"}],
893 ['Final', false, {priority: 123}],
894 ['Final', false, {mounts: {}}],
895 ['Final', false, {container_count: 2}],
896 ['Final', true, {name: "foobar"}],
897 ['Final', true, {name: "foobar", description: "baz"}],
898 ].each do |state, permitted, updates|
899 test "state=#{state} can#{'not' if !permitted} update #{updates.inspect}" do
900 act_as_user users(:active) do
901 cr = create_minimal_req!(priority: 1,
903 container_count_max: 1)
908 act_as_system_user do
909 Container.find_by_uuid(cr.container_uuid).
910 update_attributes!(state: Container::Cancelled)
914 raise 'broken test case'
916 assert_equal state, cr.state
918 assert cr.update_attributes!(updates)
920 assert_raises(ActiveRecord::RecordInvalid) do
921 cr.update_attributes!(updates)
928 test "delete container_request and check its container's priority" do
929 act_as_user users(:active) do
930 cr = ContainerRequest.find_by_uuid container_requests(:running_to_be_deleted).uuid
932 # initially the cr's container has priority > 0
933 c = Container.find_by_uuid(cr.container_uuid)
934 assert_equal 1, c.priority
938 # the cr's container now has priority of 0
939 c = Container.find_by_uuid(cr.container_uuid)
940 assert_equal 0, c.priority
944 test "delete container_request in final state and expect no error due to before_destroy callback" do
945 act_as_user users(:active) do
946 cr = ContainerRequest.find_by_uuid container_requests(:completed).uuid
947 assert_nothing_raised {cr.destroy}
951 test "Container request valid priority" do
952 set_user_from_auth :active
953 cr = create_minimal_req!
955 assert_raises(ActiveRecord::RecordInvalid) do
975 assert_raises(ActiveRecord::RecordInvalid) do
981 # Note: some of these tests might look redundant because they test
982 # that out-of-order spellings of hashes are still considered equal
983 # regardless of whether the existing (container) or new (container
984 # request) hash needs to be re-ordered.
985 secrets = {"/foo" => {"kind" => "text", "content" => "xyzzy"}}
986 same_secrets = {"/foo" => {"content" => "xyzzy", "kind" => "text"}}
987 different_secrets = {"/foo" => {"kind" => "text", "content" => "something completely different"}}
993 [true, secrets, same_secrets],
994 [true, same_secrets, secrets],
995 [false, nil, secrets],
996 [false, {}, secrets],
997 [false, secrets, {}],
998 [false, secrets, nil],
999 [false, secrets, different_secrets],
1000 ].each do |expect_reuse, sm1, sm2|
1001 test "container reuse secret_mounts #{sm1.inspect}, #{sm2.inspect}" do
1002 set_user_from_auth :active
1003 cr1 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm1)
1004 cr2 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm2)
1005 assert_not_nil cr1.container_uuid
1006 assert_not_nil cr2.container_uuid
1008 assert_equal cr1.container_uuid, cr2.container_uuid
1010 assert_not_equal cr1.container_uuid, cr2.container_uuid
1015 test "scrub secret_mounts but reuse container for request with identical secret_mounts" do
1016 set_user_from_auth :active
1017 sm = {'/secret/foo' => {'kind' => 'text', 'content' => secret_string}}
1018 cr1 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm.dup)
1022 # secret_mounts scrubbed from db
1023 c = Container.where(uuid: cr1.container_uuid).first
1024 assert_equal({}, c.secret_mounts)
1025 assert_equal({}, cr1.secret_mounts)
1027 # can reuse container if secret_mounts match
1028 cr2 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: sm.dup)
1029 assert_equal cr1.container_uuid, cr2.container_uuid
1031 # don't reuse container if secret_mounts don't match
1032 cr3 = create_minimal_req!(state: "Committed", priority: 1, secret_mounts: {})
1033 assert_not_equal cr1.container_uuid, cr3.container_uuid
1035 assert_no_secrets_logged
1038 test "conflicting key in mounts and secret_mounts" do
1039 sm = {'/secret/foo' => {'kind' => 'text', 'content' => secret_string}}
1040 set_user_from_auth :active
1041 cr = create_minimal_req!
1042 assert_equal false, cr.update_attributes(state: "Committed",
1044 mounts: cr.mounts.merge(sm),
1046 assert_equal [:secret_mounts], cr.errors.messages.keys