aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/bugs_test.py
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2021-11-25 14:58:43 -0800
committerCommit Bot <commit-bot@chromium.org>2021-12-07 23:58:46 +0000
commit443e0b362f02045134713114bb34a4afad8090da (patch)
tree3ce81fcfd3eaf9a6dedd7e55def28557a95050bf /cros_utils/bugs_test.py
parent86fe58c1efd0db1d0d95b3cb0d6c34d105b39126 (diff)
downloadtoolchain-utils-443e0b362f02045134713114bb34a4afad8090da.tar.gz
cros_utils: Add a bug reporting module
This lets us conveniently report bugs/cronjob status/etc. BUG=b:202424935, b:202425245 TEST=Unit tests Change-Id: Ic276f6ecc69a4c8c0088143177b17836cee70f97 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3319353 Reviewed-by: Jordan Abrahams <ajordanr@google.com> Commit-Queue: George Burgess <gbiv@chromium.org> Tested-by: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'cros_utils/bugs_test.py')
-rwxr-xr-xcros_utils/bugs_test.py124
1 files changed, 124 insertions, 0 deletions
diff --git a/cros_utils/bugs_test.py b/cros_utils/bugs_test.py
new file mode 100755
index 00000000..03dee64d
--- /dev/null
+++ b/cros_utils/bugs_test.py
@@ -0,0 +1,124 @@
+#!/usr/bin/env python3
+# Copyright 2021 The Chromium OS Authors. All rights reserved.
+# 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()