Skip to content

Commit

Permalink
mgmt, providerhub, add live tests (Azure#35773)
Browse files Browse the repository at this point in the history
mgmt, providerhub, add live tests
  • Loading branch information
v-hongli1 authored Jul 7, 2023
1 parent 42a83dd commit 40158cb
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
30 changes: 30 additions & 0 deletions sdk/providerhub/azure-resourcemanager-providerhub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,35 @@
<artifactId>azure-core-management</artifactId>
<version>1.11.2</version> <!-- {x-version-update;com.azure:azure-core-management;dependency} -->
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-resources</artifactId>
<version>2.28.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.18.0</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.9.1</version> <!-- {x-version-update;com.azure:azure-identity;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.providerhub;

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.providerhub.fluent.models.OperationsDefinitionInner;
import com.azure.resourcemanager.providerhub.models.OperationsContent;
import com.azure.resourcemanager.providerhub.models.OperationsDefinitionDisplay;
import com.azure.resourcemanager.providerhub.models.OperationsPutContent;
import com.azure.resourcemanager.resources.ResourceManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Random;

public class ProviderHubManagerTests extends TestBase {
private static final Random RANDOM = new Random();
private static final Region REGION = Region.US_WEST2;
private String resourceGroupName = "rg" + randomPadding();
private ProviderHubManager providerHubManager;
private ResourceManager resourceManager;
private boolean testEnv;

@Override
public void beforeTest() {
final TokenCredential credential = new DefaultAzureCredentialBuilder().build();
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);

providerHubManager = ProviderHubManager
.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 testCreateOperation() {
OperationsContent operationsContent = null;
String spaceName = "Microsoft.Contoso" + randomPadding();
String opeartionName = spaceName + "/Employees/Read";
try {
// @embedmeStart
operationsContent = providerHubManager.operations()
.createOrUpdate(spaceName,
new OperationsPutContent()
.withContents(
Arrays.asList(
new OperationsDefinitionInner()
.withName(opeartionName)
.withDisplay(new OperationsDefinitionDisplay()
.withProvider(spaceName)
.withResource("Employees")
.withOperation("Gets/List employee resources")
.withDescription("Read employees")))));
// @embedmeEnd
Assertions.assertTrue(
providerHubManager.operations().listByProviderRegistration(spaceName)
.stream().filter(operationsDefinition ->
spaceName.equals(operationsDefinition.display().provider())
&& opeartionName.equals(operationsDefinition.name()))
.findAny().isPresent());
} finally {
if (operationsContent != null) {
providerHubManager.operations().delete(spaceName);
}
}
}

private static String randomPadding() {
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
}
}
27 changes: 27 additions & 0 deletions sdk/providerhub/test-resources.bicep
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
16 changes: 16 additions & 0 deletions sdk/providerhub/tests.mgmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trigger: none

pr: none

stages:
- template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml
parameters:
ServiceDirectory: providerhub
Artifacts:
- name: azure-resourcemanager-providerhub
groupId: com.azure.resourcemanager
safeName: azureresourcemanagerproviderhub
Clouds: 'Public'
# Only run tests on Windows to save cost.
MatrixFilters:
- pool=.*(win).*

0 comments on commit 40158cb

Please sign in to comment.