CORD-1493 Unit tests for TenantWithContainer model policy

Change-Id: Ib82cc4c2ebd0f9e4e4a7c5535807aecd51a99cca
diff --git a/xos/synchronizers/new_base/model_policies/test_model_policy_tenantwithcontainer.py b/xos/synchronizers/new_base/model_policies/test_model_policy_tenantwithcontainer.py
index b137c44..968fe96 100644
--- a/xos/synchronizers/new_base/model_policies/test_model_policy_tenantwithcontainer.py
+++ b/xos/synchronizers/new_base/model_policies/test_model_policy_tenantwithcontainer.py
@@ -1,6 +1,7 @@
 import unittest
 from mock import patch
 import mock
+import pdb
 
 import os, sys
 sys.path.append("../../..")
@@ -11,19 +12,60 @@
 
 import synchronizers.new_base.modelaccessor
 
-from model_policy_tenantwithcontainer import TenantWithContainerPolicy
+import model_policy_tenantwithcontainer
+from model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler
 
-class MockTenant:
+class MockObject:
+    objects = None
+    def __init__(self, **kwargs):
+        for (k,v) in kwargs.items():
+            setattr(self,k,v)
+    def save(self):
+        pass
+    def delete(self):
+        pass
+
+class MockFlavor(MockObject):
+    name = None
+
+class MockInstance(MockObject):
+    name = None
+
+class MockDeployment(MockObject):
+    name = None
+
+class MockUser(MockObject):
+    email = None
+
+class MockSlice(MockObject):
+    name = None
+
+class MockNode(MockObject):
+    hostname = None
+    site_deployment = None
+
+class MockImage(MockObject):
+    name = None
+
+class MockTenant(MockObject):
     provider_service = None
     deleted = False
     instance = None
+    creator = None
     service_specific_attribute = {}
 
-class TestModelPolicyVsgTenant(unittest.TestCase):
+    def get_image(self):
+        return None
+
+class TestModelPolicyTenantWithContainer(unittest.TestCase):
     def setUp(self):
         self.policy = TenantWithContainerPolicy()
-        self.tenant = MockTenant()
-        
+        self.user = MockUser(email="testadmin@test.org")
+        self.tenant = MockTenant(creator=self.user)
+        self.flavor = MockFlavor(name="m1.small")
+        model_policy_tenantwithcontainer.Instance = MockInstance
+        model_policy_tenantwithcontainer.Flavor = MockFlavor
+
     @patch.object(MockTenant, "provider_service")
     def test_manage_container_no_slices(self, provider_service):
         provider_service.slices.count.return_value = 0
@@ -31,5 +73,78 @@
             self.policy.manage_container(self.tenant)
         self.assertEqual(e.exception.message, "The service has no slices")
 
+    @patch.object(MockTenant, "provider_service")
+    @patch.object(MockTenant, "save")
+    @patch.object(TenantWithContainerPolicy, "get_image")
+    @patch.object(LeastLoadedNodeScheduler, "pick")
+    @patch.object(MockNode, "site_deployment")
+    @patch.object(MockInstance, "save")
+    @patch.object(MockInstance, "delete")
+    def test_manage_container(self, instance_delete, instance_save, site_deployment, pick, get_image, tenant_save, provider_service):
+        # setup mocks
+        node = MockNode(hostname="my.node.com")
+        slice = MockSlice(name="mysite_test1", default_flavor=self.flavor, default_isolation="vm")
+        image = MockImage(name="trusty-server-multi-nic")
+        deployment = MockDeployment(name="testdeployment")
+        provider_service.slices.count.return_value = 1
+        provider_service.slices.all.return_value = [slice]
+        provider_service.slices.first.return_value = slice
+        get_image.return_value = image
+        pick.return_value = (node, None)
+        site_deployment.deployment = deployment
+        # done setup mocks
+
+        # call manage_container
+        self.policy.manage_container(self.tenant)
+
+        # make sure manage_container did what it is supposed to do
+        self.assertNotEqual(self.tenant.instance, None)
+        self.assertEqual(self.tenant.instance.creator.email, "testadmin@test.org")
+        self.assertEqual(self.tenant.instance.image.name, "trusty-server-multi-nic")
+        self.assertEqual(self.tenant.instance.flavor.name, "m1.small")
+        self.assertEqual(self.tenant.instance.isolation, "vm")
+        self.assertEqual(self.tenant.instance.node.hostname, "my.node.com")
+        self.assertEqual(self.tenant.instance.slice.name, "mysite_test1")
+        self.assertEqual(self.tenant.instance.parent, None)
+        instance_save.assert_called()
+        instance_delete.assert_not_called()
+        tenant_save.assert_called()
+
+    def test_manage_container_delete(self):
+        self.tenant.deleted = True
+
+        # call manage_container
+        self.policy.manage_container(self.tenant)
+
+        # make sure manage_container did what it is supposed to do
+        self.assertEqual(self.tenant.instance, None)
+
+    @patch.object(MockTenant, "provider_service")
+    @patch.object(MockTenant, "save")
+    @patch.object(TenantWithContainerPolicy, "get_image")
+    @patch.object(LeastLoadedNodeScheduler, "pick")
+    @patch.object(MockNode, "site_deployment")
+    @patch.object(MockInstance, "save")
+    @patch.object(MockInstance, "delete")
+    @patch.object(MockFlavor, "objects")
+    def test_manage_container_no_m1_small(self, flavor_objects, instance_delete, instance_save, site_deployment, pick, get_image, tenant_save, provider_service):
+        # setup mocks
+        node = MockNode(hostname="my.node.com")
+        slice = MockSlice(name="mysite_test1", default_flavor=None, default_isolation="vm")
+        image = MockImage(name="trusty-server-multi-nic")
+        deployment = MockDeployment(name="testdeployment")
+        provider_service.slices.count.return_value = 1
+        provider_service.slices.all.return_value = [slice]
+        provider_service.slices.first.return_value = slice
+        get_image.return_value = image
+        pick.return_value = (node, None)
+        site_deployment.deployment = deployment
+        flavor_objects.filter.return_value = []
+        # done setup mocks
+
+        with self.assertRaises(Exception) as e:
+            self.policy.manage_container(self.tenant)
+        self.assertEqual(e.exception.message, "No m1.small flavor")
+
 if __name__ == '__main__':
     unittest.main()