forked from taskcluster/taskgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decision.py
More file actions
200 lines (175 loc) · 7.25 KB
/
test_decision.py
File metadata and controls
200 lines (175 loc) · 7.25 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 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/.
import json
import os
import shutil
import tempfile
import unittest
from pathlib import Path
import pytest
from taskgraph import decision
from taskgraph.util.vcs import GitRepository, HgRepository
from taskgraph.util.yaml import load_yaml
FAKE_GRAPH_CONFIG = {"product-dir": "browser", "taskgraph": {}}
class TestDecision(unittest.TestCase):
def test_write_artifact_json(self):
data = [{"some": "data"}]
tmpdir = tempfile.mkdtemp()
try:
decision.ARTIFACTS_DIR = Path(tmpdir) / "artifacts"
decision.write_artifact("artifact.json", data)
with open(os.path.join(decision.ARTIFACTS_DIR, "artifact.json")) as f:
self.assertEqual(json.load(f), data)
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
decision.ARTIFACTS_DIR = Path("artifacts")
def test_write_artifact_yml(self):
data = [{"some": "data"}]
tmpdir = tempfile.mkdtemp()
try:
decision.ARTIFACTS_DIR = Path(tmpdir) / "artifacts"
decision.write_artifact("artifact.yml", data)
self.assertEqual(load_yaml(decision.ARTIFACTS_DIR, "artifact.yml"), data)
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
decision.ARTIFACTS_DIR = Path("artifacts")
@unittest.mock.patch.object(
GitRepository,
"get_changed_files",
)
class TestGetDecisionParameters(unittest.TestCase):
def setUp(self):
self.options = {
"base_repository": "https://hg.mozilla.org/mozilla-unified",
"base_rev": "aaaa",
"head_repository": "https://hg.mozilla.org/mozilla-central",
"head_rev": "bbbb",
"head_ref": "default",
"head_tag": "v0.0.1",
"project": "mozilla-central",
"pushlog_id": "143",
"pushdate": 1503691511,
"repository_type": "hg",
"optimize_strategies": None,
"owner": "nobody@mozilla.com",
"tasks_for": "hg-push",
"level": "3",
}
def test_simple_options(self, mock_files_changed):
mock_files_changed.return_value = ["foo.txt"]
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
mock_files_changed.assert_called_once_with(rev="bbbb", base="aaaa")
self.assertEqual(params["build_date"], 1503691511)
self.assertEqual(params["head_tag"], "v0.0.1")
self.assertEqual(params["pushlog_id"], "143")
self.assertEqual(params["moz_build_date"], "20170825200511")
self.assertEqual(params["files_changed"], ["foo.txt"])
def test_no_email_owner(self, mock_files_changed):
mock_files_changed.return_value = ["foo.txt"]
self.options["owner"] = "ffxbld"
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
self.assertEqual(params["owner"], "ffxbld@noreply.mozilla.org")
@unittest.mock.patch.object(
GitRepository,
"get_commit_message",
unittest.mock.MagicMock(return_value="Add Foo"),
)
@unittest.mock.patch.object(
HgRepository,
"get_commit_message",
unittest.mock.MagicMock(return_value="Add Foo"),
)
def test_regular_commit_message_yields_default_target_tasks_method(
self, mock_files_changed
):
"""
Ensures `target_tasks_method` is `default` when the commit message does not contain
`DONTBUILD`.
"""
mock_files_changed.return_value = ["foo.txt"]
self.options["tasks_for"] = "github-push"
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
self.assertEqual(params["target_tasks_method"], "default")
self.options["tasks_for"] = "hg-push"
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
self.assertEqual(params["target_tasks_method"], "default")
@unittest.mock.patch.object(
GitRepository,
"get_commit_message",
unittest.mock.MagicMock(return_value="DONTBUILD"),
)
@unittest.mock.patch.object(
HgRepository,
"get_commit_message",
unittest.mock.MagicMock(return_value="DONTBUILD"),
)
def test_dontbuild_commit_message_yields_default_target_tasks_method(
self, mock_files_changed
):
"""
Ensures `target_tasks_method` is `nothing` when the commit message contains `DONTBUILD`.
"""
mock_files_changed.return_value = ["foo.txt"]
self.options["tasks_for"] = "github-release"
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
self.assertNotEqual(params["target_tasks_method"], "nothing")
self.options["tasks_for"] = "github-push"
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
self.assertEqual(params["target_tasks_method"], "nothing")
self.options["tasks_for"] = "hg-push"
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
self.assertEqual(params["target_tasks_method"], "nothing")
_BASE_OPTIONS = {
"base_repository": "https://hg.mozilla.org/mozilla-unified",
"base_rev": "aaaa",
"head_repository": "https://hg.mozilla.org/mozilla-central",
"head_rev": "bbbb",
"head_ref": "default",
"head_tag": "",
"project": "mozilla-central",
"pushlog_id": "1",
"pushdate": 0,
"repository_type": "git",
"owner": "nobody@mozilla.com",
"tasks_for": "github-push",
"level": "1",
}
@unittest.mock.patch.object(
GitRepository,
"get_note",
return_value=json.dumps({"build_number": 99}),
)
@unittest.mock.patch.object(GitRepository, "get_changed_files", return_value=[])
def test_decision_parameters_note(mock_files_changed, mock_get_note):
options = {**_BASE_OPTIONS, "allow_parameter_override": True}
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, options)
mock_get_note.assert_called_once_with(
"refs/notes/decision-parameters", _BASE_OPTIONS["head_repository"]
)
assert params["build_number"] == 99
@unittest.mock.patch.object(
GitRepository,
"get_note",
return_value=json.dumps({"build_number": 99}),
)
@unittest.mock.patch.object(GitRepository, "get_changed_files", return_value=[])
def test_decision_parameters_note_disallow_override(mock_files_changed, mock_get_note):
options = {**_BASE_OPTIONS, "allow_parameter_override": False}
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, options)
mock_get_note.assert_not_called()
assert params["build_number"] == 1
@unittest.mock.patch.object(
GitRepository,
"get_note",
return_value="not valid json {",
)
@unittest.mock.patch.object(GitRepository, "get_changed_files", return_value=[])
def test_decision_parameters_note_invalid_json(mock_files_changed, mock_get_note):
options = {**_BASE_OPTIONS, "allow_parameter_override": True}
with pytest.raises(
Exception, match="Failed to parse refs/notes/decision-parameters as JSON"
):
decision.get_decision_parameters(FAKE_GRAPH_CONFIG, options)