summaryrefslogtreecommitdiffstats
path: root/comm/taskcluster/comm_taskgraph/test/test_optimization_strategies.py
blob: 8e4376071429acfba6cf2626b54abb858bf690c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Any copyright is dedicated to the public domain.
# http://creativecommons.org/publicdomain/zero/1.0/

import hashlib
from datetime import datetime
from time import mktime

import conftest  # noqa: F401
import pytest
import responses
from mozunit import main
from taskgraph.task import Task

from gecko_taskgraph.optimize import registry

from comm_taskgraph.optimize import SkipSuiteOnly


def generate_task():
    task = {}
    task.setdefault("label", "task-label")
    task.setdefault("kind", "build")
    task.setdefault("task", {})
    task.setdefault("attributes", {})

    for attr in (
        "dependencies",
        "optimization",
        "soft_dependencies",
        "release_artifacts",
    ):
        task.setdefault(attr, None)

    task["task"].setdefault("label", task["label"])
    return Task.from_json(task)


def idfn(param):
    if isinstance(param, tuple):
        return param[0].__name__
    return None


def generate_json_push_data(files_changed):
    return {"changesets": [{"desc": "commit comment", "files": files_changed, "node": "cdefgh"}]}


@pytest.fixture
def params():
    return {
        "branch": "comm-central",
        "head_repository": "https://hg.mozilla.org/mozilla-central",
        "head_rev": "zyxwvu",
        "comm_head_repository": "https://hg.mozilla.org/comm-central",
        "comm_head_rev": "abcdef",
        "comm_src_path": "comm/",
        "project": "comm-central",
        "pushlog_id": 1,
        "pushdate": mktime(datetime.now().timetuple()),
    }


def mk_rev(strings):
    data = "".join(strings).encode("utf-8")
    h = hashlib.new("sha1")
    h.update(data)
    return h.hexdigest()


@responses.activate
@pytest.mark.parametrize(
    "pushed_files,expected",
    [
        # suite-only push
        pytest.param(["suite/a/b/c.txt", "suite/b/c/d.txt"], True),
        # non-suite push
        pytest.param(["mail/a/b/c.txt", "mailnews/b/c/d.txt"], False),
        # mixed push
        pytest.param(["suite/a/b/c.txt", "calendar/b/c/d.txt"], False),
    ],
    ids=idfn,
)
def test_suite_only_strategy(params, pushed_files, expected):
    rev = mk_rev(pushed_files)
    params["comm_head_rev"] = rev

    responses.add(
        responses.GET,
        "https://hg.mozilla.org/comm-central/json-automationrelevance/{}".format(rev),
        json=generate_json_push_data(pushed_files),
        status=200,
    )
    task = generate_task()

    opt = SkipSuiteOnly()
    remove = opt.should_remove_task(task, params, None)

    assert remove == expected


@responses.activate
@pytest.mark.parametrize(
    "file_patterns,pushed_files,expected",
    [
        # suite-only push, matches files-changed
        pytest.param(["comm/**/*.txt"], ["suite/a/b/c.txt", "suite/b/c/d.js"], True),
        # suite-only push, does not match files-changed
        pytest.param(["comm/**/*.cpp"], ["suite/a/b/c.txt", "suite/b/c/d.js"], True),
        # non-suite push, matches files changed
        pytest.param(["comm/**/*.txt"], ["mail/a/b/c.txt", "mailnews/b/c/d.js"], False),
        # non-suite push, does not match files changed
        pytest.param(["comm/**/*.cpp"], ["mail/a/b/c.txt", "mailnews/b/c/d.js"], True),
    ],
    ids=idfn,
)
def test_suite_files_changed_strategy(params, file_patterns, pushed_files, expected):
    rev = mk_rev(pushed_files)
    params["comm_head_rev"] = rev

    # Fake the m-c json data
    responses.add(
        responses.GET,
        "https://hg.mozilla.org/mozilla-central/json-automationrelevance/zyxwvu",
        json=generate_json_push_data([]),
        status=200,
    )

    responses.add(
        responses.GET,
        "https://hg.mozilla.org/comm-central/json-automationrelevance/{}".format(rev),
        json=generate_json_push_data(pushed_files),
        status=200,
    )
    task = generate_task()

    opt = registry["skip-unless-changed-no-suite"]
    remove = opt.should_remove_task(task, params, file_patterns)

    assert remove == expected


if __name__ == "__main__":
    main()