tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_decision.py (5379B)


      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 os
      7 import shutil
      8 import tempfile
      9 from unittest.mock import MagicMock, patch
     10 
     11 import pytest
     12 from mozunit import MockedOpen, main
     13 from taskgraph.util import json
     14 from taskgraph.util.yaml import load_yaml
     15 
     16 from gecko_taskgraph import decision
     17 from gecko_taskgraph.parameters import register_parameters
     18 
     19 FAKE_GRAPH_CONFIG = {"product-dir": "browser", "taskgraph": {}}
     20 TTC_FILE = os.path.join(os.getcwd(), "try_task_config.json")
     21 
     22 
     23 @pytest.fixture(scope="module", autouse=True)
     24 def register():
     25    register_parameters()
     26 
     27 
     28 @pytest.fixture(scope="module")
     29 def options():
     30    return {
     31        "base_repository": "https://hg.mozilla.org/mozilla-unified",
     32        "base_ref": "mybranch",
     33        "base_rev": "1234",
     34        "head_repository": "https://hg.mozilla.org/mozilla-central",
     35        "head_rev": "abcd",
     36        "head_ref": "ef01",
     37        "head_tag": "",
     38        "message": "",
     39        "project": "mozilla-central",
     40        "pushlog_id": "143",
     41        "pushdate": 1503691511,
     42        "owner": "nobody@mozilla.com",
     43        "repository_type": "hg",
     44        "tasks_for": "hg-push",
     45        "level": "3",
     46    }
     47 
     48 
     49 def test_write_artifact_json():
     50    data = [{"some": "data"}]
     51    tmpdir = tempfile.mkdtemp()
     52    try:
     53        decision.ARTIFACTS_DIR = os.path.join(tmpdir, "artifacts")
     54        decision.write_artifact("artifact.json", data)
     55        with open(os.path.join(decision.ARTIFACTS_DIR, "artifact.json")) as f:
     56            assert json.load(f) == data
     57    finally:
     58        if os.path.exists(tmpdir):
     59            shutil.rmtree(tmpdir)
     60        decision.ARTIFACTS_DIR = "artifacts"
     61 
     62 
     63 def test_write_artifact_yml():
     64    data = [{"some": "data"}]
     65    tmpdir = tempfile.mkdtemp()
     66    try:
     67        decision.ARTIFACTS_DIR = os.path.join(tmpdir, "artifacts")
     68        decision.write_artifact("artifact.yml", data)
     69        assert load_yaml(decision.ARTIFACTS_DIR, "artifact.yml") == data
     70    finally:
     71        if os.path.exists(tmpdir):
     72            shutil.rmtree(tmpdir)
     73        decision.ARTIFACTS_DIR = "artifacts"
     74 
     75 
     76 @patch("gecko_taskgraph.decision.get_hg_revision_info")
     77 @patch("gecko_taskgraph.decision.get_hg_revision_branch")
     78 @patch("gecko_taskgraph.decision.get_repository")
     79 @patch("gecko_taskgraph.decision.get_changed_files")
     80 @pytest.mark.parametrize(
     81    "extra_options,commit_msg,ttc,expected",
     82    (
     83        pytest.param(
     84            {},
     85            None,
     86            None,
     87            {
     88                "pushlog_id": "143",
     89                "build_date": 1503691511,
     90                "files_changed": ["bar/baz.md", "foo.txt"],
     91                "hg_branch": "default",
     92                "moz_build_date": "20170825200511",
     93                "try_mode": None,
     94                "try_task_config": {},
     95                "head_git_rev": "bcde",
     96            },
     97            id="simple_options",
     98        ),
     99        pytest.param(
    100            {"owner": "ffxbld"},
    101            None,
    102            None,
    103            {
    104                "owner": "ffxbld@noreply.mozilla.org",
    105            },
    106            id="no_email_owner",
    107        ),
    108        pytest.param(
    109            {"project": "try"},
    110            "try: -b do -t all --artifact",
    111            None,
    112            {
    113                "try_mode": None,
    114                "try_task_config": {},
    115                "head_git_rev": "bcde",
    116            },
    117            id="try_options",
    118        ),
    119        pytest.param(
    120            {
    121                "project": "try",
    122            },
    123            "Fuzzy query=foo",
    124            {"tasks": ["a", "b"]},
    125            {
    126                "try_mode": "try_task_config",
    127                "try_task_config": {"tasks": ["a", "b"]},
    128                "head_git_rev": "bcde",
    129            },
    130            id="try_task_config",
    131        ),
    132    ),
    133 )
    134 def test_get_decision_parameters(
    135    mock_get_changed_files,
    136    mock_get_repository,
    137    mock_get_hg_revision_branch,
    138    mock_get_hg_revision_info,
    139    options,
    140    extra_options,
    141    commit_msg,
    142    ttc,
    143    expected,
    144 ):
    145    mock_get_hg_revision_info.return_value = "bcde"
    146    mock_get_hg_revision_branch.return_value = "default"
    147 
    148    mock_repo = MagicMock()
    149    mock_repo.default_branch = "baseref"
    150    mock_repo.get_commit_message.return_value = commit_msg or "commit message"
    151    mock_get_repository.return_value = mock_repo
    152    mock_get_changed_files.return_value = ["foo.txt", "bar/baz.md"]
    153 
    154    options.update(extra_options)
    155    contents = None
    156    if ttc:
    157        contents = json.dumps(ttc)
    158    with MockedOpen({TTC_FILE: contents}):
    159        params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, options)
    160 
    161    for key in expected:
    162        assert params[key] == expected[key], f"key {key} does not match!"
    163 
    164 
    165 @pytest.mark.parametrize(
    166    "msg, expected",
    167    (
    168        pytest.param("", "", id="empty"),
    169        pytest.param("abc | def", "", id="no_try_syntax"),
    170        pytest.param("try: -f -o -o", "try: -f -o -o", id="initial_try_syntax"),
    171        pytest.param(
    172            "some stuff\ntry: -f -o -o\nabc\ndef",
    173            "try: -f -o -o",
    174            id="embedded_try_syntax_multiline",
    175        ),
    176    ),
    177 )
    178 def test_try_syntax_from_message(msg, expected):
    179    assert decision.try_syntax_from_message(msg) == expected
    180 
    181 
    182 if __name__ == "__main__":
    183    main()