def test_transformer_cache(self): flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' with patch.object(self.flavor_mgr, 'get', return_value=flavor): transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) transformer2 = mgmtmodels.NovaNotificationTransformer( context=self.context) self.assertThat(transformer._flavor_cache, Not(Is(transformer2._flavor_cache)))
def test_transformer_cache(self): flavor = mock(Flavor) flavor.name = 'db.small' when(self.flavor_mgr).get('flavor_1').thenReturn(flavor) transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) transformer2 = mgmtmodels.NovaNotificationTransformer( context=self.context) self.assertThat(transformer._flavor_cache, Not(Is(transformer2._flavor_cache)))
def test_tranformer(self): status = rd_instance.ServiceStatuses.BUILDING.api_status instance, service_status = self.build_db_instance( status, InstanceTasks.BUILDING) flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' server = MagicMock(spec=Server) server.user_id = 'test_user_id' transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, instance, server, service_status) with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): payloads = transformer() self.assertIsNotNone(payloads) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertThat(payload['state'], Not(Is(None))) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) self.assertThat(payload['service_id'], Equals('123')) self.addCleanup(self.do_cleanup, instance, service_status)
def test_tranformer_flavor_cache(self): status = rd_instance.ServiceStatuses.BUILDING.api_status db_instance = MockMgmtInstanceTest.build_db_instance( status, InstanceTasks.BUILDING) server = mock(Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, server, None) when(mgmtmodels).load_mgmt_instances( self.context, deleted=False, client=self.client).thenReturn( [mgmt_instance]) flavor = mock(Flavor) flavor.name = 'db.small' when(self.flavor_mgr).get('flavor_1').thenReturn(flavor) transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) transformer() # call twice ensure client.flavor invoked once payloads = transformer() self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(1)) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertThat(payload['state'], Equals(status.lower())) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) # ensure cache was used to get flavor second time verify(self.flavor_mgr).get('flavor_1')
def test_tranformer_no_nova_instance(self): status = rd_instance.ServiceStatuses.SHUTDOWN.api_status db_instance = MockMgmtInstanceTest.build_db_instance(status) mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, None, None) when(Backup).running('1').thenReturn(None) self.assertThat(mgmt_instance.status, Equals('SHUTDOWN')) when(mgmtmodels).load_mgmt_instances( self.context, deleted=False, client=self.client).thenReturn( [mgmt_instance]) flavor = mock(Flavor) flavor.name = 'db.small' when(self.flavor_mgr).get('flavor_1').thenReturn(flavor) # invocation transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) payloads = transformer() # assertion that SHUTDOWN instances are not reported self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(0))
def test_tranformer_flavor_cache(self): status = rd_instance.ServiceStatuses.BUILDING.api_status instance, service_status = self.build_db_instance( status, InstanceTasks.BUILDING) server = MagicMock(spec=Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, instance, server, service_status) flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): transformer() payloads = transformer() self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(1)) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertIn(status.lower(), [db['state'] for db in payloads]) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) # ensure cache was used to get flavor second time self.flavor_mgr.get.assert_any_call('flavor_1') self.addCleanup(self.do_cleanup, instance, service_status)
def test_public_exists_events(self): status = rd_instance.ServiceStatuses.BUILDING.api_status db_instance = MockMgmtInstanceTest.build_db_instance( status, task_status=InstanceTasks.BUILDING) server = mock(Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, server, None) when(mgmtmodels).load_mgmt_instances(self.context, deleted=False, client=self.client).thenReturn([ mgmt_instance, mgmt_instance ]) flavor = mock(Flavor) flavor.name = 'db.small' when(self.flavor_mgr).get('flavor_1').thenReturn(flavor) self.assertThat(self.context.auth_token, Is('some_secret_password')) when(notifier).notify(self.context, any(str), 'trove.instance.exists', 'INFO', any(dict)).thenReturn(None) # invocation mgmtmodels.publish_exist_events( mgmtmodels.NovaNotificationTransformer(context=self.context), self.context) # assertion verify(notifier, times=2).notify(self.context, any(str), 'trove.instance.exists', 'INFO', any(dict)) self.assertThat(self.context.auth_token, Is(None))
def test_tranformer_invalid_service_type(self): status = rd_instance.ServiceStatuses.BUILDING.api_status db_instance = MockMgmtInstanceTest.build_db_instance( status, task_status=InstanceTasks.BUILDING) db_instance.service_type = 'm0ng0' server = mock(Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, server, None) when(mgmtmodels).load_mgmt_instances(self.context, deleted=False, client=self.client).thenReturn( [mgmt_instance]) flavor = mock(Flavor) flavor.name = 'db.small' when(self.flavor_mgr).get('flavor_1').thenReturn(flavor) # invocation transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) payloads = transformer() # assertions self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(1)) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertThat(payload['state'], Equals(status.lower())) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) self.assertThat(payload['service_id'], Equals('unknown-service-id-error'))
def test_tranformer_shutdown_instance(self): status = rd_instance.ServiceStatuses.SHUTDOWN.api_status db_instance = self.build_db_instance(status) server = MagicMock(spec=Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, server, None) flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' with patch.object(Backup, 'running', return_value=None): self.assertThat(mgmt_instance.status, Equals('SHUTDOWN')) with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): # invocation transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) payloads = transformer() # assertion that SHUTDOWN instances are not reported self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(0))
def test_tranformer_shutdown_instance(self): status = rd_instance.ServiceStatuses.SHUTDOWN.api_status instance, service_status = self.build_db_instance(status) service_status.set_status(rd_instance.ServiceStatuses.SHUTDOWN) server = MagicMock(spec=Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, instance, server, service_status) flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) with patch.object(Backup, 'running', return_value=None): self.assertThat(mgmt_instance.status, Equals('SHUTDOWN')) with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): payloads = transformer() # assertion that SHUTDOWN instances are not reported self.assertIsNotNone(payloads) self.assertNotIn(status.lower(), [db['status'] for db in payloads]) self.addCleanup(self.do_cleanup, instance, service_status)
def test_public_exists_events(self): status = rd_instance.ServiceStatuses.BUILDING.api_status instance, service_status = self.build_db_instance( status, task_status=InstanceTasks.BUILDING) server = MagicMock(spec=Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, instance, server, service_status) flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' notifier = MagicMock() with patch.object(rpc, 'get_notifier', return_value=notifier): with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): self.assertThat(self.context.auth_token, Is('some_secret_password')) with patch.object(notifier, 'info', return_value=None): # invocation mgmtmodels.publish_exist_events( mgmtmodels.NovaNotificationTransformer( context=self.context), self.context) # assertion notifier.info.assert_any_call(self.context, 'trove.instance.exists', ANY) self.assertThat(self.context.auth_token, Is(None)) self.addCleanup(self.do_cleanup, instance, service_status)
def test_lookup_flavor(self): flavor = mock(Flavor) flavor.name = 'flav_1' when(self.flavor_mgr).get('1').thenReturn(flavor) transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) self.assertThat(transformer._lookup_flavor('1'), Equals(flavor.name)) self.assertThat(transformer._lookup_flavor('2'), Equals('unknown'))
def test_tranformer_invalid_datastore_manager(self): status = rd_instance.ServiceStatuses.BUILDING.api_status db_instance = MockMgmtInstanceTest.build_db_instance( status, task_status=InstanceTasks.BUILDING) server = MagicMock(spec=Server) server.user_id = 'test_user_id' stub_datastore_version = MagicMock() stub_datastore_version.id = "stub_datastore_version" stub_datastore_version.manager = "m0ng0" stub_datastore = MagicMock() stub_datastore.default_datastore_version = "stub_datastore_version" flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' with patch.object(datastore_models.DatastoreVersion, 'load', return_value=stub_datastore_version): with patch.object(datastore_models.DatastoreVersion, 'load_by_uuid', return_value=stub_datastore_version): with patch.object(datastore_models.Datastore, 'load', return_value=stub_datastore): mgmt_instance = mgmtmodels.SimpleMgmtInstance( self.context, db_instance, server, None) with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): # invocation transformer = ( mgmtmodels.NovaNotificationTransformer( context=self.context)) payloads = transformer() # assertions self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(1)) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertThat(payload['state'], Equals(status.lower())) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) self.assertThat(payload['service_id'], Equals('unknown-service-id-error'))
def test_lookup_flavor(self): flavor = MagicMock(spec=Flavor) flavor.name = 'flav_1' transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) with patch.object(self.flavor_mgr, 'get', side_effect=[flavor, None]): self.assertThat(transformer._lookup_flavor('1'), Equals(flavor.name)) self.assertThat(transformer._lookup_flavor('2'), Equals('unknown'))
def test_tranformer(self): status = rd_instance.ServiceStatuses.BUILDING.api_status db_instance = MockMgmtInstanceTest.build_db_instance( status, task_status=InstanceTasks.BUILDING) stub_dsv_db_info = MagicMock(spec=datastore_models.DBDatastoreVersion) stub_dsv_db_info.id = "test_datastore_version" stub_dsv_db_info.datastore_id = "mysql_test_version" stub_dsv_db_info.name = "test_datastore_name" stub_dsv_db_info.image_id = "test_datastore_image_id" stub_dsv_db_info.packages = "test_datastore_pacakges" stub_dsv_db_info.active = 1 stub_dsv_db_info.manager = "mysql" stub_datastore_version = datastore_models.DatastoreVersion( stub_dsv_db_info) flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' server = MagicMock(spec=Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, server, None) with patch.object(DatabaseModelBase, 'find_by', return_value=stub_datastore_version): with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): # invocation transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) payloads = transformer() # assertions self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(1)) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertThat(payload['state'], Equals(status.lower())) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) self.assertThat(payload['service_id'], Equals('123'))
def test_tranformer(self): status = rd_instance.ServiceStatuses.BUILDING.api_status db_instance = MockMgmtInstanceTest.build_db_instance( status, task_status=InstanceTasks.BUILDING) stub_dsv_db_info = mock(datastore_models.DBDatastoreVersion) stub_dsv_db_info.id = "test_datastore_version" stub_dsv_db_info.datastore_id = "mysql_test_version" stub_dsv_db_info.name = "test_datastore_name" stub_dsv_db_info.image_id = "test_datastore_image_id" stub_dsv_db_info.packages = "test_datastore_pacakges" stub_dsv_db_info.active = 1 stub_dsv_db_info.manager = "mysql" stub_datastore_version = datastore_models.DatastoreVersion( stub_dsv_db_info) when(DatabaseModelBase).find_by(id=any()).thenReturn( stub_datastore_version) server = mock(Server) server.user_id = 'test_user_id' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, db_instance, server, None) when(mgmtmodels).load_mgmt_instances( self.context, deleted=False, client=self.client).thenReturn( [mgmt_instance]) flavor = mock(Flavor) flavor.name = 'db.small' when(self.flavor_mgr).get('flavor_1').thenReturn(flavor) # invocation transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) payloads = transformer() # assertions self.assertIsNotNone(payloads) self.assertThat(len(payloads), Equals(1)) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertThat(payload['state'], Equals(status.lower())) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) self.assertThat(payload['service_id'], Equals('123'))
def test_transformer_invalid_datastore_manager(self, mock_logging): status = rd_instance.ServiceStatuses.BUILDING.api_status instance, service_status = self.build_db_instance( status, InstanceTasks.BUILDING) version = datastore_models.DBDatastoreVersion.get_by( id=instance.datastore_version_id) version.update(manager='something invalid') server = MagicMock(spec=Server) server.user_id = 'test_user_id' flavor = MagicMock(spec=Flavor) flavor.name = 'db.small' mgmt_instance = mgmtmodels.SimpleMgmtInstance(self.context, instance, server, service_status) transformer = mgmtmodels.NovaNotificationTransformer( context=self.context) with patch.object(mgmtmodels, 'load_mgmt_instances', return_value=[mgmt_instance]): with patch.object(self.flavor_mgr, 'get', return_value=flavor): payloads = transformer() # assertions self.assertIsNotNone(payloads) payload = payloads[0] self.assertThat(payload['audit_period_beginning'], Not(Is(None))) self.assertThat(payload['audit_period_ending'], Not(Is(None))) self.assertIn(status.lower(), [db['state'] for db in payloads]) self.assertThat(payload['instance_type'], Equals('db.small')) self.assertThat(payload['instance_type_id'], Equals('flavor_1')) self.assertThat(payload['user_id'], Equals('test_user_id')) self.assertThat(payload['service_id'], Equals('unknown-service-id-error')) version.update(manager='mysql') self.addCleanup(self.do_cleanup, instance, service_status)