9623: Added method to find a reusable container, used by ContainerRequest#resolve...
[arvados.git] / services / api / test / unit / container_test.rb
index 9cc098117f68f3434b80ca7ee557f1ecfa89fa51..edc9db66e064be5e7a2a6861ed96c9899185271d 100644 (file)
@@ -8,6 +8,7 @@ class ContainerTest < ActiveSupport::TestCase
     container_image: 'img',
     output_path: '/tmp',
     priority: 1,
+    runtime_constraints: {"vcpus" => 1, "ram" => 1},
   }
 
   def minimal_new attrs={}
@@ -66,7 +67,7 @@ class ContainerTest < ActiveSupport::TestCase
                       mounts: {"BAR" => "FOO"},
                       output_path: "/tmp",
                       priority: 1,
-                      runtime_constraints: {})
+                      runtime_constraints: {"vcpus" => 1, "ram" => 1})
 
       check_illegal_modify c
       check_bogus_states c
@@ -77,6 +78,35 @@ class ContainerTest < ActiveSupport::TestCase
     end
   end
 
+  test "Container serialized hash attributes sorted" do
+    env = {"C" => 3, "B" => 2, "A" => 1}
+    m = {"F" => 3, "E" => 2, "D" => 1}
+    rc = {"vcpus" => 1, "ram" => 1}
+    c, _ = minimal_new(environment: env, mounts: m, runtime_constraints: rc)
+    assert_equal c.environment.to_json, Container.deep_sort_hash(env).to_json
+    assert_equal c.mounts.to_json, Container.deep_sort_hash(m).to_json
+    assert_equal c.runtime_constraints.to_json, Container.deep_sort_hash(rc).to_json
+  end
+
+  test 'deep_sort_hash on array of hashes' do
+    a = {'z' => [[{'a' => 'a', 'b' => 'b'}]]}
+    b = {'z' => [[{'b' => 'b', 'a' => 'a'}]]}
+    assert_equal Container.deep_sort_hash(a).to_json, Container.deep_sort_hash(b).to_json
+  end
+
+  test "Container find reusable method" do
+    set_user_from_auth :active
+    c = Container.find_reusable(container_image: "test",
+                                cwd: "test",
+                                command: ["echo", "hello"],
+                                output_path: "test",
+                                runtime_constraints: {"vcpus" => 4, "ram" => 12000000000},
+                                mounts: {"test" => {"kind" => "json"}},
+                                environment: {"var" => "test"})
+    assert_not_nil c
+    assert_equal c.uuid, containers(:completed).uuid
+  end
+
   test "Container running" do
     c, _ = minimal_new priority: 1