blob: 3030193d7ffb7ba1df6f9e4372fc8543e4f97f95 [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(),
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 self.MockObjectList = self.unittest_setup["MockObjectList"]
43
44 from sync_service import SyncService
45 self.step_class = SyncService
46
47 self.trust_domain = TrustDomain(name="test-trust")
48 self.service = KubernetesService()
49
50 def tearDown(self):
51 sys.path = self.unittest_setup["sys_path_save"]
52
53 def test_get_service_exists(self):
54 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
55 xos_service = Service(name="test-service")
56 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
57 xos_service.slices = self.MockObjectList([xos_slice])
58
59 step = self.step_class()
60 service = MagicMock()
61 step.v1core.read_namespaced_service.return_value = service
62
Scott Baker393d0152018-05-21 09:17:49 -070063 result = step.get_service(xos_service, self.trust_domain.name)
Scott Baker13e953c2018-05-17 09:19:15 -070064
65 self.assertEqual(result, service)
66 step.v1core.read_namespaced_service.assert_called_with("test-service", "test-trust")
67
68 def test_get_service_noexist(self):
69 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
70 xos_service = Service(name="test-service")
71 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
72 xos_service.slices = self.MockObjectList([xos_slice])
73
74 step = self.step_class()
75 step.v1core.read_namespaced_service.side_effect = step.ApiException(status=404)
76
Scott Baker393d0152018-05-21 09:17:49 -070077 result = step.get_service(xos_service, self.trust_domain.name)
Scott Baker13e953c2018-05-17 09:19:15 -070078
79 self.assertEqual(result, None)
80
81 def test_sync_record_create(self):
82 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
83 xos_service = Service(name="test-service")
84 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
85 xos_service.slices = self.MockObjectList([xos_slice])
86
87 xos_serviceport = ServicePort(service=xos_service, name="web", external_port=123, internal_port=345,
88 protocol="TCP")
89 xos_service.serviceports=self.MockObjectList([xos_serviceport])
90
91 step = self.step_class()
92 step.v1core.read_namespaced_service.side_effect = step.ApiException(status=404)
93
94 service = MagicMock()
95 service.metadata.self_link="1234"
96 step.v1core.create_namespaced_service.return_value = service
97
98 step.sync_record(xos_service)
99
100 step.v1core.create_namespaced_service.assert_called()
101 self.assertEqual(xos_service.backend_handle, "1234")
102
Scott Baker393d0152018-05-21 09:17:49 -0700103 def test_delete_record(self):
104 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
105 xos_service = Service(name="test-service")
106 xos_slice = Slice(service=xos_service, trust_domain=self.trust_domain)
107 xos_service.slices = self.MockObjectList([xos_slice])
108
109 xos_serviceport = ServicePort(service=xos_service, name="web", external_port=123, internal_port=345,
110 protocol="TCP")
111 xos_service.serviceports=self.MockObjectList([xos_serviceport])
112
113 step = self.step_class()
114 k8s_service = MagicMock()
115 step.v1core.read_namespaced_service.return_value = k8s_service
116 step.v1core.delete_namespaced_service.return_value = None
117
118 step.delete_record(xos_service)
119
120 step.v1core.delete_namespaced_service.assert_called_with("test-service", self.trust_domain.name, ANY)
121
Scott Baker13e953c2018-05-17 09:19:15 -0700122if __name__ == '__main__':
123 unittest.main()