Skip to content

Commit be95b98

Browse files
authored
Fix integration tests removing local auth for Event Hubs & Service Bus (Azure#43934)
* Disable local auth when creating resources. * Enable use of credential in impl management tests. * Move common sanitizers into testutils * Move common credential logic into TestUtils. * Disable local auth * Add supplementary header * Disable if environment variable not set. * Fix env key.
1 parent 63fcd73 commit be95b98

File tree

10 files changed

+235
-125
lines changed

10 files changed

+235
-125
lines changed

sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/IntegrationTestBase.java

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
import com.azure.core.util.ConfigurationBuilder;
1616
import com.azure.core.util.CoreUtils;
1717
import com.azure.core.util.logging.ClientLogger;
18-
import com.azure.identity.DefaultAzureCredentialBuilder;
1918
import com.azure.messaging.eventhubs.models.SendOptions;
2019
import org.junit.jupiter.api.AfterEach;
2120
import org.junit.jupiter.api.Assertions;
22-
import org.junit.jupiter.api.Assumptions;
2321
import org.junit.jupiter.api.BeforeEach;
2422
import org.mockito.Mockito;
2523
import reactor.core.Disposable;
@@ -38,6 +36,11 @@
3836
import java.util.stream.Collectors;
3937
import java.util.stream.IntStream;
4038

39+
import static com.azure.messaging.eventhubs.TestUtils.AZURE_EVENTHUBS_EVENT_HUB_NAME;
40+
import static com.azure.messaging.eventhubs.TestUtils.AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME;
41+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
42+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
43+
4144
/**
4245
* Test base for running integration tests.
4346
*/
@@ -61,9 +64,6 @@ public abstract class IntegrationTestBase extends TestBase {
6164

6265
protected String testName;
6366

64-
private static final ClientOptions OPTIONS_WITH_TRACING
65-
= new ClientOptions().setTracingOptions(new LoggingTracerProvider.LoggingTracingOptions());
66-
6767
private Scheduler scheduler;
6868
private static Map<String, IntegrationTestEventData> testEventData;
6969
private List<AutoCloseable> toClose = new ArrayList<>();
@@ -81,7 +81,7 @@ public void setupTest(TestContextManager testContextManager) {
8181
testContextManager.getTestPlaybackRecordingName());
8282

8383
testName = testContextManager.getTrackerTestName();
84-
skipIfNotRecordMode();
84+
skipIfNotRecordOrLiveMode();
8585
toClose = new ArrayList<>();
8686

8787
scheduler = Schedulers.newParallel("eh-integration");
@@ -116,62 +116,56 @@ public void teardownTest() {
116116
}
117117

118118
/**
119-
* Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings and uses a
120-
* connection string to authenticate.
119+
* Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings and does not
120+
* share a connection string.
121121
*/
122122
protected EventHubClientBuilder createBuilder() {
123123
return createBuilder(false);
124124
}
125125

126126
/**
127-
* Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings and uses a
128-
* connection string to authenticate if {@code useCredentials} is false. Otherwise, uses a service principal through
129-
* {@link com.azure.identity.ClientSecretCredential}.
127+
* Creates a new instance of {@link EventHubClientBuilder} with the default integration test settings. Assumes test
128+
* mode is not {@link TestMode#PLAYBACK}.
130129
*/
131130
protected EventHubClientBuilder createBuilder(boolean shareConnection) {
131+
skipIfNotRecordOrLiveMode();
132+
133+
// Enables tracing.
134+
final ClientOptions clientOptions
135+
= new ClientOptions().setTracingOptions(new LoggingTracerProvider.LoggingTracingOptions());
136+
132137
final EventHubClientBuilder builder = new EventHubClientBuilder().proxyOptions(ProxyOptions.SYSTEM_DEFAULTS)
133138
.retryOptions(RETRY_OPTIONS)
134-
.clientOptions(OPTIONS_WITH_TRACING)
139+
.clientOptions(clientOptions)
135140
.transportType(AmqpTransportType.AMQP)
136141
.scheduler(scheduler)
137142
.configuration(new ConfigurationBuilder().putProperty("com.azure.messaging.eventhubs.v2", "true").build());
138143

139-
final String fullyQualifiedDomainName = TestUtils.getFullyQualifiedDomainName();
140-
final String eventHubName = TestUtils.getEventHubName();
141-
142144
if (shareConnection) {
143145
builder.shareConnection();
144146
}
145147

146-
switch (getTestMode()) {
147-
case PLAYBACK:
148-
Assumptions.assumeTrue(false, "Integration tests are not enabled in playback mode.");
149-
return null;
150-
151-
case LIVE:
152-
Assumptions.assumeTrue(!CoreUtils.isNullOrEmpty(fullyQualifiedDomainName),
153-
"FullyQualifiedDomainName is not set.");
154-
Assumptions.assumeTrue(!CoreUtils.isNullOrEmpty(fullyQualifiedDomainName), "EventHubName is not set.");
155-
156-
final TokenCredential credential = TestUtils.getPipelineCredential(credentialCached);
157-
return builder.credential(fullyQualifiedDomainName, eventHubName, credential);
158-
159-
case RECORD:
160-
final String connectionString = TestUtils.getConnectionString(false);
161-
162-
Assumptions.assumeTrue(!CoreUtils.isNullOrEmpty(eventHubName), "EventHubName is not set.");
163-
Assumptions.assumeTrue(!CoreUtils.isNullOrEmpty(fullyQualifiedDomainName),
164-
"FullyQualifiedDomainName is not set.");
165-
166-
if (CoreUtils.isNullOrEmpty(connectionString)) {
167-
final TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
168-
return builder.credential(fullyQualifiedDomainName, eventHubName, tokenCredential);
169-
} else {
170-
return builder.connectionString(connectionString).eventHubName(eventHubName);
171-
}
172-
default:
173-
return null;
148+
final TokenCredential tokenCredential = TestUtils.getTokenCredential(getTestMode(), credentialCached);
149+
150+
assumeTrue(tokenCredential != null, "Token credential could not be created using " + getTestMode());
151+
152+
if (getTestMode() == TestMode.PLAYBACK) {
153+
return builder.credential(tokenCredential);
174154
}
155+
156+
final String fullyQualifiedDomainName = TestUtils.getFullyQualifiedDomainName();
157+
final String eventHubName = TestUtils.getEventHubName();
158+
159+
assumeFalse(CoreUtils.isNullOrEmpty(fullyQualifiedDomainName),
160+
"Env variable: '" + AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME + "' must be set.");
161+
assumeFalse(CoreUtils.isNullOrEmpty(eventHubName),
162+
"Env variable: '" + AZURE_EVENTHUBS_EVENT_HUB_NAME + "' must be set.");
163+
164+
return builder.credential(fullyQualifiedDomainName, eventHubName, tokenCredential);
165+
}
166+
167+
protected TokenCredential getOrCreateTokenCredential() {
168+
return TestUtils.getTokenCredential(getTestMode(), credentialCached);
175169
}
176170

177171
/**
@@ -247,8 +241,7 @@ protected void dispose() {
247241
toClose.clear();
248242
}
249243

250-
private void skipIfNotRecordMode() {
251-
Assumptions.assumeTrue(getTestMode() != TestMode.PLAYBACK, "Is not in RECORD/LIVE mode.");
244+
private void skipIfNotRecordOrLiveMode() {
245+
assumeTrue(getTestMode() != TestMode.PLAYBACK, "Is not in RECORD/LIVE mode.");
252246
}
253-
254247
}

sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/NonFederatedIntegrationTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.azure.messaging.eventhubs.models.SendOptions;
1616
import org.junit.jupiter.api.Assertions;
1717
import org.junit.jupiter.api.Assumptions;
18+
import org.junit.jupiter.api.Tag;
1819
import org.junit.jupiter.api.Test;
1920
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2021
import reactor.test.StepVerifier;
@@ -23,6 +24,7 @@
2324
import static java.nio.charset.StandardCharsets.UTF_8;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
2526

27+
@Tag("integration")
2628
public class NonFederatedIntegrationTests extends IntegrationTestBase {
2729
private static final ClientLogger LOGGER = new ClientLogger(NonFederatedIntegrationTests.class);
2830
private static final String PARTITION_ID = "2";
@@ -34,8 +36,8 @@ public class NonFederatedIntegrationTests extends IntegrationTestBase {
3436
@Test
3537
@EnabledIfEnvironmentVariable(
3638
named = "AZURE_EVENTHUBS_CONNECTION_STRING_WITH_SAS",
37-
matches = ".*ShadAccessSignature .*")
38-
void sendWithSasConnectionString() {
39+
matches = ".*SharedAccessSignature.*")
40+
public void sendWithSasConnectionString() {
3941
final String eventHubName = TestUtils.getEventHubName();
4042
final EventData event = new EventData("body");
4143
final SendOptions options = new SendOptions().setPartitionId(PARTITION_ID);
@@ -54,6 +56,7 @@ void sendWithSasConnectionString() {
5456
}
5557

5658
@Test
59+
@EnabledIfEnvironmentVariable(named = "AZURE_EVENTHUBS_CONNECTION_STRING", matches = ".*SharedAccessKey.*")
5760
public void sendAndReceiveEventByAzureSasCredential() {
5861
Assumptions.assumeTrue(TestUtils.getConnectionString(true) != null,
5962
"SAS was not set. Can't run test scenario.");
@@ -76,6 +79,7 @@ public void sendAndReceiveEventByAzureSasCredential() {
7679
}
7780

7881
@Test
82+
@EnabledIfEnvironmentVariable(named = "AZURE_EVENTHUBS_CONNECTION_STRING", matches = ".*SharedAccessKey.*")
7983
public void sendAndReceiveEventByAzureNameKeyCredential() {
8084
ConnectionStringProperties properties = getConnectionStringProperties();
8185
String fullyQualifiedNamespace = properties.getEndpoint().getHost();
@@ -101,6 +105,7 @@ public void sendAndReceiveEventByAzureNameKeyCredential() {
101105
* Verifies that error conditions are handled for fetching Event Hub metadata.
102106
*/
103107
@Test
108+
@EnabledIfEnvironmentVariable(named = "AZURE_EVENTHUBS_CONNECTION_STRING", matches = ".*SharedAccessKey.*")
104109
public void getPartitionPropertiesInvalidToken() {
105110
// Arrange
106111
final ConnectionStringProperties original = getConnectionStringProperties();
@@ -127,6 +132,7 @@ public void getPartitionPropertiesInvalidToken() {
127132
* Verifies that error conditions are handled for fetching partition metadata.
128133
*/
129134
@Test
135+
@EnabledIfEnvironmentVariable(named = "AZURE_EVENTHUBS_CONNECTION_STRING", matches = ".*SharedAccessKey.*")
130136
public void getPartitionPropertiesNonExistentHub() {
131137
// Arrange
132138
final ConnectionStringProperties original = getConnectionStringProperties();

0 commit comments

Comments
 (0)