Merge branch '18830-fix-centos-7-provisioning'
[arvados.git] / sdk / cwl / tests / test_container.py
index 41456a750fa3eddf66894300763fc490e31da817..bfffe6eacbc29aafe5ed0b28dab8766e702f299e 100644 (file)
@@ -88,9 +88,10 @@ class TestContainer(unittest.TestCase):
              "project_uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
             })
 
-        def make_tool(toolpath_object, loadingContext):
-            return arvados_cwl.ArvadosCommandTool(runner, toolpath_object, loadingContext)
-        runner.arv_make_tool.side_effect = make_tool
+        if isinstance(runner, mock.MagicMock):
+            def make_tool(toolpath_object, loadingContext):
+                return arvados_cwl.ArvadosCommandTool(runner, toolpath_object, loadingContext)
+            runner.arv_make_tool.side_effect = make_tool
 
         return loadingContext, runtimeContext
 
@@ -132,13 +133,14 @@ class TestContainer(unittest.TestCase):
                 "outputs": [],
                 "baseCommand": "ls",
                 "arguments": [{"valueFrom": "$(runtime.outdir)"}],
-                "id": "#",
-                "class": "org.w3id.cwl.cwl.CommandLineTool"
+                "id": "",
+                "class": "CommandLineTool",
+                "cwlVersion": "v1.2"
             })
 
             loadingContext, runtimeContext = self.helper(runner, enable_reuse)
 
-            arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+            arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
             arvtool.formatgraph = None
 
             for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
@@ -152,7 +154,7 @@ class TestContainer(unittest.TestCase):
                         'name': 'test_run_'+str(enable_reuse),
                         'runtime_constraints': {
                             'vcpus': 1,
-                            'ram': 1073741824
+                            'ram': 268435456
                         },
                         'use_existing': enable_reuse,
                         'priority': 500,
@@ -181,6 +183,7 @@ class TestContainer(unittest.TestCase):
     # For the remaining fields, the defaults will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
     @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
     def test_resource_requirements(self, keepdocker):
+        arvados_cwl.add_arv_hints()
         runner = mock.MagicMock()
         runner.ignore_docker_for_reuse = False
         runner.intermediate_output_ttl = 3600
@@ -212,18 +215,19 @@ class TestContainer(unittest.TestCase):
                 "class": "http://arvados.org/cwl#IntermediateOutput",
                 "outputTTL": 7200
             }, {
-                "class": "http://arvados.org/cwl#ReuseRequirement",
+                "class": "WorkReuse",
                 "enableReuse": False
             }],
             "baseCommand": "ls",
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool"
+            "id": "",
+            "class": "CommandLineTool",
+            "cwlVersion": "v1.2"
         })
 
         loadingContext, runtimeContext = self.helper(runner)
         runtimeContext.name = "test_resource_requirements"
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
         for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
             j.run(runtimeContext)
@@ -428,14 +432,15 @@ class TestContainer(unittest.TestCase):
             "stderr": "stderr.txt",
             "stdin": "/keep/99999999999999999999999999999996+99/file.txt",
             "arguments": [{"valueFrom": "$(runtime.outdir)"}],
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool"
+            "id": "",
+            "class": "CommandLineTool",
+            "cwlVersion": "v1.2"
         })
 
         loadingContext, runtimeContext = self.helper(runner)
         runtimeContext.name = "test_run_redirect"
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
         for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
             j.run(runtimeContext)
@@ -448,7 +453,7 @@ class TestContainer(unittest.TestCase):
                     'name': 'test_run_redirect',
                     'runtime_constraints': {
                         'vcpus': 1,
-                        'ram': 1073741824
+                        'ram': 268435456
                     },
                     'use_existing': True,
                     'priority': 500,
