def test_get_inputs_missing(self, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() deployment_id = uuid.uuid4() assert_that(db.get_inputs(deployment_id=deployment_id)).is_none()
def test_blueprint_used_in_deployment(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', InvocationCursor) assert_that(db.blueprint_used_in_deployment(uuid.uuid4())).is_true()
def test_get_inputs_exception(self, monkeypatch, mocker): mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetStringCursor) deployment_id = uuid.uuid4() assert_that(db.get_inputs(deployment_id=deployment_id)).is_none()
def delete_deployment(deployment_id, force=False): """Delete all deployment data This endpoint deletes all data, about deployment, that are stored on xOpera REST API. It does not modify actual deployed instance. To undeploy actual instance, use /deployment/{deployment_id}/undeploy :param deployment_id: Id of deployment :type deployment_id: :param force: Force-remove deployment data :type force: bool :rtype: str """ inv = PostgreSQL.get_deployment_status(deployment_id) if not force: if inv.state in [InvocationState.PENDING, InvocationState.IN_PROGRESS]: return f"Previous operation on this deployment still running", 403 success_deployment = PostgreSQL.delete_deployment(deployment_id) success_session_data = PostgreSQL.delete_opera_session_data(deployment_id) if not (success_deployment and success_session_data): return "Failed to delete deployment", 500 return 'Deployment deleted', 200
def test_get_last_invocation_id_success(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', InvocationCursor) assert_that(db.get_last_invocation_id(uuid.uuid4())).is_equal_to( self.invocation_id)
def test_get_deployment_status(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', InvocationCursor) assert_that(obj_to_json(db.get_deployment_status( uuid.uuid4()))).is_equal_to(self.inv.to_dict())
def test_get_deployment_ids_version(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', InvocationCursor) assert_that(db.get_deployment_ids(uuid.uuid4(), 'v1.0')).is_equal_to( [self.inv.deployment_id])
def test_get_opera_session_data_fail(self, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() assert_that( db.get_opera_session_data( self.session_data['deployment_id'])).is_none()
def test_project_domain(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintCursor) # testing assert db.get_blueprints_by_user_or_project( project_domain='project_domain', active=False) == self.blueprints
def test_active(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintCursor) # testing assert db.get_blueprints_by_user_or_project( username='******', active=True) == [self.blueprints[0]]
def test_get_inputs(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetInputsCursor) deployment_id = uuid.uuid4() assert_that(db.get_inputs(deployment_id=deployment_id)).is_equal_to( self.inputs)
def test_get_opera_session_data(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', OperaSessionDataCursor) assert_that( db.get_opera_session_data( self.session_data['deployment_id'])).is_equal_to( self.session_data)
def test_get_deployments(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) blueprint_id = uuid.uuid4() assert_that( db.get_deployments_for_blueprint( blueprint_id, active=False)).is_equal_to([self.deployment])
def test_blueprint_used_in_deployment_no_deployment_ids(self, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) mocker.patch( 'opera.api.service.sqldb_service.PostgreSQL.get_deployment_ids', return_value=None) db = PostgreSQL() assert_that(db.blueprint_used_in_deployment(uuid.uuid4(), 'v1.0')).is_false()
def test_save_success(self, mocker, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() deployment_id = uuid.uuid4() assert_that(db.save_opera_session_data(deployment_id, {})).is_true() assert_that(caplog.text).contains("Updated dot_opera_data", str(deployment_id))
def test_delete_deployment(self, mocker, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() deployment_id = uuid.uuid4() assert_that(db.delete_deployment(deployment_id)).is_true() assert_that(caplog.text).contains("Deleted deployment", str(deployment_id))
def test_get(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GitTransactionDataCursor) # testing assert_that( db.get_git_transaction_data( blueprint_id=self.git_log.blueprint_id)).is_equal_to( [obj_to_json(self.git_log)])
def test_get_blueprint_name_missing(self, mocker, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_blueprint_name(blueprint_meta.blueprint_id) is None
def test_save_fail(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) deployment_id = uuid.uuid4() assert_that(db.save_opera_session_data(deployment_id, {})).is_false() assert_that(caplog.text).contains("Failed to update dot_opera_data", str(deployment_id))
def post_new_blueprint(revision_msg=None, blueprint_name=None, aadm_id=None, username=None, project_domain=None): # noqa: E501 """Add new blueprint. :param revision_msg: Optional comment on submission :type revision_msg: str :param blueprint_name: Optional human-readable blueprint name :type blueprint_name: str :param aadm_id: End-to-end debugging id :type aadm_id: str :param username: End-to-end debugging id :type username: str :param project_domain: Optional project domain this blueprint belongs to :type project_domain: str :rtype: Blueprint """ # check roles if project_domain and not security_controller.check_roles(project_domain): return f"Unauthorized request for project: {project_domain}", 401 file = connexion.request.files['CSAR'] result, response = CSAR_db.add_revision(CSAR=file, revision_msg=revision_msg) if result is None: return f"Invalid CSAR: {response}", 406 blueprint_meta = BlueprintVersion.from_dict(result) blueprint_meta.blueprint_name = blueprint_name blueprint_meta.project_domain = project_domain blueprint_meta.aadm_id = aadm_id blueprint_meta.username = username if not PostgreSQL.save_blueprint_meta(blueprint_meta): blueprint_id = blueprint_meta.blueprint_id return f"Failed to save project data for blueprint_id={blueprint_id}", 500 PostgreSQL.save_git_transaction_data( blueprint_id=result['blueprint_id'], version_id=result['version_id'], revision_msg=f"Saved new blueprint: {revision_msg}", job='update', git_backend=str(CSAR_db.connection.git_connector), repo_url=result['url'], commit_sha=result['commit_sha']) return blueprint_meta, 201
def test_get_blueprint_name(self, mocker, caplog, monkeypatch, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_blueprint_name( blueprint_meta.blueprint_id) == blueprint_meta.blueprint_name
def test_update_deployment_log_success(self, mocker, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() assert_that(db.update_deployment_log(self.invocation_id, self.inv)).is_true() assert_that(caplog.text).contains("Updated deployment log", str(self.inv.deployment_id), str(self.invocation_id))
def test_delete_blueprint_meta_fail(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) # testing blueprint_id = uuid.uuid4() assert not db.delete_blueprint_meta(blueprint_id) assert_that(caplog.text).contains( "Failed to delete blueprint metadata", str(blueprint_id))
def test_save_blueprint_meta_exception(self, mocker, caplog, monkeypatch, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) blueprint_meta: BlueprintVersion = generic_blueprint_meta db.save_blueprint_meta(blueprint_meta) assert_that(caplog.text).contains("Fail to update blueprint meta", str(blueprint_meta.blueprint_id))
def test_get_blueprint_meta(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = self.blueprint_meta assert_that( BlueprintVersion.from_dict( db.get_blueprint_meta( blueprint_meta.blueprint_id))).is_equal_to(blueprint_meta)
def test_blueprint_has_never_existed(self, mocker, caplog): # Test preparation caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() # Testing blueprint_id = uuid.uuid4() version_id = 'v4.0' exists = db.version_exists(blueprint_id, version_id) assert_that(exists).is_false() assert_that(caplog.text).contains( f"Blueprint {blueprint_id} has never existed")
def test_version_exists(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', VersionExistsCursor) # testing blueprint_id = uuid.uuid4() version_id = 'v4.0' exists = db.version_exists(blueprint_id, version_id) assert_that(exists).is_true() assert_that(caplog.text).is_empty()
def test_update_deployment_log_fail(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) assert_that(db.update_deployment_log(self.invocation_id, self.inv)).is_false() assert_that(caplog.text).contains("Failed to update deployment log", str(self.inv.deployment_id), str(self.invocation_id))
def wrapper_check_role_auth(*args, **kwargs): blueprint_id = kwargs.get("blueprint_id") if not blueprint_id: return f"Authorization configuration error", 401 version_id = kwargs.get("version_id") if not PostgreSQL.version_exists(blueprint_id, version_id) and not CSAR_db.version_exists(blueprint_id, version_id): return f"Did not find blueprint with id: {blueprint_id} and version_id: {version_id or 'any'}", 404 project_domain = PostgreSQL.get_project_domain(blueprint_id) if project_domain and not check_roles(project_domain): return f"Unauthorized request for project: {project_domain}", 401 return func(*args, **kwargs)
def test_delete_blueprint_meta(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', NoneCursor) # testing blueprint_id = uuid.uuid4() assert db.delete_blueprint_meta(blueprint_id) command = NoneCursor.get_command() assert_that(command).contains("delete") assert_that(caplog.text).contains("Deleted blueprint meta", str(blueprint_id))