CORD-3049 Unit Tests for SimpleExampleService model policy

Change-Id: Id670827f4141edf4513e2355fec838fa5092de34
diff --git a/xos/nose2-plugins/__init__.py b/xos/nose2-plugins/__init__.py
new file mode 100644
index 0000000..42722a8
--- /dev/null
+++ b/xos/nose2-plugins/__init__.py
@@ -0,0 +1,14 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/xos/nose2-plugins/exclude.py b/xos/nose2-plugins/exclude.py
new file mode 100644
index 0000000..241eadb
--- /dev/null
+++ b/xos/nose2-plugins/exclude.py
@@ -0,0 +1,32 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+import os
+
+from nose2.events import Plugin
+
+log = logging.getLogger('nose2.plugins.excludeignoredfiles')
+
+class ExcludeIgnoredFiles(Plugin):
+    commandLineSwitch = (None, 'exclude-ignored-files', 'Exclude that which should be excluded')
+
+    def matchPath(self, event):
+        if event.path.endswith(".py"):
+            text = open(event.path, "r").read()
+            if "test_framework: ignore" in text.lower():
+                log.info("Ignoring %s" % event.path)
+                event.handled = True
+                return False
diff --git a/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py b/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py
index 8cfea04..63db3ef 100644
--- a/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py
+++ b/xos/synchronizer/model_policies/model_policy_simpleexampleserviceinstance.py
@@ -49,7 +49,8 @@
                            "url": image.url})
         fields["images"] = images
 
-        template = jinja2.Template(open("model_policies/index.html.j2").read())
+        template_fn = os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "index.html.j2")
+        template = jinja2.Template(open(template_fn).read())
 
         return template.render(fields)
 
@@ -101,9 +102,9 @@
             compute_instance = service_instance.compute_instance
             mnt = compute_instance.leaf_model.kubernetes_config_volume_mounts.first()
             config = mnt.config
-            new_data = {"index.html": self.render_index(service_instance)}
+            new_data = json.dumps({"index.html": self.render_index(service_instance)})
             if (new_data != config.data):
-                config.data = json.dumps(new_data)
+                config.data = new_data
                 config.save(always_update_timestamp=True)
                 # Force the Kubernetes syncstep
                 compute_instance.save(always_update_timestamp=True)