@@ -654,14 +659,15 @@ class TestContainer(unittest.TestCase):
             "outputs": [],
             "baseCommand": "ls",
             "arguments": [{"valueFrom": "$(runtime.outdir)"}],
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool"
+            "id": "",
+            "class": "CommandLineTool",
+            "cwlVersion": "v1.2"
         })
 
         loadingContext, runtimeContext = self.helper(runner)
         runtimeContext.name = "test_run_mounts"
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
         job_order = {
             "p1": {
@@ -691,7 +697,7 @@ class TestContainer(unittest.TestCase):
                     'name': 'test_run_mounts',
                     'runtime_constraints': {
                         'vcpus': 1,
-                        'ram': 1073741824
+                        'ram': 268435456
                     },
                     'use_existing': True,
                     'priority': 500,
@@ -724,6 +730,7 @@ class TestContainer(unittest.TestCase):
     # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
     @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
     def test_secrets(self, keepdocker):
+        arvados_cwl.add_arv_hints()
         runner = mock.MagicMock()
         runner.ignore_docker_for_reuse = False
         runner.intermediate_output_ttl = 0
@@ -737,7 +744,8 @@ class TestContainer(unittest.TestCase):
         document_loader, avsc_names, schema_metadata, metaschema_loader = cwltool.process.get_schema("v1.1")
 
         tool = cmap({"arguments": ["md5sum", "example.conf"],
-                     "class": "org.w3id.cwl.cwl.CommandLineTool",
+                     "class": "CommandLineTool",
+                     "cwlVersion": "v1.2",
                      "hints": [
                          {
                              "class": "http://commonwl.org/cwltool#Secrets",
@@ -746,7 +754,7 @@ class TestContainer(unittest.TestCase):
                              ]
                          }
                      ],
-                     "id": "#secret_job.cwl",
+                     "id": "",
                      "inputs": [
                          {
                              "id": "#secret_job.cwl/pw",
@@ -770,7 +778,7 @@ class TestContainer(unittest.TestCase):
         loadingContext, runtimeContext = self.helper(runner)
         runtimeContext.name = "test_secrets"
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
 
         job_order = {"pw": "blorp"}
@@ -787,7 +795,7 @@ class TestContainer(unittest.TestCase):
                     'name': 'test_secrets',
                     'runtime_constraints': {
                         'vcpus': 1,
-                        'ram': 1073741824
+                        'ram': 268435456
                     },
                     'use_existing': True,
                     'priority': 500,
@@ -836,8 +844,9 @@ class TestContainer(unittest.TestCase):
             "outputs": [],
             "baseCommand": "ls",
             "arguments": [{"valueFrom": "$(runtime.outdir)"}],
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool",
+            "id": "",
+            "cwlVersion": "v1.2",
+            "class": "CommandLineTool",
             "hints": [
                 {
                     "class": "ToolTimeLimit",
@@ -849,7 +858,7 @@ class TestContainer(unittest.TestCase):
         loadingContext, runtimeContext = self.helper(runner)
         runtimeContext.name = "test_timelimit"
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
 
         for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
@@ -880,8 +889,9 @@ class TestContainer(unittest.TestCase):
             "outputs": [],
             "baseCommand": "ls",
             "arguments": [{"valueFrom": "$(runtime.outdir)"}],
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool",
+            "id": "",
+            "cwlVersion": "v1.2",
+            "class": "CommandLineTool",
             "hints": [
                 {
                     "class": "http://arvados.org/cwl#OutputStorageClass",
@@ -893,7 +903,7 @@ class TestContainer(unittest.TestCase):
 
         loadingContext, runtimeContext = self.helper(runner, True)
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
 
         for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
@@ -907,7 +917,7 @@ class TestContainer(unittest.TestCase):
                     'name': 'test_run_True',
                     'runtime_constraints': {
                         'vcpus': 1,
-                        'ram': 1073741824
+                        'ram': 268435456
                     },
                     'use_existing': True,
                     'priority': 500,
@@ -955,8 +965,9 @@ class TestContainer(unittest.TestCase):
             "outputs": [],
             "baseCommand": "ls",
             "arguments": [{"valueFrom": "$(runtime.outdir)"}],
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool",
+            "id": "",
+            "class": "CommandLineTool",
+            "cwlVersion": "v1.2",
             "hints": [
             {
                 "class": "http://arvados.org/cwl#ProcessProperties",
@@ -978,7 +989,7 @@ class TestContainer(unittest.TestCase):
 
         loadingContext, runtimeContext = self.helper(runner, True)
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
 
         for j in arvtool.job({"x": "blorp"}, mock.MagicMock(), runtimeContext):
@@ -992,7 +1003,7 @@ class TestContainer(unittest.TestCase):
                     'name': 'test_run_True',
                     'runtime_constraints': {
                         'vcpus': 1,
-                        'ram': 1073741824
+                        'ram': 268435456
                     },
                     'use_existing': True,
                     'priority': 500,
@@ -1029,6 +1040,7 @@ class TestContainer(unittest.TestCase):
     # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
     @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
     def test_cuda_requirement(self, keepdocker):
+        arvados_cwl.add_arv_hints()
         arv_docker_clear_cache()
 
         runner = mock.MagicMock()
@@ -1041,67 +1053,181 @@ class TestContainer(unittest.TestCase):
         runner.api.collections().get().execute.return_value = {
             "portable_data_hash": "99999999999999999999999999999993+99"}
 
+        test_cwl_req = [{
+                "class": "http://commonwl.org/cwltool#CUDARequirement",
+                "cudaVersionMin": "11.0",
+                "cudaComputeCapability": "9.0",
+            }, {
+                "class": "http://commonwl.org/cwltool#CUDARequirement",
+                "cudaVersionMin": "11.0",
+                "cudaComputeCapability": "9.0",
+                "cudaDeviceCountMin": 2
+            }, {
+                "class": "http://commonwl.org/cwltool#CUDARequirement",
+                "cudaVersionMin": "11.0",
+                "cudaComputeCapability": ["4.0", "5.0"],
+                "cudaDeviceCountMin": 2
+            }]
+
+        test_arv_req = [{
+            'device_count': 1,
+            'driver_version': "11.0",
+            'hardware_capability': "9.0"
+        }, {
+            'device_count': 2,
+            'driver_version': "11.0",
+            'hardware_capability': "9.0"
+        }, {
+            'device_count': 2,
+            'driver_version': "11.0",
+            'hardware_capability': "4.0"
+        }]
+
+        for test_case in range(0, len(test_cwl_req)):
+
+            tool = cmap({
+                "inputs": [],
+                "outputs": [],
+                "baseCommand": "nvidia-smi",
+                "arguments": [],
+                "id": "",
+                "cwlVersion": "v1.2",
+                "class": "CommandLineTool",
+                "requirements": [test_cwl_req[test_case]]
+            })
+
+            loadingContext, runtimeContext = self.helper(runner, True)
+
+            arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
+            arvtool.formatgraph = None
+
+            for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
+                j.run(runtimeContext)
+                runner.api.container_requests().create.assert_called_with(
+                    body=JsonDiffMatcher({
+                        'environment': {
+                            'HOME': '/var/spool/cwl',
+                            'TMPDIR': '/tmp'
+                        },
+                        'name': 'test_run_True' + ("" if test_case == 0 else "_"+str(test_case+1)),
+                        'runtime_constraints': {
+                            'vcpus': 1,
+                            'ram': 268435456,
+                            'cuda': test_arv_req[test_case]
+                        },
+                        'use_existing': True,
+                        'priority': 500,
+                        'mounts': {
+                            '/tmp': {'kind': 'tmp',
+                                     "capacity": 1073741824
+                                 },
+                            '/var/spool/cwl': {'kind': 'tmp',
+                                               "capacity": 1073741824 }
+                        },
+                        'state': 'Committed',
+                        'output_name': 'Output for step test_run_True' + ("" if test_case == 0 else "_"+str(test_case+1)),
+                        'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
+                        'output_path': '/var/spool/cwl',
+                        'output_ttl': 0,
+                        'container_image': '99999999999999999999999999999993+99',
+                        'command': ['nvidia-smi'],
+                        'cwd': '/var/spool/cwl',
+                        'scheduling_parameters': {},
+                        'properties': {},
+                        'secret_mounts': {},
+                        'output_storage_classes': ["default"]
+                    }))
+
+
+    # The test passes no builder.resources
+    # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
+    @mock.patch("arvados_cwl.arvdocker.determine_image_id")
+    @mock.patch("arvados.commands.keepdocker.list_images_in_arv")
+    def test_match_local_docker(self, keepdocker, determine_image_id):
+        arvados_cwl.add_arv_hints()
+        arv_docker_clear_cache()
+
+        runner = mock.MagicMock()
+        runner.ignore_docker_for_reuse = False
+        runner.intermediate_output_ttl = 0
+        runner.secret_store = cwltool.secrets.SecretStore()
+        runner.api._rootDesc = {"revision": "20210628"}
+
+        keepdocker.return_value = [("zzzzz-4zz18-zzzzzzzzzzzzzz4", {"dockerhash": "456"}),
+                                   ("zzzzz-4zz18-zzzzzzzzzzzzzz3", {"dockerhash": "123"})]
+        determine_image_id.side_effect = lambda x: "123"
+        def execute(uuid):
+            ex = mock.MagicMock()
+            lookup = {"zzzzz-4zz18-zzzzzzzzzzzzzz4": {"portable_data_hash": "99999999999999999999999999999994+99"},
+                      "zzzzz-4zz18-zzzzzzzzzzzzzz3": {"portable_data_hash": "99999999999999999999999999999993+99"}}
+            ex.execute.return_value = lookup[uuid]
+            return ex
+        runner.api.collections().get.side_effect = execute
+
         tool = cmap({
             "inputs": [],
             "outputs": [],
-            "baseCommand": "nvidia-smi",
+            "baseCommand": "echo",
             "arguments": [],
-            "id": "#",
-            "class": "org.w3id.cwl.cwl.CommandLineTool",
-            "hints": [
-            {
-                "class": "http://arvados.org/cwl#CUDARequirement",
-                "minCUDADriverVersion": "11.0",
-                "minCUDAHardwareCapability": "9.0",
-            }
-        ]
+            "id": "",
+            "cwlVersion": "v1.2",
+            "class": "CommandLineTool"
         })
 
         loadingContext, runtimeContext = self.helper(runner, True)
 
-        arvtool = arvados_cwl.ArvadosCommandTool(runner, tool, loadingContext)
+        arvtool = cwltool.load_tool.load_tool(tool, loadingContext)
         arvtool.formatgraph = None
 
+        container_request = {
+            'environment': {
+                'HOME': '/var/spool/cwl',
+                'TMPDIR': '/tmp'
+            },
+            'name': 'test_run_True',
+            'runtime_constraints': {
+                'vcpus': 1,
+                'ram': 268435456
+            },
+            'use_existing': True,
+            'priority': 500,
+            'mounts': {
+                '/tmp': {'kind': 'tmp',
+                         "capacity": 1073741824
+                         },
+                '/var/spool/cwl': {'kind': 'tmp',
+                                   "capacity": 1073741824 }
+            },
+            'state': 'Committed',
+            'output_name': 'Output for step test_run_True',
+            'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
+            'output_path': '/var/spool/cwl',
+            'output_ttl': 0,
+            'container_image': '99999999999999999999999999999994+99',
+            'command': ['echo'],
+            'cwd': '/var/spool/cwl',
+            'scheduling_parameters': {},
+            'properties': {},
+            'secret_mounts': {},
+            'output_storage_classes': ["default"]
+        }
+
+        runtimeContext.match_local_docker = False
         for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
             j.run(runtimeContext)
             runner.api.container_requests().create.assert_called_with(
-                body=JsonDiffMatcher({
-                    'environment': {
-                        'HOME': '/var/spool/cwl',
-                        'TMPDIR': '/tmp'
-                    },
-                    'name': 'test_run_True',
-                    'runtime_constraints': {
-                        'vcpus': 1,
-                        'ram': 1073741824,
-                        'cuda': {
-                            'device_count': 1,
-                            'driver_version': "11.0",
-                            'hardware_capability': "9.0"
-                        }
-                    },
-                    'use_existing': True,
-                    'priority': 500,
-                    'mounts': {
-                        '/tmp': {'kind': 'tmp',
-                                 "capacity": 1073741824
-                             },
-                        '/var/spool/cwl': {'kind': 'tmp',
-                                           "capacity": 1073741824 }
-                    },
-                    'state': 'Committed',
-                    'output_name': 'Output for step test_run_True',
-                    'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
-                    'output_path': '/var/spool/cwl',
-                    'output_ttl': 0,
-                    'container_image': '99999999999999999999999999999993+99',
-                    'command': ['nvidia-smi'],
-                    'cwd': '/var/spool/cwl',
-                    'scheduling_parameters': {},
-                    'properties': {},
-                    'secret_mounts': {},
-                    'output_storage_classes': ["default"]
-                }))
+                body=JsonDiffMatcher(container_request))
+
+        arv_docker_clear_cache()
+        runtimeContext.match_local_docker = True
+        container_request['container_image'] = '99999999999999999999999999999993+99'
+        container_request['name'] = 'test_run_True_2'
+        container_request['output_name'] = 'Output for step test_run_True_2'
+        for j in arvtool.job({}, mock.MagicMock(), runtimeContext):
+            j.run(runtimeContext)
+            runner.api.container_requests().create.assert_called_with(
+                body=JsonDiffMatcher(container_request))
+
 
 
 class TestWorkflow(unittest.TestCase):