|
| 1 | +# Copyright 2021 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 | +# |
| 15 | +"""Tests for the "get_gcp_log_flow_filter" module.""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from google.auth.transport import requests |
| 21 | + |
| 22 | +from . import get_gcp_log_flow_filter |
| 23 | + |
| 24 | +DUMMY_FILTER_ID = "00000000-0000-0000-0000-000000000000" |
| 25 | + |
| 26 | + |
| 27 | +class GetGCPLogFlowFilter(unittest.TestCase): |
| 28 | + |
| 29 | + def test_initialize_command_line_args(self): |
| 30 | + actual = get_gcp_log_flow_filter.initialize_command_line_args([ |
| 31 | + "--credentials_file=./foo.json", "--organization_id=123", |
| 32 | + f"--filter_id={DUMMY_FILTER_ID}" |
| 33 | + ]) |
| 34 | + self.assertIsNotNone(actual) |
| 35 | + |
| 36 | + def test_initialize_command_line_args_organization_id_too_big(self): |
| 37 | + invalid_organization_id = 2**64 |
| 38 | + actual = get_gcp_log_flow_filter.initialize_command_line_args([ |
| 39 | + f"--organization_id={invalid_organization_id}", |
| 40 | + f"--filter_id={DUMMY_FILTER_ID}" |
| 41 | + ]) |
| 42 | + self.assertIsNone(actual) |
| 43 | + |
| 44 | + def test_initialize_command_line_args_negative_organization_id(self): |
| 45 | + actual = get_gcp_log_flow_filter.initialize_command_line_args( |
| 46 | + ["--organization_id=-1", f"--filter_id={DUMMY_FILTER_ID}"]) |
| 47 | + self.assertIsNone(actual) |
| 48 | + |
| 49 | + def test_initialize_command_line_args_invalid_filter_id(self): |
| 50 | + actual = get_gcp_log_flow_filter.initialize_command_line_args( |
| 51 | + ["--organization_id=123", "--filter_id=123"]) |
| 52 | + self.assertIsNone(actual) |
| 53 | + |
| 54 | + @mock.patch.object(requests, "AuthorizedSession", autospec=True) |
| 55 | + @mock.patch.object(requests.requests, "Response", autospec=True) |
| 56 | + def test_http_error(self, mock_response, mock_session): |
| 57 | + mock_session.request.return_value = mock_response |
| 58 | + type(mock_response).status_code = mock.PropertyMock(return_value=400) |
| 59 | + mock_response.raise_for_status.side_effect = ( |
| 60 | + requests.requests.exceptions.HTTPError()) |
| 61 | + |
| 62 | + with self.assertRaises(requests.requests.exceptions.HTTPError): |
| 63 | + get_gcp_log_flow_filter.get_gcp_log_flow_filter(mock_session, 123, |
| 64 | + DUMMY_FILTER_ID) |
| 65 | + |
| 66 | + @mock.patch.object(requests, "AuthorizedSession", autospec=True) |
| 67 | + @mock.patch.object(requests.requests, "Response", autospec=True) |
| 68 | + def test_happy_path(self, mock_response, mock_session): |
| 69 | + mock_session.request.return_value = mock_response |
| 70 | + type(mock_response).status_code = mock.PropertyMock(return_value=200) |
| 71 | + |
| 72 | + get_gcp_log_flow_filter.get_gcp_log_flow_filter(mock_session, 123, |
| 73 | + DUMMY_FILTER_ID) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + unittest.main() |
0 commit comments