Skip to content

Commit

Permalink
mgmt, playwrighttesting, add live tests (Azure#43414)
Browse files Browse the repository at this point in the history
* mgmt, playwrighttesting, add live tests

* fix comments
  • Loading branch information
v-hongli1 authored Dec 16, 2024
1 parent fd2e1bf commit b731a31
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,11 @@
<version>1.15.5</version> <!-- {x-version-update;testdep_net.bytebuddy:byte-buddy-agent;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-resources</artifactId>
<version>2.45.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.playwrighttesting;

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.TestProxyTestBase;
import com.azure.core.test.annotation.LiveOnly;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.identity.AzurePowerShellCredentialBuilder;
import com.azure.resourcemanager.playwrighttesting.models.Account;
import com.azure.resourcemanager.playwrighttesting.models.AccountProperties;
import com.azure.resourcemanager.playwrighttesting.models.EnablementStatus;
import com.azure.resourcemanager.resources.ResourceManager;
import com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Random;

public class PlaywrightTestingManagerTests extends TestProxyTestBase {
private static final Random RANDOM = new Random();
private static final Region REGION = Region.US_WEST3;
private String resourceGroupName = "rg" + randomPadding();
private PlaywrightTestingManager playwrightTestingManager = null;
private ResourceManager resourceManager;
private boolean testEnv;

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

resourceManager = ResourceManager.configure()
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
.authenticate(credential, profile)
.withDefaultSubscription();

playwrightTestingManager = PlaywrightTestingManager.configure()
.withPolicy(new ProviderRegistrationPolicy(resourceManager))
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
.authenticate(credential, profile);

// 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
@LiveOnly
public void testCreatePlaywrightTestingAccount() {
Account account = null;
try {
String accountName = "account" + randomPadding();
// @embedmeStart
account = playwrightTestingManager.accounts()
.define(accountName)
.withRegion(REGION)
.withExistingResourceGroup(resourceGroupName)
.withProperties(new AccountProperties().withRegionalAffinity(EnablementStatus.ENABLED))
.create();
// @embedmeEnd
account.refresh();
Assertions.assertEquals(accountName, account.name());
Assertions.assertEquals(accountName, playwrightTestingManager.accounts().getById(account.id()).name());
Assertions.assertTrue(playwrightTestingManager.accounts()
.listByResourceGroup(resourceGroupName)
.stream()
.findAny()
.isPresent());
} finally {
if (account != null) {
playwrightTestingManager.accounts().deleteById(account.id());
}
}
}

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

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_SUBSCRIPTION_ID string = subscription().subscriptionId
output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name
15 changes: 15 additions & 0 deletions sdk/playwrighttesting/tests.mgmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trigger: none

pr: none

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

0 comments on commit b731a31

Please sign in to comment.