test_transforms_test.py (9891B)
1 # Any copyright is dedicated to the Public Domain. 2 # https://creativecommons.org/publicdomain/zero/1.0/ 3 """ 4 Tests for the 'tests.py' transforms 5 """ 6 7 import hashlib 8 from functools import partial 9 from pprint import pprint 10 11 import mozunit 12 import pytest 13 from taskgraph.util import json 14 15 from gecko_taskgraph.transforms import test as test_transforms 16 17 18 @pytest.fixture 19 def make_test_task(): 20 """Create a test task definition with required default values.""" 21 22 def inner(**extra): 23 task = { 24 "attributes": {}, 25 "build-platform": "linux64", 26 "mozharness": {"extra-options": []}, 27 "test-platform": "linux64", 28 "treeherder-symbol": "g(t)", 29 "test-name": "task", 30 "try-name": "task", 31 } 32 task.update(extra) 33 return task 34 35 return inner 36 37 38 def test_split_variants(monkeypatch, run_full_config_transform, make_test_task): 39 # mock out variant definitions 40 monkeypatch.setattr( 41 test_transforms.variant, 42 "TEST_VARIANTS", 43 { 44 "foo": { 45 "description": "foo variant", 46 "suffix": "foo", 47 "mozinfo": "foo", 48 "component": "foo bar", 49 "expiration": "never", 50 "merge": { 51 "mozharness": { 52 "extra-options": [ 53 "--setpref=foo=1", 54 ], 55 }, 56 }, 57 }, 58 "bar": { 59 "description": "bar variant", 60 "suffix": "bar", 61 "mozinfo": "bar", 62 "component": "foo bar", 63 "expiration": "never", 64 "when": { 65 "$eval": "task['test-platform'][:5] == 'linux'", 66 }, 67 "merge": { 68 "mozharness": { 69 "extra-options": [ 70 "--setpref=bar=1", 71 ], 72 }, 73 }, 74 "replace": {"tier": 2}, 75 }, 76 }, 77 ) 78 79 def make_expected(variant): 80 """Helper to generate expected tasks.""" 81 return make_test_task(**{ 82 "attributes": {"unittest_variant": variant}, 83 "description": f"{variant} variant", 84 "mozharness": { 85 "extra-options": [f"--setpref={variant}=1"], 86 }, 87 "treeherder-symbol": f"g-{variant}(t)", 88 "variant-suffix": f"-{variant}", 89 }) 90 91 run_split_variants = partial( 92 run_full_config_transform, test_transforms.variant.split_variants 93 ) 94 95 # test no variants 96 input_task = make_test_task(**{ 97 "run-without-variant": True, 98 }) 99 tasks = list(run_split_variants(input_task)) 100 assert len(tasks) == 1 101 102 expected = input_task 103 expected["attributes"]["unittest_variant"] = None 104 assert tasks[0] == expected 105 106 # test variants are split into expected tasks 107 input_task = make_test_task(**{ 108 "run-without-variant": True, 109 "variants": ["foo", "bar"], 110 }) 111 tasks = list(run_split_variants(input_task)) 112 assert len(tasks) == 3 113 114 expected = make_test_task() 115 expected["attributes"]["unittest_variant"] = None 116 assert tasks[0] == expected 117 assert tasks[1] == make_expected("foo") 118 119 expected = make_expected("bar") 120 expected["tier"] = 2 121 assert tasks[2] == expected 122 123 # test composite variants 124 input_task = make_test_task(**{ 125 "run-without-variant": True, 126 "variants": ["foo+bar"], 127 }) 128 tasks = list(run_split_variants(input_task)) 129 assert len(tasks) == 2 130 assert tasks[1]["attributes"]["unittest_variant"] == "foo+bar" 131 assert tasks[1]["mozharness"]["extra-options"] == [ 132 "--setpref=foo=1", 133 "--setpref=bar=1", 134 ] 135 assert tasks[1]["treeherder-symbol"] == "g-foo-bar(t)" 136 137 # test 'when' filter 138 input_task = make_test_task(**{ 139 "run-without-variant": True, 140 # this should cause task to be filtered out of 'bar' and 'foo+bar' variants 141 "test-platform": "windows", 142 "variants": ["foo", "bar", "foo+bar"], 143 }) 144 tasks = list(run_split_variants(input_task)) 145 assert len(tasks) == 2 146 assert tasks[0]["attributes"]["unittest_variant"] is None 147 assert tasks[1]["attributes"]["unittest_variant"] == "foo" 148 149 # test 'run-without-variants=False' 150 input_task = make_test_task(**{ 151 "run-without-variant": False, 152 "variants": ["foo"], 153 }) 154 tasks = list(run_split_variants(input_task)) 155 assert len(tasks) == 1 156 assert tasks[0]["attributes"]["unittest_variant"] == "foo" 157 158 159 @pytest.mark.parametrize( 160 "task,expected", 161 ( 162 pytest.param( 163 { 164 "attributes": {"unittest_variant": "webrender-sw+1proc"}, 165 "test-platform": "linux1804-64-clang-trunk-qr/opt", 166 }, 167 { 168 "platform": { 169 "arch": "64", 170 "os": { 171 "name": "linux", 172 "version": "1804", 173 }, 174 }, 175 "build": { 176 "type": "opt", 177 "clang-trunk": True, 178 }, 179 "runtime": { 180 "1proc": True, 181 "webrender-sw": True, 182 }, 183 }, 184 id="linux", 185 ), 186 pytest.param( 187 { 188 "attributes": {}, 189 "test-platform": "linux2204-64-wayland-shippable/opt", 190 }, 191 { 192 "platform": { 193 "arch": "64", 194 "display": "wayland", 195 "os": { 196 "name": "linux", 197 "version": "2204", 198 }, 199 }, 200 "build": { 201 "type": "opt", 202 "shippable": True, 203 }, 204 "runtime": {}, 205 }, 206 id="linux wayland shippable", 207 ), 208 pytest.param( 209 { 210 "attributes": {}, 211 "test-platform": "android-hw-a51-11-0-arm7-shippable-qr/opt", 212 }, 213 { 214 "platform": { 215 "arch": "arm7", 216 "device": "a51", 217 "os": { 218 "name": "android", 219 "version": "11.0", 220 }, 221 }, 222 "build": { 223 "type": "opt", 224 "shippable": True, 225 }, 226 "runtime": {}, 227 }, 228 id="android", 229 ), 230 pytest.param( 231 { 232 "attributes": {}, 233 "test-platform": "windows11-64-2009-hw-ref-ccov/debug", 234 }, 235 { 236 "platform": { 237 "arch": "64", 238 "machine": "hw-ref", 239 "os": { 240 "build": "2009", 241 "name": "windows", 242 "version": "11", 243 }, 244 }, 245 "build": { 246 "type": "debug", 247 "ccov": True, 248 }, 249 "runtime": {}, 250 }, 251 id="windows", 252 ), 253 ), 254 ) 255 def test_set_test_setting(run_transform, task, expected): 256 # add hash to 'expected' 257 expected["_hash"] = hashlib.sha256( 258 json.dumps(expected, sort_keys=True).encode("utf-8") 259 ).hexdigest()[:12] 260 261 task = list(run_transform(test_transforms.other.set_test_setting, task))[0] 262 assert "test-setting" in task 263 assert task["test-setting"] == expected 264 265 266 def assert_spi_not_disabled(task): 267 extra_options = task["mozharness"]["extra-options"] 268 # The pref to enable this gets set outside of this transform, so only 269 # bother asserting that the pref to disable does not exist. 270 assert ( 271 "--setpref=media.peerconnection.mtransport_process=false" not in extra_options 272 ) 273 assert "--setpref=network.process.enabled=false" not in extra_options 274 275 276 def assert_spi_disabled(task): 277 extra_options = task["mozharness"]["extra-options"] 278 assert "--setpref=media.peerconnection.mtransport_process=false" in extra_options 279 assert "--setpref=media.peerconnection.mtransport_process=true" not in extra_options 280 assert "--setpref=network.process.enabled=false" in extra_options 281 assert "--setpref=network.process.enabled=true" not in extra_options 282 283 284 @pytest.mark.parametrize( 285 "task,callback", 286 ( 287 pytest.param( 288 {"attributes": {"unittest_variant": "socketprocess"}}, 289 assert_spi_not_disabled, 290 id="socketprocess", 291 ), 292 pytest.param( 293 { 294 "attributes": {"unittest_variant": "socketprocess_networking"}, 295 }, 296 assert_spi_not_disabled, 297 id="socketprocess_networking", 298 ), 299 pytest.param({}, assert_spi_disabled, id="no variant"), 300 pytest.param( 301 {"suite": "cppunit", "attributes": {"unittest_variant": "socketprocess"}}, 302 assert_spi_not_disabled, 303 id="excluded suite", 304 ), 305 pytest.param( 306 {"attributes": {"unittest_variant": "no-fission+socketprocess"}}, 307 assert_spi_not_disabled, 308 id="composite variant", 309 ), 310 ), 311 ) 312 def test_ensure_spi_disabled_on_all_but_spi( 313 make_test_task, run_transform, task, callback 314 ): 315 task.setdefault("suite", "mochitest-plain") 316 task = make_test_task(**task) 317 task = list( 318 run_transform(test_transforms.other.ensure_spi_disabled_on_all_but_spi, task) 319 )[0] 320 pprint(task) 321 callback(task) 322 323 324 if __name__ == "__main__": 325 mozunit.main()