blob: 3a55eb47cc4bd82ef663b4650ffcfa3b7e609962 [file] [log] [blame]
Scott Baker13e953c2018-05-17 09:19:15 -07001
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
16import json
17import os
18import sys
19import unittest
20from mock import patch, PropertyMock, ANY, MagicMock
21from unit_test_common import setup_sync_unit_test
22
23class ApiException(Exception):
24 def __init__(self, status, *args, **kwargs):
25 super(ApiException, self).__init__(*args, **kwargs)
26 self.status = status
27
28def fake_init_kubernetes_client(self):
29 self.kubernetes_client = MagicMock()
30 self.v1core = MagicMock()
31 self.ApiException = ApiException
32
33class 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(),
38 [("kubernetes-service", "kubernetes.proto")] )
39
40 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
41
42 from sync_principal import SyncPrincipal
43 self.step_class = SyncPrincipal
44
45 self.service = KubernetesService()
46 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
47
48 def tearDown(self):
49 sys.path = self.unittest_setup["sys_path_save"]
50
51 def test_get_service_account_exists(self):
52 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
53 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
54
55 step = self.step_class()
56 sa = MagicMock()
57 step.v1core.read_namespaced_service_account.return_value = sa
58
59 result = step.get_service_account(xos_principal)
60
61 self.assertEqual(result, sa)
62 step.v1core.read_namespaced_service_account.assert_called_with("test-principal", "test-trust")
63
64 def test_get_service_account_noexist(self):
65 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
66 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
67
68 step = self.step_class()
69 step.v1core.read_namespaced_service_account.side_effect = step.ApiException(status=404)
70
71 result = step.get_service_account(xos_principal)
72
73 self.assertEqual(result, None)
74
75 def test_sync_record_create(self):
76 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
77 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
78
79 step = self.step_class()
80 step.v1core.read_namespaced_service_account.side_effect = step.ApiException(status=404)
81
82 sa = MagicMock()
83 sa.metadata.self_link="1234"
84 step.v1core.create_namespaced_service_account.return_value = sa
85
86 step.sync_record(xos_principal)
87
88 step.v1core.create_namespaced_service_account.assert_called()
89 self.assertEqual(xos_principal.backend_handle, "1234")
90
Scott Baker393d0152018-05-21 09:17:49 -070091 def test_delete_record(self):
92 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
93 xos_principal = Principal(name="test-principal", trust_domain=self.trust_domain)
94
95 step = self.step_class()
96 k8s_sa = MagicMock()
97 step.v1core.read_namespaced_service_account.return_value = k8s_sa
98
99 step.delete_record(xos_principal)
100
101 step.v1core.delete_namespaced_service_account.assert_called_with("test-principal", self.trust_domain.name, ANY)
102
Scott Baker13e953c2018-05-17 09:19:15 -0700103if __name__ == '__main__':
104 unittest.main()