Browse Source

Isolate unit tests from integration tests data

Reasons:
 - unit tests are depending on integration tests data, such as:
  - datastore and its versions;
 - distribution package maintainers are not able to package Trove since ice house RC1.

Gate behaviour:
 - gate tests are not failing because unit tests are running right after
   fake-mode tests, so sqlite still exist.

Changes:
 - fixing those unit tests that are failing while building Trove package
   (more info see at bug-report).

Change-Id: Ib9a41d68481d32cdd81fc4d7c015a63c4da33bb3
Closes-Bug: #1302784
tags/2014.2.rc1
Denis M 5 years ago
parent
commit
2de5c0f66d

+ 59
- 58
trove/tests/unittests/instance/test_instance_status.py View File

@@ -13,12 +13,16 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 #
16
+
17
+import uuid
18
+
16 19
 from testtools import TestCase
17 20
 from trove.common.instance import ServiceStatuses
21
+from trove.datastore import models
18 22
 from trove.instance.models import InstanceStatus
19 23
 from trove.instance.models import InstanceServiceStatus
20 24
 from trove.instance.models import SimpleInstance
21
-from trove.tests.util import test_config
25
+from trove.tests.unittests.util import util
22 26
 
23 27
 
24 28
 class FakeInstanceTask(object):
@@ -31,108 +35,105 @@ class FakeInstanceTask(object):
31 35
 class FakeDBInstance(object):
32 36
 
33 37
     def __init__(self):
34
-        self.id = None
38
+        self.id = str(uuid.uuid4())
35 39
         self.deleted = False
36
-        self.datastore_version_id = test_config.dbaas_datastore_version_id
40
+        self.datastore_version_id = str(uuid.uuid4())
37 41
         self.server_status = "ACTIVE"
38 42
         self.task_status = FakeInstanceTask()
39 43
 
40 44
 
41
-class InstanceStatusTest(TestCase):
45
+class BaseInstanceStatusTestCase(TestCase):
42 46
 
43 47
     def setUp(self):
44
-        super(InstanceStatusTest, self).setUp()
48
+        util.init_db()
49
+        self.db_info = FakeDBInstance()
50
+        self.status = InstanceServiceStatus(
51
+            ServiceStatuses.RUNNING)
52
+        self.datastore = models.DBDatastore.create(
53
+            id=str(uuid.uuid4()),
54
+            name='mysql',
55
+            default_version_id=self.db_info.datastore_version_id
56
+        )
57
+        self.version = models.DBDatastoreVersion.create(
58
+            id=self.db_info.datastore_version_id,
59
+            datastore_id=self.datastore.id,
60
+            name='5.5',
61
+            manager='mysql',
62
+            image_id=str(uuid.uuid4()),
63
+            active=1,
64
+            packages="mysql-server-5.5"
65
+        )
66
+        super(BaseInstanceStatusTestCase, self).setUp()
45 67
 
46 68
     def tearDown(self):
47
-        super(InstanceStatusTest, self).tearDown()
69
+        self.datastore.delete()
70
+        self.version.delete()
71
+        super(BaseInstanceStatusTestCase, self).tearDown()
72
+
73
+
74
+class InstanceStatusTest(BaseInstanceStatusTestCase):
48 75
 
49 76
     def test_task_status_error_reports_error(self):
50
-        fake_db_info = FakeDBInstance()
51
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
52
-        fake_db_info.task_status.is_error = True
53
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
77
+        self.db_info.task_status.is_error = True
78
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
54 79
         self.assertEqual(InstanceStatus.ERROR, instance.status)
55 80
 
56 81
     def test_task_status_action_building_reports_build(self):
57
-        fake_db_info = FakeDBInstance()
58
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
59
-        fake_db_info.task_status.action = "BUILDING"
60
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
82
+        self.db_info.task_status.action = "BUILDING"
83
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
61 84
         self.assertEqual(InstanceStatus.BUILD, instance.status)
62 85
 
63 86
     def test_task_status_action_rebooting_reports_reboot(self):
