Skip to content

Commit

Permalink
Adjust unit tests because of a problem with combineDependencies in th…
Browse files Browse the repository at this point in the history
…e generated sources
  • Loading branch information
klustria committed Feb 10, 2025
1 parent 9253978 commit 309fb70
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 272 deletions.
10 changes: 0 additions & 10 deletions integrations/oci/authentication/instance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,24 @@

import java.util.Properties;

import io.helidon.webserver.WebServer;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;

import io.helidon.service.registry.ServiceRegistry;
import io.helidon.service.registry.ServiceRegistryManager;

import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ServerTest
public class AuthenticationMethodInstancePrincipalTest {
private static ServiceRegistryManager registryManager;
private static ServiceRegistry registry;
private static String imdsBaseUri;

AuthenticationMethodInstancePrincipalTest(WebServer server) {
imdsBaseUri = "http://localhost:%d/opc/v2/".formatted(server.port());
}

@SetUpRoute
static void routing(HttpRules rules) {
rules.get("/opc/v2/instance", ImdsEmulator::emulateImdsInstance);
}

void setUp(Properties p) {
p.put("helidon.oci.authentication-method", "instance-principal");
p.put("helidon.oci.imds-timeout", "PT1S");
p.put("helidon.oci.imds-detect-retries", "0");
p.put("helidon.oci.imds-base-uri", imdsBaseUri);
System.setProperties(p);

registryManager = ServiceRegistryManager.create();
Expand All @@ -73,37 +52,20 @@ void cleanUp() {

@Test
public void testInstancePrincipalConfigurationAndInstantiation() {
final String IMDS_BASE_URI = "http://localhost:8000/opc/v2/";
final String FEDERATION_ENDPOINT = "https://auth.us-myregion-1.oraclecloud.com";
final String TENANT_ID = "ocid1.tenancy.oc1..mytenancyid";

Properties p = System.getProperties();
p.put("helidon.oci.imds-base-uri", IMDS_BASE_URI);
p.put("helidon.oci.federation-endpoint", FEDERATION_ENDPOINT);
p.put("helidon.oci.tenant-id", TENANT_ID);
setUp(p);

// This error indicates that the instance principal provider has been instantiated
var thrown = assertThrows(IllegalArgumentException.class,
() -> registry.get(BasicAuthenticationDetailsProvider.class));
assertThat(thrown.getMessage(),
containsString(MockedInstancePrincipalBuilderProvider.INSTANCE_PRINCIPAL_INSTANTIATION_MESSAGE));

var builder = registry.get(InstancePrincipalsAuthenticationDetailsProvider.InstancePrincipalsAuthenticationDetailsProviderBuilder.class);
// The following validation indicates that the instance principal provider has been configured properly
assertThat(MockedAuthenticationMethodInstancePrincipal.getBuilder().getMetadataBaseUrl(), is(imdsBaseUri));
assertThat(MockedAuthenticationMethodInstancePrincipal.getBuilder().getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(MockedAuthenticationMethodInstancePrincipal.getBuilder().getTenancyId(), is(TENANT_ID));
}

public static class ImdsEmulator {
// This will allow HelidonOci.imdsAvailable() to be tested by making the server that simulates IMDS to return a JSON value
// when instance property is queried
private static final String IMDS_INSTANCE_RESPONSE = """
{
"displayName": "helidon-server"
}
""";

private static void emulateImdsInstance(ServerRequest req, ServerResponse res) {
res.send(IMDS_INSTANCE_RESPONSE);
}
assertThat(builder.getMetadataBaseUrl(), is(IMDS_BASE_URI));
assertThat(builder.getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(builder.getTenancyId(), is(TENANT_ID));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.helidon.service.registry.ServiceRegistryManager;

import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.okeworkloadidentity.OkeWorkloadIdentityAuthenticationDetailsProvider.OkeWorkloadIdentityAuthenticationDetailsProviderBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -62,10 +63,12 @@ public void testOkeWorkloadIdentityConfigurationAndInstantiation() {

// This error indicates that the oke-workload-identity provider has been instantiated
var thrown = assertThrows(IllegalArgumentException.class,
() -> registry.get(BasicAuthenticationDetailsProvider.class));
() -> registry.get(BasicAuthenticationDetailsProvider.class));
assertThat(thrown.getMessage(), containsString("Invalid Kubernetes ca certification"));

var builder = registry.get(OkeWorkloadIdentityAuthenticationDetailsProviderBuilder.class);
// The following validation indicates that the oke-workload-identity provider has been configured properly
assertThat(MockedAuthenticationMethodOkeWorkload.getBuilder().getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(MockedAuthenticationMethodOkeWorkload.getBuilder().getTenancyId(), is(TENANT_ID));
assertThat(builder.getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(builder.getTenancyId(), is(TENANT_ID));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.helidon.service.registry.ServiceRegistryManager;

import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.ResourcePrincipalAuthenticationDetailsProvider.ResourcePrincipalAuthenticationDetailsProviderBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -65,8 +66,10 @@ public void testResourcePrincipalConfigurationAndInstantiation() {
() -> registry.get(BasicAuthenticationDetailsProvider.class));
assertThat(thrown.getMessage(),
containsString("Resource principals authentication can only be used in certain OCI services"));

var builder = registry.get(ResourcePrincipalAuthenticationDetailsProviderBuilder.class);
// The following validation indicates that the resource principal provider has been configured properly
assertThat(MockedAuthenticationMethodResourcePrincipal.getBuilder().getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(MockedAuthenticationMethodResourcePrincipal.getBuilder().getTenancyId(), is(TENANT_ID));
assertThat(builder.getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(builder.getTenancyId(), is(TENANT_ID));
}
}

This file was deleted.

0 comments on commit 309fb70

Please sign in to comment.