Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support injecting system properties into JUnit tests #19348

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
package io.trino.plugin.bigquery;

import com.google.common.collect.ImmutableMap;
import io.trino.junit.SystemPropertyParameterResolver;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.testing.QueryRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Map;

import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SystemPropertyParameterResolver.class)
public class TestBigQueryWithDifferentProjectIdConnectorSmokeTest
extends BaseBigQueryConnectorSmokeTest
{
Expand All @@ -31,12 +35,15 @@ public class TestBigQueryWithDifferentProjectIdConnectorSmokeTest

protected String alternateProjectId;

public TestBigQueryWithDifferentProjectIdConnectorSmokeTest(@SystemProperty("testing.alternate-bq-project-id") String alternateProjectId)
{
this.alternateProjectId = requireNonNull(alternateProjectId, "alternateProjectId is null");
}

@Override
protected QueryRunner createQueryRunner()
throws Exception
{
this.alternateProjectId = requireNonNull(System.getProperty("testing.alternate-bq-project-id"), "testing.alternate-bq-project-id system property not set");

QueryRunner queryRunner = BigQueryQueryRunner.createQueryRunner(
ImmutableMap.of(),
ImmutableMap.of("bigquery.project-id", alternateProjectId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import com.google.common.io.ByteSource;
import com.google.common.io.Resources;
import com.google.common.reflect.ClassPath;
import io.trino.junit.SystemPropertyParameterResolver;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.plugin.hive.containers.HiveHadoop;
import io.trino.testing.QueryRunner;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -53,6 +56,7 @@
import static org.testcontainers.containers.Network.newNetwork;

@TestInstance(PER_CLASS)
@ExtendWith(SystemPropertyParameterResolver.class)
public class TestDeltaLakeAdlsConnectorSmokeTest
extends BaseDeltaLakeConnectorSmokeTest
{
Expand All @@ -62,11 +66,14 @@ public class TestDeltaLakeAdlsConnectorSmokeTest
private final BlobContainerClient azureContainerClient;
private final String adlsDirectory;

public TestDeltaLakeAdlsConnectorSmokeTest()
public TestDeltaLakeAdlsConnectorSmokeTest(
@SystemProperty("hive.hadoop2.azure-abfs-container") String container,
@SystemProperty("hive.hadoop2.azure-abfs-account") String account,
@SystemProperty("hive.hadoop2.azure-abfs-access-key") String accessKey)
{
this.container = requireNonNull(System.getProperty("hive.hadoop2.azure-abfs-container"), "container is null");
this.account = requireNonNull(System.getProperty("hive.hadoop2.azure-abfs-account"), "account is null");
this.accessKey = requireNonNull(System.getProperty("hive.hadoop2.azure-abfs-access-key"), "accessKey is null");
this.container = requireNonNull(container, "container is null");
this.account = requireNonNull(account, "account is null");
this.accessKey = requireNonNull(accessKey, "accessKey is null");

String connectionString = format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net", account, accessKey);
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import io.trino.junit.SystemPropertyParameterResolver;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.plugin.hive.containers.HiveHadoop;
import io.trino.testing.AbstractTestQueryFramework;
import io.trino.testing.QueryRunner;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.Network;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.nio.file.Files;
Expand All @@ -44,6 +46,7 @@
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;

@ExtendWith(SystemPropertyParameterResolver.class)
public class TestDeltaLakeAdlsStorage
extends AbstractTestQueryFramework
{
Expand All @@ -58,11 +61,10 @@ public class TestDeltaLakeAdlsStorage

private HiveHadoop hiveHadoop;

@Parameters({
"hive.hadoop2.azure-abfs-container",
"hive.hadoop2.azure-abfs-account",
"hive.hadoop2.azure-abfs-access-key"})
public TestDeltaLakeAdlsStorage(String container, String account, String accessKey)
public TestDeltaLakeAdlsStorage(
@SystemProperty("hive.hadoop2.azure-abfs-container") String container,
@SystemProperty("hive.hadoop2.azure-abfs-account") String account,
@SystemProperty("hive.hadoop2.azure-abfs-access-key") String accessKey)
{
requireNonNull(container, "container is null");
this.account = requireNonNull(account, "account is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.trino.hadoop.ConfigurationInstantiator;
import io.trino.hdfs.gcs.GoogleGcsConfigurationInitializer;
import io.trino.hdfs.gcs.HiveGcsConfig;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.plugin.hive.containers.HiveHadoop;
import io.trino.testing.QueryRunner;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -76,10 +77,12 @@ public class TestDeltaLakeGcsConnectorSmokeTest
private String gcpCredentials;
private TrinoFileSystem fileSystem;

public TestDeltaLakeGcsConnectorSmokeTest()
public TestDeltaLakeGcsConnectorSmokeTest(
@SystemProperty("testing.gcp-storage-bucket") String gcpStorageBucket,
@SystemProperty("testing.gcp-credentials-key") String gcpCredentialKey)
{
this.gcpStorageBucket = requireNonNull(System.getProperty("testing.gcp-storage-bucket"), "GCP storage bucket is null");
this.gcpCredentialKey = requireNonNull(System.getProperty("testing.gcp-credentials-key"), "GCP credential key is null");
this.gcpStorageBucket = requireNonNull(gcpStorageBucket, "gcpStorageBucket is null");
this.gcpCredentialKey = requireNonNull(gcpCredentialKey, "gcpCredentialKey is null");
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-hive-hadoop2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import io.trino.junit.SystemPropertyParameterResolver;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.plugin.hive.containers.HiveHadoop;
import io.trino.plugin.hive.metastore.thrift.ThriftMetastoreConfig;
import io.trino.plugin.hive.s3.S3HiveQueryRunner;
import io.trino.testing.AbstractTestQueryFramework;
import io.trino.testing.QueryRunner;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.io.IOException;
Expand All @@ -42,6 +44,7 @@
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SystemPropertyParameterResolver.class)
public class TestHiveThriftMetastoreWithS3
extends AbstractTestQueryFramework
{
Expand All @@ -53,17 +56,11 @@ public class TestHiveThriftMetastoreWithS3
private final Path hadoopCoreSiteXmlTempFile;
private final AmazonS3 s3Client;

@Parameters({
"hive.hadoop2.s3.endpoint",
"hive.hadoop2.s3.awsAccessKey",
"hive.hadoop2.s3.awsSecretKey",
"hive.hadoop2.s3.writableBucket",
wendigo marked this conversation as resolved.
Show resolved Hide resolved
})
public TestHiveThriftMetastoreWithS3(
String s3endpoint,
String awsAccessKey,
String awsSecretKey,
String writableBucket)
@SystemProperty("hive.hadoop2.s3.endpoint") String s3endpoint,
@SystemProperty("hive.hadoop2.s3.awsAccessKey") String awsAccessKey,
@SystemProperty("hive.hadoop2.s3.awsSecretKey") String awsSecretKey,
@SystemProperty("hive.hadoop2.s3.writableBucket") String writableBucket)
throws IOException
{
this.s3endpoint = requireNonNull(s3endpoint, "s3endpoint is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import io.trino.filesystem.Location;
import io.trino.junit.SystemPropertyParameterResolver;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.plugin.hive.containers.HiveHadoop;
import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.thrift.BridgingHiveMetastore;
import io.trino.testing.QueryRunner;
import org.junit.jupiter.api.Test;
import org.testng.annotations.Parameters;
import org.junit.jupiter.api.extension.ExtendWith;

import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
Expand All @@ -40,6 +42,7 @@
import static org.apache.iceberg.FileFormat.ORC;
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SystemPropertyParameterResolver.class)
public class TestIcebergAbfsConnectorSmokeTest
extends BaseIcebergConnectorSmokeTest
{
Expand All @@ -51,11 +54,10 @@ public class TestIcebergAbfsConnectorSmokeTest

private HiveHadoop hiveHadoop;

@Parameters({
"hive.hadoop2.azure-abfs-container",
"hive.hadoop2.azure-abfs-account",
"hive.hadoop2.azure-abfs-access-key"})
public TestIcebergAbfsConnectorSmokeTest(String container, String account, String accessKey)
public TestIcebergAbfsConnectorSmokeTest(
@SystemProperty("hive.hadoop2.azure-abfs-container") String container,
@SystemProperty("hive.hadoop2.azure-abfs-account") String account,
@SystemProperty("hive.hadoop2.azure-abfs-access-key") String accessKey)
{
super(ORC);
this.container = requireNonNull(container, "container is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.common.io.Resources;
import io.airlift.log.Logger;
import io.trino.filesystem.Location;
import io.trino.junit.SystemPropertyParameterResolver;
import io.trino.junit.SystemPropertyParameterResolver.SystemProperty;
import io.trino.plugin.hive.containers.HiveHadoop;
import io.trino.plugin.hive.metastore.HiveMetastore;
import io.trino.plugin.hive.metastore.thrift.BridgingHiveMetastore;
Expand All @@ -25,6 +27,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -46,6 +49,7 @@
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;

@TestInstance(PER_CLASS)
@ExtendWith(SystemPropertyParameterResolver.class)
public class TestIcebergGcsConnectorSmokeTest
extends BaseIcebergConnectorSmokeTest
{
Expand All @@ -58,11 +62,13 @@ public class TestIcebergGcsConnectorSmokeTest

private HiveHadoop hiveHadoop;

public TestIcebergGcsConnectorSmokeTest()
public TestIcebergGcsConnectorSmokeTest(
@SystemProperty("testing.gcp-storage-bucket") String gcpStorageBucket,
@SystemProperty("testing.gcp-credentials-key") String gcpCredentialKey)
{
super(ORC);
this.gcpStorageBucket = requireNonNull(System.getProperty("testing.gcp-storage-bucket"), "gcpStorageBucket is null");
this.gcpCredentialKey = requireNonNull(System.getProperty("testing.gcp-credentials-key"), "gcpCredentialKey is null");
this.gcpStorageBucket = requireNonNull(gcpStorageBucket, "gcpStorageBucket is null");
this.gcpCredentialKey = requireNonNull(gcpCredentialKey, "gcpCredentialKey is null");
this.schema = "test_iceberg_gcs_connector_smoke_test_" + randomNameSuffix();
}

Expand Down
2 changes: 1 addition & 1 deletion testing/trino-testing-containers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion testing/trino-testing-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions testing/trino-testing-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.junit;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class SystemPropertyParameterResolver
implements ParameterResolver
{
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException
{
return parameterContext.isAnnotated(SystemProperty.class);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException
{
SystemProperty annotation = parameterContext.findAnnotation(SystemProperty.class).orElseThrow(() ->
new ParameterResolutionException("Missing @SystemProperty for class: %s, parameter: %s".formatted(extensionContext.getRequiredTestClass(), parameterContext.getParameter().getName())));

String propertyName = annotation.value();
String value = System.getProperty(propertyName);
if (value == null) {
throw new ParameterResolutionException("Could not resolve system property '%s' for class: %s, parameter: %s".formatted(propertyName, extensionContext.getRequiredTestClass(), parameterContext.getParameter().getName()));
}
return value;
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface SystemProperty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
Define as top level class.
This helps Intellij generate imports

(for nested class Intellij strongly prefers OutClass.InnerClass syntax which is all but pretty)

{
String value();
}
}
Loading