Skip to content

Commit

Permalink
Refactor resourcemanager tests to work with latest TestProxy (#41301)
Browse files Browse the repository at this point in the history
Refactor resourcemanager tests to work with latest TestProxy.
  • Loading branch information
v-hongli1 committed Jul 31, 2024
1 parent 16bf4bf commit 9cca837
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/resourcemanager/azure-resourcemanager-compute",
"Tag": "java/resourcemanager/azure-resourcemanager-compute_b77244749d"
"Tag": "java/resourcemanager/azure-resourcemanager-compute_2e5d9e7287"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
package com.azure.resourcemanager.compute;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.test.annotation.LiveOnly;
import com.azure.core.management.Region;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.resourcemanager.authorization.models.BuiltInRole;
import com.azure.resourcemanager.authorization.models.RoleAssignment;
import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage;
import com.azure.resourcemanager.compute.models.ResourceIdentityType;
import com.azure.resourcemanager.compute.models.VirtualMachine;
import com.azure.resourcemanager.authorization.models.BuiltInRole;
import com.azure.resourcemanager.authorization.models.RoleAssignment;
import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes;
import com.azure.resourcemanager.msi.models.Identity;
import com.azure.resourcemanager.network.models.Network;
import com.azure.resourcemanager.resources.models.ResourceGroup;
import com.azure.core.management.Region;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
import java.util.Iterator;
import java.util.Set;
import com.azure.resourcemanager.resources.models.ResourceGroup;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

@LiveOnly
import java.util.Iterator;
import java.util.Set;

public class VirtualMachineEMSILMSIOperationsTests extends ComputeManagementTest {
// LiveOnly because test needs to be refactored for storing/evaluating PrincipalId
private String rgName = "";
private Region region = Region.US_WEST_CENTRAL;
private Region region = Region.US_WEST2;
private final String vmName = "javavm";

@Override
Expand All @@ -35,7 +35,9 @@ protected void cleanUpResources() {
}

@Test
@DoNotRecord(skipInPlayback = true)
public void canCreateUpdateVirtualMachineWithEMSI() {
// LiveOnly because "test timing out after latest test proxy update"
// this.resourceManager.resourceGroups().beginDeleteByName("41522c6e938c4f6");
rgName = generateRandomResourceName("java-emsi-c-rg", 15);
String identityName1 = generateRandomResourceName("msi-id", 15);
Expand Down Expand Up @@ -254,7 +256,9 @@ public void canCreateUpdateVirtualMachineWithEMSI() {
}

@Test
@DoNotRecord(skipInPlayback = true)
public void canCreateVirtualMachineWithLMSIAndEMSI() {
// LiveOnly because "test timing out after latest test proxy update"
rgName = generateRandomResourceName("java-emsi-c-rg", 15);
String identityName1 = generateRandomResourceName("msi-id", 15);
String networkName = generateRandomResourceName("nw", 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,39 @@
package com.azure.resourcemanager.compute;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.test.annotation.LiveOnly;
import com.azure.core.management.Region;
import com.azure.resourcemanager.authorization.models.BuiltInRole;
import com.azure.resourcemanager.authorization.models.RoleAssignment;
import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage;
import com.azure.resourcemanager.compute.models.ResourceIdentityType;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSet;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetSkuTypes;
import com.azure.resourcemanager.authorization.models.BuiltInRole;
import com.azure.resourcemanager.authorization.models.RoleAssignment;
import com.azure.resourcemanager.msi.models.Identity;
import com.azure.resourcemanager.network.models.LoadBalancer;
import com.azure.resourcemanager.network.models.Network;
import com.azure.resourcemanager.resources.models.ResourceGroup;
import com.azure.core.management.Region;
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
import java.util.Iterator;
import java.util.Set;

import com.azure.resourcemanager.resources.models.ResourceGroup;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

@LiveOnly
import java.util.Iterator;
import java.util.Set;

public class VirtualMachineScaleSetEMSILMSIOperationsTests extends ComputeManagementTest {
// LiveOnly because test needs to be refactored for storing/evaluating PrincipalId
private String rgName = "";
private Region region = Region.US_WEST_CENTRAL;
private Region region = Region.US_WEST2;
private final String vmssName = "javavmss";
private ResourceGroup resourceGroup;

@BeforeEach
public void initResources() {
// Create a resource group
rgName = generateRandomResourceName("java-emsi-c-rg", 15);
resourceGroup = this.resourceManager.resourceGroups().define(rgName).withRegion(region).create();
}

@Override
protected void cleanUpResources() {
Expand All @@ -38,13 +45,10 @@ protected void cleanUpResources() {

@Test
public void canCreateUpdateVirtualMachineScaleSetWithEMSI() throws Exception {
rgName = generateRandomResourceName("java-ems-c-rg", 15);
String identityName1 = generateRandomResourceName("msi-id", 15);
String identityName2 = generateRandomResourceName("msi-id", 15);
String networkName = generateRandomResourceName("nw", 10);

ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(rgName).withRegion(region).create();

// Create a virtual network to which we will assign "EMSI" with reader access
//
Network network =
Expand Down Expand Up @@ -148,56 +152,60 @@ public void canCreateUpdateVirtualMachineScaleSetWithEMSI() throws Exception {

// Ensure expected role assignment exists for explicitly created EMSI
//
PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
this.msiManager.authorizationManager().roleAssignments().listByScope(network.id());
boolean found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
if (roleAssignment.principalId() != null
&& roleAssignment.principalId().equalsIgnoreCase(createdIdentity.principalId())) {
found = true;
break;
RoleAssignment assignment;
if (!isPlaybackMode()) {
// principalId redacted
PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
this.msiManager.authorizationManager().roleAssignments().listByScope(network.id());
for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
if (roleAssignment.principalId() != null
&& roleAssignment.principalId().equalsIgnoreCase(createdIdentity.principalId())) {
found = true;
break;
}
}
}
Assertions
.assertTrue(
found,
"Expected role assignment not found for the virtual network for identity" + createdIdentity.name());

RoleAssignment assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(network.id(), BuiltInRole.READER, createdIdentity.principalId())
.block();

Assertions
.assertNotNull(
assignment, "Expected role assignment with ROLE not found for the virtual network for identity");
Assertions
.assertTrue(
found,
"Expected role assignment not found for the virtual network for identity" + createdIdentity.name());

// Ensure expected role assignment exists for explicitly created EMSI
//
PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
this.msiManager.authorizationManager().roleAssignments().listByScope(resourceGroup.id());
assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(network.id(), BuiltInRole.READER, createdIdentity.principalId())
.block();

found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
if (roleAssignment.principalId() != null
&& roleAssignment.principalId().equalsIgnoreCase(implicitlyCreatedIdentity.principalId())) {
found = true;
break;
Assertions
.assertNotNull(
assignment, "Expected role assignment with ROLE not found for the virtual network for identity");

// Ensure expected role assignment exists for explicitly created EMSI
//
PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
this.msiManager.authorizationManager().roleAssignments().listByScope(resourceGroup.id());

found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
if (roleAssignment.principalId() != null
&& roleAssignment.principalId().equalsIgnoreCase(implicitlyCreatedIdentity.principalId())) {
found = true;
break;
}
}
}
Assertions
.assertTrue(
found,
"Expected role assignment not found for the resource group for identity"
+ implicitlyCreatedIdentity.name());
Assertions
.assertTrue(
found,
"Expected role assignment not found for the resource group for identity"
+ implicitlyCreatedIdentity.name());

assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(
assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(
resourceGroup.id(), BuiltInRole.CONTRIBUTOR, implicitlyCreatedIdentity.principalId())
.block();
.block();

Assertions
.assertNotNull(
assignment, "Expected role assignment with ROLE not found for the resource group for identity");
Assertions
.assertNotNull(
assignment, "Expected role assignment with ROLE not found for the resource group for identity");
}

emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
Iterator<String> itr = emsiIds.iterator();
Expand Down Expand Up @@ -296,14 +304,9 @@ public void canCreateUpdateVirtualMachineScaleSetWithEMSI() throws Exception {

@Test
public void canCreateVirtualMachineScaleSetWithLMSIAndEMSI() throws Exception {
rgName = generateRandomResourceName("java-emsi-c-rg", 15);
String identityName1 = generateRandomResourceName("msi-id", 15);
String networkName = generateRandomResourceName("nw", 10);

// Create a resource group
//
ResourceGroup resourceGroup = resourceManager.resourceGroups().define(rgName).withRegion(region).create();

// Create a virtual network to which we will assign "EMSI" with reader access
//
Network network =
Expand Down Expand Up @@ -380,78 +383,76 @@ public void canCreateVirtualMachineScaleSetWithLMSIAndEMSI() throws Exception {

// Ensure expected role assignment exists for LMSI
//
PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
this.msiManager.authorizationManager().roleAssignments().listByScope(network.id());
boolean found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
if (roleAssignment.principalId() != null
&& roleAssignment
.principalId()
.equalsIgnoreCase(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId())) {
found = true;
break;
if (!isPlaybackMode()) {
// principalId redacted
PagedIterable<RoleAssignment> roleAssignmentsForNetwork =
this.msiManager.authorizationManager().roleAssignments().listByScope(network.id());
boolean found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForNetwork) {
if (roleAssignment.principalId() != null
&& roleAssignment
.principalId()
.equalsIgnoreCase(virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId())) {
found = true;
break;
}
}
}
Assertions
.assertTrue(
found,
"Expected role assignment not found for the virtual network for local identity"
+ virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());
Assertions
.assertTrue(
found,
"Expected role assignment not found for the virtual network for local identity"
+ virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId());

RoleAssignment assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(
RoleAssignment assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(
network.id(),
BuiltInRole.CONTRIBUTOR,
virtualMachineScaleSet.systemAssignedManagedServiceIdentityPrincipalId())
.block();

Assertions
.assertNotNull(
assignment,
"Expected role assignment with ROLE not found for the virtual network for system assigned identity");
.block();

// Ensure expected role assignment exists for EMSI
//
PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
this
.msiManager
.authorizationManager()
.roleAssignments()
.listByScope(
resourceManager.resourceGroups().getByName(virtualMachineScaleSet.resourceGroupName()).id());
found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
if (roleAssignment.principalId() != null
&& roleAssignment.principalId().equalsIgnoreCase(identity.principalId())) {
found = true;
break;
Assertions
.assertNotNull(
assignment,
"Expected role assignment with ROLE not found for the virtual network for system assigned identity");

// Ensure expected role assignment exists for EMSI
//
PagedIterable<RoleAssignment> roleAssignmentsForResourceGroup =
this
.msiManager
.authorizationManager()
.roleAssignments()
.listByScope(
resourceManager.resourceGroups().getByName(virtualMachineScaleSet.resourceGroupName()).id());
found = false;
for (RoleAssignment roleAssignment : roleAssignmentsForResourceGroup) {
if (roleAssignment.principalId() != null
&& roleAssignment.principalId().equalsIgnoreCase(identity.principalId())) {
found = true;
break;
}
}
}
Assertions
.assertTrue(
found, "Expected role assignment not found for the resource group for identity" + identity.name());
Assertions
.assertTrue(
found, "Expected role assignment not found for the resource group for identity" + identity.name());

assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(
assignment =
lookupRoleAssignmentUsingScopeAndRoleAsync(
resourceGroup.id(), BuiltInRole.CONTRIBUTOR, identity.principalId())
.block();
.block();

Assertions
.assertNotNull(
assignment,
"Expected role assignment with ROLE not found for the resource group for system assigned identity");
Assertions
.assertNotNull(
assignment,
"Expected role assignment with ROLE not found for the resource group for system assigned identity");
}
}

@Test
public void canUpdateVirtualMachineScaleSetWithEMSIAndLMSI() throws Exception {
rgName = generateRandomResourceName("java-emsi-c-rg", 15);
String identityName1 = generateRandomResourceName("msi-id-1", 15);
String identityName2 = generateRandomResourceName("msi-id-2", 15);

// Create a resource group
//
ResourceGroup resourceGroup = resourceManager.resourceGroups().define(rgName).withRegion(region).create();

// Create a virtual network for VMSS
//
Network vmssNetwork =
Expand Down Expand Up @@ -542,7 +543,7 @@ public void canUpdateVirtualMachineScaleSetWithEMSIAndLMSI() throws Exception {

// Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine
//
emsiIds = virtualMachineScaleSet.userAssignedManagedServiceIdentityIds();
emsiIds = virtualMachineScaleSet.innerModel().identity().userAssignedIdentities().keySet();
Assertions.assertNotNull(emsiIds);
Assertions.assertEquals(1, emsiIds.size());

Expand Down
Loading

0 comments on commit 9cca837

Please sign in to comment.