diff --git a/sdk/computefleet/azure-resourcemanager-computefleet/pom.xml b/sdk/computefleet/azure-resourcemanager-computefleet/pom.xml index 77c4280e28a1a..04bd3cea2d7b4 100644 --- a/sdk/computefleet/azure-resourcemanager-computefleet/pom.xml +++ b/sdk/computefleet/azure-resourcemanager-computefleet/pom.xml @@ -81,5 +81,17 @@ 1.14.0 test + + com.azure.resourcemanager + azure-resourcemanager-resources + 2.43.0 + test + + + com.azure.resourcemanager + azure-resourcemanager-network + 2.43.0 + test + diff --git a/sdk/computefleet/azure-resourcemanager-computefleet/src/test/java/com/azure/resourcemanager/computefleet/ComputeFleetManagerTests.java b/sdk/computefleet/azure-resourcemanager-computefleet/src/test/java/com/azure/resourcemanager/computefleet/ComputeFleetManagerTests.java new file mode 100644 index 0000000000000..6b24b00ace534 --- /dev/null +++ b/sdk/computefleet/azure-resourcemanager-computefleet/src/test/java/com/azure/resourcemanager/computefleet/ComputeFleetManagerTests.java @@ -0,0 +1,281 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.resourcemanager.computefleet; + +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.computefleet.models.ApiEntityReference; +import com.azure.resourcemanager.computefleet.models.BaseVirtualMachineProfile; +import com.azure.resourcemanager.computefleet.models.CachingTypes; +import com.azure.resourcemanager.computefleet.models.ComputeProfile; +import com.azure.resourcemanager.computefleet.models.DeleteOptions; +import com.azure.resourcemanager.computefleet.models.DiskControllerTypes; +import com.azure.resourcemanager.computefleet.models.DiskCreateOptionTypes; +import com.azure.resourcemanager.computefleet.models.DiskDeleteOptionTypes; +import com.azure.resourcemanager.computefleet.models.EvictionPolicy; +import com.azure.resourcemanager.computefleet.models.Fleet; +import com.azure.resourcemanager.computefleet.models.FleetProperties; +import com.azure.resourcemanager.computefleet.models.ImageReference; +import com.azure.resourcemanager.computefleet.models.LinuxConfiguration; +import com.azure.resourcemanager.computefleet.models.NetworkApiVersion; +import com.azure.resourcemanager.computefleet.models.OperatingSystemTypes; +import com.azure.resourcemanager.computefleet.models.RegularPriorityAllocationStrategy; +import com.azure.resourcemanager.computefleet.models.RegularPriorityProfile; +import com.azure.resourcemanager.computefleet.models.SpotAllocationStrategy; +import com.azure.resourcemanager.computefleet.models.SpotPriorityProfile; +import com.azure.resourcemanager.computefleet.models.StorageAccountTypes; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetIPConfiguration; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetIPConfigurationProperties; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetManagedDiskParameters; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetNetworkConfiguration; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetNetworkConfigurationProperties; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetNetworkProfile; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetOSDisk; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetOSProfile; +import com.azure.resourcemanager.computefleet.models.VirtualMachineScaleSetStorageProfile; +import com.azure.resourcemanager.computefleet.models.VmSizeProfile; +import com.azure.resourcemanager.network.NetworkManager; +import com.azure.resourcemanager.network.models.LoadBalancer; +import com.azure.resourcemanager.network.models.LoadBalancerSkuType; +import com.azure.resourcemanager.network.models.Network; +import com.azure.resourcemanager.network.models.TransportProtocol; +import com.azure.resourcemanager.resources.ResourceManager; +import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils; +import com.azure.resourcemanager.resources.models.Provider; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.UUID; + +public class ComputeFleetManagerTests extends TestProxyTestBase { + private static final Random RANDOM = new Random(); + private static final Region REGION = Region.US_WEST2; + private String resourceGroupName = "rg" + randomPadding(); + private ComputeFleetManager computeFleetManager = null; + private NetworkManager networkManager = null; + private ResourceManager resourceManager; + private boolean testEnv; + + @Override + public void beforeTest() { + final TokenCredential credential = new AzurePowerShellCredentialBuilder().build(); + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); + + computeFleetManager = ComputeFleetManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile); + + networkManager = NetworkManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile); + + resourceManager = ResourceManager + .configure() + .withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC)) + .authenticate(credential, profile) + .withDefaultSubscription(); + + canRegisterProviders(Arrays.asList("Microsoft.AzureFleet")); + + // 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 testCreateComputeFleet() { + Fleet fleet = null; + try { + String fleetName = "fleet" + randomPadding(); + String vmName = "vm" + randomPadding(); + String vnetName = "vnet" + randomPadding(); + String loadBalancerName = "loadBalancer" + randomPadding(); + String adminUser = "adminUser" + randomPadding(); + String adminPwd = UUID.randomUUID().toString().replace("-", "@").substring(0, 13); + // @embedmeStart + Network network = networkManager.networks() + .define(vnetName) + .withRegion(REGION) + .withExistingResourceGroup(resourceGroupName) + .withAddressSpace("172.16.0.0/16") + .defineSubnet("default") + .withAddressPrefix("172.16.0.0/24") + .attach() + .create(); + + LoadBalancer loadBalancer = networkManager.loadBalancers() + .define(loadBalancerName) + .withRegion(REGION) + .withExistingResourceGroup(resourceGroupName) + .defineLoadBalancingRule(loadBalancerName + "-lbrule") + .withProtocol(TransportProtocol.TCP) + .fromExistingSubnet(network, "default") + .fromFrontendPort(80) + .toBackend(loadBalancerName + "-backend") + .toBackendPort(80) + .attach() + .withSku(LoadBalancerSkuType.STANDARD) + .create(); + + fleet = computeFleetManager.fleets() + .define(fleetName) + .withRegion(REGION) + .withExistingResourceGroup(resourceGroupName) + .withProperties( + new FleetProperties() + .withSpotPriorityProfile( + new SpotPriorityProfile() + .withMaintain(false) + .withCapacity(1) + .withEvictionPolicy(EvictionPolicy.DELETE) + .withAllocationStrategy(SpotAllocationStrategy.LOWEST_PRICE) + ) + .withVmSizesProfile( + Arrays.asList( + new VmSizeProfile().withName("Standard_D4s_v3") + ) + ) + .withComputeProfile( + new ComputeProfile() + .withBaseVirtualMachineProfile( + new BaseVirtualMachineProfile() + .withStorageProfile( + new VirtualMachineScaleSetStorageProfile() + .withImageReference( + new ImageReference() + .withPublisher("canonical") + .withOffer("ubuntu-24_04-lts") + .withSku("server") + .withVersion("latest") + ) + .withOsDisk( + new VirtualMachineScaleSetOSDisk() + .withManagedDisk( + new VirtualMachineScaleSetManagedDiskParameters() + .withStorageAccountType(StorageAccountTypes.PREMIUM_LRS) + ) + .withOsType(OperatingSystemTypes.LINUX) + .withDiskSizeGB(30) + .withCreateOption(DiskCreateOptionTypes.FROM_IMAGE) + .withDeleteOption(DiskDeleteOptionTypes.DELETE) + .withCaching(CachingTypes.READ_WRITE) + ) + .withDiskControllerType(DiskControllerTypes.SCSI) + ) + .withOsProfile( + new VirtualMachineScaleSetOSProfile() + .withComputerNamePrefix(randomPadding()) + .withAdminUsername(adminUser) + .withAdminPassword(adminPwd) + .withLinuxConfiguration( + new LinuxConfiguration().withDisablePasswordAuthentication(false) + ) + ) + .withNetworkProfile( + new VirtualMachineScaleSetNetworkProfile() + .withNetworkInterfaceConfigurations( + Arrays.asList( + new VirtualMachineScaleSetNetworkConfiguration() + .withName(vmName) + .withProperties( + new VirtualMachineScaleSetNetworkConfigurationProperties() + .withPrimary(true) + .withEnableAcceleratedNetworking(false) + .withDeleteOption(DeleteOptions.DELETE) + .withIpConfigurations( + Arrays.asList( + new VirtualMachineScaleSetIPConfiguration() + .withName(vmName) + .withProperties( + new VirtualMachineScaleSetIPConfigurationProperties() + .withPrimary(true) + .withSubnet( + new ApiEntityReference() + .withId(network.subnets().get("default").id()) + ) + .withLoadBalancerBackendAddressPools( + loadBalancer.loadBalancingRules() + .get(loadBalancerName + "-lbrule") + .innerModel().backendAddressPools() + ) + ) + ) + ) + ) + ) + ) + .withNetworkApiVersion(NetworkApiVersion.fromString("2024-03-01")) + ) + ) + .withComputeApiVersion("2024-03-01") + .withPlatformFaultDomainCount(1) + ) + .withRegularPriorityProfile(new RegularPriorityProfile() + .withAllocationStrategy(RegularPriorityAllocationStrategy.LOWEST_PRICE) + .withMinCapacity(1) + .withCapacity(2) + ) + ) + .create(); + // @embedmeEnd + fleet.refresh(); + Assertions.assertEquals(fleetName, fleet.name()); + Assertions.assertEquals(fleetName, computeFleetManager.fleets().getById(fleet.id()).name()); + Assertions.assertTrue(computeFleetManager.fleets().listByResourceGroup(resourceGroupName).stream().findAny().isPresent()); + } finally { + if (fleet != null) { + computeFleetManager.fleets().deleteById(fleet.id()); + } + } + } + + private static String randomPadding() { + return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000)); + } + + private void canRegisterProviders(List providerNamespaces) { + providerNamespaces.forEach(providerNamespace -> { + Provider provider = resourceManager.providers().getByName(providerNamespace); + if (!"Registered".equalsIgnoreCase(provider.registrationState()) + && !"Registering".equalsIgnoreCase(provider.registrationState())) { + provider = resourceManager.providers().register(providerNamespace); + } + while (!"Registered".equalsIgnoreCase(provider.registrationState())) { + ResourceManagerUtils.sleep(Duration.ofSeconds(5)); + provider = resourceManager.providers().getByName(provider.namespace()); + } + }); + } +} diff --git a/sdk/computefleet/test-resources.bicep b/sdk/computefleet/test-resources.bicep new file mode 100644 index 0000000000000..3ff2bac58becc --- /dev/null +++ b/sdk/computefleet/test-resources.bicep @@ -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 diff --git a/sdk/computefleet/tests.mgmt.yml b/sdk/computefleet/tests.mgmt.yml new file mode 100644 index 0000000000000..c01b98ae1b3ba --- /dev/null +++ b/sdk/computefleet/tests.mgmt.yml @@ -0,0 +1,17 @@ +trigger: none + +pr: none + +extends: + template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml + parameters: + ServiceDirectory: computefleet + Artifacts: + - name: azure-resourcemanager-computefleet + groupId: com.azure.resourcemanager + safeName: azureresourcemanagercomputefleet + Clouds: 'Public' + UseFederatedAuth: true + # Only run tests on Windows to save cost. + MatrixFilters: + - pool=.*(win).*