• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2021 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# We're testing protected methods, so allow protected access.
7# pylint: disable=protected-access
8
9"""Tests bug filing bits."""
10
11import json
12import tempfile
13import unittest
14from unittest.mock import patch
15
16import bugs
17
18
19class Tests(unittest.TestCase):
20  """Tests for the bugs module."""
21  def testWritingJSONFileSeemsToWork(self):
22    """Tests JSON file writing."""
23    old_x20_path = bugs.X20_PATH
24
25    def restore_x20_path():
26      bugs.X20_PATH = old_x20_path
27
28    self.addCleanup(restore_x20_path)
29
30    with tempfile.TemporaryDirectory() as tempdir:
31      bugs.X20_PATH = tempdir
32      file_path = bugs._WriteBugJSONFile(
33          'ObjectType', {
34              'foo': 'bar',
35              'baz': bugs.WellKnownComponents.CrOSToolchainPublic,
36          })
37
38      self.assertTrue(file_path.startswith(tempdir),
39                      f'Expected {file_path} to start with {tempdir}')
40
41      with open(file_path) as f:
42        self.assertEqual(
43            json.load(f),
44            {
45                'type': 'ObjectType',
46                'value': {
47                    'foo': 'bar',
48                    'baz': int(bugs.WellKnownComponents.CrOSToolchainPublic),
49                },
50            },
51        )
52
53  @patch('bugs._WriteBugJSONFile')
54  def testAppendingToBugsSeemsToWork(self, mock_write_json_file):
55    """Tests AppendToExistingBug."""
56    bugs.AppendToExistingBug(1234, 'hello, world!')
57    mock_write_json_file.assert_called_once_with(
58        'AppendToExistingBugRequest',
59        {
60            'body': 'hello, world!',
61            'bug_id': 1234,
62        },
63    )
64
65  @patch('bugs._WriteBugJSONFile')
66  def testBugCreationSeemsToWork(self, mock_write_json_file):
67    """Tests CreateNewBug."""
68    test_case_additions = (
69        {},
70        {
71            'component_id': bugs.WellKnownComponents.CrOSToolchainPublic,
72        },
73        {
74            'assignee': 'foo@gbiv.com',
75            'cc': ['bar@baz.com'],
76        },
77    )
78
79    for additions in test_case_additions:
80      test_case = {
81          'component_id': 123,
82          'title': 'foo',
83          'body': 'bar',
84          **additions,
85      }
86
87      bugs.CreateNewBug(**test_case)
88
89      expected_output = {
90          'component_id': test_case['component_id'],
91          'subject': test_case['title'],
92          'body': test_case['body'],
93      }
94
95      assignee = test_case.get('assignee')
96      if assignee:
97        expected_output['assignee'] = assignee
98
99      cc = test_case.get('cc')
100      if cc:
101        expected_output['cc'] = cc
102
103      mock_write_json_file.assert_called_once_with(
104          'FileNewBugRequest',
105          expected_output,
106      )
107      mock_write_json_file.reset_mock()
108
109  @patch('bugs._WriteBugJSONFile')
110  def testCronjobLogSendingSeemsToWork(self, mock_write_json_file):
111    """Tests SendCronjobLog."""
112    bugs.SendCronjobLog('my_name', False, 'hello, world!')
113    mock_write_json_file.assert_called_once_with(
114        'ChrotomationCronjobUpdate',
115        {
116            'name': 'my_name',
117            'message': 'hello, world!',
118            'failed': False,
119        },
120    )
121
122
123if __name__ == '__main__':
124  unittest.main()
125