Skip to content

Commit

Permalink
Add unique ID to TestEnvironment (#436)
Browse files Browse the repository at this point in the history
* Add unique ID to TestEnvironment

* Add comment about caching

* Consistent record access

* uniqueId -> testId

---------

Co-authored-by: Eric Maynard <eric.maynard+oss@snowflake.com>
  • Loading branch information
andrew4699 and eric-maynard authored Nov 12, 2024
1 parent 73b743c commit 81d8eb1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public static void setup(
EXT.client()
.target(
String.format(
"http://localhost:%d/api/management/v1/principals/snowman/principal-roles",
EXT.getLocalPort()))
"http://localhost:%d/api/management/v1/principals/%s/principal-roles",
EXT.getLocalPort(), snowmanCredentials.identifier().principalName()))
.request("application/json")
.header("Authorization", "Bearer " + PolarisApplicationIntegrationTest.userToken)
.header(REALM_PROPERTY_KEY, realm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void before(
.getTestMethod()
.ifPresent(
method -> {
String catalogName = method.getName();
String catalogName = method.getName() + testEnv.testId();
try (Response response =
testEnv
.apiClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ public static RESTCatalog createSnowmanManagedCatalog(
client
.target(
String.format(
"%s/api/management/v1/principal-roles/catalog-admin/catalog-roles/%s",
baseUrl, currentCatalogName))
"%s/api/management/v1/principal-roles/%s/catalog-roles/%s",
baseUrl,
snowmanCredentials.identifier().principalRoleName(),
currentCatalogName))
.request("application/json")
.header("Authorization", "Bearer " + adminToken.token())
.header(REALM_PROPERTY_KEY, realm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public class SnowmanCredentialsExtension
private static final Logger LOGGER = LoggerFactory.getLogger(SnowmanCredentialsExtension.class);
private SnowmanCredentials snowmanCredentials;

public record SnowmanCredentials(String clientId, String clientSecret) {}
public record SnowmanIdentifier(String principalName, String principalRoleName) {}

public record SnowmanCredentials(
String clientId, String clientSecret, SnowmanIdentifier identifier) {}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
Expand All @@ -73,7 +76,8 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
adminSecrets.getMainSecret(),
realm);

PrincipalRole principalRole = new PrincipalRole("catalog-admin");
SnowmanIdentifier snowmanIdentifier = getSnowmanIdentifier(testEnv);
PrincipalRole principalRole = new PrincipalRole(snowmanIdentifier.principalRoleName());
try (Response createPrResponse =
testEnv
.apiClient()
Expand All @@ -86,7 +90,7 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
.returns(Response.Status.CREATED.getStatusCode(), Response::getStatus);
}

Principal principal = new Principal("snowman");
Principal principal = new Principal(snowmanIdentifier.principalName());

try (Response createPResponse =
testEnv
Expand All @@ -105,7 +109,8 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
.apiClient()
.target(
String.format(
"%s/api/management/v1/principals/%s/rotate", testEnv.baseUri(), "snowman"))
"%s/api/management/v1/principals/%s/rotate",
testEnv.baseUri(), principal.getName()))
.request(MediaType.APPLICATION_JSON)
.header(
"Authorization",
Expand All @@ -127,14 +132,16 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
snowmanCredentials =
new SnowmanCredentials(
snowmanWithCredentials.getCredentials().getClientId(),
snowmanWithCredentials.getCredentials().getClientSecret());
snowmanWithCredentials.getCredentials().getClientSecret(),
snowmanIdentifier);
}
try (Response assignPrResponse =
testEnv
.apiClient()
.target(
String.format(
"%s/api/management/v1/principals/snowman/principal-roles", testEnv.baseUri()))
"%s/api/management/v1/principals/%s/principal-roles",
testEnv.baseUri(), principal.getName()))
.request("application/json")
.header("Authorization", "Bearer " + userToken) // how is token getting used?
.header(REALM_PROPERTY_KEY, realm)
Expand Down Expand Up @@ -169,11 +176,13 @@ public void afterAll(ExtensionContext extensionContext) throws Exception {
adminSecrets.getMainSecret(),
realm);

SnowmanIdentifier snowmanIdentifier = getSnowmanIdentifier(testEnv);
testEnv
.apiClient()
.target(
String.format(
"%s/api/management/v1/principal-roles/%s", testEnv.baseUri(), "catalog-admin"))
"%s/api/management/v1/principal-roles/%s",
testEnv.baseUri(), snowmanIdentifier.principalRoleName()))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
Expand All @@ -182,7 +191,10 @@ public void afterAll(ExtensionContext extensionContext) throws Exception {

testEnv
.apiClient()
.target(String.format("%s/api/management/v1/principals/%s", testEnv.baseUri(), "snowman"))
.target(
String.format(
"%s/api/management/v1/principals/%s",
testEnv.baseUri(), snowmanIdentifier.principalName()))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
Expand All @@ -208,4 +220,8 @@ public Object resolveParameter(
throws ParameterResolutionException {
return snowmanCredentials;
}

private static SnowmanIdentifier getSnowmanIdentifier(TestEnvironment testEnv) {
return new SnowmanIdentifier("snowman" + testEnv.testId(), "catalog-admin" + testEnv.testId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@

import jakarta.ws.rs.client.Client;
import java.net.URI;
import java.util.UUID;

/** Defines the test environment that a test should run in. */
public record TestEnvironment(Client apiClient, URI baseUri) {
/**
* Defines the test environment that a test should run in.
*
* @param apiClient The HTTP client to use when making requests
* @param baseUri The base URL that requests should target, for example http://localhost:1234
* @param testId An ID unique to this test. This can be used to prefix resource names, such as
* catalog names, to prevent collision.
*/
public record TestEnvironment(Client apiClient, URI baseUri, String testId) {
public TestEnvironment(Client apiClient, String baseUri) {
this(apiClient, URI.create(baseUri));
this(apiClient, URI.create(baseUri), UUID.randomUUID().toString().replace("-", ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ public class TestEnvironmentExtension implements ParameterResolver {
public static final String ENV_TEST_ENVIRONMENT_RESOLVER_IMPL =
"INTEGRATION_TEST_ENVIRONMENT_RESOLVER_IMPL";

private static final String ENV_PROPERTY_KEY = "testenvironment";

public static TestEnvironment getEnv(ExtensionContext extensionContext)
throws IllegalAccessException {
return getTestEnvironmentResolver().resolveTestEnvironment(extensionContext);
// This must be cached because the TestEnvironment has a randomly generated ID
return extensionContext
.getStore(ExtensionContext.Namespace.create(extensionContext.getRequiredTestClass()))
.getOrComputeIfAbsent(
ENV_PROPERTY_KEY,
(k) -> getTestEnvironmentResolver().resolveTestEnvironment(extensionContext),
TestEnvironment.class);
}

@Override
Expand Down

0 comments on commit 81d8eb1

Please sign in to comment.