Skip to content
Merged
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
1 change: 1 addition & 0 deletions eng/versioning/version_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ unreleased_com.azure.v2:azure-core;2.0.0-beta.1
unreleased_com.azure.v2:azure-identity;2.0.0-beta.1
unreleased_io.clientcore:http-netty4;1.0.0-beta.1
unreleased_com.azure.resourcemanager:azure-resourcemanager-storage;2.54.0-beta.1
unreleased_com.azure.resourcemanager:azure-resourcemanager-test;2.0.0-beta.2

# Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current
# version and set the version to the released beta. Released beta dependencies are only valid
Expand Down
6 changes: 6 additions & 0 deletions sdk/netapp/azure-resourcemanager-netapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,11 @@
<version>2.53.1</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-test</artifactId>
<version>2.0.0-beta.2</version> <!-- {x-version-update;unreleased_com.azure.resourcemanager:azure-resourcemanager-test;dependency} -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.core.test.InterceptorManager;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.annotation.LiveOnly;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.identity.AzureCliCredentialBuilder;
import com.azure.identity.AzureDeveloperCliCredentialBuilder;
import com.azure.identity.AzurePipelinesCredentialBuilder;
import com.azure.identity.AzurePowerShellCredentialBuilder;
import com.azure.identity.ChainedTokenCredentialBuilder;
import com.azure.identity.EnvironmentCredentialBuilder;
import com.azure.resourcemanager.netapp.models.NetAppAccount;
import com.azure.resourcemanager.resources.ResourceManager;
import com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy;
import com.azure.resourcemanager.test.utils.TestUtilities;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -39,7 +32,7 @@ public class NetAppFilesManagerTests extends TestProxyTestBase {

@Override
public void beforeTest() {
final TokenCredential credential = getIdentityTestCredential(super.interceptorManager);
final TokenCredential credential = TestUtilities.getTokenCredentialForTest(getTestMode());
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);

resourceManager = ResourceManager.configure()
Expand Down Expand Up @@ -96,42 +89,4 @@ protected void afterTest() {
private static String randomPadding() {
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
}

/**
* Gets a token credential for use in tests.
* @param interceptorManager the interceptor manager
* @return the TokenCredential
*/
private static TokenCredential getIdentityTestCredential(InterceptorManager interceptorManager) {
if (interceptorManager.isPlaybackMode()) {
return new MockTokenCredential();
}

Configuration config = Configuration.getGlobalConfiguration();

ChainedTokenCredentialBuilder builder
= new ChainedTokenCredentialBuilder().addLast(new EnvironmentCredentialBuilder().build())
.addLast(new AzureCliCredentialBuilder().build())
.addLast(new AzureDeveloperCliCredentialBuilder().build());

String serviceConnectionId = config.get("AZURESUBSCRIPTION_SERVICE_CONNECTION_ID");
String clientId = config.get("AZURESUBSCRIPTION_CLIENT_ID");
String tenantId = config.get("AZURESUBSCRIPTION_TENANT_ID");
String systemAccessToken = config.get("SYSTEM_ACCESSTOKEN");

if (!CoreUtils.isNullOrEmpty(serviceConnectionId)
&& !CoreUtils.isNullOrEmpty(clientId)
&& !CoreUtils.isNullOrEmpty(tenantId)
&& !CoreUtils.isNullOrEmpty(systemAccessToken)) {

builder.addLast(new AzurePipelinesCredentialBuilder().systemAccessToken(systemAccessToken)
.clientId(clientId)
.tenantId(tenantId)
.serviceConnectionId(serviceConnectionId)
.build());
}

builder.addLast(new AzurePowerShellCredentialBuilder().build());
return builder.build();
}
}
8 changes: 2 additions & 6 deletions sdk/resourcemanager/azure-resourcemanager-test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Release History

## 2.0.0-beta.2 (Unreleased)
## 2.0.0-beta.2 (2025-08-20)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
- Added `getTokenCredentialForTest` to `TestUtilities`.

## 2.0.0-beta.1 (2025-08-12)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.test.models.TestProxySanitizerType;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.test.utils.ResourceNamer;
import com.azure.core.util.Configuration;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.resourcemanager.test.model.AzureUser;
import com.azure.resourcemanager.test.policy.HttpDebugLoggingPolicy;
import com.azure.resourcemanager.test.utils.CliRunner;
import com.azure.resourcemanager.test.utils.TestUtilities;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
Expand Down Expand Up @@ -305,10 +304,12 @@ protected void beforeTest() {
}
}

credential = TestUtilities.getTokenCredentialForTest(getTestMode());

