summaryrefslogtreecommitdiffstats
path: root/taskcluster/gecko_taskgraph/test/test_decision.py
blob: 81be7e0c44287ee7399d6d299aa8eed3597be9a9 (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
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
# 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 unittest.mock import patch

import pytest
from mozunit import MockedOpen, main
from taskgraph.util.yaml import load_yaml

from gecko_taskgraph import decision
from gecko_taskgraph.parameters import register_parameters

FAKE_GRAPH_CONFIG = {"product-dir": "browser", "taskgraph": {}}


@pytest.fixture(scope="module", autouse=True)
def register():
    register_parameters()


class TestDecision(unittest.TestCase):
    def test_write_artifact_json(self):
        data = [{"some": "data"}]
        tmpdir = tempfile.mkdtemp()
        try:
            decision.ARTIFACTS_DIR = os.path.join(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 = "artifacts"

    def test_write_artifact_yml(self):
        data = [{"some": "data"}]
        tmpdir = tempfile.mkdtemp()
        try:
            decision.ARTIFACTS_DIR = os.path.join(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 = "artifacts"


class TestGetDecisionParameters(unittest.TestCase):

    ttc_file = os.path.join(os.getcwd(), "try_task_config.json")

    def setUp(self):
        self.options = {
            "base_repository": "https://hg.mozilla.org/mozilla-unified",
            "head_repository": "https://hg.mozilla.org/mozilla-central",
            "head_rev": "abcd",
            "head_ref": "ef01",
            "head_tag": "",
            "message": "",
            "project": "mozilla-central",
            "pushlog_id": "143",
            "pushdate": 1503691511,
            "owner": "nobody@mozilla.com",
            "repository_type": "hg",
            "tasks_for": "hg-push",
            "level": "3",
        }

    @patch("gecko_taskgraph.decision.get_hg_revision_branch")
    @patch("gecko_taskgraph.decision._determine_more_accurate_base_rev")
    def test_simple_options(
        self, mock_determine_more_accurate_base_rev, mock_get_hg_revision_branch
    ):
        mock_get_hg_revision_branch.return_value = "default"
        mock_determine_more_accurate_base_rev.return_value = "baserev"
        with MockedOpen({self.ttc_file: None}):
            params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
        self.assertEqual(params["pushlog_id"], "143")
        self.assertEqual(params["build_date"], 1503691511)
        self.assertEqual(params["hg_branch"], "default")
        self.assertEqual(params["moz_build_date"], "20170825200511")
        self.assertEqual(params["try_mode"], None)
        self.assertEqual(params["try_options"], None)
        self.assertEqual(params["try_task_config"], {})

    @patch("gecko_taskgraph.decision.get_hg_revision_branch")
    @patch("gecko_taskgraph.decision._determine_more_accurate_base_rev")
    def test_no_email_owner(
        self, mock_determine_more_accurate_base_rev, mock_get_hg_revision_branch
    ):
        mock_get_hg_revision_branch.return_value = "default"
        mock_determine_more_accurate_base_rev.return_value = "baserev"
        self.options["owner"] = "ffxbld"
        with MockedOpen({self.ttc_file: None}):
            params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
        self.assertEqual(params["owner"], "ffxbld@noreply.mozilla.org")

    @patch("gecko_taskgraph.decision.get_hg_revision_branch")
    @patch("gecko_taskgraph.decision.get_hg_commit_message")
    @patch("gecko_taskgraph.decision._determine_more_accurate_base_rev")
    def test_try_options(
        self,
        mock_determine_more_accurate_base_rev,
        mock_get_hg_commit_message,
        mock_get_hg_revision_branch,
    ):
        mock_get_hg_commit_message.return_value = "try: -b do -t all --artifact"
        mock_get_hg_revision_branch.return_value = "default"
        mock_determine_more_accurate_base_rev.return_value = "baserev"
        self.options["project"] = "try"
        with MockedOpen({self.ttc_file: None}):
            params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
        self.assertEqual(params["try_mode"], "try_option_syntax")
        self.assertEqual(params["try_options"]["build_types"], "do")
        self.assertEqual(params["try_options"]["unittests"], "all")
        self.assertEqual(
            params["try_task_config"],
            {
                "gecko-profile": False,
                "use-artifact-builds": True,
                "env": {},
            },
        )

    @patch("gecko_taskgraph.decision.get_hg_revision_branch")
    @patch("gecko_taskgraph.decision.get_hg_commit_message")
    @patch("gecko_taskgraph.decision._determine_more_accurate_base_rev")
    def test_try_task_config(
        self,
        mock_get_hg_commit_message,
        mock_get_hg_revision_branch,
        mock_determine_more_accurate_base_rev,
    ):
        mock_get_hg_commit_message.return_value = "Fuzzy query=foo"
        mock_get_hg_revision_branch.return_value = "default"
        mock_determine_more_accurate_base_rev.return_value = "baserev"
        ttc = {"tasks": ["a", "b"]}
        self.options["project"] = "try"
        with MockedOpen({self.ttc_file: json.dumps(ttc)}):
            params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
            self.assertEqual(params["try_mode"], "try_task_config")
            self.assertEqual(params["try_options"], None)
            self.assertEqual(params["try_task_config"], ttc)

    def test_try_syntax_from_message_empty(self):
        self.assertEqual(decision.try_syntax_from_message(""), "")

    def test_try_syntax_from_message_no_try_syntax(self):
        self.assertEqual(decision.try_syntax_from_message("abc | def"), "")

    def test_try_syntax_from_message_initial_try_syntax(self):
        self.assertEqual(
            decision.try_syntax_from_message("try: -f -o -o"), "try: -f -o -o"
        )

    def test_try_syntax_from_message_initial_try_syntax_multiline(self):
        self.assertEqual(
            decision.try_syntax_from_message("try: -f -o -o\nabc\ndef"), "try: -f -o -o"
        )

    def test_try_syntax_from_message_embedded_try_syntax_multiline(self):
        self.assertEqual(
            decision.try_syntax_from_message("some stuff\ntry: -f -o -o\nabc\ndef"),
            "try: -f -o -o",
        )


if __name__ == "__main__":
    main()