Search in sources :

Example 1 with MockConnectorPlugin

use of io.trino.connector.MockConnectorPlugin in project trino by trinodb.

the class TestRefreshMaterializedView method createQueryRunner.

@Override
protected QueryRunner createQueryRunner() throws Exception {
    Session session = testSessionBuilder().setCatalog("mock").setSchema("default").build();
    DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(session).build();
    queryRunner.installPlugin(new MockConnectorPlugin(MockConnectorFactory.builder().withListSchemaNames(connectionSession -> ImmutableList.of("default")).withGetColumns(schemaTableName -> ImmutableList.of(new ColumnMetadata("nationkey", BIGINT))).withGetTableHandle((connectorSession, tableName) -> new MockConnectorTableHandle(tableName)).withGetMaterializedViews((connectorSession, schemaTablePrefix) -> ImmutableMap.of(new SchemaTableName("default", "delegate_refresh_to_connector"), new ConnectorMaterializedViewDefinition("SELECT nationkey FROM mock.default.test_table", Optional.of(new CatalogSchemaTableName("mock", "default", "test_storage")), Optional.of("mock"), Optional.of("default"), ImmutableList.of(new ConnectorMaterializedViewDefinition.Column("nationkey", BIGINT.getTypeId())), Optional.empty(), Optional.of("alice"), ImmutableMap.of()))).withDelegateMaterializedViewRefreshToConnector((connectorSession, schemaTableName) -> true).withRefreshMaterializedView(((connectorSession, schemaTableName) -> {
        startRefreshMaterializedView.set(null);
        SettableFuture<Void> refreshMaterializedView = SettableFuture.create();
        finishRefreshMaterializedView.addListener(() -> refreshMaterializedView.set(null), directExecutor());
        addExceptionCallback(refreshMaterializedView, () -> refreshInterrupted.set(null));
        return toCompletableFuture(refreshMaterializedView);
    })).build()));
    queryRunner.createCatalog("mock", "mock");
    return queryRunner;
}
Also used : MoreExecutors.listeningDecorator(com.google.common.util.concurrent.MoreExecutors.listeningDecorator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RUNNING(io.trino.execution.QueryState.RUNNING) Test(org.testng.annotations.Test) SettableFuture(com.google.common.util.concurrent.SettableFuture) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) AbstractTestQueryFramework(io.trino.testing.AbstractTestQueryFramework) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) MoreFutures.toCompletableFuture(io.airlift.concurrent.MoreFutures.toCompletableFuture) AfterClass(org.testng.annotations.AfterClass) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) MoreFutures.addExceptionCallback(io.airlift.concurrent.MoreFutures.addExceptionCallback) SchemaTableName(io.trino.spi.connector.SchemaTableName) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) Futures(com.google.common.util.concurrent.Futures) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) BIGINT(io.trino.spi.type.BigintType.BIGINT) Assert.assertEventually(io.trino.testing.assertions.Assert.assertEventually) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) QueryRunner(io.trino.testing.QueryRunner) Optional(java.util.Optional) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Session(io.trino.Session) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Session(io.trino.Session)

Example 2 with MockConnectorPlugin

use of io.trino.connector.MockConnectorPlugin in project trino by trinodb.

the class TestRevokeOnTable method initClass.

