Skip to content

Commit

Permalink
Code review: use StandaloneQueryRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
vagaerg authored and dain committed Mar 6, 2024
1 parent dc8a1dc commit f896e2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@

import io.trino.Session;
import io.trino.spi.security.Identity;
import io.trino.testing.DistributedQueryRunner;
import io.trino.testing.QueryRunner;
import io.trino.testing.StandaloneQueryRunner;
import org.intellij.lang.annotations.Language;

import java.util.Set;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.plugin.opa.TestHelpers.opaConfigToDict;
import static io.trino.testing.TestingSession.testSession;
import static io.trino.testing.TestingSession.testSessionBuilder;

public final class DistributedQueryRunnerHelper
public final class QueryRunnerHelper
{
private final DistributedQueryRunner runner;
private final QueryRunner runner;

private DistributedQueryRunnerHelper(DistributedQueryRunner runner)
private QueryRunnerHelper(QueryRunner runner)
{
this.runner = runner;
}

public static DistributedQueryRunnerHelper withOpaConfig(OpaConfig opaConfig)
throws Exception
public static QueryRunnerHelper withOpaConfig(OpaConfig opaConfig)
{
return new DistributedQueryRunnerHelper(
DistributedQueryRunner.builder(testSessionBuilder().build())
.setSystemAccessControl(new OpaAccessControlFactory().create(opaConfigToDict(opaConfig)))
.setNodeCount(1)
.build());
return new QueryRunnerHelper(
new StandaloneQueryRunner(
testSession(),
builder -> builder.setSystemAccessControl(new OpaAccessControlFactory().create(opaConfigToDict(opaConfig)))));
}

public Set<String> querySetOfStrings(String user, String query)
Expand All @@ -53,7 +53,7 @@ public Set<String> querySetOfStrings(Session session, @Language("SQL") String qu
return runner.execute(session, query).getMaterializedRows().stream().map(row -> row.getField(0) == null ? "<NULL>" : row.getField(0).toString()).collect(toImmutableSet());
}

public DistributedQueryRunner getBaseQueryRunner()
public QueryRunner getBaseQueryRunner()
{
return runner;
}
Expand All @@ -65,6 +65,6 @@ public void teardown()

private static Session userSession(String user)
{
return testSessionBuilder().setIdentity(Identity.ofUser(user)).build();
return testSessionBuilder().setOriginalIdentity(Identity.ofUser(user)).setIdentity(Identity.ofUser(user)).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class TestOpaAccessControlDataFilteringSystem
.addAll(DUMMY_CUSTOMERS_IN_TABLE)
.build();

private DistributedQueryRunnerHelper runner;
private QueryRunnerHelper runner;

@AfterEach
public void teardown()
Expand Down Expand Up @@ -249,9 +249,8 @@ private void assertResultsForUser(String asUser, String query, Set<String> expec
}

private void setupTrinoWithOpa(OpaConfig opaConfig)
throws Exception
{
this.runner = DistributedQueryRunnerHelper.withOpaConfig(opaConfig);
this.runner = QueryRunnerHelper.withOpaConfig(opaConfig);
MockConnectorFactory connectorFactory = MockConnectorFactory.builder()
.withListSchemaNames(session -> ImmutableList.of("sample_schema"))
.withListTables((session, schema) -> ImmutableList.<String>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@TestInstance(PER_CLASS)
public class TestOpaAccessControlSystem
{
private DistributedQueryRunnerHelper runner;
private QueryRunnerHelper runner;
private static final String OPA_ALLOW_POLICY_NAME = "allow";
private static final String OPA_BATCH_ALLOW_POLICY_NAME = "batchAllow";
@Container
Expand All @@ -47,7 +47,6 @@ class UnbatchedAuthorizerTests
{
@BeforeAll
public void setupTrino()
throws Exception
{
setupTrinoWithOpa(new OpaConfig().setOpaUri(OPA_CONTAINER.getOpaUriForPolicyPath(OPA_ALLOW_POLICY_NAME)));
}
Expand Down Expand Up @@ -122,7 +121,6 @@ class BatchedAuthorizerTests
{
@BeforeAll
public void setupTrino()
throws Exception
{
setupTrinoWithOpa(new OpaConfig()
.setOpaUri(OPA_CONTAINER.getOpaUriForPolicyPath(OPA_ALLOW_POLICY_NAME))
Expand Down Expand Up @@ -214,9 +212,8 @@ public void testAllowUnbatchedQuery()
}

private void setupTrinoWithOpa(OpaConfig opaConfig)
throws Exception
{
this.runner = DistributedQueryRunnerHelper.withOpaConfig(opaConfig);
this.runner = QueryRunnerHelper.withOpaConfig(opaConfig);
runner.getBaseQueryRunner().installPlugin(new BlackHolePlugin());
runner.getBaseQueryRunner().createCatalog("catalog_one", "blackhole");
runner.getBaseQueryRunner().createCatalog("catalog_two", "blackhole");
Expand Down

0 comments on commit f896e2b

Please sign in to comment.