Skip to content

Commit

Permalink
Switch EventHubsExporterIntegrationTest to managed identity (#43181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanbisutti authored Feb 28, 2025
1 parent 3bf1aea commit 8ae755a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

package com.azure.monitor.opentelemetry.autoconfigure;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpPipelineCallContext;
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.test.annotation.LiveOnly;
import com.azure.core.util.Configuration;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.data.appconfiguration.ConfigurationClient;
Expand Down Expand Up @@ -37,6 +39,15 @@

@LiveOnly
public class AppConfigurationExporterIntegrationTest extends MonitorExporterClientTestBase {

private TokenCredential credential;

@Override
public void beforeTest() {
super.beforeTest();
credential = TokenCredentialUtil.getTestTokenCredential(interceptorManager);
}

@Test
public void setConfigurationTest() throws InterruptedException {
CountDownLatch exporterCountDown = new CountDownLatch(1);
Expand Down Expand Up @@ -87,8 +98,9 @@ public void testDisableTracing() throws InterruptedException {
assertTrue(exporterCountDown.await(60, TimeUnit.SECONDS));
}

private static ConfigurationClient getConfigurationClient() {
return new ConfigurationClientBuilder().connectionString(System.getenv("AZURE_APPCONFIG_CONNECTION_STRING"))
private ConfigurationClient getConfigurationClient() {
return new ConfigurationClientBuilder().credential(credential)
.endpoint(Configuration.getGlobalConfiguration().get("MONITOR_RESOURCE_MANAGER_URL"))
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.monitor.opentelemetry.autoconfigure;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.test.annotation.LiveOnly;
import com.azure.core.util.FluxUtil;
Expand Down Expand Up @@ -37,6 +38,14 @@ public class EventHubsExporterIntegrationTest extends MonitorExporterClientTestB
private static final String STORAGE_CONNECTION_STRING = System.getenv("STORAGE_CONNECTION_STRING");
private static final String CONTAINER_NAME = System.getenv("STORAGE_CONTAINER_NAME");

private TokenCredential credential;

@Override
public void beforeTest() {
super.beforeTest();
credential = TokenCredentialUtil.getTestTokenCredential(interceptorManager);
}

@Test
public void producerTest() throws InterruptedException {
CountDownLatch exporterCountDown = new CountDownLatch(2);
Expand All @@ -55,8 +64,10 @@ public void producerTest() throws InterruptedException {
return next.process();
};
Tracer tracer = TestUtils.createOpenTelemetrySdk(getHttpPipeline(validationPolicy)).getTracer("Sample");
EventHubProducerAsyncClient producer
= new EventHubClientBuilder().connectionString(CONNECTION_STRING).buildAsyncProducerClient();
EventHubProducerAsyncClient producer = new EventHubClientBuilder().credential(credential)
.fullyQualifiedNamespace("namespace")
.eventHubName("event-hub")
.buildAsyncProducerClient();
Span span = tracer.spanBuilder(spanName).startSpan();
Scope scope = span.makeCurrent();
try {
Expand All @@ -68,7 +79,7 @@ public void producerTest() throws InterruptedException {
span.end();
scope.close();
}
assertTrue(exporterCountDown.await(5, TimeUnit.SECONDS));
assertTrue(exporterCountDown.await(60, TimeUnit.SECONDS));
}

@Disabled("Processor integration tests require separate consumer group to not have partition contention in CI - https://github.com/Azure/azure-sdk-for-java/issues/23567")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.opentelemetry.autoconfigure;

import com.azure.core.credential.TokenCredential;
import com.azure.core.test.InterceptorManager;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.identity.AzurePowerShellCredentialBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder;

class TokenCredentialUtil {

/**
* Gets a token credential for use in tests.
* @param interceptorManager the interceptor manager
* @return the TokenCredential
*/
static TokenCredential getTestTokenCredential(InterceptorManager interceptorManager) {
if (interceptorManager.isLiveMode()) {
return new AzurePowerShellCredentialBuilder().build();
} else if (interceptorManager.isRecordMode()) {
return new DefaultAzureCredentialBuilder().build();
} else {
return new MockTokenCredential();
}
}

}

0 comments on commit 8ae755a

Please sign in to comment.