@BeforeClass
public void initClass() throws Exception {
    SchemaTableName table = new SchemaTableName("default", "table_one");
    queryRunner = DistributedQueryRunner.builder(userWithAllPrivileges).build();
    Grants<SchemaTableName> tableGrants = new MutableGrants<>();
    tableGrants.grant(new TrinoPrincipal(USER, admin.getUser()), table, EnumSet.allOf(Privilege.class), true);
    tableGrants.grant(new TrinoPrincipal(USER, userWithAllPrivileges.getUser()), table, EnumSet.allOf(Privilege.class), true);
    tableGrants.grant(new TrinoPrincipal(USER, userWithCreate.getUser()), table, ImmutableSet.of(Privilege.CREATE), true);
    tableGrants.grant(new TrinoPrincipal(USER, userWithSelect.getUser()), table, ImmutableSet.of(Privilege.SELECT), true);
    tableGrants.grant(new TrinoPrincipal(USER, userWithInsert.getUser()), table, ImmutableSet.of(Privilege.INSERT), true);
    tableGrants.grant(new TrinoPrincipal(USER, userWithUpdate.getUser()), table, ImmutableSet.of(Privilege.UPDATE), true);
    tableGrants.grant(new TrinoPrincipal(USER, userWithDelete.getUser()), table, ImmutableSet.of(Privilege.DELETE), true);
    MockConnectorFactory connectorFactory = MockConnectorFactory.builder().withListSchemaNames(session -> ImmutableList.of("default")).withListTables((session, schemaName) -> "default".equalsIgnoreCase(schemaName) ? ImmutableList.of(table) : ImmutableList.of()).withGetTableHandle((session, tableName) -> tableName.equals(table) ? new MockConnectorTableHandle(tableName) : null).withSchemaGrants(new MutableGrants<>()).withTableGrants(tableGrants).build();
    queryRunner.installPlugin(new MockConnectorPlugin(connectorFactory));
    queryRunner.createCatalog("local", "mock");
    assertions = new QueryAssertions(queryRunner);
}
Also used : DataProvider(org.testng.annotations.DataProvider) USER(io.trino.spi.security.PrincipalType.USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Identity(io.trino.spi.security.Identity) Grants(io.trino.connector.Grants) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) Privilege(io.trino.spi.security.Privilege) EnumSet(java.util.EnumSet) AfterClass(org.testng.annotations.AfterClass) ImmutableSet(com.google.common.collect.ImmutableSet) BeforeClass(org.testng.annotations.BeforeClass) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Randoms.randomUsername(io.trino.common.Randoms.randomUsername) QueryAssertions(io.trino.sql.query.QueryAssertions) MutableGrants(io.trino.connector.MutableGrants) Session(io.trino.Session) MockConnectorFactory(io.trino.connector.MockConnectorFactory) QueryAssertions(io.trino.sql.query.QueryAssertions) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MutableGrants(io.trino.connector.MutableGrants) Privilege(io.trino.spi.security.Privilege) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) SchemaTableName(io.trino.spi.connector.SchemaTableName) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with MockConnectorPlugin

use of io.trino.connector.MockConnectorPlugin in project trino by trinodb.

the class TestAccessControl method createQueryRunner.