diff --git a/xos/synchronizer/tests/test_config.yaml b/xos/synchronizer/tests/test_config.yaml
new file mode 100644
index 0000000..acd2ba7
--- /dev/null
+++ b/xos/synchronizer/tests/test_config.yaml
@@ -0,0 +1,29 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+name: test-model-policies
+accessor:
+  username: xosadmin@opencord.org
+  password: "sample"
+  kind: "testframework"
+logging:
+  version: 1
+  handlers:
+    console:
+      class: logging.StreamHandler
+  loggers:
+    'multistructlog':
+      handlers:
+          - console
diff --git a/xos/synchronizer/tests/test_model_policy_exampleserviceinstance.py b/xos/synchronizer/tests/test_model_policy_exampleserviceinstance.py
new file mode 100644
index 0000000..3d85123
--- /dev/null
+++ b/xos/synchronizer/tests/test_model_policy_exampleserviceinstance.py
@@ -0,0 +1,224 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Tests for SimpleExampleServiceInstance model policies
+
+import base64
+import json
+import os
+import sys
+import unittest
+from mock import patch, PropertyMock, ANY, MagicMock
+from unit_test_common import setup_sync_unit_test
+
+
+class TestSimpleExampleServiceInstancePolicy(unittest.TestCase):
+
+    def setUp(self):
+        self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
+                                                   globals(),
+                                                   [("simpleexampleservice", "simpleexampleservice.xproto"),
+                                                    ("kubernetes-service", "kubernetes.xproto")] )
+
+        self.MockObjectList = self.unittest_setup["MockObjectList"]
+
+        sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../model_policies"))
+
+        from model_policy_simpleexampleserviceinstance import SimpleExampleServiceInstancePolicy
+        self.policy_class = SimpleExampleServiceInstancePolicy
+
+        self.service = SimpleExampleService(service_message="hello", service_secret="p@ssw0rd")
+        self.k8s_service = KubernetesService(id=1111)
+        self.k8s_service.get_service_instance_class=MagicMock(return_value=KubernetesServiceInstance)
+        self.trust_domain = TrustDomain(owner=self.k8s_service, name="test-trust")
+        self.image = Image(name="test-image", tag="1.2", kind="container")
+        self.slice = Slice(trust_domain=self.trust_domain, service=self.service, default_image = self.image)
+        self.service.slices = self.MockObjectList([self.slice])
+
+    def tearDown(self):
+        sys.path = self.unittest_setup["sys_path_save"]
+
+    def test_policy_create(self):
+        with patch.object(KubernetesService.objects, "get_items") as k8s_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save, \
+                patch.object(KubernetesConfigMap, "save", autospec=True) as kcfm_save, \
+                patch.object(KubernetesConfigVolumeMount, "save", autospec=True) as kcfm_mnt_save, \
+                patch.object(KubernetesSecret, "save", autospec=True) as ksec_save, \
+                patch.object(KubernetesSecretVolumeMount, "save", autospec=True) as ksec_mnt_save:
+            k8s_service_objects.return_value = [self.k8s_service]
+            service_objects.return_value = [self.k8s_service, self.service]
+
+            si = SimpleExampleServiceInstance(name="test-simple-instance",
+                                              id=1112,
+                                              owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
+            si.embedded_images = self.MockObjectList([])
+
+            step = self.policy_class()
+
+            desired_data = json.dumps({"index.html": step.render_index(si)})
+
+            desired_secret_data = json.dumps({"service_secret.txt": base64.b64encode("p@ssw0rd"),
+                                              "tenant_secret.txt": base64.b64encode("l3tm31n")})
+
+            step.handle_create(si)
+
+            # Saved twice, once with no_sync=True and once with no_sync=False
+            self.assertEqual(ksi_save.call_count, 2)
+            saved_ksi = ksi_save.call_args[0][0]
+            self.assertEqual(saved_ksi.slice, self.slice)
+            self.assertEqual(saved_ksi.owner, self.k8s_service)
+            self.assertEqual(saved_ksi.image, self.image)
+            self.assertEqual(saved_ksi.name, "simpleexampleserviceinstance-1112")
+
+            # Config Map
+            self.assertEqual(kcfm_save.call_count, 1)
+            saved_cfm = kcfm_save.call_args[0][0]
+            self.assertEqual(saved_cfm.name, "simpleexampleserviceinstance-map-1112")
+            self.assertEqual(saved_cfm.trust_domain, self.trust_domain)
+            self.assertEqual(saved_cfm.data, desired_data)
+
+            # Mouhnt of Config Map to Service Instance
+            self.assertEqual(kcfm_mnt_save.call_count, 1)
+            saved_cfm_mnt = kcfm_mnt_save.call_args[0][0]
+            self.assertEqual(saved_cfm_mnt.config, saved_cfm)
+            self.assertEqual(saved_cfm_mnt.service_instance, saved_ksi)
+
+            # Secret
+            self.assertEqual(ksec_save.call_count, 1)
+            saved_sec = ksec_save.call_args[0][0]
+            self.assertEqual(saved_sec.name, "simpleexampleserviceinstance-secret-1112")
+            self.assertEqual(saved_sec.trust_domain, self.trust_domain)
+            self.assertEqual(saved_sec.data, desired_secret_data)
+
+            # Mount of Secret to Service Instance
+            self.assertEqual(ksec_mnt_save.call_count, 1)
+            saved_sec_mnt = ksec_mnt_save.call_args[0][0]
+            self.assertEqual(saved_sec_mnt.secret, saved_sec)
+            self.assertEqual(saved_sec_mnt.service_instance, saved_ksi)
+
+    def test_policy_update(self):
+        with patch.object(KubernetesService.objects, "get_items") as k8s_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save, \
+                patch.object(KubernetesConfigMap, "save", autospec=True) as kcfm_save, \
+                patch.object(KubernetesConfigVolumeMount, "save", autospec=True) as kcfm_mnt_save, \
+                patch.object(KubernetesSecret, "save", autospec=True) as ksec_save, \
+                patch.object(KubernetesSecretVolumeMount, "save", autospec=True) as ksec_mnt_save:
+            k8s_service_objects.return_value = [self.k8s_service]
+            service_objects.return_value = [self.k8s_service, self.service]
+
+            si = SimpleExampleServiceInstance(name="test-simple-instance",
+                                              id=1112,
+                                              owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
+            si.embedded_images = self.MockObjectList([])
+
+            ksi = KubernetesServiceInstance(owner=self.k8s_service, slice=self.slice, image=self.image,
+                                            name="simpleexampleserviceinstance-1112")
+
+            cfm = KubernetesConfigMap(trust_domain=self.trust_domain, name="simpleexampleserviceinstance-map-1112",
+                                      data="junk")
+
+            cfm_mnt = KubernetesConfigVolumeMount(config=cfm, service_instance=ksi)
+
+            si.compute_instance = ksi
+            ksi.kubernetes_config_volume_mounts = self.MockObjectList([cfm_mnt])
+
+            step = self.policy_class()
+
+            desired_data = json.dumps({"index.html": step.render_index(si)})
+
+            step.handle_update(si)
+
+            self.assertEqual(ksi_save.call_count, 1)
+
+            # Config Map
+            self.assertEqual(kcfm_save.call_count, 1)
+            saved_cfm = kcfm_save.call_args[0][0]
+            self.assertEqual(saved_cfm.name, "simpleexampleserviceinstance-map-1112")
+            self.assertEqual(saved_cfm.trust_domain, self.trust_domain)
+            self.assertEqual(saved_cfm.data, desired_data)
+
+            self.assertEqual(kcfm_mnt_save.call_count, 0)
+            self.assertEqual(ksec_save.call_count, 0)
+            self.assertEqual(ksec_mnt_save.call_count, 0)
+
+    def test_policy_update_no_difference(self):
+        with patch.object(KubernetesService.objects, "get_items") as k8s_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(KubernetesServiceInstance, "save", autospec=True) as ksi_save, \
+                patch.object(KubernetesConfigMap, "save", autospec=True) as kcfm_save, \
+                patch.object(KubernetesConfigVolumeMount, "save", autospec=True) as kcfm_mnt_save, \
+                patch.object(KubernetesSecret, "save", autospec=True) as ksec_save, \
+                patch.object(KubernetesSecretVolumeMount, "save", autospec=True) as ksec_mnt_save:
+            k8s_service_objects.return_value = [self.k8s_service]
+            service_objects.return_value = [self.k8s_service, self.service]
+
+            step = self.policy_class()
+
+            si = SimpleExampleServiceInstance(name="test-simple-instance",
+                                              id=1112,
+                                              owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
+            si.embedded_images = self.MockObjectList([])
+
+            desired_data = json.dumps({"index.html": step.render_index(si)})
+
+            ksi = KubernetesServiceInstance(owner=self.k8s_service, slice=self.slice, image=self.image,
+                                            name="simpleexampleserviceinstance-1112")
+
+            cfm = KubernetesConfigMap(trust_domain=self.trust_domain, name="simpleexampleserviceinstance-map-1112",
+                                      data=desired_data)
+
+            cfm_mnt = KubernetesConfigVolumeMount(config=cfm, service_instance=ksi)
+
+            si.compute_instance = ksi
+            ksi.kubernetes_config_volume_mounts = self.MockObjectList([cfm_mnt])
+
+            step.handle_update(si)
+
+            self.assertEqual(ksi_save.call_count, 0)
+            self.assertEqual(kcfm_save.call_count, 0)
+            self.assertEqual(kcfm_mnt_save.call_count, 0)
+            self.assertEqual(ksec_save.call_count, 0)
+            self.assertEqual(ksec_mnt_save.call_count, 0)
+
+    def test_policy_delete(self):
+        with patch.object(KubernetesServiceInstance, "delete", autospec=True) as ksi_delete, \
+                patch.object(SimpleExampleServiceInstance, "save", autospec=True) as sesi_save:
+            si = SimpleExampleServiceInstance(name="test-simple-instance",
+                                              id=1112,
+                                              owner=self.service, tenant_message="world", tenant_secret="l3tm31n")
+
+            ksi = KubernetesServiceInstance(owner=self.k8s_service, slice=self.slice, image=self.image,
+                                            name="simpleexampleserviceinstance-1112")
+            si.compute_instance = ksi
+
+            step = self.policy_class()
+
+            step.handle_delete(si)
+
+            # The compute instance should have been deleted
+            self.assertEqual(ksi_delete.call_count, 1)
+            deleted_ksi = ksi_delete.call_args[0][0]
+            self.assertEqual(deleted_ksi, ksi)
+
+            # The serviceInstance should have had its compute_instance set to none
+            self.assertEqual(si.compute_instance.None)
+
+            # The SimpleExampleServiceInstance should have been saved
+            self.assertEqual(sesi_save.call_count, 1)
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/xos/synchronizer/tests/unit_test_common.py b/xos/synchronizer/tests/unit_test_common.py
new file mode 100644
index 0000000..68f6743
--- /dev/null
+++ b/xos/synchronizer/tests/unit_test_common.py
@@ -0,0 +1,84 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+import sys
+
+def setup_sync_unit_test(test_path, globals_dict, models, config_fn="test_config.yaml"):
+    """ Perform the common steps associated with setting up a synchronizer unit test.
+           1) Add synchronizers/new_base to sys.path
+           2) Import xosconfig.Config and set it up to test_config.yaml in the current dir
+           3) Build the mock modelaccessor and import it
+           4) Import all model accessor classes into global space
+
+        Arguments:
+            test_path - path to the test case that is being run
+            globals_dict - a dictionary to add global models to
+            models - a list of pairs (service_name, xproto_name,
+            config_fn - filename of config file)
+
+        Returns:
+            Dictionary containing the following:
+                sys_path_save: the original sys.path
+                model_accessor: model accessor class
+                Config: the Config object
+                xos_dir: xos directory
+                services_dir: services directory
+    """
+    def get_models_fn(services_dir, service_name, xproto_name):
+        name = os.path.join(service_name, "xos", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+        else:
+            name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+            if os.path.exists(os.path.join(services_dir, name)):
+                return name
+        raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+
+    sys_path_save = sys.path
+
+    xos_dir = os.path.join(test_path, "../../..")
+    if not os.path.exists(os.path.join(test_path, "new_base")):
+        xos_dir = os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+        services_dir = os.path.join(xos_dir, "../../xos_services")
+    sys.path.append(xos_dir)
+    sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+    # Setting up the config module
+    from xosconfig import Config
+    config = os.path.join(test_path, config_fn)
+    Config.clear()
+    Config.init(config, "synchronizer-config-schema.yaml")
+
+    xprotos = []
+    for (service_name, xproto_name) in models:
+        xprotos.append(get_models_fn(services_dir, service_name, xproto_name))
+
+    from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+    build_mock_modelaccessor(xos_dir, services_dir, xprotos)
+    import synchronizers.new_base.modelaccessor
+    from synchronizers.new_base.modelaccessor import model_accessor
+    from mock_modelaccessor import MockObjectList
+
+    # import all class names to globals
+    for (k, v) in model_accessor.all_model_classes.items():
+        globals_dict[k] = v
+
+    return {"sys_path_save": sys_path_save,
+            "model_accessor": model_accessor,
+            "Config": Config,
+            "xos_dir": xos_dir,
+            "services_dir": services_dir,
+            "MockObjectList": MockObjectList}
diff --git a/xos/unittest.cfg b/xos/unittest.cfg
new file mode 100644
index 0000000..44c6ea4
--- /dev/null
+++ b/xos/unittest.cfg
@@ -0,0 +1,6 @@
+[unittest]
+plugins=nose2-plugins.exclude
+code-directories=synchronizer
+                 model_policies
+                 steps
+                 pull_steps