64
-        fake_db_info = FakeDBInstance()
65
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
66
-        fake_db_info.task_status.action = "REBOOTING"
67
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
87
+        self.db_info.task_status.action = "REBOOTING"
88
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
68 89
         self.assertEqual(InstanceStatus.REBOOT, instance.status)
69 90
 
70 91
     def test_task_status_action_resizing_reports_resize(self):
71
-        fake_db_info = FakeDBInstance()
72
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
73
-        fake_db_info.task_status.action = "RESIZING"
74
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
92
+        self.db_info.task_status.action = "RESIZING"
93
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
75 94
         self.assertEqual(InstanceStatus.RESIZE, instance.status)
76 95
 
77 96
     def test_task_status_action_deleting_reports_shutdown(self):
78
-        fake_db_info = FakeDBInstance()
79
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
80
-        fake_db_info.task_status.action = "DELETING"
81
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
97
+        self.db_info.task_status.action = "DELETING"
98
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
82 99
         self.assertEqual(InstanceStatus.SHUTDOWN, instance.status)
83 100
 
84 101
     def test_nova_server_build_reports_build(self):
85
-        fake_db_info = FakeDBInstance()
86
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
87
-        fake_db_info.server_status = "BUILD"
88
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
102
+        self.db_info.server_status = "BUILD"
103
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
89 104
         self.assertEqual(InstanceStatus.BUILD, instance.status)
90 105
 
91 106
     def test_nova_server_error_reports_error(self):
92
-        fake_db_info = FakeDBInstance()
93
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
94
-        fake_db_info.server_status = "ERROR"
95
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
107
+        self.db_info.server_status = "ERROR"
108
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
96 109
         self.assertEqual(InstanceStatus.ERROR, instance.status)
97 110
 
98 111
     def test_nova_server_reboot_reports_reboot(self):
99
-        fake_db_info = FakeDBInstance()
100
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
101
-        fake_db_info.server_status = "REBOOT"
102
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
112
+        self.db_info.server_status = "REBOOT"
113
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
103 114
         self.assertEqual(InstanceStatus.REBOOT, instance.status)
104 115
 
105 116
     def test_nova_server_resize_reports_resize(self):
106
-        fake_db_info = FakeDBInstance()
107
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
108
-        fake_db_info.server_status = "RESIZE"
109
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
117
+        self.db_info.server_status = "RESIZE"
118
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
110 119
         self.assertEqual(InstanceStatus.RESIZE, instance.status)
111 120
 
112 121
     def test_nova_server_verify_resize_reports_resize(self):
113
-        fake_db_info = FakeDBInstance()
114
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
115
-        fake_db_info.server_status = "VERIFY_RESIZE"
116
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
122
+        self.db_info.server_status = "VERIFY_RESIZE"
123
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
117 124
         self.assertEqual(InstanceStatus.RESIZE, instance.status)
118 125
 
119 126
     def test_service_status_paused_reports_reboot(self):
120
-        fake_db_info = FakeDBInstance()
121
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
122
-        fake_status.set_status(ServiceStatuses.PAUSED)
123
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
127
+        self.status.set_status(ServiceStatuses.PAUSED)
128
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
124 129
         self.assertEqual(InstanceStatus.REBOOT, instance.status)
125 130
 
126 131
     def test_service_status_new_reports_build(self):
127
-        fake_db_info = FakeDBInstance()
128
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
129
-        fake_status.set_status(ServiceStatuses.NEW)
130
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
132
+        self.status.set_status(ServiceStatuses.NEW)
133
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
131 134
         self.assertEqual(InstanceStatus.BUILD, instance.status)
132 135
 
133 136
     def test_service_status_running_reports_active(self):
134
-        fake_db_info = FakeDBInstance()
135
-        fake_status = InstanceServiceStatus(ServiceStatuses.RUNNING)
136
-        fake_status.set_status(ServiceStatuses.RUNNING)
137
-        instance = SimpleInstance('dummy context', fake_db_info, fake_status)
137
+        self.status.set_status(ServiceStatuses.RUNNING)
138
+        instance = SimpleInstance('dummy context', self.db_info, self.status)
138 139
         self.assertEqual(InstanceStatus.ACTIVE, instance.status)

