3 class ContainerRequestTest < ActiveSupport::TestCase
4 def create_minimal_req! attrs={}
6 command: ["echo", "foo"],
7 container_image: links(:docker_image_collection_tag).name,
10 mounts: {"/out" => {"kind" => "tmp", "capacity" => 1000000}},
12 runtime_constraints: {"vcpus" => 1, "ram" => 2},
16 cr = ContainerRequest.create!(defaults.merge(attrs))
21 def check_bogus_states cr
22 [nil, "Flubber"].each do |state|
23 assert_raises(ActiveRecord::RecordInvalid) do
31 test "Container request create" do
32 set_user_from_auth :active
33 cr = create_minimal_req!
35 assert_nil cr.container_uuid
36 assert_nil cr.priority
40 # Ensure we can modify all attributes
41 cr.command = ["echo", "foo3"]
42 cr.container_image = "img3"
44 cr.environment = {"BUP" => "BOP"}
45 cr.mounts = {"BAR" => "BAZ"}
46 cr.output_path = "/tmp4"
48 cr.runtime_constraints = {"vcpus" => 4}
50 cr.description = "bar3"
53 assert_nil cr.container_uuid
58 {"vcpus" => 1, "ram" => nil},
59 {"vcpus" => 0, "ram" => 123},
60 {"vcpus" => "1", "ram" => "123"}
61 ].each do |invalid_constraints|
62 test "Create with #{invalid_constraints}" do
63 set_user_from_auth :active
64 assert_raises(ActiveRecord::RecordInvalid) do
65 cr = create_minimal_req!(state: "Committed",
67 runtime_constraints: invalid_constraints)
72 test "Update with #{invalid_constraints}" do
73 set_user_from_auth :active
74 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
76 assert_raises(ActiveRecord::RecordInvalid) do
77 cr = ContainerRequest.find_by_uuid cr.uuid
78 cr.update_attributes!(state: "Committed",
79 runtime_constraints: invalid_constraints)
84 test "Update from fixture" do
85 set_user_from_auth :active
86 cr = ContainerRequest.find_by_uuid(container_requests(:running).uuid)
87 cr.update_attributes!(description: "New description")
88 assert_equal "New description", cr.description
91 test "Update with valid runtime constraints" do
92 set_user_from_auth :active
93 cr = create_minimal_req!(state: "Uncommitted", priority: 1)
95 cr = ContainerRequest.find_by_uuid cr.uuid
96 cr.update_attributes!(state: "Committed",
97 runtime_constraints: {"vcpus" => 1, "ram" => 23})
98 assert_not_nil cr.container_uuid
101 test "Container request priority must be non-nil" do
102 set_user_from_auth :active
103 cr = create_minimal_req!(priority: nil)
104 cr.state = "Committed"
105 assert_raises(ActiveRecord::RecordInvalid) do
110 test "Container request commit" do
111 set_user_from_auth :active
112 cr = create_minimal_req!(runtime_constraints: {"vcpus" => 2, "ram" => 30})
114 assert_nil cr.container_uuid
117 cr.state = "Committed"
123 assert_not_nil cr.container_uuid
124 c = Container.find_by_uuid cr.container_uuid
126 assert_equal ["echo", "foo"], c.command
127 assert_equal collections(:docker_image).portable_data_hash, c.container_image
128 assert_equal "/tmp", c.cwd
129 assert_equal({}, c.environment)
130 assert_equal({"/out" => {"kind"=>"tmp", "capacity"=>1000000}}, c.mounts)
131 assert_equal "/out", c.output_path
132 assert_equal({"vcpus" => 2, "ram" => 30}, c.runtime_constraints)
133 assert_equal 1, c.priority
135 assert_raises(ActiveRecord::RecordInvalid) do
145 assert_equal 0, cr.priority
146 assert_equal 0, c.priority
150 test "Container request max priority" do
151 set_user_from_auth :active
152 cr = create_minimal_req!(priority: 5, state: "Committed")
154 c = Container.find_by_uuid cr.container_uuid
155 assert_equal 5, c.priority
157 cr2 = create_minimal_req!
159 cr2.state = "Committed"
160 cr2.container_uuid = cr.container_uuid
161 act_as_system_user do
165 # cr and cr2 have priority 5 and 10, and are being satisfied by
166 # the same container c, so c's priority should be
169 assert_equal 10, c.priority
171 cr2.update_attributes!(priority: 0)
174 assert_equal 5, c.priority
176 cr.update_attributes!(priority: 0)
179 assert_equal 0, c.priority
183 test "Independent container requests" do
184 set_user_from_auth :active
185 cr1 = create_minimal_req!(command: ["foo", "1"], priority: 5, state: "Committed")
186 cr2 = create_minimal_req!(command: ["foo", "2"], priority: 10, state: "Committed")
188 c1 = Container.find_by_uuid cr1.container_uuid
189 assert_equal 5, c1.priority
191 c2 = Container.find_by_uuid cr2.container_uuid
192 assert_equal 10, c2.priority
194 cr1.update_attributes!(priority: 0)
197 assert_equal 0, c1.priority
200 assert_equal 10, c2.priority
203 test "Request is finalized when its container is cancelled" do
204 set_user_from_auth :active
205 cr = create_minimal_req!(priority: 1, state: "Committed")
207 act_as_system_user do
208 Container.find_by_uuid(cr.container_uuid).
209 update_attributes!(state: Container::Cancelled)
213 assert_equal "Final", cr.state
216 test "Request is finalized when its container is completed" do
217 set_user_from_auth :active
218 cr = create_minimal_req!(priority: 1, state: "Committed")
220 c = act_as_system_user do
221 c = Container.find_by_uuid(cr.container_uuid)
222 c.update_attributes!(state: Container::Locked)
223 c.update_attributes!(state: Container::Running)
228 assert_equal "Committed", cr.state
230 act_as_system_user do
231 c.update_attributes!(state: Container::Complete)
235 assert_equal "Final", cr.state
238 test "Container makes container request, then is cancelled" do
239 set_user_from_auth :active
240 cr = create_minimal_req!(priority: 5, state: "Committed")
242 c = Container.find_by_uuid cr.container_uuid
243 assert_equal 5, c.priority
245 cr2 = create_minimal_req!(priority: 10, state: "Committed", requesting_container_uuid: c.uuid)
247 c2 = Container.find_by_uuid cr2.container_uuid
248 assert_equal 10, c2.priority
250 act_as_system_user do
251 c.state = "Cancelled"
256 assert_equal "Final", cr.state
259 assert_equal 0, cr2.priority
262 assert_equal 0, c2.priority
266 ['active', 'zzzzz-dz642-runningcontainr'],
267 ['active_no_prefs', nil],
268 ].each do |token, expected|
269 test "create as #{token} and expect requesting_container_uuid to be #{expected}" do
270 set_user_from_auth token
271 cr = ContainerRequest.create(container_image: "img", output_path: "/tmp", command: ["echo", "foo"])
272 assert_not_nil cr.uuid, 'uuid should be set for newly created container_request'
273 assert_equal expected, cr.requesting_container_uuid
277 [[{"vcpus" => [2, nil]},
278 lambda { |resolved| resolved["vcpus"] == 2 }],
279 [{"vcpus" => [3, 7]},
280 lambda { |resolved| resolved["vcpus"] == 3 }],
282 lambda { |resolved| resolved["vcpus"] == 4 }],
283 [{"ram" => [1000000000, 2000000000]},
284 lambda { |resolved| resolved["ram"] == 1000000000 }],
285 [{"ram" => [1234234234]},
286 lambda { |resolved| resolved["ram"] == 1234234234 }],
287 ].each do |rc, okfunc|
288 test "resolve runtime constraint range #{rc} to values" do
289 cr = ContainerRequest.new(runtime_constraints: rc)
290 resolved = cr.send :runtime_constraints_for_container
291 assert(okfunc.call(resolved),
292 "container runtime_constraints was #{resolved.inspect}")
297 "kind" => "collection",
298 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
301 resolved["/out"] == {
302 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
303 "kind" => "collection",
308 "kind" => "collection",
309 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
310 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
313 resolved["/out"] == {
314 "portable_data_hash" => "1f4b0bc7583c2a7f9102c395f4ffc5e3+45",
315 "kind" => "collection",
319 ].each do |mounts, okfunc|
320 test "resolve mounts #{mounts.inspect} to values" do
321 set_user_from_auth :active
322 cr = ContainerRequest.new(mounts: mounts)
323 resolved = cr.send :mounts_for_container
324 assert(okfunc.call(resolved),
325 "mounts_for_container returned #{resolved.inspect}")
329 test 'mount unreadable collection' do
330 set_user_from_auth :spectator
333 "kind" => "collection",
334 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
338 cr = ContainerRequest.new(mounts: m)
339 assert_raises(ArvadosModel::UnresolvableContainerError) do
340 cr.send :mounts_for_container
344 test 'mount collection with mismatched UUID and PDH' do
345 set_user_from_auth :active
348 "kind" => "collection",
349 "uuid" => "zzzzz-4zz18-znfnqtbbv4spc3w",
350 "portable_data_hash" => "fa7aeb5140e2848d39b416daeef4ffc5+45",
354 cr = ContainerRequest.new(mounts: m)
355 assert_raises(ArgumentError) do
356 cr.send :mounts_for_container
360 ['arvados/apitestfixture:latest',
361 'arvados/apitestfixture',
362 'd8309758b8fe2c81034ffc8a10c36460b77db7bc5e7b448c4e5b684f9d95a678',
364 test "container_image_for_container(#{tag.inspect})" do
365 set_user_from_auth :active
366 cr = ContainerRequest.new(container_image: tag)
367 resolved = cr.send :container_image_for_container
368 assert_equal resolved, collections(:docker_image).portable_data_hash
372 test "container_image_for_container(pdh)" do
373 set_user_from_auth :active
374 pdh = collections(:docker_image).portable_data_hash
375 cr = ContainerRequest.new(container_image: pdh)
376 resolved = cr.send :container_image_for_container
377 assert_equal resolved, pdh
380 ['acbd18db4cc2f85cedef654fccc4a4d8+3',
382 'arvados/apitestfixture:ENOEXIST',
384 test "container_image_for_container(#{img.inspect}) => 422" do
385 set_user_from_auth :active
386 cr = ContainerRequest.new(container_image: img)
387 assert_raises(ArvadosModel::UnresolvableContainerError) do
388 cr.send :container_image_for_container
393 test "requestor can retrieve container owned by dispatch" do
394 assert_not_empty Container.readable_by(users(:admin)).where(uuid: containers(:running).uuid)
395 assert_not_empty Container.readable_by(users(:active)).where(uuid: containers(:running).uuid)
396 assert_empty Container.readable_by(users(:spectator)).where(uuid: containers(:running).uuid)