blob: 12d7ba52e90ed666ddf6307dfc9e42685b8cc52e [file] [log] [blame]
Scott Baker8b20bb12018-05-18 10:51: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
16# Tests for SimpleExampleServiceInstance model policies
17
18import base64
19import json
20import os
21import sys
22import unittest
23from mock import patch, PropertyMock, ANY, MagicMock
24from unit_test_common import setup_sync_unit_test
25
26
27class TestSimpleExampleServiceInstancePolicy(unittest.TestCase):
28
29 def setUp(self):
30 self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
31 globals(),
32 [("simpleexampleservice", "simpleexampleservice.xproto"),
33 ("kubernetes-service", "kubernetes.xproto")] )
34
35 self.MockObjectList = self.unittest_setup["MockObjectList"]
Scott Bakerc6ed1c42019-01-29 16:57:10 -080036 self.model_accessor = self.unittest_setup["model_accessor"]
Scott Baker8b20bb12018-05-18 10:51:15 -070037
38 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../model_policies"))
39
40 from model_policy_simpleexampleserviceinstance import SimpleExampleServiceInstancePolicy
41 self.policy_class = SimpleExampleServiceInstancePolicy
42
43 self.service = SimpleExampleService(service_message="hello", service_secret="p@ssw0rd")
44 self.k8s_service = KubernetesService(id=1111)
45 self.k8s_service.get_service_instance_class=MagicMock(return_value=KubernetesServiceInstance)
46 self.trust_domain = TrustDomain(owner=self.k8s_service, name="test-trust")
47 self.image = Image(name="test-image", tag="1.2", kind="container")
48 self.slice = Slice(trust_domain=self.trust_domain, service=self.service, default_image = self.image)
49 self.service.slices = self.MockObjectList([self.slice])
50
51 def tearDown(self):
52 sys.path = self.unittest_setup["sys_path_save"]
53
54 def test_policy_create(self):
55 with patch.object(KubernetesService.objects, "get_items") as k8s_service_objects, \
56 patch.object(Service.objects, "get_items") as service_objects, \
57 patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save, \
58 patch.object(KubernetesConfigMap, "save", autospec=True) as kcfm_save, \
59 patch.object(KubernetesConfigVolumeMount, "save", autospec=True) as kcfm_mnt_save, \
60 patch.object(KubernetesSecret, "save", autospec=True) as ksec_save, \
61 patch.object(KubernetesSecretVolumeMount, "save", autospec=True) as ksec_mnt_save:
62 k8s_service_objects.return_value = [self.k8s_service]
63 service_objects.return_value = [self.k8s_service, self.service]
64
65 si = SimpleExampleServiceInstance(name="test-simple-instance",
66 id=1112,
67 owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
68 si.embedded_images = self.MockObjectList([])
69
Scott Bakerc6ed1c42019-01-29 16:57:10 -080070 step = self.policy_class(model_accessor=self.model_accessor)
Scott Baker8b20bb12018-05-18 10:51:15 -070071
72 desired_data = json.dumps({"index.html": step.render_index(si)})
73
74 desired_secret_data = json.dumps({"service_secret.txt": base64.b64encode("p@ssw0rd"),
75 "tenant_secret.txt": base64.b64encode("l3tm31n")})
76
77 step.handle_create(si)
78
79 # Saved twice, once with no_sync=True and once with no_sync=False
80 self.assertEqual(ksi_save.call_count, 2)
81 saved_ksi = ksi_save.call_args[0][0]
82 self.assertEqual(saved_ksi.slice, self.slice)
83 self.assertEqual(saved_ksi.owner, self.k8s_service)
84 self.assertEqual(saved_ksi.image, self.image)
85 self.assertEqual(saved_ksi.name, "simpleexampleserviceinstance-1112")
86
87 # Config Map
88 self.assertEqual(kcfm_save.call_count, 1)
89 saved_cfm = kcfm_save.call_args[0][0]
90 self.assertEqual(saved_cfm.name, "simpleexampleserviceinstance-map-1112")
91 self.assertEqual(saved_cfm.trust_domain, self.trust_domain)
92 self.assertEqual(saved_cfm.data, desired_data)
93
94 # Mouhnt of Config Map to Service Instance
95 self.assertEqual(kcfm_mnt_save.call_count, 1)
96 saved_cfm_mnt = kcfm_mnt_save.call_args[0][0]
97 self.assertEqual(saved_cfm_mnt.config, saved_cfm)
98 self.assertEqual(saved_cfm_mnt.service_instance, saved_ksi)
99
100 # Secret
101 self.assertEqual(ksec_save.call_count, 1)
102 saved_sec = ksec_save.call_args[0][0]
103 self.assertEqual(saved_sec.name, "simpleexampleserviceinstance-secret-1112")
104 self.assertEqual(saved_sec.trust_domain, self.trust_domain)
105 self.assertEqual(saved_sec.data, desired_secret_data)
106
107 # Mount of Secret to Service Instance
108 self.assertEqual(ksec_mnt_save.call_count, 1)
109 saved_sec_mnt = ksec_mnt_save.call_args[0][0]
110 self.assertEqual(saved_sec_mnt.secret, saved_sec)
111 self.assertEqual(saved_sec_mnt.service_instance, saved_ksi)
112
113 def test_policy_update(self):
114 with patch.object(KubernetesService.objects, "get_items") as k8s_service_objects, \
115 patch.object(Service.objects, "get_items") as service_objects, \
116 patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save, \
117 patch.object(KubernetesConfigMap, "save", autospec=True) as kcfm_save, \
118 patch.object(KubernetesConfigVolumeMount, "save", autospec=True) as kcfm_mnt_save, \
119 patch.object(KubernetesSecret, "save", autospec=True) as ksec_save, \
120 patch.object(KubernetesSecretVolumeMount, "save", autospec=True) as ksec_mnt_save:
121 k8s_service_objects.return_value = [self.k8s_service]
122 service_objects.return_value = [self.k8s_service, self.service]
123
124 si = SimpleExampleServiceInstance(name="test-simple-instance",
125 id=1112,
126 owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
127 si.embedded_images = self.MockObjectList([])
128
129 ksi = KubernetesServiceInstance(owner=self.k8s_service, slice=self.slice, image=self.image,
130 name="simpleexampleserviceinstance-1112")
131
132 cfm = KubernetesConfigMap(trust_domain=self.trust_domain, name="simpleexampleserviceinstance-map-1112",
133 data="junk")
134
135 cfm_mnt = KubernetesConfigVolumeMount(config=cfm, service_instance=ksi)
136
137 si.compute_instance = ksi
138 ksi.kubernetes_config_volume_mounts = self.MockObjectList([cfm_mnt])
139
Scott Bakerc6ed1c42019-01-29 16:57:10 -0800140 step = self.policy_class(model_accessor=self.model_accessor)
Scott Baker8b20bb12018-05-18 10:51:15 -0700141
142 desired_data = json.dumps({"index.html": step.render_index(si)})
143
144 step.handle_update(si)
145
146 self.assertEqual(ksi_save.call_count, 1)
147
148 # Config Map
149 self.assertEqual(kcfm_save.call_count, 1)
150 saved_cfm = kcfm_save.call_args[0][0]
151 self.assertEqual(saved_cfm.name, "simpleexampleserviceinstance-map-1112")
152 self.assertEqual(saved_cfm.trust_domain, self.trust_domain)
153 self.assertEqual(saved_cfm.data, desired_data)
154
155 self.assertEqual(kcfm_mnt_save.call_count, 0)
156 self.assertEqual(ksec_save.call_count, 0)
157 self.assertEqual(ksec_mnt_save.call_count, 0)
158
159 def test_policy_update_no_difference(self):
160 with patch.object(KubernetesService.objects, "get_items") as k8s_service_objects, \
161 patch.object(Service.objects, "get_items") as service_objects, \
162 patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save, \
163 patch.object(KubernetesConfigMap, "save", autospec=True) as kcfm_save, \
164 patch.object(KubernetesConfigVolumeMount, "save", autospec=True) as kcfm_mnt_save, \
165 patch.object(KubernetesSecret, "save", autospec=True) as ksec_save, \
166 patch.object(KubernetesSecretVolumeMount, "save", autospec=True) as ksec_mnt_save:
167 k8s_service_objects.return_value = [self.k8s_service]
168 service_objects.return_value = [self.k8s_service, self.service]
169
Scott Bakerc6ed1c42019-01-29 16:57:10 -0800170 step = self.policy_class(model_accessor=self.model_accessor)
Scott Baker8b20bb12018-05-18 10:51:15 -0700171
172 si = SimpleExampleServiceInstance(name="test-simple-instance",
173 id=1112,
174 owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
175 si.embedded_images = self.MockObjectList([])
176
177 desired_data = json.dumps({"index.html": step.render_index(si)})
178
179 ksi = KubernetesServiceInstance(owner=self.k8s_service, slice=self.slice, image=self.image,
180 name="simpleexampleserviceinstance-1112")
181
182 cfm = KubernetesConfigMap(trust_domain=self.trust_domain, name="simpleexampleserviceinstance-map-1112",
183 data=desired_data)
184
185 cfm_mnt = KubernetesConfigVolumeMount(config=cfm, service_instance=ksi)
186
187 si.compute_instance = ksi
188 ksi.kubernetes_config_volume_mounts = self.MockObjectList([cfm_mnt])
189
190 step.handle_update(si)
191
192 self.assertEqual(ksi_save.call_count, 0)
193 self.assertEqual(kcfm_save.call_count, 0)
194 self.assertEqual(kcfm_mnt_save.call_count, 0)
195 self.assertEqual(ksec_save.call_count, 0)
196 self.assertEqual(ksec_mnt_save.call_count, 0)
197
198 def test_policy_delete(self):
199 with patch.object(KubernetesServiceInstance, "delete", autospec=True) as ksi_delete, \
200 patch.object(SimpleExampleServiceInstance, "save", autospec=True) as sesi_save:
201 si = SimpleExampleServiceInstance(name="test-simple-instance",
202 id=1112,
203 owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
204
205 ksi = KubernetesServiceInstance(owner=self.k8s_service, slice=self.slice, image=self.image,
206 name="simpleexampleserviceinstance-1112")
207 si.compute_instance = ksi
208
Scott Bakerc6ed1c42019-01-29 16:57:10 -0800209 step = self.policy_class(model_accessor=self.model_accessor)
Scott Baker8b20bb12018-05-18 10:51:15 -0700210
211 step.handle_delete(si)
212
213 # The compute instance should have been deleted
214 self.assertEqual(ksi_delete.call_count, 1)
215 deleted_ksi = ksi_delete.call_args[0][0]
216 self.assertEqual(deleted_ksi, ksi)
217
218 # The serviceInstance should have had its compute_instance set to none
Scott Bakerc12f3362018-05-31 10:42:28 -0700219 self.assertEqual(si.compute_instance, None)
Scott Baker8b20bb12018-05-18 10:51:15 -0700220
221 # The SimpleExampleServiceInstance should have been saved
222 self.assertEqual(sesi_save.call_count, 1)
223
224if __name__ == '__main__':
225 unittest.main()