|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for external_testcase_reader.""" |
| 15 | + |
| 16 | +import unittest |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +from clusterfuzz._internal.cron import external_testcase_reader |
| 20 | + |
| 21 | +BASIC_ATTACHMENT = { |
| 22 | + 'attachmentId': '60127668', |
| 23 | + 'contentType': 'text/html', |
| 24 | + 'length': '458', |
| 25 | + 'filename': 'test.html', |
| 26 | + 'attachmentDataRef': { |
| 27 | + 'resourceName': 'attachment:373893311:60127668' |
| 28 | + }, |
| 29 | + 'etag': 'TXpjek9Ea3pNekV4TFRZd01USTNOalk0TFRjNE9URTROVFl4TlE9PQ==' |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +class ExternalTestcaseReaderTest(unittest.TestCase): |
| 34 | + """external_testcase_reader tests.""" |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + self.issue_tracker = mock.MagicMock() |
| 38 | + self.mock_submit_testcase = mock.MagicMock() |
| 39 | + self.mock_close_invalid_issue = mock.MagicMock() |
| 40 | + |
| 41 | + def test_handle_testcases(self): |
| 42 | + """Test a basic handle_testcases where issue is valid.""" |
| 43 | + mock_iter = mock.MagicMock() |
| 44 | + mock_iter.__iter__.return_value = [mock.MagicMock()] |
| 45 | + self.issue_tracker.find_issues.return_value = mock_iter |
| 46 | + self.mock_close_invalid_issue.return_value = False |
| 47 | + external_testcase_reader.close_invalid_issue = self.mock_close_invalid_issue |
| 48 | + external_testcase_reader.submit_testcase = self.mock_submit_testcase |
| 49 | + |
| 50 | + external_testcase_reader.handle_testcases(self.issue_tracker) |
| 51 | + self.mock_close_invalid_issue.assert_called_once() |
| 52 | + self.issue_tracker.get_attachment.assert_called_once() |
| 53 | + self.mock_submit_testcase.assert_called_once() |
| 54 | + |
| 55 | + def test_handle_testcases_invalid(self): |
| 56 | + """Test a basic handle_testcases where issue is invalid.""" |
| 57 | + mock_iter = mock.MagicMock() |
| 58 | + mock_iter.__iter__.return_value = [mock.MagicMock()] |
| 59 | + self.issue_tracker.find_issues.return_value = mock_iter |
| 60 | + self.mock_close_invalid_issue.return_value = True |
| 61 | + external_testcase_reader.close_invalid_issue = self.mock_close_invalid_issue |
| 62 | + external_testcase_reader.submit_testcase = self.mock_submit_testcase |
| 63 | + |
| 64 | + external_testcase_reader.handle_testcases(self.issue_tracker) |
| 65 | + self.mock_close_invalid_issue.assert_called_once() |
| 66 | + self.issue_tracker.get_attachment.assert_not_called() |
| 67 | + self.mock_submit_testcase.assert_not_called() |
| 68 | + |
| 69 | + def test_handle_testcases_no_issues(self): |
| 70 | + """Test a basic handle_testcases that returns no issues.""" |
| 71 | + self.issue_tracker.find_issues.return_value = None |
| 72 | + |
| 73 | + external_testcase_reader.handle_testcases(self.issue_tracker) |
| 74 | + self.mock_close_invalid_issue.assert_not_called() |
| 75 | + self.issue_tracker.get_attachment.assert_not_called() |
| 76 | + self.mock_submit_testcase.assert_not_called() |
| 77 | + |
| 78 | + def test_close_invalid_issue_basic(self): |
| 79 | + """Test a basic _close_invalid_issue with valid flags.""" |
| 80 | + upload_request = mock.Mock() |
| 81 | + attachment_info = [BASIC_ATTACHMENT] |
| 82 | + description = '--flag-one --flag_two' |
| 83 | + self.assertEqual( |
| 84 | + False, |
| 85 | + external_testcase_reader.close_invalid_issue( |
| 86 | + upload_request, attachment_info, description)) |
| 87 | + |
| 88 | + def test_close_invalid_issue_no_flag(self): |
| 89 | + """Test a basic _close_invalid_issue with no flags.""" |
| 90 | + upload_request = mock.Mock() |
| 91 | + attachment_info = [BASIC_ATTACHMENT] |
| 92 | + description = '' |
| 93 | + self.assertEqual( |
| 94 | + False, |
| 95 | + external_testcase_reader.close_invalid_issue( |
| 96 | + upload_request, attachment_info, description)) |
| 97 | + |
| 98 | + def test_close_invalid_issue_too_many_attachments(self): |
| 99 | + """Test _close_invalid_issue with too many attachments.""" |
| 100 | + upload_request = mock.Mock() |
| 101 | + attachment_info = [BASIC_ATTACHMENT, BASIC_ATTACHMENT] |
| 102 | + description = '' |
| 103 | + self.assertEqual( |
| 104 | + True, |
| 105 | + external_testcase_reader.close_invalid_issue( |
| 106 | + upload_request, attachment_info, description)) |
| 107 | + |
| 108 | + def test_close_invalid_issue_no_attachments(self): |
| 109 | + """Test _close_invalid_issue with no attachments.""" |
| 110 | + upload_request = mock.Mock() |
| 111 | + attachment_info = [] |
| 112 | + description = '' |
| 113 | + self.assertEqual( |
| 114 | + True, |
| 115 | + external_testcase_reader.close_invalid_issue( |
| 116 | + upload_request, attachment_info, description)) |
| 117 | + |
| 118 | + def test_close_invalid_issue_invalid_upload(self): |
| 119 | + """Test _close_invalid_issue with an invalid upload.""" |
| 120 | + upload_request = mock.Mock() |
| 121 | + attachment_info = [{ |
| 122 | + 'attachmentId': '60127668', |
| 123 | + 'contentType': 'application/octet-stream', |
| 124 | + 'length': '458', |
| 125 | + 'filename': 'test.html', |
| 126 | + 'attachmentDataRef': {}, |
| 127 | + 'etag': 'TXpjek9Ea3pNekV4TFRZd01USTNOalk0TFRjNE9URTROVFl4TlE9PQ==' |
| 128 | + }] |
| 129 | + description = '' |
| 130 | + self.assertEqual( |
| 131 | + True, |
| 132 | + external_testcase_reader.close_invalid_issue( |
| 133 | + upload_request, attachment_info, description)) |
| 134 | + |
| 135 | + def test_close_invalid_issue_invalid_content_type(self): |
| 136 | + """Test _close_invalid_issue with an invalid content type.""" |
| 137 | + upload_request = mock.Mock() |
| 138 | + attachment_info = [{ |
| 139 | + 'attachmentId': '60127668', |
| 140 | + 'contentType': 'application/octet-stream', |
| 141 | + 'length': '458', |
| 142 | + 'filename': 'test.html', |
| 143 | + 'attachmentDataRef': { |
| 144 | + 'resourceName': 'attachment:373893311:60127668' |
| 145 | + }, |
| 146 | + 'etag': 'TXpjek9Ea3pNekV4TFRZd01USTNOalk0TFRjNE9URTROVFl4TlE9PQ==' |
| 147 | + }] |
| 148 | + description = '' |
| 149 | + self.assertEqual( |
| 150 | + True, |
| 151 | + external_testcase_reader.close_invalid_issue( |
| 152 | + upload_request, attachment_info, description)) |
0 commit comments