diff --git a/sdk/elasticsan/azure-resourcemanager-elasticsan/pom.xml b/sdk/elasticsan/azure-resourcemanager-elasticsan/pom.xml index a6b86fc0dcbc3..c3b730730be12 100644 --- a/sdk/elasticsan/azure-resourcemanager-elasticsan/pom.xml +++ b/sdk/elasticsan/azure-resourcemanager-elasticsan/pom.xml @@ -69,6 +69,12 @@ 1.11.2 test + + com.azure.resourcemanager + azure-resourcemanager-resources + 2.35.0 + test + org.junit.jupiter junit-jupiter-api diff --git a/sdk/elasticsan/azure-resourcemanager-elasticsan/src/test/java/com/azure/resourcemanager/elasticsan/ElasticSanManagerTests.java b/sdk/elasticsan/azure-resourcemanager-elasticsan/src/test/java/com/azure/resourcemanager/elasticsan/ElasticSanManagerTests.java new file mode 100644 index 0000000000000..ac44f5f6a0ffc --- /dev/null +++ b/sdk/elasticsan/azure-resourcemanager-elasticsan/src/test/java/com/azure/resourcemanager/elasticsan/ElasticSanManagerTests.java @@ -0,0 +1,101 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.resourcemanager.elasticsan; + +import com.azure.core.credential.TokenCredential; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.management.AzureEnvironment; +import com.azure.core.management.Region; +import com.azure.core.management.profile.AzureProfile; +import com.azure.core.test.TestBase; +import com.azure.core.test.annotation.DoNotRecord; +import com.azure.core.util.Configuration; +import com.azure.core.util.CoreUtils; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.resourcemanager.elasticsan.models.ElasticSan; +import com.azure.resourcemanager.elasticsan.models.Sku; +import com.azure.resourcemanager.elasticsan.models.SkuName; +import com.azure.resourcemanager.elasticsan.models.SkuTier; +import com.azure.resourcemanager.resources.ResourceManager; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Random; + +public class ElasticSanManagerTests extends TestBase { + private static final Random RANDOM = new Random(); + private static final Region REGION = Region.US_EAST; + private String resourceGroupName = "rg" + randomPadding(); + private ElasticSanManager elasticSanManager = null; + private ResourceManager resourceManager; + private boolean testEnv; + + @Override + public void beforeTest() { + final TokenCredential credential = new DefaultAzureCredentialBuilder().build(); + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); + + elasticSanManager = ElasticSanManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile); + + resourceManager = ResourceManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile) + .withDefaultSubscription(); + + // use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI + String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME"); + testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup); + if (testEnv) { + resourceGroupName = testResourceGroup; + } else { + resourceManager.resourceGroups() + .define(resourceGroupName) + .withRegion(REGION) + .create(); + } + } + + @Override + protected void afterTest() { + if (!testEnv) { + resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); + } + } + + @Test + @DoNotRecord(skipInPlayback = true) + public void testCreateElasticSan() { + ElasticSan elasticSan = null; + try { + String elasticSanName = "elasticsan" + randomPadding(); + // @embedStart + elasticSan = elasticSanManager.elasticSans() + .define(elasticSanName) + .withRegion(REGION) + .withExistingResourceGroup(resourceGroupName) + .withSku(new Sku().withName(SkuName.PREMIUM_LRS).withTier(SkuTier.PREMIUM)) + .withBaseSizeTiB(1L) + .withExtendedCapacitySizeTiB(1L) + .create(); + // @embedEnd + elasticSan.refresh(); + Assertions.assertEquals(elasticSan.name(), elasticSanName); + Assertions.assertEquals(elasticSan.name(), elasticSanManager.elasticSans().getById(elasticSan.id()).name()); + Assertions.assertTrue(elasticSanManager.elasticSans().listByResourceGroup(resourceGroupName).stream().findAny().isPresent()); + } finally { + if (elasticSan != null) { + elasticSanManager.elasticSans().deleteById(elasticSan.id()); + } + } + } + + private static String randomPadding() { + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); + } +} diff --git a/sdk/elasticsan/test-resources.bicep b/sdk/elasticsan/test-resources.bicep new file mode 100644 index 0000000000000..2250946806c02 --- /dev/null +++ b/sdk/elasticsan/test-resources.bicep @@ -0,0 +1,27 @@ +@description('The tenant id to which the application and resources belong.') +param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47' + +@description('The client id of the service principal used to run tests.') +param testApplicationId string + +@description('This is the object id of the service principal used to run tests.') +param testApplicationOid string + +@description('The application client secret used to run tests.') +param testApplicationSecret string + +var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c' + +resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = { + name: guid('contributorRoleId${resourceGroup().name}') + properties: { + roleDefinitionId: contributorRoleId + principalId: testApplicationOid + } +} + +output AZURE_TENANT_ID string = tenantId +output AZURE_CLIENT_ID string = testApplicationId +output AZURE_CLIENT_SECRET string = testApplicationSecret +output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId +output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name diff --git a/sdk/elasticsan/tests.mgmt.yml b/sdk/elasticsan/tests.mgmt.yml new file mode 100644 index 0000000000000..a24b736d1f959 --- /dev/null +++ b/sdk/elasticsan/tests.mgmt.yml @@ -0,0 +1,16 @@ +trigger: none + +pr: none + +stages: + - template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml + parameters: + ServiceDirectory: elasticsan + Artifacts: + - name: azure-resourcemanager-elasticsan + groupId: com.azure.resourcemanager + safeName: azureresourcemanagerelasticsan + Clouds: 'Public' + # Only run tests on Windows to save cost. + MatrixFilters: + - pool=.*(win).*