def setUp(self): super(CatalogDBTestCase, self).setUp() self.tenant_id = str(uuid.uuid4()) self.tenant_id_2 = str(uuid.uuid4()) self.context = utils.dummy_context(tenant_id=self.tenant_id) self.context_2 = utils.dummy_context(tenant_id=self.tenant_id_2) self.context_admin = utils.dummy_context(tenant_id=self.tenant_id) self.context_admin.is_admin = True
def test_configure(self, _, mock_envs, mock_session_services): mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_session = mock.MagicMock(to_dict=mock.MagicMock( return_value={'test_env_id', 'test_user_id'})) mock_session_services.create.return_value = mock_session result = self.sessions_controller.configure(mock_request, 'test_env_id') self.assertEqual({'test_env_id', 'test_user_id'}, result)
def test_check_env(self, mock_db_session): """Test check env.""" mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_env = mock.MagicMock(environment_id='test_env_id', tenant_id=mock_request.context.tenant) mock_db_session.get_session().query().get.return_value = mock_env env = utils.check_env(mock_request, mock_env.environment_id) self.assertEqual(mock_env, env)
def test_verify_session_with_null_session(self, mock_db_session): """Test null session throws expected exception.""" mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_request.context.session = mock.MagicMock( return_value='test_sess_id') mock_db_session.get_session().query().get.return_value = None expected_error_message = 'Session <SessionId {0}> is not found'\ .format(mock_request.context.session) with self.assertRaisesRegexp(exc.HTTPNotFound, expected_error_message): self._test_verify_session(mock_request)
def test_deploy_with_invalid_session(self, mock_db_session, mock_session_services, _): mock_db_session.get_session().query().get.return_value = None mock_session_services.validate.return_value = False mock_request = mock.MagicMock(context=test_utils.dummy_context()) expected_error_msg = 'Session <SessionId {0}> is invalid: environment'\ ' has been updated or updating right now with'\ ' other session'.format('test_sess_id') with self.assertRaisesRegex(exc.HTTPForbidden, expected_error_msg): self.sessions_controller.deploy(mock_request, None, 'test_sess_id')
def test_delete_with_invalid_user(self, mock_db_session, _): mock_session = mock.MagicMock(user_id='test_user_id') mock_db_session.get_session().query().get.return_value = mock_session mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_request.context.user = '******' expected_error_msg = 'User <UserId {0}> is not authorized to '\ 'access session <SessionId {1}>.'\ .format(mock_request.context.user, 'test_sess_id') with self.assertRaisesRegex(exc.HTTPUnauthorized, expected_error_msg): self.sessions_controller.delete(mock_request, None, 'test_sess_id')
def test_check_env_with_null_environment_id(self, mock_db_session): """Test check env with null environment id throws exception.""" mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_db_session.get_session().query().get.return_value = None test_env_id = 'test_env_id' expected_error_message = 'Environment with id {env_id} not found'\ .format(env_id=test_env_id) with self.assertRaisesRegexp(exc.HTTPNotFound, expected_error_message): utils.check_env(mock_request, test_env_id)
def test_verify_session_with_invalid_request_header(self): """Test session with invalid request header throws exception.""" dummy_context = test_utils.dummy_context() if dummy_context.session: dummy_context.session = None mock_request = mock.MagicMock(context=dummy_context) expected_error_message = 'X-Configuration-Session header which '\ 'indicates to the session is missed' with self.assertRaisesRegexp(exc.HTTPBadRequest, expected_error_message): self._test_verify_session(mock_request)
def test_get_schema(self, mock_rpc, *args): dummy_context = test_utils.dummy_context() dummy_context.GET = { 'classVersion': 'test_class_version', 'packageName': 'test_package_name' } mock_request = mock.MagicMock(context=dummy_context) mock_rpc.engine().generate_schema.return_value = 'test_schema' result = self.controller.get_schema(mock_request, 'test_class') self.assertEqual('test_schema', result)
def test_check_env_with_mismatching_tenant_id(self, mock_db_session): """Test check env without matching tenant ids throws exception.""" mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_env = mock.MagicMock(environment_id='test_env_id', tenant_id='another_test_tenant_id') mock_db_session.get_session().query().get.return_value = mock_env expected_error_message = 'User is not authorized to access these '\ 'tenant resources' with self.assertRaisesRegexp(exc.HTTPForbidden, expected_error_message): utils.check_env(mock_request, mock_env.environment_id)
def test_verify_env_template_with_invalid_tenant(self, mock_db_session): """Test session validation failure throws expected exception.""" mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_request.context.tenant = mock.MagicMock( return_value='test_tenant_id') mock_env_template = mock.MagicMock(tenant_id='another_test_tenant_id') mock_db_session.get_session().query().get.return_value =\ mock_env_template expected_error_message = 'User is not authorized to access this'\ ' tenant resources' with self.assertRaisesRegexp(exc.HTTPForbidden, expected_error_message): self._test_verify_env_template(mock_request, None)
def test_delete_with_deploying_session(self, mock_db_session, mock_session_services, _): mock_session = mock.MagicMock(user_id='test_user_id', state=states.SessionState.DEPLOYING) mock_db_session.get_session().query().get.return_value = mock_session mock_session_services.validate.return_value = False mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_request.context.user = '******' expected_error_msg = 'Session <SessionId: {0}> is in deploying '\ 'state and could not be deleted'\ .format('test_sess_id') with self.assertRaisesRegex(exc.HTTPForbidden, expected_error_msg): self.sessions_controller.delete(mock_request, None, 'test_sess_id')
def test_configure_with_env_in_illegal_state(self, _, mock_envs): mock_request = mock.MagicMock(context=test_utils.dummy_context()) illegal_states = [states.EnvironmentStatus.DEPLOYING, states.EnvironmentStatus.DELETING] expected_error_msg = 'Could not open session for environment '\ '<EnvId: {env_id}>, environment has '\ 'deploying or deleting status.'\ .format(env_id='test_env_id') for state in illegal_states: mock_envs.EnvironmentServices.get_status.return_value = state with self.assertRaisesRegexp(exc.HTTPForbidden, expected_error_msg): self.sessions_controller.configure(mock_request, 'test_env_id')
def test_verify_session_in_deploying_state(self, mock_db_session, mock_validate): """Test deploying session throws expected exception.""" mock_validate.return_value = True mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_request.context.session = mock.MagicMock( return_value='test_sess_id') mock_db_session.get_session().query().get.return_value =\ mock.MagicMock(state=states.SessionState.DEPLOYING) expected_error_message = 'Session <SessionId {0}> is already in '\ 'deployment state'\ .format(mock_request.context.session) with self.assertRaisesRegexp(exc.HTTPForbidden, expected_error_message): self._test_verify_session(mock_request)
def _simple_request(self, path, params=None, method="GET", user=DEFAULT_USER, tenant=DEFAULT_TENANT): """Returns a request with a fake but valid-looking context and sets the request environment variables. If `params` is given, it should be a dictionary or sequence of tuples. """ environ = self._environ(path) environ["REQUEST_METHOD"] = method if params: qs = urllib.urlencode(params) environ["QUERY_STRING"] = qs req = wsgi.Request(environ) req.context = utils.dummy_context(user, tenant, is_admin=self.is_admin) self.context = req.context return req
def _data_request(self, path, data, content_type='application/json', method='POST', params={}): environ = self._environ(path) environ['REQUEST_METHOD'] = method req = wsgi.Request(environ) req.context = utils.dummy_context('api_test_user', self.tenant) self.context = req.context req.content_type = content_type req.body = data if params: qs = urllib.urlencode(params) environ['QUERY_STRING'] = qs return req
def test_verify_session_with_invalid_session(self, mock_db_session, mock_validate): """Test session validation failure throws expected exception.""" mock_validate.return_value = False mock_request = mock.MagicMock(context=test_utils.dummy_context()) mock_request.context.session = mock.MagicMock( return_value='test_sess_id') mock_db_session.get_session().query().get.return_value =\ mock.MagicMock() expected_error_message = 'Session <SessionId {0}> is invalid: '\ 'environment has been updated or '\ 'updating right now with other session'\ .format(mock_request.context.session) with self.assertRaisesRegexp(exc.HTTPForbidden, expected_error_message): self._test_verify_session(mock_request)
def _data_request(self, path, data, content_type='application/json', method='POST', params={}, user=DEFAULT_USER, tenant=DEFAULT_TENANT): environ = self._environ(path) environ['REQUEST_METHOD'] = method req = wsgi.Request(environ) req.context = utils.dummy_context(user, tenant, is_admin=self.is_admin) self.context = req.context req.content_type = content_type req.body = data if params: qs = urllib.urlencode(params) environ['QUERY_STRING'] = qs return req
def _simple_request(self, path, params=None, method='GET'): """Returns a request with a fake but valid-looking context and sets the request environment variables. If `params` is given, it should be a dictionary or sequence of tuples. """ environ = self._environ(path) environ['REQUEST_METHOD'] = method if params: qs = urllib.urlencode(params) environ['QUERY_STRING'] = qs req = wsgi.Request(environ) req.context = utils.dummy_context('api_test_user', self.tenant, is_admin=self.is_admin) self.context = req.context return req
def _data_request(self, path, data, content_type='application/json', method='POST', params=None, user=DEFAULT_USER, tenant=DEFAULT_TENANT): if params is None: params = {} environ = self._environ(path) environ['REQUEST_METHOD'] = method req = wsgi.Request(environ) req.context = utils.dummy_context(user, tenant, is_admin=self.is_admin) self.context = req.context req.content_type = content_type req.body = data if params: qs = urllib.parse.urlencode(params) environ['QUERY_STRING'] = qs return req
def test_deploy_with_session_in_invalid_state(self, mock_db_session, mock_session_services, _): mock_session_services.validate.return_value = True mock_request = mock.MagicMock(context=test_utils.dummy_context()) expected_error_msg = 'Session <SessionId {0}> is already deployed or '\ 'deployment is in progress'.format('test_sess_id') invalid_states = [ states.SessionState.DEPLOYING, states.SessionState.DEPLOYED, states.SessionState.DEPLOY_FAILURE, states.SessionState.DELETING, states.SessionState.DELETE_FAILURE ] for state in invalid_states: mock_session = mock.MagicMock(state=state) mock_db_session.get_session().query().get.return_value =\ mock_session with self.assertRaisesRegex(exc.HTTPForbidden, expected_error_msg): self.sessions_controller.deploy(mock_request, None, 'test_sess_id')
def test_get_schema_negative(self, mock_rpc, *args): dummy_context = test_utils.dummy_context() dummy_context.GET = { 'classVersion': 'test_class_version', 'packageName': 'test_package_name' } mock_request = mock.MagicMock(context=dummy_context) # Test exception handling for pre-defined exception types. exc_types = ('NoClassFound', 'NoPackageForClassFound', 'NoPackageFound') for exc_type in exc_types: dummy_error = client.RemoteError(exc_type=exc_type, value='dummy_value') mock_rpc.engine().generate_schema.side_effect = dummy_error with self.assertRaisesRegex(exc.HTTPNotFound, dummy_error.value): self.controller.get_schema(mock_request, 'test_class') # Test exception handling for miscellaneous exception type. dummy_error = client.RemoteError(exc_type='TestExcType', value='dummy_value') mock_rpc.engine().generate_schema.side_effect = dummy_error with self.assertRaisesRegex(client.RemoteError, dummy_error.value): self.controller.get_schema(mock_request, 'test_class')
def test_get_network_driver(self, mock_authentication): self.tenant_id = str(uuid.uuid4()) self.context = utils.dummy_context(tenant_id=self.tenant_id) driver_context = self.env_services.get_network_driver(self.context) self.assertEqual(driver_context, "neutron")
def setUpClass(cls): super(ServerTest, cls).setUpClass() cls.result_endpoint = server.ResultEndpoint() cls.dummy_context = test_utils.dummy_context()
def setUp(self): super(CatalogDBTestCase, self).setUp() self.tenant_id = str(uuid.uuid4()) self.context = utils.dummy_context(tenant_id=self.tenant_id)