blob: 3badbf4e3cfd5173ee7db9c60c4158efdf1aa320 [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 TestSyncKubernetesServiceInstance(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")] )
Scott Baker13e953c2018-05-17 09:19:15 -070039
40 self.MockObjectList = self.unittest_setup["MockObjectList"]
Scott Bakera30fae72019-02-01 16:14:43 -080041 self.model_accessor = self.unittest_setup["model_accessor"]
Scott Baker13e953c2018-05-17 09:19:15 -070042
43 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
44
45 from sync_kubernetesserviceinstance import SyncKubernetesServiceInstance
46 self.step_class = SyncKubernetesServiceInstance
47
48 self.service = KubernetesService()
49 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
50 self.principal = Principal(name="test-principal", trust_domain=self.trust_domain)
51 self.slice = Slice(name="test-slice", trust_domain=self.trust_domain, principal=self.principal)
52 self.image = Image(name="test-image", tag="1.2", kind="container")
53
54 def tearDown(self):
55 sys.path = self.unittest_setup["sys_path_save"]
56
57 def test_get_pod_exists(self):
58 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
59 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.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 pod = MagicMock()
63 step.v1core.read_namespaced_pod.return_value = pod
64
65 result = step.get_pod(xos_si)
66
67 self.assertEqual(result, pod)
68 step.v1core.read_namespaced_pod.assert_called_with("test-instance", "test-trust")
69
70 def test_get_pod_noexist(self):
71 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
72 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.slice)
73
Scott Bakera30fae72019-02-01 16:14:43 -080074 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070075 step.v1core.read_namespaced_pod.side_effect = step.ApiException(status=404)
76
77 result = step.get_pod(xos_si)
78
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_si = KubernetesServiceInstance(name="test-instance", slice=self.slice, image=self.image)
84 xos_si.kubernetes_config_volume_mounts = self.MockObjectList([])
85 xos_si.kubernetes_secret_volume_mounts = self.MockObjectList([])
86
Scott Bakera30fae72019-02-01 16:14:43 -080087 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070088 step.v1core.read_namespaced_pod.side_effect = step.ApiException(status=404)
89
90 pod = MagicMock()
91 pod.metadata.self_link="1234"
92 step.v1core.create_namespaced_pod.return_value = pod
93
94 step.sync_record(xos_si)
95
96 step.v1core.create_namespaced_pod.assert_called()
97 self.assertEqual(xos_si.backend_handle, "1234")
98
Scott Baker393d0152018-05-21 09:17:49 -070099 def test_delete_record(self):
100 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
101 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.slice, image=self.image)
102 xos_si.kubernetes_config_volume_mounts = self.MockObjectList([])
103 xos_si.kubernetes_secret_volume_mounts = self.MockObjectList([])
104
Scott Bakera30fae72019-02-01 16:14:43 -0800105 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker393d0152018-05-21 09:17:49 -0700106 pod = MagicMock()
107 step.v1core.read_namespaced_pod.return_value = pod
108
109 step.delete_record(xos_si)
110 step.v1core.delete_namespaced_pod.assert_called_with("test-instance", self.trust_domain.name, ANY)
111
Scott Baker13e953c2018-05-17 09:19:15 -0700112if __name__ == '__main__':
113 unittest.main()