blob: 2a208683f27c41acf54e01c53a5707c644d89a02 [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(),
38 [("kubernetes-service", "kubernetes.proto")] )
39
40 self.MockObjectList = self.unittest_setup["MockObjectList"]
41
42 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
43
44 from sync_kubernetesserviceinstance import SyncKubernetesServiceInstance
45 self.step_class = SyncKubernetesServiceInstance
46
47 self.service = KubernetesService()
48 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
49 self.principal = Principal(name="test-principal", trust_domain=self.trust_domain)
50 self.slice = Slice(name="test-slice", trust_domain=self.trust_domain, principal=self.principal)
51 self.image = Image(name="test-image", tag="1.2", kind="container")
52
53 def tearDown(self):
54 sys.path = self.unittest_setup["sys_path_save"]
55
56 def test_get_pod_exists(self):
57 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
58 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.slice)
59
60 step = self.step_class()
61 pod = MagicMock()
62 step.v1core.read_namespaced_pod.return_value = pod
63
64 result = step.get_pod(xos_si)
65
66 self.assertEqual(result, pod)
67 step.v1core.read_namespaced_pod.assert_called_with("test-instance", "test-trust")
68
69 def test_get_pod_noexist(self):
70 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
71 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.slice)
72
73 step = self.step_class()
74 step.v1core.read_namespaced_pod.side_effect = step.ApiException(status=404)
75
76 result = step.get_pod(xos_si)
77
78 self.assertEqual(result, None)
79
80 def test_sync_record_create(self):
81 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
82 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.slice, image=self.image)
83 xos_si.kubernetes_config_volume_mounts = self.MockObjectList([])
84 xos_si.kubernetes_secret_volume_mounts = self.MockObjectList([])
85
86 step = self.step_class()
87 step.v1core.read_namespaced_pod.side_effect = step.ApiException(status=404)
88
89 pod = MagicMock()
90 pod.metadata.self_link="1234"
91 step.v1core.create_namespaced_pod.return_value = pod
92
93 step.sync_record(xos_si)
94
95 step.v1core.create_namespaced_pod.assert_called()
96 self.assertEqual(xos_si.backend_handle, "1234")
97
Scott Baker393d0152018-05-21 09:17:49 -070098 def test_delete_record(self):
99 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
100 xos_si = KubernetesServiceInstance(name="test-instance", slice=self.slice, image=self.image)
101 xos_si.kubernetes_config_volume_mounts = self.MockObjectList([])
102 xos_si.kubernetes_secret_volume_mounts = self.MockObjectList([])
103
104 step = self.step_class()
105 pod = MagicMock()
106 step.v1core.read_namespaced_pod.return_value = pod
107
108 step.delete_record(xos_si)
109 step.v1core.delete_namespaced_pod.assert_called_with("test-instance", self.trust_domain.name, ANY)
110
Scott Baker13e953c2018-05-17 09:19:15 -0700111if __name__ == '__main__':
112 unittest.main()