Bug 1414921 - [taskgraph] Convert test_morph.py to the pytest format, r?dustin draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Wed, 04 Apr 2018 15:22:42 -0400
changeset 778035 8a17d3fe18f612e6046fa3902a88f37cacec55cb
parent 777823 1b258f938525fda65ef80ffa0408bc665d5d8948
child 778036 f1936a4966feec3f891cb461976ef08c9f3b2a32
child 778037 9a2bde7ff85c2334b6df321796c2e44d4c7fdda8
push id105373
push userahalberstadt@mozilla.com
push dateThu, 05 Apr 2018 18:52:18 +0000
reviewersdustin
bugs1414921
milestone61.0a1
Bug 1414921 - [taskgraph] Convert test_morph.py to the pytest format, r?dustin I was going to add a new test to this file and realized there was a lot of duplication happening. Since I find it a lot easier to abstract code using pytest's fixtures than it is to store instance variables and methods on a unittest.TestCase class, I decided to convert this file to the pytest format first. Besides, I believe pytest's format makes the test a lot easier to read and understand. MozReview-Commit-ID: CfW9K11tFhk
taskcluster/taskgraph/test/test_morph.py
--- a/taskcluster/taskgraph/test/test_morph.py
+++ b/taskcluster/taskgraph/test/test_morph.py
@@ -1,219 +1,220 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
-import unittest
+import pytest
 
 from taskgraph import morph
 from taskgraph.graph import Graph
 from taskgraph.taskgraph import TaskGraph
 from taskgraph.task import Task
 
 from mozunit import main
 
 
-class MorphTestCase(unittest.TestCase):
-
-    def make_taskgraph(self, tasks):
+@pytest.fixture
+def make_taskgraph():
+    def inner(tasks):
         label_to_taskid = {k: k + '-tid' for k in tasks}
         for label, task_id in label_to_taskid.iteritems():
             tasks[label].task_id = task_id
         graph = Graph(nodes=set(tasks), edges=set())
         taskgraph = TaskGraph(tasks, graph)
         return taskgraph, label_to_taskid
 
+    return inner
 
-class TestIndexTask(MorphTestCase):
 
-    def test_make_index_tasks(self):
-        task_def = {
-            'routes': [
-                "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.es-MX",
-                "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.fy-NL",
-                "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.sk",
-                "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.sl",
-                "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.uk",
-                "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.zh-CN",
-                "index.gecko.v2.mozilla-central.pushdate."
-                "2017.04.04.20170404100210.firefox-l10n.linux64-opt.es-MX",
-                "index.gecko.v2.mozilla-central.pushdate."
-                "2017.04.04.20170404100210.firefox-l10n.linux64-opt.fy-NL",
-                "index.gecko.v2.mozilla-central.pushdate."
-                "2017.04.04.20170404100210.firefox-l10n.linux64-opt.sk",
-                "index.gecko.v2.mozilla-central.pushdate."
-                "2017.04.04.20170404100210.firefox-l10n.linux64-opt.sl",
-                "index.gecko.v2.mozilla-central.pushdate."
-                "2017.04.04.20170404100210.firefox-l10n.linux64-opt.uk",
-                "index.gecko.v2.mozilla-central.pushdate."
-                "2017.04.04.20170404100210.firefox-l10n.linux64-opt.zh-CN",
-                "index.gecko.v2.mozilla-central.revision."
-                "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.es-MX",
-                "index.gecko.v2.mozilla-central.revision."
-                "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.fy-NL",
-                "index.gecko.v2.mozilla-central.revision."
-                "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.sk",
-                "index.gecko.v2.mozilla-central.revision."
-                "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.sl",
-                "index.gecko.v2.mozilla-central.revision."
-                "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.uk",
-                "index.gecko.v2.mozilla-central.revision."
-                "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.zh-CN"
-            ],
-            'deadline': 'soon',
-            'metadata': {
-                'description': 'desc',
-                'owner': 'owner@foo.com',
-                'source': 'https://source',
-            },
-        }
-        task = Task(kind='test', label='a', attributes={}, task=task_def)
-        docker_task = Task(kind='docker-image', label='build-docker-image-index-task',
-                           attributes={}, task={})
-        taskgraph, label_to_taskid = self.make_taskgraph({
-            task.label: task,
-            docker_task.label: docker_task,
-        })
+def test_make_index_tasks(make_taskgraph):
+    task_def = {
+        'routes': [
+            "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.es-MX",
+            "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.fy-NL",
+            "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.sk",
+            "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.sl",
+            "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.uk",
+            "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.zh-CN",
+            "index.gecko.v2.mozilla-central.pushdate."
+            "2017.04.04.20170404100210.firefox-l10n.linux64-opt.es-MX",
+            "index.gecko.v2.mozilla-central.pushdate."
+            "2017.04.04.20170404100210.firefox-l10n.linux64-opt.fy-NL",
+            "index.gecko.v2.mozilla-central.pushdate."
+            "2017.04.04.20170404100210.firefox-l10n.linux64-opt.sk",
+            "index.gecko.v2.mozilla-central.pushdate."
+            "2017.04.04.20170404100210.firefox-l10n.linux64-opt.sl",
+            "index.gecko.v2.mozilla-central.pushdate."
+            "2017.04.04.20170404100210.firefox-l10n.linux64-opt.uk",
+            "index.gecko.v2.mozilla-central.pushdate."
+            "2017.04.04.20170404100210.firefox-l10n.linux64-opt.zh-CN",
+            "index.gecko.v2.mozilla-central.revision."
+            "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.es-MX",
+            "index.gecko.v2.mozilla-central.revision."
+            "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.fy-NL",
+            "index.gecko.v2.mozilla-central.revision."
+            "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.sk",
+            "index.gecko.v2.mozilla-central.revision."
+            "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.sl",
+            "index.gecko.v2.mozilla-central.revision."
+            "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.uk",
+            "index.gecko.v2.mozilla-central.revision."
+            "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.zh-CN"
+        ],
+        'deadline': 'soon',
+        'metadata': {
+            'description': 'desc',
+            'owner': 'owner@foo.com',
+            'source': 'https://source',
+        },
+    }
+    task = Task(kind='test', label='a', attributes={}, task=task_def)
+    docker_task = Task(kind='docker-image', label='build-docker-image-index-task',
+                       attributes={}, task={})
+    taskgraph, label_to_taskid = make_taskgraph({
+        task.label: task,
+        docker_task.label: docker_task,
+    })
 
