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