+ 175
- 174
trove/tests/unittests/mgmt/test_models.py View File

@@ -13,6 +13,7 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 #
16
+import uuid
16 17
 from mock import MagicMock, patch, ANY
17 18
 from testtools import TestCase
18 19
 from testtools.matchers import Equals, Is, Not
@@ -25,21 +26,40 @@ from trove.backup.models import Backup
25 26
 from trove.common.context import TroveContext
26 27
 from trove.common import instance as rd_instance
27 28
 from trove.datastore import models as datastore_models
28
-from trove.db.models import DatabaseModelBase
29 29
 from trove.instance.models import DBInstance
30 30
 from trove.instance.models import InstanceServiceStatus
31 31
 from trove.instance.tasks import InstanceTasks
32 32
 import trove.extensions.mgmt.instances.models as mgmtmodels
33 33
 from trove.openstack.common.notifier import api as notifier
34 34
 from trove.common import remote
35
-from trove.tests.util import test_config
35
+from trove.tests.unittests.util import util
36 36
 
37 37
 CONF = cfg.CONF
38 38
 
39 39
 
40 40
 class MockMgmtInstanceTest(TestCase):
41
+
42
+    @classmethod
43
+    def setUpClass(cls):
44
+        util.init_db()
45
+        cls.version_id = str(uuid.uuid4())
46
+        cls.datastore = datastore_models.DBDatastore.create(
47
+            id=str(uuid.uuid4()),
48
+            name='mysql',
49
+            default_version_id=cls.version_id
50
+        )
51
+        cls.version = datastore_models.DBDatastoreVersion.create(
52
+            id=cls.version_id,
53
+            datastore_id=cls.datastore.id,
54
+            name='5.5',
55
+            manager='mysql',
56
+            image_id=str(uuid.uuid4()),
57
+            active=1,
58
+            packages="mysql-server-5.5"
59
+        )
60
+        super(MockMgmtInstanceTest, cls).setUpClass()
61
+
41 62
     def setUp(self):
42
-        super(MockMgmtInstanceTest, self).setUp()
43 63
         self.context = TroveContext()
44 64
         self.context.auth_token = 'some_secret_password'
45 65
         self.client = MagicMock(spec=Client)
@@ -53,63 +73,59 @@ class MockMgmtInstanceTest(TestCase):
53 73
         CONF.set_override('report_interval', 20)
54 74
         CONF.set_override('notification_service_id', {'mysql': '123'})
55 75
 
56
-    def tearDown(self):
57
-        super(MockMgmtInstanceTest, self).tearDown()
76
+        super(MockMgmtInstanceTest, self).setUp()
58 77
 
59
-    @staticmethod
60
-    def build_db_instance(status, task_status=InstanceTasks.DELETING):
61
-        return DBInstance(task_status,
62
-                          created='xyz',
63
-                          name='test_name',
64
-                          id='1',
65
-                          flavor_id='flavor_1',
66
-                          datastore_version_id=
67
-                          test_config.dbaas_datastore_version_id,
68
-                          compute_instance_id='compute_id_1',
69
-                          server_id='server_id_1',
70
-                          tenant_id='tenant_id_1',
71
-                          server_status=status)
78
+    def do_cleanup(self, instance, status):
79
+        instance.delete()
80
+        status.delete()
81
+
82
+    def build_db_instance(self, status, task_status=InstanceTasks.NONE):
83
+        version = datastore_models.DBDatastoreVersion.get_by(name='5.5')
84
+        instance = DBInstance(InstanceTasks.NONE,
85
+                              name='test_name',
86
+                              id=str(uuid.uuid4()),
87
+                              flavor_id='flavor_1',
88
+                              datastore_version_id=
89
+                              version.id,
90
+                              compute_instance_id='compute_id_1',
91
+                              server_id='server_id_1',
92
+                              tenant_id='tenant_id_1',
93
+                              server_status=
94
+                              rd_instance.ServiceStatuses.
95
+                              BUILDING.api_status,
96
+                              deleted=False)
97
+        instance.save()
98
+        service_status = InstanceServiceStatus(
99
+            rd_instance.ServiceStatuses.RUNNING,
100
+            id=str(uuid.uuid4()),
101
+            instance_id=instance.id,
102
+        )
103
+        service_status.save()
104
+        instance.set_task_status(task_status)
105
+        instance.server_status = status
106
+        instance.save()
107
+        return instance, service_status
72 108
 