@Override
protected QueryRunner createQueryRunner() throws Exception {
    Session session = testSessionBuilder().setCatalog("blackhole").setSchema("default").build();
    DistributedQueryRunner queryRunner = DistributedQueryRunner.builder(session).setNodeCount(1).build();
    queryRunner.installPlugin(new BlackHolePlugin());
    queryRunner.createCatalog("blackhole", "blackhole");
    queryRunner.installPlugin(new TpchPlugin());
    queryRunner.createCatalog("tpch", "tpch");
    queryRunner.installPlugin(new MockConnectorPlugin(MockConnectorFactory.builder().withGetViews((connectorSession, prefix) -> {
        ConnectorViewDefinition definitionRunAsDefiner = new ConnectorViewDefinition("select 1", Optional.of("mock"), Optional.of("default"), ImmutableList.of(new ConnectorViewDefinition.ViewColumn("test", BIGINT.getTypeId())), Optional.of("comment"), Optional.of("admin"), false);
        ConnectorViewDefinition definitionRunAsInvoker = new ConnectorViewDefinition("select 1", Optional.of("mock"), Optional.of("default"), ImmutableList.of(new ConnectorViewDefinition.ViewColumn("test", BIGINT.getTypeId())), Optional.of("comment"), Optional.empty(), true);
        return ImmutableMap.of(new SchemaTableName("default", "test_view_definer"), definitionRunAsDefiner, new SchemaTableName("default", "test_view_invoker"), definitionRunAsInvoker);
    }).withListRoleGrants((connectorSession, roles, grantees, limit) -> ImmutableSet.of(new RoleGrant(new TrinoPrincipal(USER, "alice"), "alice_role", false))).build()));
    queryRunner.createCatalog("mock", "mock");
    for (String tableName : ImmutableList.of("orders", "nation", "region", "lineitem")) {
        queryRunner.execute(format("CREATE TABLE %1$s AS SELECT * FROM tpch.tiny.%1$s WITH NO DATA", tableName));
    }
    return queryRunner;
}
Also used : EXECUTE_QUERY(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.EXECUTE_QUERY) SHOW_COLUMNS(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SHOW_COLUMNS) USER(io.trino.spi.security.PrincipalType.USER) Test(org.testng.annotations.Test) TRUNCATE_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.TRUNCATE_TABLE) CREATE_VIEW_WITH_SELECT_COLUMNS(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.CREATE_VIEW_WITH_SELECT_COLUMNS) SHOW_CREATE_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SHOW_CREATE_TABLE) BlackHolePlugin(io.trino.plugin.blackhole.BlackHolePlugin) AbstractTestQueryFramework(io.trino.testing.AbstractTestQueryFramework) Assertions(io.airlift.testing.Assertions) DELETE_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.DELETE_TABLE) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) MockConnectorFactory(io.trino.connector.MockConnectorFactory) TestingPrivilege(io.trino.testing.TestingAccessControlManager.TestingPrivilege) SET_TABLE_PROPERTIES(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SET_TABLE_PROPERTIES) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) TpchPlugin(io.trino.plugin.tpch.TpchPlugin) CREATE_MATERIALIZED_VIEW(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.CREATE_MATERIALIZED_VIEW) TestTable.randomTableSuffix(io.trino.testing.sql.TestTable.randomTableSuffix) INSERT_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.INSERT_TABLE) RENAME_COLUMN(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_COLUMN) ImmutableSet(com.google.common.collect.ImmutableSet) QUERY_MAX_MEMORY(io.trino.SystemSessionProperties.QUERY_MAX_MEMORY) ImmutableMap(com.google.common.collect.ImmutableMap) DROP_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.DROP_TABLE) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) SelectedRole(io.trino.spi.security.SelectedRole) DROP_COLUMN(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.DROP_COLUMN) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) BIGINT(io.trino.spi.type.BigintType.BIGINT) RENAME_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.RENAME_TABLE) ROLE(io.trino.spi.security.SelectedRole.Type.ROLE) UPDATE_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.UPDATE_TABLE) TestingSession(io.trino.testing.TestingSession) Optional(java.util.Optional) CREATE_VIEW(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.CREATE_VIEW) SET_USER(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SET_USER) Session(io.trino.Session) SELECT_COLUMN(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SELECT_COLUMN) CREATE_TABLE(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.CREATE_TABLE) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Identity(io.trino.spi.security.Identity) SET_SESSION(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.SET_SESSION) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) EXECUTE_FUNCTION(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.EXECUTE_FUNCTION) GRANT_EXECUTE_FUNCTION(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.GRANT_EXECUTE_FUNCTION) RoleGrant(io.trino.spi.security.RoleGrant) TestingAccessControlManager.privilege(io.trino.testing.TestingAccessControlManager.privilege) QueryRunner(io.trino.testing.QueryRunner) ADD_COLUMN(io.trino.testing.TestingAccessControlManager.TestingPrivilegeType.ADD_COLUMN) RoleGrant(io.trino.spi.security.RoleGrant) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) BlackHolePlugin(io.trino.plugin.blackhole.BlackHolePlugin) TpchPlugin(io.trino.plugin.tpch.TpchPlugin) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) SchemaTableName(io.trino.spi.connector.SchemaTableName) TestingSession(io.trino.testing.TestingSession) Session(io.trino.Session) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition)

Example 4 with MockConnectorPlugin

use of io.trino.connector.MockConnectorPlugin in project trino by trinodb.

the class TestGrantOnSchema method initClass.

