blob: 45b0bb67dbe795484902779268aa84f0e14746f8 [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 TestSyncService(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 self.MockObjectList = self.unittest_setup["MockObjectList"]
45
46 from sync_service import SyncService
47 self.step_class = SyncService
48
49 self.trust_domain = TrustDomain(name="test-trust")
50 self.service = KubernetesService()
51
52 def tearDown(self):
53 sys.path = self.unittest_setup["sys_path_save"]
54
55 def test_get_service_exists(self):
56 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
57 xos_service = Service(name="test-service")
58 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
59 xos_service.slices = self.MockObjectList([xos_slice])
60
Scott Bakera30fae72019-02-01 16:14:43 -080061 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070062 service = MagicMock()
63 step.v1core.read_namespaced_service.return_value = service
64
Scott Baker393d0152018-05-21 09:17:49 -070065 result = step.get_service(xos_service, self.trust_domain.name)
Scott Baker13e953c2018-05-17 09:19:15 -070066
67 self.assertEqual(result, service)
68 step.v1core.read_namespaced_service.assert_called_with("test-service", "test-trust")
69
70 def test_get_service_noexist(self):
71 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
72 xos_service = Service(name="test-service")
73 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
74 xos_service.slices = self.MockObjectList([xos_slice])
75
Scott Bakera30fae72019-02-01 16:14:43 -080076 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070077 step.v1core.read_namespaced_service.side_effect = step.ApiException(status=404)
78
Scott Baker393d0152018-05-21 09:17:49 -070079 result = step.get_service(xos_service, self.trust_domain.name)
Scott Baker13e953c2018-05-17 09:19:15 -070080
81 self.assertEqual(result, None)
82
83 def test_sync_record_create(self):
84 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
85 xos_service = Service(name="test-service")
86 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
87 xos_service.slices = self.MockObjectList([xos_slice])
88
89 xos_serviceport = ServicePort(service=xos_service, name="web", external_port=123, internal_port=345,
90 protocol="TCP")
91 xos_service.serviceports=self.MockObjectList([xos_serviceport])
92
Scott Bakera30fae72019-02-01 16:14:43 -080093 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070094 step.v1core.read_namespaced_service.side_effect = step.ApiException(status=404)
95
96 service = MagicMock()
97 service.metadata.self_link="1234"
98 step.v1core.create_namespaced_service.return_value = service
99
100 step.sync_record(xos_service)
101
102 step.v1core.create_namespaced_service.assert_called()
103 self.assertEqual(xos_service.backend_handle, "1234")
104
Scott Baker393d0152018-05-21 09:17:49 -0700105 def test_delete_record(self):
106 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
107 xos_service = Service(name="test-service")
108 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
109 xos_service.slices = self.MockObjectList([xos_slice])
110
111 xos_serviceport = ServicePort(service=xos_service, name="web", external_port=123, internal_port=345,
112 protocol="TCP")
113 xos_service.serviceports=self.MockObjectList([xos_serviceport])
114
Scott Bakera30fae72019-02-01 16:14:43 -0800115 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker393d0152018-05-21 09:17:49 -0700116 k8s_service = MagicMock()
117 step.v1core.read_namespaced_service.return_value = k8s_service
118 step.v1core.delete_namespaced_service.return_value = None
119
120 step.delete_record(xos_service)
121
122 step.v1core.delete_namespaced_service.assert_called_with("test-service", self.trust_domain.name, ANY)
123
Scott Baker13e953c2018-05-17 09:19:15 -0700124if __name__ == '__main__':
125 unittest.main()