blob: 4b12b9aebc6b3dfd377825fea4ac3ca152cde3e0 [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 TestSyncTrustDomain(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_trustdomain import SyncTrustDomain
43 self.step_class = SyncTrustDomain
44
45 self.service = KubernetesService()
46
47 def tearDown(self):
48 sys.path = self.unittest_setup["sys_path_save"]
49
50 def test_get_namespace_exists(self):
51 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
52 xos_trustdomain = TrustDomain(name="test-trust")
53
54 step = self.step_class()
55 td = MagicMock()
56 step.v1core.read_namespace.return_value = td
57
58 result = step.get_namespace(xos_trustdomain)
59
60 self.assertEqual(result, td)
61 step.v1core.read_namespace.assert_called_with("test-trust")
62
63 def test_get_config_namespace_noexist(self):
64 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
65 xos_trustdomain = TrustDomain(name="test-trust")
66
67 step = self.step_class()
68 step.v1core.read_namespace.side_effect = step.ApiException(status=404)
69
70 result = step.get_namespace(xos_trustdomain)
71
72 self.assertEqual(result, None)
73
74 def test_sync_record_create(self):
75 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
76 xos_trustdomain = TrustDomain(name="test-trust")
77
78 step = self.step_class()
79 step.v1core.read_namespace.side_effect = step.ApiException(status=404)
80
81 td = MagicMock()
82 td.metadata.self_link="1234"
83 step.v1core.create_namespace.return_value = td
84
85 step.sync_record(xos_trustdomain)
86
87 step.v1core.create_namespace.assert_called()
88 self.assertEqual(xos_trustdomain.backend_handle, "1234")
89
Scott Baker393d0152018-05-21 09:17:49 -070090 def test_delete_record(self):
91 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
92 xos_trustdomain = TrustDomain(name="test-trust")
93
94 step = self.step_class()
95 td = MagicMock()
96 step.v1core.read_namespace.return_value = td
97
98 step.delete_record(xos_trustdomain)
99
100 step.v1core.delete_namespace.assert_called_with("test-trust", ANY)
101
Scott Baker13e953c2018-05-17 09:19:15 -0700102if __name__ == '__main__':
103 unittest.main()