|
| 1 | +# Copyright 2015-2016 Yelp Inc. |
| 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 | +from datetime import datetime |
| 15 | +from unittest.mock import call |
| 16 | +from unittest.mock import MagicMock |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources import ( |
| 20 | + clean_namespace, |
| 21 | +) |
| 22 | +from paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources import main |
| 23 | + |
| 24 | + |
| 25 | +def _create_mock_kube_resource(name: str, creation_time: datetime): |
| 26 | + mock_resource = MagicMock() |
| 27 | + mock_resource.metadata.name = name |
| 28 | + mock_resource.metadata.creation_timestamp = creation_time |
| 29 | + return mock_resource |
| 30 | + |
| 31 | + |
| 32 | +@patch( |
| 33 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.get_remote_run_role_bindings", |
| 34 | + autospec=True, |
| 35 | +) |
| 36 | +@patch( |
| 37 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.get_remote_run_roles", |
| 38 | + autospec=True, |
| 39 | +) |
| 40 | +@patch( |
| 41 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.get_remote_run_service_accounts", |
| 42 | + autospec=True, |
| 43 | +) |
| 44 | +def test_clean_namespace(mock_get_sa, mock_get_roles, mock_get_bindings): |
| 45 | + mock_client = MagicMock() |
| 46 | + mock_get_sa.return_value = [ |
| 47 | + _create_mock_kube_resource("foobar", datetime(2025, 1, 1, 0, 0, 0)), |
| 48 | + _create_mock_kube_resource("remote-run-abc", datetime(2025, 1, 1, 0, 0, 0)), |
| 49 | + ] |
| 50 | + mock_get_roles.return_value = [ |
| 51 | + _create_mock_kube_resource("remote-run-abc", datetime(2025, 1, 1, 2, 0, 0)), |
| 52 | + _create_mock_kube_resource("remote-run-def", datetime(2025, 1, 1, 0, 0, 0)), |
| 53 | + ] |
| 54 | + mock_get_bindings.return_value = [ |
| 55 | + _create_mock_kube_resource("whatever", datetime(2025, 1, 1, 0, 0, 0)), |
| 56 | + ] |
| 57 | + clean_namespace(mock_client, "abc", datetime(2025, 1, 1, 1, 1, 1)) |
| 58 | + mock_client.core.delete_namespaced_service_account.assert_has_calls( |
| 59 | + [call("remote-run-abc", "abc")] |
| 60 | + ) |
| 61 | + mock_client.rbac.delete_namespaced_role.assert_has_calls( |
| 62 | + [call("remote-run-def", "abc")] |
| 63 | + ) |
| 64 | + mock_client.rbac.delete_namespaced_role_binding.assert_not_called() |
| 65 | + |
| 66 | + |
| 67 | +@patch( |
| 68 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.get_all_managed_namespaces", |
| 69 | + autospec=True, |
| 70 | +) |
| 71 | +@patch( |
| 72 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.clean_namespace", |
| 73 | + autospec=True, |
| 74 | +) |
| 75 | +@patch( |
| 76 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.KubeClient", |
| 77 | + autospec=True, |
| 78 | +) |
| 79 | +@patch( |
| 80 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.parse_args", |
| 81 | + autospec=True, |
| 82 | +) |
| 83 | +@patch( |
| 84 | + "paasta_tools.kubernetes.bin.paasta_cleanup_remote_run_resources.datetime", |
| 85 | + autospec=True, |
| 86 | +) |
| 87 | +def test_main( |
| 88 | + mock_datetime, mock_parse_args, mock_kube, mock_clean, mock_get_namespaces |
| 89 | +): |
| 90 | + mock_parse_args.return_value.max_age = 60 |
| 91 | + mock_datetime.now.return_value = datetime(2025, 1, 1, 0, 1, 0) |
| 92 | + mock_get_namespaces.return_value = ["a", "b", "c"] |
| 93 | + main() |
| 94 | + mock_clean.assert_has_calls( |
| 95 | + [ |
| 96 | + call(mock_kube.return_value, "a", datetime(2025, 1, 1, 0, 0, 0)), |
| 97 | + call(mock_kube.return_value, "b", datetime(2025, 1, 1, 0, 0, 0)), |
| 98 | + call(mock_kube.return_value, "c", datetime(2025, 1, 1, 0, 0, 0)), |
| 99 | + ] |
| 100 | + ) |
0 commit comments