73 109
 
74 110
 class TestNotificationTransformer(MockMgmtInstanceTest):
75 111
 
112
+    @classmethod
113
+    def setUpClass(cls):
114
+        super(TestNotificationTransformer, cls).setUpClass()
115
+
76 116
     def test_tranformer(self):
77
-        transformer = mgmtmodels.NotificationTransformer(context=self.context)
78 117
         status = rd_instance.ServiceStatuses.BUILDING.api_status
79
-        db_instance = MockMgmtInstanceTest.build_db_instance(
118
+        instance, service_status = self.build_db_instance(
80 119
             status, InstanceTasks.BUILDING)
81
-
82
-        with patch.object(DatabaseModelBase, 'find_all',
83
-                          return_value=[db_instance]):
84
-            stub_dsv_db_info = MagicMock(
85
-                spec=datastore_models.DBDatastoreVersion)
86
-            stub_dsv_db_info.id = "test_datastore_version"
87
-            stub_dsv_db_info.datastore_id = "mysql_test_version"
88
-            stub_dsv_db_info.name = "test_datastore_name"
89
-            stub_dsv_db_info.image_id = "test_datastore_image_id"
90
-            stub_dsv_db_info.packages = "test_datastore_pacakges"
91
-            stub_dsv_db_info.active = 1
92
-            stub_dsv_db_info.manager = "mysql"
93
-            stub_datastore_version = datastore_models.DatastoreVersion(
94
-                stub_dsv_db_info)
95
-
96
-            def side_effect_func(*args, **kwargs):
97
-                if 'instance_id' in kwargs:
98
-                    return InstanceServiceStatus(
99
-                        rd_instance.ServiceStatuses.BUILDING)
100
-                else:
101
-                    return stub_datastore_version
102
-
103
-            with patch.object(DatabaseModelBase, 'find_by',
104
-                              side_effect=side_effect_func):
105
-                payloads = transformer()
106
-                self.assertIsNotNone(payloads)
107
-                self.assertThat(len(payloads), Equals(1))
108
-                payload = payloads[0]
109
-                self.assertThat(payload['audit_period_beginning'],
110
-                                Not(Is(None)))
111
-                self.assertThat(payload['audit_period_ending'], Not(Is(None)))
112
-                self.assertThat(payload['state'], Equals(status.lower()))
120
+        payloads = mgmtmodels.NotificationTransformer(
121
+            context=self.context)()
122
+        self.assertIsNotNone(payloads)
123
+        payload = payloads[0]
124
+        self.assertThat(payload['audit_period_beginning'],
125
+                        Not(Is(None)))
126
+        self.assertThat(payload['audit_period_ending'], Not(Is(None)))
127
+        self.assertTrue(status.lower() in [db['state'] for db in payloads])
128
+        self.addCleanup(self.do_cleanup, instance, service_status)
113 129
 
114 130
     def test_get_service_id(self):
115 131
         id_map = {
@@ -131,6 +147,11 @@ class TestNotificationTransformer(MockMgmtInstanceTest):
131 147
 
132 148
 
133 149
 class TestNovaNotificationTransformer(MockMgmtInstanceTest):
150
+
151
+    @classmethod
152
+    def setUpClass(cls):
153
+        super(TestNovaNotificationTransformer, cls).setUpClass()
154
+
134 155
     def test_transformer_cache(self):
135 156
         flavor = MagicMock(spec=Flavor)
136 157
         flavor.name = 'db.small'
@@ -145,9 +166,9 @@ class TestNovaNotificationTransformer(MockMgmtInstanceTest):
145 166
     def test_lookup_flavor(self):
146 167
         flavor = MagicMock(spec=Flavor)
147 168
         flavor.name = 'flav_1'
169
+        transformer = mgmtmodels.NovaNotificationTransformer(
170
+            context=self.context)
148 171
         with patch.object(self.flavor_mgr, 'get', side_effect=[flavor, None]):
149
-            transformer = mgmtmodels.NovaNotificationTransformer(
150
-                context=self.context)
151 172
             self.assertThat(transformer._lookup_flavor('1'),
152 173
                             Equals(flavor.name))
153 174
             self.assertThat(transformer._lookup_flavor('2'),
@@ -155,188 +176,160 @@ class TestNovaNotificationTransformer(MockMgmtInstanceTest):
155 176
 
156 177
     def test_tranformer(self):
157 178
         status = rd_instance.ServiceStatuses.BUILDING.api_status
158
-        db_instance = MockMgmtInstanceTest.build_db_instance(
159
-            status, task_status=InstanceTasks.BUILDING)
160
-
161
-        stub_dsv_db_info = MagicMock(spec=datastore_models.DBDatastoreVersion)
162
-        stub_dsv_db_info.id = "test_datastore_version"
163
-        stub_dsv_db_info.datastore_id = "mysql_test_version"
164
-        stub_dsv_db_info.name = "test_datastore_name"
165
-        stub_dsv_db_info.image_id = "test_datastore_image_id"
166
-        stub_dsv_db_info.packages = "test_datastore_pacakges"
167
-        stub_dsv_db_info.active = 1
168
-        stub_dsv_db_info.manager = "mysql"
169
-        stub_datastore_version = datastore_models.DatastoreVersion(
170
-            stub_dsv_db_info)
179
+        instance, service_status = self.build_db_instance(
180
+            status, InstanceTasks.BUILDING)
171 181
 
172 182
         flavor = MagicMock(spec=Flavor)
173 183
         flavor.name = 'db.small'
174 184
 
175 185
         server = MagicMock(spec=Server)
176 186
         server.user_id = 'test_user_id'
187
+        transformer = mgmtmodels.NovaNotificationTransformer(
188
+            context=self.context)
177 189
         mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
178
-                                                      db_instance,
190
+                                                      instance,
179 191
                                                       server,
180
-                                                      None)
181
-
182
-        with patch.object(DatabaseModelBase, 'find_by',
183
-                          return_value=stub_datastore_version):
192
+                                                      service_status)
184 193
 
185
-            with patch.object(mgmtmodels, 'load_mgmt_instances',
186
-                              return_value=[mgmt_instance]):
187
-
188
-                with patch.object(self.flavor_mgr, 'get', return_value=flavor):
194
+        with patch.object(mgmtmodels, 'load_mgmt_instances',
195
+                          return_value=[mgmt_instance]):
196
+            with patch.object(self.flavor_mgr, 'get', return_value=flavor):
189 197
 
190
-                    # invocation
191
-                    transformer = mgmtmodels.NovaNotificationTransformer(
192
-                        context=self.context)
193
-                    payloads = transformer()
198
+                payloads = transformer()
194 199
 
195
-                    # assertions
196
-                    self.assertIsNotNone(payloads)
197
-                    self.assertThat(len(payloads), Equals(1))
198
-                    payload = payloads[0]
199
-                    self.assertThat(payload['audit_period_beginning'],
200
-                                    Not(Is(None)))
201
-                    self.assertThat(payload['audit_period_ending'],
202
-                                    Not(Is(None)))
203
-                    self.assertThat(payload['state'], Equals(status.lower()))
204
-                    self.assertThat(payload['instance_type'],
205
-                                    Equals('db.small'))
206
-                    self.assertThat(payload['instance_type_id'],
207
-                                    Equals('flavor_1'))
208
-                    self.assertThat(payload['user_id'], Equals('test_user_id'))
209
-                    self.assertThat(payload['service_id'], Equals('123'))
200
+                self.assertIsNotNone(payloads)
201
+                payload = payloads[0]
202
+                self.assertThat(payload['audit_period_beginning'],
203
+                                Not(Is(None)))
204
+                self.assertThat(payload['audit_period_ending'],
205
+                                Not(Is(None)))
206
+                self.assertThat(payload['state'], Not(Is(None)))
207
+                self.assertThat(payload['instance_type'],
208
+                                Equals('db.small'))
209
+                self.assertThat(payload['instance_type_id'],
210
+                                Equals('flavor_1'))
211
+                self.assertThat(payload['user_id'], Equals('test_user_id'))
212
+                self.assertThat(payload['service_id'], Equals('123'))
213
+        self.addCleanup(self.do_cleanup, instance, service_status)
210 214
 
211 215
     def test_tranformer_invalid_datastore_manager(self):
212 216
         status = rd_instance.ServiceStatuses.BUILDING.api_status
213
-        db_instance = MockMgmtInstanceTest.build_db_instance(
214
-            status, task_status=InstanceTasks.BUILDING)
215
-
217
+        instance, service_status = self.build_db_instance(
218
+            status, InstanceTasks.BUILDING)
219
+        version = datastore_models.DBDatastoreVersion.get_by(
220
+            id=instance.datastore_version_id)
221
+        version.update(manager='something invalid')
216 222
         server = MagicMock(spec=Server)
217 223
         server.user_id = 'test_user_id'
218
-        stub_datastore_version = MagicMock()
219
-        stub_datastore_version.id = "stub_datastore_version"
220
-        stub_datastore_version.manager = "m0ng0"
221
-        stub_datastore = MagicMock()
222
-        stub_datastore.default_datastore_version = "stub_datastore_version"
223 224
 
224 225
         flavor = MagicMock(spec=Flavor)
225 226
         flavor.name = 'db.small'
226 227
 
227
-        with patch.object(datastore_models.DatastoreVersion, 'load',
228
-                          return_value=stub_datastore_version):
229
-            with patch.object(datastore_models.DatastoreVersion,
230
-                              'load_by_uuid',
231
-                              return_value=stub_datastore_version):
232
-                with patch.object(datastore_models.Datastore, 'load',
233
-                                  return_value=stub_datastore):
234
-                    mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
235
-                                                                  db_instance,
236
-                                                                  server,
237
-                                                                  None)
238
-                    with patch.object(mgmtmodels, 'load_mgmt_instances',
239
-                                      return_value=[mgmt_instance]):
240
-                        with patch.object(self.flavor_mgr,
241
-                                          'get', return_value=flavor):
242
-
243
-                            # invocation
244
-                            transformer = (
245
-                                mgmtmodels.NovaNotificationTransformer(
246
-                                    context=self.context)
247
-                            )
248
-
249
-                            payloads = transformer()
250
-                            # assertions
251
-                            self.assertIsNotNone(payloads)
252
-                            self.assertThat(len(payloads), Equals(1))
253
-                            payload = payloads[0]
254
-                            self.assertThat(payload['audit_period_beginning'],
255
-                                            Not(Is(None)))
256
-                            self.assertThat(payload['audit_period_ending'],
257
-                                            Not(Is(None)))
258
-                            self.assertThat(payload['state'],
259
-                                            Equals(status.lower()))
260
-                            self.assertThat(payload['instance_type'],
261
-                                            Equals('db.small'))
262
-                            self.assertThat(payload['instance_type_id'],
263
-                                            Equals('flavor_1'))
264
-                            self.assertThat(payload['user_id'],
265
-                                            Equals('test_user_id'))
266
-                            self.assertThat(payload['service_id'],
267
-                                            Equals('unknown-service-id-error'))
228
+        mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
229
+                                                      instance,
230
+                                                      server,
231
+                                                      service_status)
232
+        transformer = mgmtmodels.NovaNotificationTransformer(
233
+            context=self.context)
234
+        with patch.object(mgmtmodels, 'load_mgmt_instances',
235
+                          return_value=[mgmt_instance]):
236
+            with patch.object(self.flavor_mgr,
237
+                              'get', return_value=flavor):
238
+                payloads = transformer()
239
+                # assertions
240
+                self.assertIsNotNone(payloads)
241
+                payload = payloads[0]
242
+                self.assertThat(payload['audit_period_beginning'],
243
+                                Not(Is(None)))
244
+                self.assertThat(payload['audit_period_ending'],
245
+                                Not(Is(None)))
246
+                self.assertIn(status.lower(),
247
+                              [db['state']
248
+                              for db in payloads])
249
+                self.assertThat(payload['instance_type'],
250
+                                Equals('db.small'))
251
+                self.assertThat(payload['instance_type_id'],
252
+                                Equals('flavor_1'))
253
+                self.assertThat(payload['user_id'],
254
+                                Equals('test_user_id'))
255
+                self.assertThat(payload['service_id'],
256
+                                Equals('unknown-service-id-error'))
257
+        version.update(manager='mysql')
258
+        self.addCleanup(self.do_cleanup, instance, service_status)
268 259
 
