Skip to content

Commit

Permalink
mgmt, standbypool, add live test (#42019)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Sep 25, 2024
1 parent 4be024e commit 026bf31
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sdk/standbypool/azure-resourcemanager-standbypool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ Code generated by Microsoft (R) TypeSpec Code Generator.
<version>1.27.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-compute</artifactId>
<version>2.42.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-compute;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.standbypool;

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.core.util.logging.ClientLogger;
import com.azure.identity.AzurePowerShellCredentialBuilder;
import com.azure.resourcemanager.compute.ComputeManager;
import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSet;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetSkuTypes;
import com.azure.resourcemanager.network.models.Network;
import com.azure.resourcemanager.standbypool.models.StandbyVirtualMachinePoolElasticityProfile;
import com.azure.resourcemanager.standbypool.models.StandbyVirtualMachinePoolResource;
import com.azure.resourcemanager.standbypool.models.StandbyVirtualMachinePoolResourceProperties;
import com.azure.resourcemanager.standbypool.models.VirtualMachineState;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.Random;

public class StandbyPoolTests extends TestProxyTestBase {
private static final ClientLogger LOGGER = new ClientLogger(StandbyPoolTests.class);

private boolean testEnv;

private static final Random RANDOM = new Random();
private static final Region REGION = Region.US_WEST2;
private String resourceGroupName = "rg" + randomPadding();
private StandbyPoolManager standbyPoolManager;
private ComputeManager computeManager;

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

standbyPoolManager = StandbyPoolManager
.configure()
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
.authenticate(credential, profile);

computeManager = ComputeManager
.configure()
.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 {
computeManager.resourceManager().resourceGroups()
.define(resourceGroupName)
.withRegion(REGION)
.create();
}
}

@Override
protected void afterTest() {
if (!testEnv) {
computeManager.resourceManager().resourceGroups().beginDeleteByName(resourceGroupName);
}
}

@Test
@LiveOnly
public void testStandbyVirtualMachinePool() {
String poolName = "pool" + randomPadding();
StandbyVirtualMachinePoolResource standbyVirtualMachinePool = null;
VirtualMachineScaleSet virtualMachineScaleSet = null;
Network virtualNetwork = null;
try {
// @embedmeStart
// reference https://learn.microsoft.com/azure/virtual-machine-scale-sets/standby-pools-create

// Create virtual network and virtual machine scale set
virtualNetwork = this.computeManager.networkManager()
.networks()
.define("vmssvnet")
.withRegion(REGION)
.withExistingResourceGroup(resourceGroupName)
.withAddressSpace("10.0.0.0/27")
.withSubnet("default", "10.0.0.0/27")
.create();

virtualMachineScaleSet = computeManager.virtualMachineScaleSets()
.define("vmss")
.withRegion(REGION)
.withExistingResourceGroup(resourceGroupName)
.withFlexibleOrchestrationMode()
.withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0)
.withExistingPrimaryNetworkSubnet(virtualNetwork, "default")
.withoutPrimaryInternetFacingLoadBalancer()
.withoutPrimaryInternalLoadBalancer()
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_18_04_LTS)
.withRootUsername("Foo12")
.withSsh(sshPublicKey())
.withVirtualMachinePublicIp()
.withCapacity(3L)
.create();

// create standby virtual machine pool
standbyVirtualMachinePool = standbyPoolManager.standbyVirtualMachinePools()
.define(poolName)
.withRegion(REGION)
.withExistingResourceGroup(resourceGroupName)
.withProperties(new StandbyVirtualMachinePoolResourceProperties()
.withAttachedVirtualMachineScaleSetId(virtualMachineScaleSet.id())
.withVirtualMachineState(VirtualMachineState.DEALLOCATED)
.withElasticityProfile(new StandbyVirtualMachinePoolElasticityProfile()
.withMaxReadyCapacity(3L)
.withMinReadyCapacity(1L)))
.create();
// @embedmeEnd
standbyVirtualMachinePool.refresh();
Assertions.assertEquals(poolName, standbyVirtualMachinePool.name());
Assertions.assertTrue(standbyPoolManager.standbyVirtualMachinePools().listByResourceGroup(resourceGroupName).stream().count() > 0);
} finally {
if (standbyVirtualMachinePool != null) {
standbyPoolManager.standbyVirtualMachinePools().deleteById(standbyVirtualMachinePool.id());

computeManager.virtualMachineScaleSets().deleteById(virtualMachineScaleSet.id());

computeManager.networkManager().networks().deleteById(virtualNetwork.id());
}
}
}

private static String randomPadding() {
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
}

private static String sshPublicKey() {
String sshPublicKey;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair pair = keyGen.generateKeyPair();
PublicKey publicKey = pair.getPublic();

RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(byteOs);
dos.writeInt("ssh-rsa".getBytes(StandardCharsets.US_ASCII).length);
dos.write("ssh-rsa".getBytes(StandardCharsets.US_ASCII));
dos.writeInt(rsaPublicKey.getPublicExponent().toByteArray().length);
dos.write(rsaPublicKey.getPublicExponent().toByteArray());
dos.writeInt(rsaPublicKey.getModulus().toByteArray().length);
dos.write(rsaPublicKey.getModulus().toByteArray());
String publicKeyEncoded = new String(Base64.getEncoder().encode(byteOs.toByteArray()), StandardCharsets.US_ASCII);
sshPublicKey = "ssh-rsa " + publicKeyEncoded;
} catch (NoSuchAlgorithmException | IOException e) {
throw LOGGER.logExceptionAsError(new IllegalStateException("failed to generate ssh key", e));
}
return sshPublicKey;
}
}
22 changes: 22 additions & 0 deletions sdk/standbypool/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
17 changes: 17 additions & 0 deletions sdk/standbypool/tests.mgmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trigger: none

pr: none

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

0 comments on commit 026bf31

Please sign in to comment.