]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
Add SingleCellSimple fixture
authorDan Smith <dansmith@redhat.com>
Thu, 1 Dec 2016 16:13:01 +0000 (08:13 -0800)
committerDan Smith <dansmith@redhat.com>
Wed, 7 Dec 2016 18:24:02 +0000 (10:24 -0800)
This fixture makes it trivial to mock out all the cell listing stuff
for the simple case where you just want to assume a single cell that
is configured as the default database.

Change-Id: I7eecee388780677d2b342556fef0179b02707f3c

nova/tests/fixtures.py
nova/tests/unit/test_fixtures.py

index 6ff0fecfec69ca3fd121650162a27da7fa9decb0..be42c66286c42009d512176f08f94b5e50ae4a5c 100644 (file)
@@ -42,6 +42,7 @@ from nova.objects import service as service_obj
 from nova import rpc
 from nova import service
 from nova.tests.functional.api import client
+from nova.tests import uuidsentinel
 
 _TRUE_VALUES = ('True', 'true', '1', 'yes')
 
@@ -250,6 +251,43 @@ class DatabasePoisonFixture(fixtures.Fixture):
                         'do use the database and cause failures later.')
 
 
+class SingleCellSimple(fixtures.Fixture):
+    """Setup the simplest cells environment possible
+
+    This should be used when you do not care about multiple cells,
+    or having a "real" environment for tests that should not care.
+    This will give you a single cell, and map any and all accesses
+    to that cell (even things that would go to cell0).
+
+    If you need to distinguish between cell0 and cellN, then you
+    should use the CellDatabases fixture.
+    """
+
+    def setUp(self):
+        super(SingleCellSimple, self).setUp()
+        self.useFixture(fixtures.MonkeyPatch(
+            'nova.objects.CellMappingList._get_all_from_db',
+            self._fake_cell_list))
+        self.useFixture(fixtures.MonkeyPatch(
+            'nova.context.target_cell',
+            self._fake_target_cell))
+
+    def _fake_cell_list(self, *args):
+        return [{'id': 1,
+                 'updated_at': None,
+                 'created_at': None,
+                 'uuid': uuidsentinel.cell1,
+                 'name': 'onlycell',
+                 'transport_url': 'fake://nowhere/',
+                 'database_connection': 'sqlite:///'}]
+
+    @contextmanager
+    def _fake_target_cell(self, context, target_cell):
+        # NOTE(danms): Just pass through the context without actually
+        # targetting anything.
+        yield context
+
+
 class CellDatabases(fixtures.Fixture):
     """Create per-cell databases for testing.
 
index d655d1a028a3d35d60a79f0f086377d970acbd60..839e7b5cc2c4516ac944500ce26b222c12b5f17f 100644 (file)
@@ -26,8 +26,10 @@ import testtools
 
 from nova.compute import rpcapi as compute_rpcapi
 from nova import conductor
+from nova import context
 from nova.db.sqlalchemy import api as session
 from nova import exception
+from nova import objects
 from nova.objects import base as obj_base
 from nova.objects import service as service_obj
 from nova.tests import fixtures
@@ -453,3 +455,15 @@ class TestNoopConductorFixture(testtools.TestCase):
         self.useFixture(fixtures.NoopConductorFixture())
         conductor.API().wait_until_ready()
         self.assertFalse(mock_wait.called)
+
+
+class TestSingleCellSimpleFixture(testtools.TestCase):
+    def test_single_cell(self):
+        self.useFixture(fixtures.SingleCellSimple())
+        cml = objects.CellMappingList.get_all(None)
+        self.assertEqual(1, len(cml))
+
+    def test_target_cell(self):
+        self.useFixture(fixtures.SingleCellSimple())
+        with context.target_cell(mock.sentinel.context, None) as c:
+            self.assertIs(mock.sentinel.context, c)