269 260
     def test_tranformer_shutdown_instance(self):
270 261
         status = rd_instance.ServiceStatuses.SHUTDOWN.api_status
271
-        db_instance = self.build_db_instance(status)
272
-
262
+        instance, service_status = self.build_db_instance(status)
263
+        service_status.set_status(rd_instance.ServiceStatuses.SHUTDOWN)
273 264
         server = MagicMock(spec=Server)
274 265
         server.user_id = 'test_user_id'
266
+
275 267
         mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
276
-                                                      db_instance,
268
+                                                      instance,
277 269
                                                       server,
278
-                                                      None)
270
+                                                      service_status)
279 271
         flavor = MagicMock(spec=Flavor)
280 272
         flavor.name = 'db.small'
281
-
273
+        transformer = mgmtmodels.NovaNotificationTransformer(
274
+            context=self.context)
282 275
         with patch.object(Backup, 'running', return_value=None):
283 276
             self.assertThat(mgmt_instance.status, Equals('SHUTDOWN'))
284 277
             with patch.object(mgmtmodels, 'load_mgmt_instances',
285 278
                               return_value=[mgmt_instance]):
286 279
                 with patch.object(self.flavor_mgr, 'get', return_value=flavor):
287
-                    # invocation
288
-                    transformer = mgmtmodels.NovaNotificationTransformer(
289
-                        context=self.context)
290 280
                     payloads = transformer()
