Skip to content

Commit b3ddaea

Browse files
authored
mgmt, postgresqlflexibleserver, add live tests (Azure#35398)
mgmt, postgresqlflexibleserver, add live tests
1 parent a52460c commit b3ddaea

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

sdk/postgresqlflexibleserver/azure-resourcemanager-postgresqlflexibleserver/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
<artifactId>azure-core-management</artifactId>
5858
<version>1.11.2</version> <!-- {x-version-update;com.azure:azure-core-management;dependency} -->
5959
</dependency>
60+
<dependency>
61+
<groupId>com.azure.resourcemanager</groupId>
62+
<artifactId>azure-resourcemanager-resources</artifactId>
63+
<version>2.27.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
64+
<scope>test</scope>
65+
</dependency>
6066
<dependency>
6167
<groupId>com.azure</groupId>
6268
<artifactId>azure-core-test</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.postgresqlflexibleserver;
5+
6+
import com.azure.core.credential.TokenCredential;
7+
import com.azure.core.http.policy.HttpLogDetailLevel;
8+
import com.azure.core.http.policy.HttpLogOptions;
9+
import com.azure.core.management.AzureEnvironment;
10+
import com.azure.core.management.Region;
11+
import com.azure.core.management.profile.AzureProfile;
12+
import com.azure.core.test.TestBase;
13+
import com.azure.core.test.annotation.DoNotRecord;
14+
import com.azure.core.util.Configuration;
15+
import com.azure.core.util.CoreUtils;
16+
import com.azure.identity.DefaultAzureCredentialBuilder;
17+
import com.azure.resourcemanager.postgresqlflexibleserver.models.ActiveDirectoryAuthEnum;
18+
import com.azure.resourcemanager.postgresqlflexibleserver.models.ArmServerKeyType;
19+
import com.azure.resourcemanager.postgresqlflexibleserver.models.AuthConfig;
20+
import com.azure.resourcemanager.postgresqlflexibleserver.models.Backup;
21+
import com.azure.resourcemanager.postgresqlflexibleserver.models.DataEncryption;
22+
import com.azure.resourcemanager.postgresqlflexibleserver.models.GeoRedundantBackupEnum;
23+
import com.azure.resourcemanager.postgresqlflexibleserver.models.HighAvailability;
24+
import com.azure.resourcemanager.postgresqlflexibleserver.models.HighAvailabilityMode;
25+
import com.azure.resourcemanager.postgresqlflexibleserver.models.IdentityType;
26+
import com.azure.resourcemanager.postgresqlflexibleserver.models.PasswordAuthEnum;
27+
import com.azure.resourcemanager.postgresqlflexibleserver.models.ReplicationRole;
28+
import com.azure.resourcemanager.postgresqlflexibleserver.models.Server;
29+
import com.azure.resourcemanager.postgresqlflexibleserver.models.ServerVersion;
30+
import com.azure.resourcemanager.postgresqlflexibleserver.models.Sku;
31+
import com.azure.resourcemanager.postgresqlflexibleserver.models.SkuTier;
32+
import com.azure.resourcemanager.postgresqlflexibleserver.models.Storage;
33+
import com.azure.resourcemanager.postgresqlflexibleserver.models.UserAssignedIdentity;
34+
import com.azure.resourcemanager.resources.ResourceManager;
35+
import io.netty.util.internal.StringUtil;
36+
import org.junit.jupiter.api.Assertions;
37+
import org.junit.jupiter.api.Test;
38+
39+
import java.util.Random;
40+
import java.util.UUID;
41+
42+
public class PostgreSqlManagerTests extends TestBase {
43+
private static final Random RANDOM = new Random();
44+
private static final Region REGION = Region.US_EAST;
45+
private String resourceGroupName = "rg" + randomPadding();
46+
private PostgreSqlManager postgreSqlManager;
47+
private ResourceManager resourceManager;
48+
private boolean testEnv;
49+
50+
@Override
51+
public void beforeTest() {
52+
final TokenCredential credential = new DefaultAzureCredentialBuilder().build();
53+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
54+
55+
postgreSqlManager = PostgreSqlManager
56+
.configure()
57+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
58+
.authenticate(credential, profile);
59+
60+
resourceManager = ResourceManager
61+
.configure()
62+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
63+
.authenticate(credential, profile)
64+
.withDefaultSubscription();
65+
66+
// use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI
67+
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
68+
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
69+
if (testEnv) {
70+
resourceGroupName = testResourceGroup;
71+
} else {
72+
resourceManager.resourceGroups()
73+
.define(resourceGroupName)
74+
.withRegion(REGION)
75+
.create();
76+
}
77+
}
78+
79+
@Override
80+
protected void afterTest() {
81+
if (!testEnv) {
82+
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
83+
}
84+
}
85+
86+
@Test
87+
@DoNotRecord(skipInPlayback = true)
88+
public void testCreateServer() {
89+
Server server = null;
90+
String randomPadding = randomPadding();
91+
try {
92+
String serverName = "postgresql" + randomPadding;
93+
String adminName = "sqlAdmin" + randomPadding;
94+
String adminPwd = "sqlAdmin"
95+
+ UUID.randomUUID().toString().replace("-", StringUtil.EMPTY_STRING).substring(0, 8);
96+
// @embedmeStart
97+
server = postgreSqlManager.servers()
98+
.define(serverName)
99+
.withRegion(REGION)
100+
.withExistingResourceGroup(resourceGroupName)
101+
.withAdministratorLogin(adminName)
102+
.withAdministratorLoginPassword(adminPwd)
103+
.withSku(new Sku().withName("Standard_D2ds_v4").withTier(SkuTier.GENERAL_PURPOSE))
104+
.withAuthConfig(new AuthConfig()
105+
.withActiveDirectoryAuth(ActiveDirectoryAuthEnum.DISABLED)
106+
.withPasswordAuth(PasswordAuthEnum.ENABLED))
107+
.withIdentity(new UserAssignedIdentity().withType(IdentityType.NONE))
108+
.withDataEncryption(new DataEncryption().withType(ArmServerKeyType.SYSTEM_MANAGED))
109+
.withVersion(ServerVersion.ONE_FOUR)
110+
.withAvailabilityZone("2")
111+
.withStorage(new Storage().withStorageSizeGB(128))
112+
.withBackup(new Backup()
113+
.withGeoRedundantBackup(GeoRedundantBackupEnum.DISABLED)
114+
.withBackupRetentionDays(7))
115+
.withHighAvailability(new HighAvailability().withMode(HighAvailabilityMode.DISABLED))
116+
.withReplicationRole(ReplicationRole.PRIMARY)
117+
.withReplicaCapacity(5)
118+
.create();
119+
// @embedmeEnd
120+
server.refresh();
121+
Assertions.assertEquals(server.name(), serverName);
122+
Assertions.assertEquals(server.name(), postgreSqlManager.servers().getById(server.id()).name());
123+
Assertions.assertTrue(postgreSqlManager.servers().list().stream().count() > 0);
124+
} finally {
125+
if (server != null) {
126+
postgreSqlManager.servers().deleteById(server.id());
127+
}
128+
}
129+
}
130+
131+
private static String randomPadding() {
132+
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
133+
}
134+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@description('The tenant id to which the application and resources belong.')
2+
param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47'
3+
4+
@description('The client id of the service principal used to run tests.')
5+
param testApplicationId string
6+
7+
@description('This is the object id of the service principal used to run tests.')
8+
param testApplicationOid string
9+
10+
@description('The application client secret used to run tests.')
11+
param testApplicationSecret string
12+
13+
var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c'
14+
15+
resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
16+
name: guid('contributorRoleId${resourceGroup().name}')
17+
properties: {
18+
roleDefinitionId: contributorRoleId
19+
principalId: testApplicationOid
20+
}
21+
}
22+
23+
output AZURE_TENANT_ID string = tenantId
24+
output AZURE_CLIENT_ID string = testApplicationId
25+
output AZURE_CLIENT_SECRET string = testApplicationSecret
26+
output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId
27+
output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trigger: none
2+
3+
pr: none
4+
5+
stages:
6+
- template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml
7+
parameters:
8+
ServiceDirectory: postgresqlflexibleserver
9+
Artifacts:
10+
- name: azure-resourcemanager-postgresqlflexibleserver
11+
groupId: com.azure.resourcemanager
12+
safeName: azureresourcemanagerpostgresqlflexibleserver
13+
Clouds: 'Public'
14+
# Only run tests on Windows to save cost.
15+
MatrixFilters:
16+
- pool=.*(win).*

0 commit comments

Comments
 (0)