blob: 9b3f64f7ade222fdf9c7d2df86bc2b842951a7c5 [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(),
Scott Bakera30fae72019-02-01 16:14:43 -080038 [("kubernetes-service", "kubernetes.xproto")] )
39
40 self.model_accessor = self.unittest_setup["model_accessor"]
Scott Baker13e953c2018-05-17 09:19:15 -070041
42 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
43
44 from sync_trustdomain import SyncTrustDomain
45 self.step_class = SyncTrustDomain
46
47 self.service = KubernetesService()
48
49 def tearDown(self):
50 sys.path = self.unittest_setup["sys_path_save"]
51
52 def test_get_namespace_exists(self):
53 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
54 xos_trustdomain = TrustDomain(name="test-trust")
55
Scott Bakera30fae72019-02-01 16:14:43 -080056 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070057 td = MagicMock()
58 step.v1core.read_namespace.return_value = td
59
60 result = step.get_namespace(xos_trustdomain)
61
62 self.assertEqual(result, td)
63 step.v1core.read_namespace.assert_called_with("test-trust")
64
65 def test_get_config_namespace_noexist(self):
66 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
67 xos_trustdomain = TrustDomain(name="test-trust")
68
Scott Bakera30fae72019-02-01 16:14:43 -080069 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070070 step.v1core.read_namespace.side_effect = step.ApiException(status=404)
71
72 result = step.get_namespace(xos_trustdomain)
73
74 self.assertEqual(result, None)
75
76 def test_sync_record_create(self):
77 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
78 xos_trustdomain = TrustDomain(name="test-trust")
79
Scott Bakera30fae72019-02-01 16:14:43 -080080 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070081 step.v1core.read_namespace.side_effect = step.ApiException(status=404)
82
83 td = MagicMock()
84 td.metadata.self_link="1234"
85 step.v1core.create_namespace.return_value = td
86
87 step.sync_record(xos_trustdomain)
88
89 step.v1core.create_namespace.assert_called()
90 self.assertEqual(xos_trustdomain.backend_handle, "1234")
91
Scott Baker393d0152018-05-21 09:17:49 -070092 def test_delete_record(self):
93 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
94 xos_trustdomain = TrustDomain(name="test-trust")
95
Scott Bakera30fae72019-02-01 16:14:43 -080096 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker393d0152018-05-21 09:17:49 -070097 td = MagicMock()
98 step.v1core.read_namespace.return_value = td
99
100 step.delete_record(xos_trustdomain)
101
102 step.v1core.delete_namespace.assert_called_with("test-trust", ANY)
103
Scott Baker13e953c2018-05-17 09:19:15 -0700104if __name__ == '__main__':
105 unittest.main()