291 281
                     # assertion that SHUTDOWN instances are not reported
292 282
                     self.assertIsNotNone(payloads)
293
-                    self.assertThat(len(payloads), Equals(0))
283
+                    self.assertNotIn(status.lower(),
284
+                                     [db['status']
285
+                                      for db in payloads])
286
+        self.addCleanup(self.do_cleanup, instance, service_status)
294 287
 
295 288
     def test_tranformer_no_nova_instance(self):
296 289
         status = rd_instance.ServiceStatuses.SHUTDOWN.api_status
297
-        db_instance = MockMgmtInstanceTest.build_db_instance(status)
298
-
290
+        instance, service_status = self.build_db_instance(status)
291
+        service_status.set_status(rd_instance.ServiceStatuses.SHUTDOWN)
299 292
         mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
300
-                                                      db_instance,
293
+                                                      instance,
301 294
                                                       None,
302
-                                                      None)
295
+                                                      service_status)
303 296
         flavor = MagicMock(spec=Flavor)
304 297
         flavor.name = 'db.small'
305
-
298
+        transformer = mgmtmodels.NovaNotificationTransformer(
299
+            context=self.context)
306 300
         with patch.object(Backup, 'running', return_value=None):
307 301
             self.assertThat(mgmt_instance.status, Equals('SHUTDOWN'))
