forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mgmt, sphere, add live tests (Azure#40443)
* mgmt, sphere, add live tests
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...anager-sphere/src/test/java/com/azure/resourcemanager/sphere/AzureSphereManagerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.resourcemanager.sphere; | ||
|
||
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.LiveOnly; | ||
import com.azure.core.util.Configuration; | ||
import com.azure.core.util.CoreUtils; | ||
import com.azure.identity.DefaultAzureCredentialBuilder; | ||
import com.azure.resourcemanager.resources.ResourceManager; | ||
import com.azure.resourcemanager.sphere.models.Catalog; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Random; | ||
|
||
public class AzureSphereManagerTests extends TestBase { | ||
private static final Random RANDOM = new Random(); | ||
private static final Region REGION_USEAST = Region.US_EAST; | ||
private static final Region REGION_GLOBAL = Region.create("global", "Global"); | ||
private String resourceGroupName = "rg" + randomPadding(); | ||
private AzureSphereManager azureSphereManager = null; | ||
private ResourceManager resourceManager; | ||
private boolean testEnv; | ||
|
||
@Override | ||
public void beforeTest() { | ||
final TokenCredential credential = new DefaultAzureCredentialBuilder().build(); | ||
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); | ||
|
||
azureSphereManager = AzureSphereManager | ||
.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_USEAST) | ||
.create(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void afterTest() { | ||
if (!testEnv) { | ||
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName); | ||
} | ||
} | ||
|
||
@Test | ||
@LiveOnly | ||
public void testCreateCatalog() { | ||
Catalog catalog = null; | ||
try { | ||
String catalogName = "catalog" + randomPadding(); | ||
// @embedStart | ||
catalog = azureSphereManager.catalogs() | ||
.define(catalogName) | ||
.withRegion(REGION_GLOBAL) | ||
.withExistingResourceGroup(resourceGroupName) | ||
.create(); | ||
// @embedEnd | ||
catalog.refresh(); | ||
Assertions.assertEquals(catalogName, catalog.name()); | ||
Assertions.assertEquals(catalogName, azureSphereManager.catalogs().getById(catalog.id()).name()); | ||
Assertions.assertTrue(azureSphereManager.catalogs().listByResourceGroup(resourceGroupName).stream().findAny().isPresent()); | ||
} finally { | ||
if (catalog != null) { | ||
azureSphereManager.catalogs().deleteById(catalog.id()); | ||
} | ||
} | ||
} | ||
|
||
private static String randomPadding() { | ||
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
trigger: none | ||
|
||
pr: none | ||
|
||
extends: | ||
template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml | ||
parameters: | ||
ServiceDirectory: sphere | ||
Artifacts: | ||
- name: azure-resourcemanager-sphere | ||
groupId: com.azure.resourcemanager | ||
safeName: azureresourcemanagersphere | ||
Clouds: 'Public' | ||
# Only run tests on Windows to save cost. | ||
MatrixFilters: | ||
- pool=.*(win).* |