Skip to content

Commit a52460c

Browse files
authored
mgmt, postgresql, add live tests (Azure#35396)
mgmt, postgresql, add live tests
1 parent a64a89b commit a52460c

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,35 @@
5252
<artifactId>azure-core-management</artifactId>
5353
<version>1.11.2</version> <!-- {x-version-update;com.azure:azure-core-management;dependency} -->
5454
</dependency>
55+
<dependency>
56+
<groupId>com.azure</groupId>
57+
<artifactId>azure-core-test</artifactId>
58+
<version>1.18.0</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.azure</groupId>
63+
<artifactId>azure-identity</artifactId>
64+
<version>1.9.1</version> <!-- {x-version-update;com.azure:azure-identity;dependency} -->
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.junit.jupiter</groupId>
69+
<artifactId>junit-jupiter-api</artifactId>
70+
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} -->
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.junit.jupiter</groupId>
75+
<artifactId>junit-jupiter-engine</artifactId>
76+
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.azure.resourcemanager</groupId>
81+
<artifactId>azure-resourcemanager-resources</artifactId>
82+
<version>2.27.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
83+
<scope>test</scope>
84+
</dependency>
5585
</dependencies>
5686
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.postgresql;
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.postgresql.models.*;
18+
import com.azure.resourcemanager.resources.ResourceManager;
19+
import io.netty.util.internal.StringUtil;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Random;
24+
import java.util.UUID;
25+
26+
public class PostgreSqlManagerTests extends TestBase {
27+
private static final Random RANDOM = new Random();
28+
private static final Region REGION = Region.EUROPE_WEST;
29+
private String resourceGroupName = "rg" + randomPadding();
30+
private PostgreSqlManager postgreSqlManager;
31+
private ResourceManager resourceManager;
32+
private boolean testEnv;
33+
34+
@Override
35+
public void beforeTest() {
36+
final TokenCredential credential = new DefaultAzureCredentialBuilder().build();
37+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
38+
39+
postgreSqlManager = PostgreSqlManager
40+
.configure()
41+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
42+
.authenticate(credential, profile);
43+
44+
resourceManager = ResourceManager
45+
.configure()
46+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
47+
.authenticate(credential, profile)
48+
.withDefaultSubscription();
49+
50+
// use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI
51+
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
52+
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
53+
if (testEnv) {
54+
resourceGroupName = testResourceGroup;
55+
} else {
56+
resourceManager.resourceGroups()
57+
.define(resourceGroupName)
58+
.withRegion(REGION)
59+
.create();
60+
}
61+
}
62+
63+
@Override
64+
protected void afterTest() {
65+
if (!testEnv) {
66+
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
67+
}
68+
}
69+
70+
@Test
71+
@DoNotRecord(skipInPlayback = true)
72+
public void testCreateServer() {
73+
Server server = null;
74+
String randomPadding = randomPadding();
75+
try {
76+
String serverName = "postgresql" + randomPadding;
77+
String adminName = "sqlAdmin" + randomPadding;
78+
String adminPwd = "sqlAdmin"
79+
+ UUID.randomUUID().toString().replace("-", StringUtil.EMPTY_STRING).substring(0, 8);
80+
// @embedmeStart
81+
server = postgreSqlManager.servers()
82+
.define(serverName)
83+
.withRegion(REGION)
84+
.withExistingResourceGroup(resourceGroupName)
85+
.withProperties(
86+
new ServerPropertiesForDefaultCreate()
87+
.withAdministratorLogin(adminName)
88+
.withAdministratorLoginPassword(adminPwd)
89+
.withStorageProfile(new StorageProfile()
90+
.withBackupRetentionDays(7)
91+
.withGeoRedundantBackup(GeoRedundantBackup.DISABLED)
92+
.withStorageMB(102400)
93+
.withStorageAutogrow(StorageAutogrow.ENABLED))
94+
.withSslEnforcement(SslEnforcementEnum.ENABLED)
95+
.withPublicNetworkAccess(PublicNetworkAccessEnum.ENABLED)
96+
.withInfrastructureEncryption(InfrastructureEncryption.DISABLED)
97+
.withVersion(ServerVersion.ONE_ONE)
98+
)
99+
.withIdentity(new ResourceIdentity().withType(IdentityType.SYSTEM_ASSIGNED))
100+
.withSku(new Sku().withName("GP_Gen5_4").withTier(SkuTier.GENERAL_PURPOSE).withCapacity(4).withFamily("Gen5"))
101+
.create();
102+
// @embedmeEnd
103+
server.refresh();
104+
Assertions.assertEquals(server.name(), serverName);
105+
Assertions.assertEquals(server.name(), postgreSqlManager.servers().getById(server.id()).name());
106+
Assertions.assertTrue(postgreSqlManager.servers().list().stream().count() > 0);
107+
} finally {
108+
if (server != null) {
109+
postgreSqlManager.servers().deleteById(server.id());
110+
}
111+
}
112+
}
113+
114+
private static String randomPadding() {
115+
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
116+
}
117+
}

sdk/postgresql/test-resources.bicep

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

sdk/postgresql/tests.mgmt.yml

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: postgresql
9+
Artifacts:
10+
- name: azure-resourcemanager-postgresql
11+
groupId: com.azure.resourcemanager
12+
safeName: azureresourcemanagerpostgresql
13+
Clouds: 'Public'
14+
# Only run tests on Windows to save cost.
15+
MatrixFilters:
16+
- pool=.*(win).*

0 commit comments

Comments
 (0)