308 302
             with patch.object(mgmtmodels, 'load_mgmt_instances',
309 303
                               return_value=[mgmt_instance]):
310 304
                 with patch.object(self.flavor_mgr, 'get', return_value=flavor):
311
-                    # invocation
312
-                    transformer = mgmtmodels.NovaNotificationTransformer(
313
-                        context=self.context)
314 305
                     payloads = transformer()
315 306
                     # assertion that SHUTDOWN instances are not reported
316 307
                     self.assertIsNotNone(payloads)
317
-                    self.assertThat(len(payloads), Equals(0))
308
+                    self.assertNotIn(status.lower(),
309
+                                     [db['status']
310
+                                      for db in payloads])
311
+        self.addCleanup(self.do_cleanup, instance, service_status)
318 312
 
319 313
     def test_tranformer_flavor_cache(self):
320 314
         status = rd_instance.ServiceStatuses.BUILDING.api_status
321
-        db_instance = MockMgmtInstanceTest.build_db_instance(
315
+        instance, service_status = self.build_db_instance(
322 316
             status, InstanceTasks.BUILDING)
323 317
 
324 318
         server = MagicMock(spec=Server)
325 319
         server.user_id = 'test_user_id'
326 320
         mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
327
-                                                      db_instance,
321
+                                                      instance,
328 322
                                                       server,
329
-                                                      None)
323
+                                                      service_status)
330 324
         flavor = MagicMock(spec=Flavor)
