use of com.microsoft.azure.vmagent.AzureVMDeploymentInfo in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMManagementServiceDelegate method createDeploymentWithAvailabilitySet.
@Test
public void createDeploymentWithAvailabilitySet() throws Exception {
azureClient.resourceGroups().define(testEnv.azureResourceGroup).withRegion(testEnv.azureLocation).create();
AvailabilitySet availabilitySet = azureClient.availabilitySets().define("test-av-set").withRegion(testEnv.azureLocation).withExistingResourceGroup(testEnv.azureResourceGroup).withFaultDomainCount(2).withUpdateDomainCount(4).withSku(AvailabilitySetSkuTypes.ALIGNED).create();
Assert.assertNotNull("Failed to create availability set in resourceGroup " + testEnv.azureResourceGroup, availabilitySet);
testEnv.availabilityType = AvailabilityType.AVAILABILITY_SET.getName();
testEnv.availabilitySet = availabilitySet.name();
AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
AzureVMDeploymentInfo deploymentInfo;
deploymentInfo = createDefaultDeployment(1, deploymentRegistrar);
verify(deploymentRegistrar).registerDeployment("testCloud", testEnv.azureResourceGroup, deploymentInfo.getDeploymentName(), null);
Network actualVNet = null;
StorageAccount actualStorageAcc = null;
try {
actualVNet = azureClient.networks().getByResourceGroup(testEnv.azureResourceGroup, "jenkinsarm-vnet");
actualStorageAcc = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, testEnv.azureStorageAccountName);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
}
Assert.assertNotNull("The deployed VNet doesn't exist: " + testEnv.azureResourceGroup, actualVNet);
Assert.assertNotNull("The deployed Storage Account doesn't exist: " + testEnv.azureResourceGroup, actualStorageAcc);
final String baseName = deploymentInfo.getVmBaseName() + "0";
final String commonAssertMsg = testEnv.azureResourceGroup + ":" + baseName;
VirtualMachine actualVM = null;
NetworkInterface actualNetIface = null;
PublicIpAddress actualIP = null;
try {
actualVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, baseName);
actualNetIface = azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, baseName + "NIC");
actualIP = azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, baseName + "IPName");
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
}
Assert.assertNotNull("The deployed VM doesn't exist: " + commonAssertMsg, actualVM);
Assert.assertNotNull("The deployed Network interface doesn't exist: " + commonAssertMsg, actualNetIface);
Assert.assertNotNull("The deployed public IP doesn't exist: " + commonAssertMsg, actualIP);
assertThat(availabilitySet.id(), is(equalToIgnoringCase(actualVM.availabilitySetId())));
}
use of com.microsoft.azure.vmagent.AzureVMDeploymentInfo in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMManagementServiceDelegate method createDeploymentTest.
@Test
public void createDeploymentTest() throws Exception {
Random rand = new Random();
final int numberOfAgents = rand.nextInt(4) + 1;
AzureVMDeploymentInfo deploymentInfo = null;
AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
deploymentInfo = createDefaultDeployment(numberOfAgents, deploymentRegistrar);
verify(deploymentRegistrar).registerDeployment("testCloud", testEnv.azureResourceGroup, deploymentInfo.getDeploymentName(), null);
Network actualVNet = null;
StorageAccount actualStorageAcc = null;
try {
actualVNet = azureClient.networks().getByResourceGroup(testEnv.azureResourceGroup, "jenkinsarm-vnet");
actualStorageAcc = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, testEnv.azureStorageAccountName);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
}
Assert.assertNotNull("The deployed VNet doesn't exist: " + testEnv.azureResourceGroup, actualVNet);
Assert.assertNotNull("The deployed Storage Account doesn't exist: " + testEnv.azureResourceGroup, actualStorageAcc);
for (int i = 0; i < numberOfAgents; i++) {
final String baseName = deploymentInfo.getVmBaseName() + i;
final String commonAssertMsg = testEnv.azureResourceGroup + ":" + baseName;
VirtualMachine actualVM = null;
NetworkInterface actualNetIface = null;
PublicIpAddress actualIP = null;
try {
actualVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, baseName);
actualNetIface = azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, baseName + "NIC");
actualIP = azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, baseName + "IPName");
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
}
Assert.assertNotNull("The deployed VM doesn't exist: " + commonAssertMsg, actualVM);
Assert.assertNotNull("The deployed Network interface doesn't exist: " + commonAssertMsg, actualNetIface);
Assert.assertNotNull("The deployed public IP doesn't exist: " + commonAssertMsg, actualIP);
Assert.assertFalse(actualVM.isManagedServiceIdentityEnabled());
Assert.assertEquals(getDefaultOsDiskSize(OS_TYPE), actualVM.osDiskSize());
Assert.assertEquals("gavin", actualVM.tags().get("author"));
Assert.assertEquals("test", actualVM.tags().get("env"));
Assert.assertEquals("gavin", actualIP.tags().get("author"));
Assert.assertEquals("test", actualIP.tags().get("env"));
Assert.assertEquals("gavin", actualNetIface.tags().get("author"));
Assert.assertEquals("test", actualNetIface.tags().get("env"));
Assert.assertEquals("gavin", actualStorageAcc.tags().get("author"));
Assert.assertEquals("test", actualStorageAcc.tags().get("env"));
Assert.assertEquals("gavin", actualVNet.tags().get("author"));
Assert.assertEquals("test", actualVNet.tags().get("env"));
}
}
use of com.microsoft.azure.vmagent.AzureVMDeploymentInfo in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMManagementServiceDelegate method removeIPNameTest.
@Test
public void removeIPNameTest() {
try {
final AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, null);
final String nodeName = deploymentInfo.getVmBaseName() + "0";
delegate.removeIPName(testEnv.azureResourceGroup, nodeName);
// should fail because the VM is still using them
Assert.assertNotNull(azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, nodeName + "IPName"));
Assert.assertNotNull(azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, nodeName + "NIC"));
// destroy the vm first
azureClient.virtualMachines().deleteByResourceGroup(testEnv.azureResourceGroup, nodeName);
delegate.removeIPName(testEnv.azureResourceGroup, nodeName);
ManagementException managementException = assertThrows(ManagementException.class, () -> azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, nodeName + "IPName"));
assertThat(managementException.getResponse().getStatusCode(), equalTo(404));
managementException = assertThrows(ManagementException.class, () -> azureClient.networkInterfaces().getByResourceGroup(testEnv.azureResourceGroup, nodeName + "NIC"));
assertThat(managementException.getResponse().getStatusCode(), equalTo(404));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
Assert.fail(e.getMessage());
}
}
use of com.microsoft.azure.vmagent.AzureVMDeploymentInfo in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMManagementServiceDelegate method setVirtualMachineDetailsWithPublicIP.
@Test
public void setVirtualMachineDetailsWithPublicIP() {
try {
final AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, null);
final String nodeName = deploymentInfo.getVmBaseName() + "0";
final PublicIpAddress publicIP = azureClient.publicIpAddresses().getByResourceGroup(testEnv.azureResourceGroup, nodeName + "IPName");
final String privateIP = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, nodeName).getPrimaryNetworkInterface().primaryPrivateIP();
setVirtualMachineDetailsCommonVerification(nodeName, publicIP.fqdn(), privateIP, publicIP.ipAddress());
} catch (Exception e) {
LOGGER.log(Level.SEVERE, null, e);
Assert.fail(e.getMessage());
}
}
use of com.microsoft.azure.vmagent.AzureVMDeploymentInfo in project azure-vm-agents-plugin by jenkinsci.
the class ITAzureVMAgentCleanUpTask method cleanLeakedResourcesRemovesDeployedResources.
@Test
public void cleanLeakedResourcesRemovesDeployedResources() throws Exception {
final AzureUtil.DeploymentTag tagValue = new AzureUtil.DeploymentTag("some_value/123");
final AzureUtil.DeploymentTag matchingTagValue = new AzureUtil.DeploymentTag("some_value/9999123");
final String cloudName = "some_cloud_name";
AzureVMCloud cloud = mock(AzureVMCloud.class);
when(cloud.getCloudName()).thenReturn(cloudName);
when(cloud.getAzureClient()).thenReturn(azureClient);
when(cloud.getServiceDelegate()).thenReturn(delegate);
final DeploymentRegistrar deploymentRegistrarMock = mock(DeploymentRegistrar.class);
when(deploymentRegistrarMock.getDeploymentTag()).thenReturn(tagValue);
final DeploymentRegistrar deploymentRegistrarMock_matching = mock(DeploymentRegistrar.class);
when(deploymentRegistrarMock_matching.getDeploymentTag()).thenReturn(matchingTagValue);
final AzureVMDeploymentInfo deployment = createDefaultDeployment(2, deploymentRegistrarMock);
final List<String> validVMs = Collections.singletonList(deployment.getVmBaseName() + "0");
AzureVMAgentCleanUpTask cleanUpTask = spy(AzureVMAgentCleanUpTask.class);
when(cleanUpTask.getValidVMs()).thenReturn(validVMs);
// should remove second deployment
cleanUpTask.cleanLeakedResources(cloud, testEnv.azureResourceGroup, deploymentRegistrarMock_matching);
// give time for azure to realize that some resources are missing
Thread.sleep(20 * 1000);
PagedIterable<GenericResource> resources = azureClient.genericResources().listByResourceGroup(testEnv.azureResourceGroup);
for (GenericResource resource : resources) {
if (StringUtils.containsIgnoreCase(resource.type(), "storageAccounts") || StringUtils.containsIgnoreCase(resource.type(), "virtualNetworks")) {
continue;
}
if (resource.tags().get(Constants.AZURE_RESOURCES_TAG_NAME) != null && matchingTagValue.matches(new AzureUtil.DeploymentTag(resource.tags().get(Constants.AZURE_RESOURCES_TAG_NAME)))) {
String resourceName = resource.name();
String depl = deployment.getVmBaseName() + "0";
Assert.assertTrue("Resource shouldn't exist: " + resourceName + " (vmbase: " + depl + " )", resourceName.contains(depl));
}
}
}
Aggregations