if (isPlaybackMode()) {
testProfile = PLAYBACK_PROFILE;
List<HttpPipelinePolicy> policies = new ArrayList<>();
httpPipeline = buildHttpPipeline(new MockTokenCredential(), testProfile,
httpPipeline = buildHttpPipeline(credential, testProfile,
new HttpLogOptions().setLogLevel(httpLogDetailLevel), policies, interceptorManager.getPlaybackClient());
if (!testContextManager.doNotRecordTest()) {
// don't match api-version when matching url
Expand All @@ -327,9 +328,6 @@ protected void beforeTest() {
= Objects.requireNonNull(configuration.get(Configuration.PROPERTY_AZURE_SUBSCRIPTION_ID),
"'AZURE_SUBSCRIPTION_ID' environment variable cannot be null.");
testProfile = new AzureProfile(tenantId, subscriptionId, AzureCloud.AZURE_PUBLIC_CLOUD);
credential = new DefaultAzureCredentialBuilder()
.authorityHost(testProfile.getEnvironment().getActiveDirectoryEndpoint())
.build();

List<HttpPipelinePolicy> policies = new ArrayList<>();
if (interceptorManager.isRecordMode() && !testContextManager.doNotRecordTest()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@

package com.azure.resourcemanager.test.utils;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.test.TestMode;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.identity.AzureCliCredentialBuilder;
import com.azure.identity.AzureDeveloperCliCredentialBuilder;
import com.azure.identity.AzurePipelinesCredentialBuilder;
import com.azure.identity.AzurePowerShellCredentialBuilder;
import com.azure.identity.ChainedTokenCredentialBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.EnvironmentCredentialBuilder;

import java.util.Iterator;

Expand Down Expand Up @@ -59,4 +71,70 @@ public static <T> int getSize(Iterable<T> iterable) {
public static <T> boolean isEmpty(PagedIterable<T> iterable) {
return !iterable.iterator().hasNext();
}

/**
* Creates a comprehensive {@link TokenCredential} chain optimized for test environments.
* <p>
* This method constructs a credential chain that attempts multiple authentication methods
* in a specific order, making it suitable for various testing scenarios including local
* development and live tests in CI/CD pipelines.
* </p>
*
* <strong>Azure Pipelines Configuration:</strong><br>
* For Azure Pipelines authentication, the following environment variables must be set:
* <ul>
* <li>{@code AZURESUBSCRIPTION_SERVICE_CONNECTION_ID}</li>
* <li>{@code AZURESUBSCRIPTION_CLIENT_ID}</li>
* <li>{@code AZURESUBSCRIPTION_TENANT_ID}</li>
* <li>{@code SYSTEM_ACCESSTOKEN}</li>
* </ul>
*
* <strong>Local run Configuration:</strong><br>
* For local run authentication, use Azure CLI for example, the following environment variables must be set:
* <ul>
* <li>{@code AZURE_SUBSCRIPTION_ID}</li>
* <li>{@code AZURE_TENANT_ID}</li>
* </ul>
*
* @param testMode {@link TestMode} that the test is running in, usually set through {@code AZURE_TEST_MODE} env var
* @return a {@link TokenCredential} appropriate for the test environment:
* {@link MockTokenCredential} for playback mode, or a
* {@link com.azure.identity.ChainedTokenCredential} for live testing
* @see MockTokenCredential
* @see com.azure.identity.ChainedTokenCredential
*/
public static TokenCredential getTokenCredentialForTest(TestMode testMode) {
if (testMode == TestMode.LIVE) {
Configuration config = Configuration.getGlobalConfiguration();

ChainedTokenCredentialBuilder builder
= new ChainedTokenCredentialBuilder().addLast(new EnvironmentCredentialBuilder().build())
.addLast(new AzureCliCredentialBuilder().build())
.addLast(new AzureDeveloperCliCredentialBuilder().build());

String serviceConnectionId = config.get("AZURESUBSCRIPTION_SERVICE_CONNECTION_ID");
String clientId = config.get("AZURESUBSCRIPTION_CLIENT_ID");
String tenantId = config.get("AZURESUBSCRIPTION_TENANT_ID");
String systemAccessToken = config.get("SYSTEM_ACCESSTOKEN");

if (!CoreUtils.isNullOrEmpty(serviceConnectionId)
&& !CoreUtils.isNullOrEmpty(clientId)
&& !CoreUtils.isNullOrEmpty(tenantId)
&& !CoreUtils.isNullOrEmpty(systemAccessToken)) {

builder.addLast(new AzurePipelinesCredentialBuilder().systemAccessToken(systemAccessToken)
.clientId(clientId)
.tenantId(tenantId)
.serviceConnectionId(serviceConnectionId)
.build());
}

builder.addLast(new AzurePowerShellCredentialBuilder().build());
return builder.build();
} else if (testMode == TestMode.RECORD) {
return new DefaultAzureCredentialBuilder().build();
} else {
return new MockTokenCredential();
}
}
}