test_morph.py (7141B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 6 import pytest 7 from mozunit import main 8 from taskgraph.graph import Graph 9 from taskgraph.parameters import Parameters 10 from taskgraph.task import Task 11 from taskgraph.taskgraph import TaskGraph 12 13 from gecko_taskgraph import morph 14 15 16 @pytest.fixture 17 def make_taskgraph(): 18 def inner(tasks): 19 label_to_taskid = {k: k + "-tid" for k in tasks} 20 for label, task_id in label_to_taskid.items(): 21 tasks[label].task_id = task_id 22 graph = Graph(nodes=set(tasks), edges=set()) 23 taskgraph = TaskGraph(tasks, graph) 24 return taskgraph, label_to_taskid 25 26 return inner 27 28 29 @pytest.mark.parametrize( 30 "params,expected", 31 ( 32 pytest.param( 33 { 34 "try_mode": "try_task_config", 35 "try_task_config": { 36 "rebuild": 10, 37 "tasks": ["b"], 38 }, 39 "project": "try", 40 }, 41 {"b": 10}, 42 id="duplicates no chunks", 43 ), 44 pytest.param( 45 { 46 "try_mode": "try_task_config", 47 "try_task_config": { 48 "rebuild": 10, 49 "tasks": ["a-*"], 50 }, 51 "project": "try", 52 }, 53 {"a-1": 10, "a-2": 10}, 54 id="duplicates with chunks", 55 ), 56 pytest.param( 57 { 58 "try_mode": "try_task_config", 59 "try_task_config": { 60 "rebuild": 10, 61 "tasks": ["a-*", "b"], 62 }, 63 "project": "try", 64 }, 65 {"a-1": 10, "a-2": 10, "b": 10}, 66 id="duplicates with and without chunks", 67 ), 68 pytest.param( 69 { 70 "try_mode": "try_task_config", 71 "try_task_config": { 72 "tasks": ["a-*"], 73 }, 74 "project": "try", 75 }, 76 {"a-1": -1, "a-2": -1}, 77 id="no rebuild, no duplicates", 78 ), 79 pytest.param( 80 { 81 "try_mode": "try_task_config", 82 "try_task_config": { 83 "rebuild": 0, 84 "tasks": ["a-*"], 85 }, 86 "project": "try", 87 }, 88 {"a-1": -1, "a-2": -1}, 89 id="rebuild of zero", 90 ), 91 pytest.param( 92 { 93 "try_mode": "try_task_config", 94 "try_task_config": { 95 "rebuild": 100, 96 "tasks": ["a-*"], 97 }, 98 "project": "try", 99 }, 100 {"a-1": 100, "a-2": 100}, 101 id="rebuild 100", 102 ), 103 ), 104 ) 105 def test_try_task_duplicates(make_taskgraph, graph_config, params, expected): 106 taskb = Task(kind="test", label="b", attributes={}, task={}) 107 task1 = Task(kind="test", label="a-1", attributes={}, task={}) 108 task2 = Task(kind="test", label="a-2", attributes={}, task={}) 109 taskgraph, label_to_taskid = make_taskgraph({ 110 taskb.label: taskb, 111 task1.label: task1, 112 task2.label: task2, 113 }) 114 115 taskgraph, label_to_taskid = morph._add_try_task_duplicates( 116 taskgraph, label_to_taskid, params, graph_config 117 ) 118 for label in expected: 119 task = taskgraph.tasks[label] 120 assert task.attributes.get("task_duplicates", -1) == expected[label] 121 122 123 def test_make_index_tasks(make_taskgraph, graph_config): 124 task_def = { 125 "routes": [ 126 "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.es-MX", 127 "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.fy-NL", 128 "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.sk", 129 "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.sl", 130 "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.uk", 131 "index.gecko.v2.mozilla-central.latest.firefox-l10n.linux64-opt.zh-CN", 132 "index.gecko.v2.mozilla-central.pushdate." 133 "2017.04.04.20170404100210.firefox-l10n.linux64-opt.es-MX", 134 "index.gecko.v2.mozilla-central.pushdate." 135 "2017.04.04.20170404100210.firefox-l10n.linux64-opt.fy-NL", 136 "index.gecko.v2.mozilla-central.pushdate." 137 "2017.04.04.20170404100210.firefox-l10n.linux64-opt.sk", 138 "index.gecko.v2.mozilla-central.pushdate." 139 "2017.04.04.20170404100210.firefox-l10n.linux64-opt.sl", 140 "index.gecko.v2.mozilla-central.pushdate." 141 "2017.04.04.20170404100210.firefox-l10n.linux64-opt.uk", 142 "index.gecko.v2.mozilla-central.pushdate." 143 "2017.04.04.20170404100210.firefox-l10n.linux64-opt.zh-CN", 144 "index.gecko.v2.mozilla-central.revision." 145 "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.es-MX", 146 "index.gecko.v2.mozilla-central.revision." 147 "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.fy-NL", 148 "index.gecko.v2.mozilla-central.revision." 149 "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.sk", 150 "index.gecko.v2.mozilla-central.revision." 151 "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.sl", 152 "index.gecko.v2.mozilla-central.revision." 153 "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.uk", 154 "index.gecko.v2.mozilla-central.revision." 155 "b5d8b27a753725c1de41ffae2e338798f3b5cacd.firefox-l10n.linux64-opt.zh-CN", 156 ], 157 "deadline": "soon", 158 "metadata": { 159 "description": "desc", 160 "owner": "owner@foo.com", 161 "source": "https://source", 162 }, 163 "extra": { 164 "index": {"rank": 1540722354}, 165 }, 166 } 167 task = Task(kind="test", label="a", attributes={}, task=task_def) 168 docker_task = Task( 169 kind="docker-image", label="docker-image-index-task", attributes={}, task={} 170 ) 171 taskgraph, label_to_taskid = make_taskgraph({ 172 task.label: task, 173 docker_task.label: docker_task, 174 }) 175 176 index_paths = [ 177 r.split(".", 1)[1] for r in task_def["routes"] if r.startswith("index.") 178 ] 179 index_task = morph.make_index_task( 180 task, 181 taskgraph, 182 label_to_taskid, 183 Parameters(strict=False), 184 graph_config, 185 index_paths=index_paths, 186 index_rank=1540722354, 187 purpose="index-task", 188 dependencies={}, 189 ) 190 191 assert index_task.task["payload"]["command"][0] == "insert-indexes.js" 192 assert index_task.task["payload"]["env"]["TARGET_TASKID"] == "a-tid" 193 assert index_task.task["payload"]["env"]["INDEX_RANK"] == "1540722354" 194 195 # check the scope summary 196 assert index_task.task["scopes"] == ["index:insert-task:gecko.v2.mozilla-central.*"] 197 198 199 if __name__ == "__main__": 200 main()