def test_manage_snapshot_ok(self, mock_db, mock_create_snapshot, mock_rpcapi): """Test successful manage volume execution. Tests for correct operation when valid arguments are passed in the request body. We ensure that cinder.volume.api.API.manage_existing got called with the correct arguments, and that we return the correct HTTP code to the caller. """ ctxt = context.RequestContext('admin', 'fake', True) mock_db.return_value = fake_service.fake_service_obj(ctxt) body = {'snapshot': {'volume_id': 'fake_volume_id', 'ref': 'fake_ref'}} res = self._get_resp(body) self.assertEqual(202, res.status_int, res) # Check the db.service_get_by_host_and_topic was called with correct # arguments. self.assertEqual(1, mock_db.call_count) args = mock_db.call_args[0] self.assertEqual('fake_host', args[1]) # Check the create_snapshot_in_db was called with correct arguments. self.assertEqual(1, mock_create_snapshot.call_count) args = mock_create_snapshot.call_args[0] self.assertEqual('fake_volume_id', args[1].get('id')) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual('fake_ref', args[2])
def test_manage_snapshot_ok(self, mock_db, mock_create_snapshot, mock_rpcapi): """Test successful manage volume execution. Tests for correct operation when valid arguments are passed in the request body. We ensure that cinder.volume.api.API.manage_existing got called with the correct arguments, and that we return the correct HTTP code to the caller. """ mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary='cinder-volume') body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': 'fake_ref'}} res = self._get_resp_post(body) self.assertEqual(202, res.status_int, res) # Check the db.service_get was called with correct arguments. mock_db.assert_called_once_with( mock.ANY, host='fake_host', binary='cinder-volume') # Check the create_snapshot_in_db was called with correct arguments. self.assertEqual(1, mock_create_snapshot.call_count) args = mock_create_snapshot.call_args[0] named_args = mock_create_snapshot.call_args[1] self.assertEqual(fake.VOLUME_ID, args[1].get('id')) # We should commit quota in cinder-volume layer for this operation. self.assertFalse(named_args['commit_quota']) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual('fake_ref', args[2])
def test_get_manageable_snapshots_is_down(self, mock_db, mock_is_up): mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt) res = self._get_resp_get('host_ok', False, True) self.assertEqual(http_client.BAD_REQUEST, res.status_int, res) self.assertEqual(exception.ServiceUnavailable.message, res.json['badRequest']['message']) self.assertTrue(mock_is_up.called)
def test_manage_snapshot_ok(self, mock_db, mock_create_snapshot, mock_rpcapi): """Test successful manage snapshot execution. Tests for correct operation when valid arguments are passed in the request body. We ensure that cinder.volume.api.API.manage_existing got called with the correct arguments, and that we return the correct HTTP code to the caller. """ mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary='cinder-volume') body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': 'fake_ref'}} res = self._get_resp_post(body) self.assertEqual(202, res.status_int, res) # Check the db.service_get was called with correct arguments. mock_db.assert_called_once_with(mock.ANY, host='fake_host', binary='cinder-volume') # Check the create_snapshot_in_db was called with correct arguments. self.assertEqual(1, mock_create_snapshot.call_count) args = mock_create_snapshot.call_args[0] named_args = mock_create_snapshot.call_args[1] self.assertEqual(fake.VOLUME_ID, args[1].get('id')) # We should commit quota in cinder-volume layer for this operation. self.assertFalse(named_args['commit_quota']) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual('fake_ref', args[2])
def test_get_manageable_snapshots_disabled(self, mock_db, mock_is_up): mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt, disabled=True) res = self._get_resp_get('host_ok', False, True) self.assertEqual(400, res.status_int, res) self.assertEqual(exception.ServiceUnavailable.message, res.json['badRequest']['message']) mock_is_up.assert_not_called()
def test_thaw_host(self): service = fake_service.fake_service_obj(self.context, host='fake_host', binary='cinder-volume') self._test_rpc_api('thaw_host', rpc_method='call', server='fake_host', service=service, retval=True)
def test_thaw_host(self): service = fake_service.fake_service_obj(self.context, host='fake_host', binary=constants.VOLUME_BINARY) self._test_rpc_api('thaw_host', rpc_method='call', server='fake_host', service=service, retval=True)
def test_failover(self, mock_get_all, mock_db_update, mock_failover): """Test replication failover.""" service = fake_service.fake_service_obj(self.context, binary='cinder-volume') mock_get_all.return_value = [service] mock_db_update.return_value = {'replication_status': 'enabled'} volume_api = cinder.volume.api.API() volume_api.failover(self.context, host=CONF.host, cluster_name=None) mock_failover.assert_called_once_with(self.context, service, None)
def test_thaw_host(self, mock_get_all, mock_db_update, mock_thaw): """Test replication thaw_host.""" service = fake_service.fake_service_obj(self.context, binary=constants.VOLUME_BINARY) mock_get_all.return_value = [service] mock_thaw.return_value = True volume_api = cinder.volume.api.API() volume_api.thaw_host(self.context, host=CONF.host, cluster_name=None) mock_thaw.assert_called_once_with(self.context, service)
def test_freeze_host(self, mock_get_all, mock_db_update, mock_freeze): """Test replication freeze_host.""" service = fake_service.fake_service_obj(self.context, binary='cinder-volume') mock_get_all.return_value = [service] mock_freeze.return_value = True volume_api = cinder.volume.api.API() volume_api.freeze_host(self.context, host=CONF.host, cluster_name=None) mock_freeze.assert_called_once_with(self.context, service)
def test_thaw_host(self, mock_get_all, mock_db_update, mock_thaw): """Test replication thaw_host.""" service = fake_service.fake_service_obj(self.context, binary='cinder-volume') mock_get_all.return_value = [service] mock_thaw.return_value = True volume_api = cinder.volume.api.API() volume_api.thaw_host(self.context, host=CONF.host, cluster_name=None) mock_thaw.assert_called_once_with(self.context, service)
def test_freeze_host(self, mock_get_all, mock_db_update, mock_freeze): """Test replication freeze_host.""" service = fake_service.fake_service_obj(self.context, binary=constants.VOLUME_BINARY) mock_get_all.return_value = [service] mock_freeze.return_value = True volume_api = cinder.volume.api.API() volume_api.freeze_host(self.context, host=CONF.host, cluster_name=None) mock_freeze.assert_called_once_with(self.context, service)
def test_lazy_loading_cluster_field(self, cluster_get): cluster_orm = fake_cluster.fake_cluster_orm(name='mycluster') cluster_get.return_value = cluster_orm cluster = objects.Cluster._from_db_object(self.context, objects.Cluster(), cluster_orm) service = fake_service.fake_service_obj(self.context, cluster_name='mycluster') self.assertEqual(cluster, service.cluster) cluster_get.assert_called_once_with(self.context, None, name='mycluster')
def test_manage_snapshot_is_down(self, mock_db, mock_create_snapshot, mock_rpcapi, mock_is_up): """Test manage snapshot failure due to down service.""" mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt) body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': 'fake_ref'}} res = self._get_resp_post(body) self.assertEqual(400, res.status_int, res) self.assertEqual(exception.ServiceUnavailable.message, res.json['badRequest']['message']) mock_create_snapshot.assert_not_called() mock_rpcapi.assert_not_called() self.assertTrue(mock_is_up.called)
def test_manage_snapshot_with_null_validate( self, mock_db, mock_create_snapshot, mock_rpcapi): mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary=constants.VOLUME_BINARY) body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': {'fake_key': 'fake_ref'}, 'name': None, 'description': None}} res = self._get_resp_post(body) self.assertEqual(http_client.ACCEPTED, res.status_int, res) self.assertIn('snapshot', jsonutils.loads(res.body))
def test_thaw_host_unexpected_status(self, mock_get_all, mock_db_update, mock_thaw): """Test replication thaw_host unexpected status.""" mock_get_all.return_value = [fake_service.fake_service_obj( self.context, binary='cinder-volume')] mock_db_update.return_value = None volume_api = cinder.volume.api.API() self.assertRaises(exception.InvalidInput, volume_api.thaw_host, self.context, host=CONF.host, cluster_name=None)
def test_failover_unexpected_status(self, mock_db_get_all, mock_db_update, mock_failover): """Test replication failover unexpected status.""" mock_db_get_all.return_value = [fake_service.fake_service_obj( self.context, binary=constants.VOLUME_BINARY)] mock_db_update.return_value = None volume_api = cinder.volume.api.API() self.assertRaises(exception.InvalidInput, volume_api.failover, self.context, host=CONF.host, cluster_name=None)
def test_manage_snapshot_route(self, mock_service_get, mock_create_snapshot, mock_rpcapi): """Test call to manage snapshot. There is currently no change between the API in contrib and the API in v3, so here we simply check that the call is routed properly, rather than copying all the tests. """ mock_service_get.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary='cinder-volume') body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': 'fake_ref'}} res = self._get_resp_post(body) self.assertEqual(202, res.status_int, res)
def test_manage_snapshot_ok_ref_as_string(self, mock_db, mock_create_snapshot, mock_rpcapi): mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary=constants.VOLUME_BINARY) body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': "string"}} res = self._get_resp_post(body) self.assertEqual(HTTPStatus.ACCEPTED, res.status_int, res) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual(body['snapshot']['ref'], args[3])
def test_manage_snapshot_ok_with_metadata_null( self, mock_db, mock_create_snapshot, mock_rpcapi): mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary=constants.VOLUME_BINARY) body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': {'fake_key': 'fake_ref'}, 'name': 'test', 'description': 'test', 'metadata': None}} res = self._get_resp_post(body) self.assertEqual(http_client.ACCEPTED, res.status_int) args = mock_create_snapshot.call_args[0] # 5th argument of args is metadata. self.assertIsNone(args[5])
def test_manage_snapshot_ok_ref_as_string(self, mock_db, mock_create_snapshot, mock_rpcapi): mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary=constants.VOLUME_BINARY) body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': "string"}} res = self._get_resp_post(body) self.assertEqual(http_client.ACCEPTED, res.status_int, res) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual(body['snapshot']['ref'], args[3])
def test_manage_snapshot_ok(self, mock_db, mock_create_snapshot, mock_rpcapi): """Test successful manage snapshot execution. Tests for correct operation when valid arguments are passed in the request body. We ensure that cinder.volume.api.API.manage_existing got called with the correct arguments, and that we return the correct HTTP code to the caller. """ mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary=constants.VOLUME_BINARY) body = { 'snapshot': { 'volume_id': fake.VOLUME_ID, 'ref': { 'fake_key': 'fake_ref' } } } res = self._get_resp_post(body) self.assertEqual(HTTPStatus.ACCEPTED, res.status_int, res) # Check the db.service_get was called with correct arguments. mock_db.assert_called_once_with(mock.ANY, None, host='fake_host', binary=constants.VOLUME_BINARY, cluster_name=None) # Check the create_snapshot_in_db was called with correct arguments. self.assertEqual(1, mock_create_snapshot.call_count) args = mock_create_snapshot.call_args[0] named_args = mock_create_snapshot.call_args[1] self.assertEqual(fake.VOLUME_ID, args[1].get('id')) self.assertTrue(named_args['commit_quota']) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual({'fake_key': 'fake_ref'}, args[3])
def test_service_is_up(self): # NOTE(mdovgal): don't use @ddt.data with the real timestamp value # for this test. # When using ddt decorators ddt.data seems to have been calculated # not at the time of test's execution but at the tests's beginning. # And this one depends on utcnow func. So it won't be utcnow at the # execution moment and the result will be unexpected. down_time = 5 self.flags(service_down_time=down_time) # test if service is up service = fake_service.fake_service_obj(self.context) self.assertTrue(service.is_up) service.updated_at = timeutils.utcnow() self.assertTrue(service.is_up) # test is service is down now past_time = timeutils.utcnow() - datetime.timedelta(seconds=64) service.updated_at = past_time self.assertFalse(service.is_up)
def test_manage_snapshot_ok(self, mock_db, mock_create_snapshot, mock_rpcapi): """Test successful manage snapshot execution. Tests for correct operation when valid arguments are passed in the request body. We ensure that cinder.volume.api.API.manage_existing got called with the correct arguments, and that we return the correct HTTP code to the caller. """ mock_db.return_value = fake_service.fake_service_obj( self._admin_ctxt, binary=constants.VOLUME_BINARY) body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': {'fake_key': 'fake_ref'}}} res = self._get_resp_post(body) self.assertEqual(http_client.ACCEPTED, res.status_int, res) # Check the db.service_get was called with correct arguments. mock_db.assert_called_once_with( mock.ANY, None, host='fake_host', binary=constants.VOLUME_BINARY, cluster_name=None) # Check the create_snapshot_in_db was called with correct arguments. self.assertEqual(1, mock_create_snapshot.call_count) args = mock_create_snapshot.call_args[0] named_args = mock_create_snapshot.call_args[1] self.assertEqual(fake.VOLUME_ID, args[1].get('id')) self.assertTrue(named_args['commit_quota']) # Check the volume_rpcapi.manage_existing_snapshot was called with # correct arguments. self.assertEqual(1, mock_rpcapi.call_count) args = mock_rpcapi.call_args[0] self.assertEqual({u'fake_key': u'fake_ref'}, args[3])