aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/bugs_test.py
blob: 5a07dbd889e88dfe82d5c346854b5b9d37ebecab (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
#!/usr/bin/env python3
# Copyright 2021 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# We're testing protected methods, so allow protected access.
# pylint: disable=protected-access

"""Tests bug filing bits."""

import json
import tempfile
import unittest
from unittest.mock import patch

import bugs


class Tests(unittest.TestCase):
    """Tests for the bugs module."""

    def testWritingJSONFileSeemsToWork(self):
        """Tests JSON file writing."""
        old_x20_path = bugs.X20_PATH

        def restore_x20_path():
            bugs.X20_PATH = old_x20_path

        self.addCleanup(restore_x20_path)

        with tempfile.TemporaryDirectory() as tempdir:
            bugs.X20_PATH = tempdir
            file_path = bugs._WriteBugJSONFile(
                "ObjectType",
                {
                    "foo": "bar",
                    "baz": bugs.WellKnownComponents.CrOSToolchainPublic,
                },
            )

            self.assertTrue(
                file_path.startswith(tempdir),
                f"Expected {file_path} to start with {tempdir}",
            )

            with open(file_path) as f:
                self.assertEqual(
                    json.load(f),
                    {
                        "type": "ObjectType",
                        "value": {
                            "foo": "bar",
                            "baz": int(
                                bugs.WellKnownComponents.CrOSToolchainPublic
                            ),
                        },
                    },
                )

    @patch("bugs._WriteBugJSONFile")
    def testAppendingToBugsSeemsToWork(self, mock_write_json_file):
        """Tests AppendToExistingBug."""
        bugs.AppendToExistingBug(1234, "hello, world!")
        mock_write_json_file.assert_called_once_with(
            "AppendToExistingBugRequest",
            {
                "body": "hello, world!",
                "bug_id": 1234,
            },
        )

    @patch("bugs._WriteBugJSONFile")
    def testBugCreationSeemsToWork(self, mock_write_json_file):
        """Tests CreateNewBug."""
        test_case_additions = (
            {},
            {
                "component_id": bugs.WellKnownComponents.CrOSToolchainPublic,
            },
            {
                "assignee": "foo@gbiv.com",
                "cc": ["bar@baz.com"],
            },
        )

        for additions in test_case_additions:
            test_case = {
                "component_id": 123,
                "title": "foo",
                "body": "bar",
                **additions,
            }

            bugs.CreateNewBug(**test_case)

            expected_output = {
                "component_id": test_case["component_id"],
                "subject": test_case["title"],
                "body": test_case["body"],
            }

            assignee = test_case.get("assignee")
            if assignee:
                expected_output["assignee"] = assignee

            cc = test_case.get("cc")
            if cc:
                expected_output["cc"] = cc

            mock_write_json_file.assert_called_once_with(
                "FileNewBugRequest",
                expected_output,
            )
            mock_write_json_file.reset_mock()

    @patch("bugs._WriteBugJSONFile")
    def testCronjobLogSendingSeemsToWork(self, mock_write_json_file):
        """Tests SendCronjobLog."""
        bugs.SendCronjobLog("my_name", False, "hello, world!")
        mock_write_json_file.assert_called_once_with(
            "ChrotomationCronjobUpdate",
            {
                "name": "my_name",
                "message": "hello, world!",
                "failed": False,
            },
        )


if __name__ == "__main__":
    unittest.main()