-        index_task = morph.make_index_task(task, taskgraph, label_to_taskid)
+    index_task = morph.make_index_task(task, taskgraph, label_to_taskid)
 
-        self.assertEqual(index_task.task['payload']['command'][0], 'insert-indexes.js')
-        self.assertEqual(index_task.task['payload']['env']['TARGET_TASKID'], 'a-tid')
+    assert index_task.task['payload']['command'][0] == 'insert-indexes.js'
+    assert index_task.task['payload']['env']['TARGET_TASKID'] == 'a-tid'
 
-        # check the scope summary
-        self.assertEqual(index_task.task['scopes'],
-                         ['index:insert-task:gecko.v2.mozilla-central.*'])
+    # check the scope summary
+    assert index_task.task['scopes'] == ['index:insert-task:gecko.v2.mozilla-central.*']
 
 
-class TestApplyJSONeTemplates(MorphTestCase):
+TASKS = [
+    {
+        'kind': 'build',
+        'label': 'a',
+        'attributes': {},
+        'task': {
+            'extra': {
+                'treeherder': {
+                    'group': 'tc',
+                    'symbol': 'B'
+                }
+            },
+            'payload': {
+                'env': {
+                    'FOO': 'BAR'
+                }
+            },
+            'tags': {
+                'kind': 'build'
+            }
+        }
+    },
+    {
+        'kind': 'test',
+        'label': 'b',
+        'attributes': {},
+        'task': {
+            'extra': {
+                'treeherder': {
+                    'group': 'tc',
+                    'symbol': 't'
+                }
+            },
+            'payload': {
+                'env': {
+                    'FOO': 'BAR'
+                }
+            },
+            'tags': {
+                'kind': 'test'
+            }
+        }
+    },
+]
 