331 325
         flavor.name = 'db.small'
332
-
326
+        transformer = mgmtmodels.NovaNotificationTransformer(
327
+            context=self.context)
333 328
         with patch.object(mgmtmodels, 'load_mgmt_instances',
334 329
                           return_value=[mgmt_instance]):
335 330
             with patch.object(self.flavor_mgr, 'get', return_value=flavor):
336
-                transformer = mgmtmodels.NovaNotificationTransformer(
337
-                    context=self.context)
331
+
338 332
                 transformer()
339
-                # call twice ensure client.flavor invoked once
340 333
                 payloads = transformer()
341 334
                 self.assertIsNotNone(payloads)
342 335
                 self.assertThat(len(payloads), Equals(1))
@@ -344,27 +337,34 @@ class TestNovaNotificationTransformer(MockMgmtInstanceTest):
344 337
                 self.assertThat(payload['audit_period_beginning'],
345 338
                                 Not(Is(None)))
346 339
                 self.assertThat(payload['audit_period_ending'], Not(Is(None)))
347
-                self.assertThat(payload['state'], Equals(status.lower()))
340
+                self.assertIn(status.lower(),
341
+                              [db['state']
342
+                              for db in payloads])
348 343
                 self.assertThat(payload['instance_type'], Equals('db.small'))
349 344
                 self.assertThat(payload['instance_type_id'],
350 345
                                 Equals('flavor_1'))
351 346
                 self.assertThat(payload['user_id'], Equals('test_user_id'))
352 347
                 # ensure cache was used to get flavor second time
353 348
                 self.flavor_mgr.get.assert_any_call('flavor_1')
349
+        self.addCleanup(self.do_cleanup, instance, service_status)
354 350
 
355 351
 
356 352
 class TestMgmtInstanceTasks(MockMgmtInstanceTest):
353
+
354
+    @classmethod
355
+    def setUpClass(cls):
356
+        super(TestMgmtInstanceTasks, cls).setUpClass()
357
+
357 358
     def test_public_exists_events(self):
358 359
         status = rd_instance.ServiceStatuses.BUILDING.api_status
359
-        db_instance = MockMgmtInstanceTest.build_db_instance(
360
+        instance, service_status = self.build_db_instance(
360 361
             status, task_status=InstanceTasks.BUILDING)
361
-
362 362
         server = MagicMock(spec=Server)
363 363
         server.user_id = 'test_user_id'
364 364
         mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context,
365
-                                                      db_instance,
365
+                                                      instance,
366 366
                                                       server,
367
-                                                      None)
367
+                                                      service_status)
368 368
 
369 369
         flavor = MagicMock(spec=Flavor)
370 370
         flavor.name = 'db.small'
@@ -387,3 +387,4 @@ class TestMgmtInstanceTasks(MockMgmtInstanceTest):
387 387
                                                     'INFO',
388 388
                                                     ANY)
389 389
                     self.assertThat(self.context.auth_token, Is(None))
390
+        self.addCleanup(self.do_cleanup, instance, service_status)

Loading…
Cancel
Save