Merge branch 'master' into 8766-cwl-collection-project
[arvados.git] / sdk / cwl / tests / test_job.py
1 import unittest
2 import mock
3 import arvados_cwl
4
5 class TestJob(unittest.TestCase):
6
7     # The test passes no builder.resources
8     # Hence the default resources will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
9     def test_run(self):
10         runner = mock.MagicMock()
11         runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
12         tool = {
13             "inputs": [],
14             "outputs": [],
15             "baseCommand": "ls"
16         }
17         arvtool = arvados_cwl.ArvadosCommandTool(runner, tool)
18         arvtool.formatgraph = None
19         for j in arvtool.job({}, "", mock.MagicMock()):
20             j.run()
21         runner.api.jobs().create.assert_called_with(body={
22             'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
23             'runtime_constraints': {},
24             'script_parameters': {
25                 'tasks': [{
26                     'task.env': {'TMPDIR': '$(task.tmpdir)'},
27                     'command': ['ls']
28                 }],
29                 'crunchrunner': arvados_cwl.crunchrunner_pdh + '/crunchrunner'
30             },
31             'script_version': 'master',
32             'minimum_script_version': '9e5b98e8f5f4727856b53447191f9c06e3da2ba6',
33             'repository': 'arvados',
34             'script': 'crunchrunner',
35             'runtime_constraints': {
36                 'min_cores_per_node': 1,
37                 'min_ram_mb_per_node': 1024,
38                 'min_scratch_mb_per_node': 2048 # tmpdirSize + outdirSize
39             }
40         }, find_or_create=True)
41
42     # The test passes some fields in builder.resources
43     # For the remaining fields, the defaults will apply: {'cores': 1, 'ram': 1024, 'outdirSize': 1024, 'tmpdirSize': 1024}
44     def test_resource_requirements(self):
45         runner = mock.MagicMock()
46         runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
47         tool = {
48             "inputs": [],
49             "outputs": [],
50             "hints": [{
51                 "class": "ResourceRequirement",
52                 "coresMin": 3,
53                 "ramMin": 3000,
54                 "tmpdirMin": 4000
55             }],
56             "baseCommand": "ls"
57         }
58         arvtool = arvados_cwl.ArvadosCommandTool(runner, tool)
59         arvtool.formatgraph = None
60         for j in arvtool.job({}, "", mock.MagicMock()):
61             j.run()
62         runner.api.jobs().create.assert_called_with(body={
63             'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
64             'runtime_constraints': {},
65             'script_parameters': {
66                 'tasks': [{
67                     'task.env': {'TMPDIR': '$(task.tmpdir)'},
68                     'command': ['ls']
69                 }],
70 <<<<<<< HEAD
71                 'crunchrunner': arvados_cwl.crunchrunner_pdh + '/crunchrunner'
72 =======
73                 'crunchrunner': 'ff6fc71e593081ef9733afacaeee15ea+140/crunchrunner'
74 >>>>>>> master
75             },
76             'script_version': 'master',
77             'minimum_script_version': '9e5b98e8f5f4727856b53447191f9c06e3da2ba6',
78             'repository': 'arvados',
79             'script': 'crunchrunner',
80             'runtime_constraints': {
81                 'min_cores_per_node': 3,
82                 'min_ram_mb_per_node': 3000,
83                 'min_scratch_mb_per_node': 5024 # tmpdirSize + outdirSize
84             }
85         }, find_or_create=True)
86
87     @mock.patch("arvados.collection.Collection")
88     def test_done(self, col):
89         api = mock.MagicMock()
90
91         runner = mock.MagicMock()
92         runner.api = api
93         runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
94         runner.num_retries = 0
95
96         col().open.return_value = []
97         api.collections().list().execute.side_effect = ({"items": []},
98                                                         {"items": [{"manifest_text": "XYZ"}]})
99
100         arvjob = arvados_cwl.ArvadosJob(runner)
101         arvjob.name = "testjob"
102         arvjob.builder = mock.MagicMock()
103         arvjob.output_callback = mock.MagicMock()
104         arvjob.collect_outputs = mock.MagicMock()
105
106         arvjob.done({
107             "state": "Complete",
108             "output": "99999999999999999999999999999993+99",
109             "log": "99999999999999999999999999999994+99",
110             "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
111         })
112
113         api.collections().list.assert_has_calls([
114             mock.call(),
115             mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
116                           ['portable_data_hash', '=', '99999999999999999999999999999993+99'],
117                           ['name', '=', 'Output 9999999 of testjob']]),
118             mock.call().execute(num_retries=0),
119             mock.call(limit=1, filters=[['portable_data_hash', '=', '99999999999999999999999999999993+99']],
120                  select=['manifest_text']),
121             mock.call().execute(num_retries=0)])
122
123         api.collections().create.assert_called_with(
124             ensure_unique_name=True,
125             body={'portable_data_hash': '99999999999999999999999999999993+99',
126                   'manifest_text': 'XYZ',
127                   'owner_uuid': 'zzzzz-8i9sb-zzzzzzzzzzzzzzz',
128                   'name': 'Output 9999999 of testjob'})
129
130     @mock.patch("arvados.collection.Collection")
131     def test_done_use_existing_collection(self, col):
132         api = mock.MagicMock()
133
134         runner = mock.MagicMock()
135         runner.api = api
136         runner.project_uuid = "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
137         runner.num_retries = 0
138
139         col().open.return_value = []
140         api.collections().list().execute.side_effect = ({"items": [{"uuid": "zzzzz-4zz18-zzzzzzzzzzzzzz2"}]},)
141
142         arvjob = arvados_cwl.ArvadosJob(runner)
143         arvjob.name = "testjob"
144         arvjob.builder = mock.MagicMock()
145         arvjob.output_callback = mock.MagicMock()
146         arvjob.collect_outputs = mock.MagicMock()
147
148         arvjob.done({
149             "state": "Complete",
150             "output": "99999999999999999999999999999993+99",
151             "log": "99999999999999999999999999999994+99",
152             "uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"
153         })
154
155         api.collections().list.assert_has_calls([
156             mock.call(),
157             mock.call(filters=[['owner_uuid', '=', 'zzzzz-8i9sb-zzzzzzzzzzzzzzz'],
158                                ['portable_data_hash', '=', '99999999999999999999999999999993+99'],
159                                ['name', '=', 'Output 9999999 of testjob']]),
160             mock.call().execute(num_retries=0)])
161
162         self.assertFalse(api.collections().create.called)