]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Unit test for DBInstance resource.
authorSteve Baker <sbaker@redhat.com>
Wed, 26 Sep 2012 03:53:03 +0000 (15:53 +1200)
committerSteve Baker <sbaker@redhat.com>
Wed, 26 Sep 2012 21:38:15 +0000 (09:38 +1200)
Coverage is 100% assuming you don't count the
nested template as code.

Change-Id: I846ed736d75eeef8723c33db7cae889a26049d9a

heat/engine/dbinstance.py
heat/tests/test_dbinstance.py [new file with mode: 0644]

index f9a2d36ee37ca94596b0bf53ddcf55cc47279dbc..0529986b795d29d6704878f627800438231bf318 100644 (file)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import urllib2
 import json
 
 from heat.common import exception
 from heat.engine import stack
-from heat.db import api as db_api
-from heat.engine import parser
-from novaclient.exceptions import NotFound
 
 from heat.openstack.common import log as logging
 
diff --git a/heat/tests/test_dbinstance.py b/heat/tests/test_dbinstance.py
new file mode 100644 (file)
index 0000000..092ddce
--- /dev/null
@@ -0,0 +1,120 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+#    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 sys
+import os
+
+import nose
+import unittest
+import mox
+import json
+
+from nose.plugins.attrib import attr
+
+from heat.common import exception
+from heat.engine import dbinstance as dbi
+from heat.engine import parser
+from heat.engine import stack
+
+
+@attr(tag=['unit', 'resource'])
+@attr(speed='fast')
+class DBInstanceTest(unittest.TestCase):
+    def setUp(self):
+        self.m = mox.Mox()
+        self.m.StubOutWithMock(stack.Stack, 'create_with_template')
+        self.m.StubOutWithMock(dbi.DBInstance, 'nested')
+
+    def tearDown(self):
+        self.m.UnsetStubs()
+        print "DBInstanceTest teardown complete"
+
+    def load_template(self):
+        self.path = os.path.dirname(os.path.realpath(__file__)).\
+            replace('heat/tests', 'templates')
+        f = open("%s/WordPress_With_RDS.template" % self.path)
+        t = json.loads(f.read())
+        f.close()
+        return t
+
+    def parse_stack(self, t):
+        class DummyContext():
+            tenant = 'test_tenant'
+            tenant_id = '1234abcd'
+            username = 'test_username'
+            password = 'password'
+            auth_url = 'http://localhost:5000/v2.0'
+        t['Parameters']['KeyName']['Value'] = 'test'
+        stack = parser.Stack(DummyContext(), 'test_stack', parser.Template(t),
+                             stack_id=-1)
+
+        return stack
+
+    def create_dbinstance(self, t, stack, resource_name):
+        resource = dbi.DBInstance(resource_name,
+                                      t['Resources'][resource_name],
+                                      stack)
+        self.assertEqual(None, resource.validate())
+        self.assertEqual(None, resource.create())
+        self.assertEqual(dbi.DBInstance.CREATE_COMPLETE, resource.state)
+        return resource
+
+    def test_dbinstance(self):
+
+        class FakeDatabaseInstance:
+            def _ipaddress(self):
+                return '10.0.0.1'
+
+        class FakeNested:
+            resources = {'DatabaseInstance': FakeDatabaseInstance()}
+
+        stack.Stack.create_with_template(mox.IgnoreArg()).AndReturn(None)
+
+        fn = FakeNested()
+
+        dbi.DBInstance.nested().AndReturn(None)
+        dbi.DBInstance.nested().MultipleTimes().AndReturn(fn)
+        self.m.ReplayAll()
+
+        t = self.load_template()
+        s = self.parse_stack(t)
+        resource = self.create_dbinstance(t, s, 'DatabaseServer')
+
+        self.assertEqual({'AllocatedStorage': u'5',
+            'DBInstanceClass': u'db.m1.small',
+            'DBName': u'wordpress',
+            'DBSecurityGroups': [],
+            'KeyName': 'test',
+            'MasterUserPassword': u'admin',
+            'MasterUsername': u'admin',
+            'Port': '3306'}, resource._params())
+
+        self.assertEqual('0.0.0.0', resource.FnGetAtt('Endpoint.Address'))
+        self.assertEqual('10.0.0.1', resource.FnGetAtt('Endpoint.Address'))
+        self.assertEqual('3306', resource.FnGetAtt('Endpoint.Port'))
+
+        try:
+            resource.FnGetAtt('foo')
+        except exception.InvalidTemplateAttribute:
+            pass
+        else:
+            raise Exception('Expected InvalidTemplateAttribute')
+
+        self.m.VerifyAll()
+
+    # allows testing of the test directly, shown below
+    if __name__ == '__main__':
+        sys.argv.append(__file__)
+        nose.main()