@BeforeClass
public void initClass() throws Exception {
    queryRunner = DistributedQueryRunner.builder(admin).build();
    MockConnectorFactory connectorFactory = MockConnectorFactory.builder().withListSchemaNames(session -> ImmutableList.of("information_schema", "default")).withListTables((session, schema) -> "default".equalsIgnoreCase(schema) ? ImmutableList.of(new SchemaTableName(schema, "table_one")) : ImmutableList.of()).withSchemaGrants(schemaGrants).build();
    queryRunner.installPlugin(new MockConnectorPlugin(connectorFactory));
    queryRunner.createCatalog("local", "mock");
    assertions = new QueryAssertions(queryRunner);
    schemaGrants.grant(new TrinoPrincipal(USER, admin.getUser()), "default", EnumSet.allOf(Privilege.class), true);
}
Also used : AfterClass(org.testng.annotations.AfterClass) DataProvider(org.testng.annotations.DataProvider) DataProviders(io.trino.testing.DataProviders) USER(io.trino.spi.security.PrincipalType.USER) BeforeClass(org.testng.annotations.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Randoms.randomUsername(io.trino.common.Randoms.randomUsername) Identity(io.trino.spi.security.Identity) Grants(io.trino.connector.Grants) QueryAssertions(io.trino.sql.query.QueryAssertions) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) MutableGrants(io.trino.connector.MutableGrants) Privilege(io.trino.spi.security.Privilege) EnumSet(java.util.EnumSet) Session(io.trino.Session) MockConnectorFactory(io.trino.connector.MockConnectorFactory) QueryAssertions(io.trino.sql.query.QueryAssertions) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) Privilege(io.trino.spi.security.Privilege) SchemaTableName(io.trino.spi.connector.SchemaTableName) BeforeClass(org.testng.annotations.BeforeClass)

Example 5 with MockConnectorPlugin

use of io.trino.connector.MockConnectorPlugin in project trino by trinodb.

the class TestGrantOnTable method initClass.

@BeforeClass
public void initClass() throws Exception {
    queryRunner = DistributedQueryRunner.builder(admin).build();
    MockConnectorFactory connectorFactory = MockConnectorFactory.builder().withListSchemaNames(session -> ImmutableList.of("default")).withListTables((session, schemaName) -> "default".equalsIgnoreCase(schemaName) ? ImmutableList.of(table) : ImmutableList.of()).withGetTableHandle((session, tableName) -> tableName.equals(table) ? new MockConnectorTableHandle(tableName) : null).withSchemaGrants(new MutableGrants<>()).withTableGrants(tableGrants).build();
    queryRunner.installPlugin(new MockConnectorPlugin(connectorFactory));
    queryRunner.createCatalog("local", "mock");
    assertions = new QueryAssertions(queryRunner);
    tableGrants.grant(new TrinoPrincipal(USER, "admin"), table, EnumSet.allOf(Privilege.class), true);
}
Also used : DataProvider(org.testng.annotations.DataProvider) USER(io.trino.spi.security.PrincipalType.USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) DistributedQueryRunner(io.trino.testing.DistributedQueryRunner) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Identity(io.trino.spi.security.Identity) Grants(io.trino.connector.Grants) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) Privilege(io.trino.spi.security.Privilege) EnumSet(java.util.EnumSet) AfterClass(org.testng.annotations.AfterClass) DataProviders(io.trino.testing.DataProviders) BeforeClass(org.testng.annotations.BeforeClass) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Randoms.randomUsername(io.trino.common.Randoms.randomUsername) QueryAssertions(io.trino.sql.query.QueryAssertions) MutableGrants(io.trino.connector.MutableGrants) Session(io.trino.Session) MockConnectorFactory(io.trino.connector.MockConnectorFactory) QueryAssertions(io.trino.sql.query.QueryAssertions) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MockConnectorPlugin(io.trino.connector.MockConnectorPlugin) Privilege(io.trino.spi.security.Privilege) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

MockConnectorPlugin (io.trino.connector.MockConnectorPlugin)13 DistributedQueryRunner (io.trino.testing.DistributedQueryRunner)12 MockConnectorFactory (io.trino.connector.MockConnectorFactory)9 ImmutableList (com.google.common.collect.ImmutableList)8 Session (io.trino.Session)8 SchemaTableName (io.trino.spi.connector.SchemaTableName)8 TestingSession.testSessionBuilder (io.trino.testing.TestingSession.testSessionBuilder)8 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)8 BeforeClass (org.testng.annotations.BeforeClass)8 Test (org.testng.annotations.Test)8 TrinoPrincipal (io.trino.spi.security.TrinoPrincipal)7 MockConnectorTableHandle (io.trino.connector.MockConnectorTableHandle)6 MutableGrants (io.trino.connector.MutableGrants)6 Identity (io.trino.spi.security.Identity)6 USER (io.trino.spi.security.PrincipalType.USER)6 Privilege (io.trino.spi.security.Privilege)6 QueryAssertions (io.trino.sql.query.QueryAssertions)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 AfterClass (org.testng.annotations.AfterClass)6 ImmutableSet (com.google.common.collect.ImmutableSet)5