Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import json |
| 17 | import os |
| 18 | import sys |
| 19 | import unittest |
| 20 | from mock import patch, PropertyMock, ANY, MagicMock |
| 21 | from unit_test_common import setup_sync_unit_test |
| 22 | |
| 23 | class ApiException(Exception): |
| 24 | def __init__(self, status, *args, **kwargs): |
| 25 | super(ApiException, self).__init__(*args, **kwargs) |
| 26 | self.status = status |
| 27 | |
| 28 | def fake_init_kubernetes_client(self): |
| 29 | self.kubernetes_client = MagicMock() |
| 30 | self.v1core = MagicMock() |
| 31 | self.ApiException = ApiException |
| 32 | |
| 33 | class TestSyncPrincipal(unittest.TestCase): |
| 34 | |
| 35 | def setUp(self): |
| 36 | self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), |
| 37 | globals(), |
Scott Baker | a30fae7 | 2019-02-01 16:14:43 -0800 | [diff] [blame^] | 38 | [("kubernetes-service", "kubernetes.xproto")] ) |
| 39 | |
| 40 | self.model_accessor = self.unittest_setup["model_accessor"] |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 41 | |
| 42 | sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps")) |
| 43 | |
| 44 | from sync_principal import SyncPrincipal |
| 45 | self.step_class = SyncPrincipal |
| 46 | |
| 47 | self.service = KubernetesService() |
| 48 | self.trust_domain = TrustDomain(owner=self.service, name="test-trust") |
| 49 | |
| 50 | def tearDown(self): |
| 51 | sys.path = self.unittest_setup["sys_path_save"] |
| 52 | |
| 53 | def test_get_service_account_exists(self): |
| 54 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 55 | xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain) |
| 56 | |
Scott Baker | a30fae7 | 2019-02-01 16:14:43 -0800 | [diff] [blame^] | 57 | step = self.step_class(model_accessor = self.model_accessor) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 58 | sa = MagicMock() |
| 59 | step.v1core.read_namespaced_service_account.return_value = sa |
| 60 | |
| 61 | result = step.get_service_account(xos_principal) |
| 62 | |
| 63 | self.assertEqual(result, sa) |
| 64 | step.v1core.read_namespaced_service_account.assert_called_with("test-principal", "test-trust") |
| 65 | |
| 66 | def test_get_service_account_noexist(self): |
| 67 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 68 | xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain) |
| 69 | |
Scott Baker | a30fae7 | 2019-02-01 16:14:43 -0800 | [diff] [blame^] | 70 | step = self.step_class(model_accessor = self.model_accessor) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 71 | step.v1core.read_namespaced_service_account.side_effect = step.ApiException(status=404) |
| 72 | |
| 73 | result = step.get_service_account(xos_principal) |
| 74 | |
| 75 | self.assertEqual(result, None) |
| 76 | |
| 77 | def test_sync_record_create(self): |
| 78 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 79 | xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain) |
| 80 | |
Scott Baker | a30fae7 | 2019-02-01 16:14:43 -0800 | [diff] [blame^] | 81 | step = self.step_class(model_accessor = self.model_accessor) |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 82 | step.v1core.read_namespaced_service_account.side_effect = step.ApiException(status=404) |
| 83 | |
| 84 | sa = MagicMock() |
| 85 | sa.metadata.self_link="1234" |
| 86 | step.v1core.create_namespaced_service_account.return_value = sa |
| 87 | |
| 88 | step.sync_record(xos_principal) |
| 89 | |
| 90 | step.v1core.create_namespaced_service_account.assert_called() |
| 91 | self.assertEqual(xos_principal.backend_handle, "1234") |
| 92 | |
Scott Baker | 393d015 | 2018-05-21 09:17:49 -0700 | [diff] [blame] | 93 | def test_delete_record(self): |
| 94 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 95 | xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain) |
| 96 | |
Scott Baker | a30fae7 | 2019-02-01 16:14:43 -0800 | [diff] [blame^] | 97 | step = self.step_class(model_accessor = self.model_accessor) |
Scott Baker | 393d015 | 2018-05-21 09:17:49 -0700 | [diff] [blame] | 98 | k8s_sa = MagicMock() |
| 99 | step.v1core.read_namespaced_service_account.return_value = k8s_sa |
| 100 | |
| 101 | step.delete_record(xos_principal) |
| 102 | |
| 103 | step.v1core.delete_namespaced_service_account.assert_called_with("test-principal", self.trust_domain.name, ANY) |
| 104 | |
Scott Baker | 13e953c | 2018-05-17 09:19:15 -0700 | [diff] [blame] | 105 | if __name__ == '__main__': |
| 106 | unittest.main() |