-    tasks = [
-        {
-            'kind': 'build',
-            'label': 'a',
-            'attributes': {},
-            'task': {
-                'extra': {
-                    'treeherder': {
-                        'group': 'tc',
-                        'symbol': 'B'
-                    }
-                },
-                'payload': {
-                    'env': {
-                        'FOO': 'BAR'
-                    }
-                },
-                'tags': {
-                    'kind': 'build'
-                }
+
+@pytest.fixture
+def taskgraph(make_taskgraph):
+    return make_taskgraph({
+        t['label']: Task(**t) for t in TASKS[:]
+    })
+
+
+@pytest.fixture
+def get_morphed(taskgraph):
+    def inner(try_task_config):
+        fn = morph.apply_jsone_templates(try_task_config)
+        return fn(*taskgraph)[0]
+    return inner
+
+
+def test_template_artifact(get_morphed):
+    morphed = get_morphed({
+        'templates': {
+            'artifact': {'enabled': 1}
+        },
+    })
+
+    assert len(morphed.tasks) == 2
+
+    for t in morphed.tasks.values():
+        if t.kind == 'build':
+            assert t.task['extra']['treeherder']['group'] == 'tc'
+            assert t.task['extra']['treeherder']['symbol'] == 'Ba'
+            assert t.task['payload']['env']['USE_ARTIFACT'] == 1
+        else:
+            assert t.task['extra']['treeherder']['group'] == 'tc'
+            assert t.task['extra']['treeherder']['symbol'] == 't'
+            assert 'USE_ARTIFACT' not in t.task['payload']['env']
+
+
+def test_template_env(get_morphed):
+    morphed = get_morphed({
+        'templates': {
+            'env': {
+                'ENABLED': 1,
+                'FOO': 'BAZ',
             }
         },
-        {
-            'kind': 'test',
-            'label': 'b',
-            'attributes': {},
-            'task': {
-                'extra': {
-                    'treeherder': {
-                        'group': 'tc',
-                        'symbol': 't'
-                    }
-                },
-                'payload': {
-                    'env': {
-                        'FOO': 'BAR'
-                    }
-                },
-                'tags': {
-                    'kind': 'test'
-                }
+    })
+
+    assert len(morphed.tasks) == 2
+    for t in morphed.tasks.values():
+        assert len(t.task['payload']['env']) == 2
+        assert t.task['payload']['env']['ENABLED'] == 1
+        assert t.task['payload']['env']['FOO'] == 'BAZ'
+
+    morphed = get_morphed({
+        'templates': {
+            'env': {
+                'ENABLED': 0,
+                'FOO': 'BAZ',
             }
         },
-    ]
-
-    def test_template_artifact(self):
-        tg, label_to_taskid = self.make_taskgraph({
-            t['label']: Task(**t) for t in self.tasks[:]
-        })
-
-        try_task_config = {
-            'templates': {
-                'artifact': {'enabled': 1}
-            },
-        }
-        fn = morph.apply_jsone_templates(try_task_config)
-        morphed = fn(tg, label_to_taskid)[0]
+    })
 
-        self.assertEqual(len(morphed.tasks), 2)
+    assert len(morphed.tasks) == 2
+    for t in morphed.tasks.values():
+        assert len(t.task['payload']['env']) == 2
+        assert t.task['payload']['env']['ENABLED'] == 0
+        assert t.task['payload']['env']['FOO'] == 'BAZ'
 
-        for t in morphed.tasks.values():
-            if t.kind == 'build':
-                self.assertEqual(t.task['extra']['treeherder']['group'], 'tc')
-                self.assertEqual(t.task['extra']['treeherder']['symbol'], 'Ba')
-                self.assertEqual(t.task['payload']['env']['USE_ARTIFACT'], 1)
-            else:
-                self.assertEqual(t.task['extra']['treeherder']['group'], 'tc')
-                self.assertEqual(t.task['extra']['treeherder']['symbol'], 't')
-                self.assertNotIn('USE_ARTIFACT', t.task['payload']['env'])
-
-    def test_template_env(self):
-        tg, label_to_taskid = self.make_taskgraph({
-            t['label']: Task(**t) for t in self.tasks[:]
-        })
 
-        try_task_config = {
-            'templates': {
-                'env': {
-                    'ENABLED': 1,
-                    'FOO': 'BAZ',
-                }
-            },
-        }
-        fn = morph.apply_jsone_templates(try_task_config)
-        morphed = fn(tg, label_to_taskid)[0]
-
-        self.assertEqual(len(morphed.tasks), 2)
-        for t in morphed.tasks.values():
-            self.assertEqual(len(t.task['payload']['env']), 2)
-            self.assertEqual(t.task['payload']['env']['ENABLED'], 1)
-            self.assertEqual(t.task['payload']['env']['FOO'], 'BAZ')
-
-        try_task_config['templates']['env'] = {
-            'ENABLED': 0,
-        }
-        fn = morph.apply_jsone_templates(try_task_config)
-        morphed = fn(tg, label_to_taskid)[0]
+def test_template_rebuild(get_morphed):
+    morphed = get_morphed({
+        'tasks': ['b'],
+        'templates': {
+            'rebuild': 4,
+        },
+    })
+    tasks = morphed.tasks.values()
+    assert len(tasks) == 2
 
-        self.assertEqual(len(morphed.tasks), 2)
-        for t in morphed.tasks.values():
-            self.assertEqual(len(t.task['payload']['env']), 2)
-            self.assertEqual(t.task['payload']['env']['ENABLED'], 0)
-            self.assertEqual(t.task['payload']['env']['FOO'], 'BAZ')
-
-    def test_template_rebuild(self):
-        tg, label_to_taskid = self.make_taskgraph({
-            t['label']: Task(**t) for t in self.tasks[:]
-        })
-
-        try_task_config = {
-            'tasks': ['b'],
-            'templates': {
-                'rebuild': 4,
-            },
-        }
-        fn = morph.apply_jsone_templates(try_task_config)
-        tasks = fn(tg, label_to_taskid)[0].tasks.values()
-        self.assertEqual(len(tasks), 2)
-
-        for t in tasks:
-            if t.label == 'a':
-                self.assertNotIn('task_duplicates', t.attributes)
-            elif t.label == 'b':
-                self.assertIn('task_duplicates', t.attributes)
-                self.assertEqual(t.attributes['task_duplicates'], 4)
+    for t in tasks:
+        if t.label == 'a':
+            assert 'task_duplicates' not in t.attributes
+        elif t.label == 'b':
+            assert 'task_duplicates' in t.attributes
+            assert t.attributes['task_duplicates'] == 4
 
 
 if __name__ == '__main__':
     main()