Search in sources :

Example 1 with AzureVMDeploymentInfo

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())));
}
Also used : AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) NetworkInterface(com.azure.resourcemanager.network.models.NetworkInterface) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) Network(com.azure.resourcemanager.network.models.Network) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) AvailabilitySet(com.azure.resourcemanager.compute.models.AvailabilitySet) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 2 with AzureVMDeploymentInfo

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"));
    }
}
Also used : AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) NetworkInterface(com.azure.resourcemanager.network.models.NetworkInterface) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) Random(java.util.Random) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) Network(com.azure.resourcemanager.network.models.Network) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 3 with AzureVMDeploymentInfo

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());
    }
}
Also used : ManagementException(com.azure.core.management.exception.ManagementException) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.Test)

Example 4 with AzureVMDeploymentInfo

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());
    }
}
Also used : PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.Test)

Example 5 with AzureVMDeploymentInfo

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));
        }
    }
}
Also used : DeploymentRegistrar(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) GenericResource(com.azure.resourcemanager.resources.models.GenericResource) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) Test(org.junit.Test)

Aggregations

AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)17 Test (org.junit.Test)16 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)15 ManagementException (com.azure.core.management.exception.ManagementException)14 IOException (java.io.IOException)14 PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)10 AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)10 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)9 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)8 Network (com.azure.resourcemanager.network.models.Network)7 NetworkInterface (com.azure.resourcemanager.network.models.NetworkInterface)7 StorageAccount (com.azure.resourcemanager.storage.models.StorageAccount)7 AzureVMCloud (com.microsoft.azure.vmagent.AzureVMCloud)6 AzureVMAgentTemplate (com.microsoft.azure.vmagent.AzureVMAgentTemplate)4 AzureVMAgent (com.microsoft.azure.vmagent.AzureVMAgent)3 DeploymentRegistrar (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar)2 AvailabilitySet (com.azure.resourcemanager.compute.models.AvailabilitySet)1 NetworkSecurityGroup (com.azure.resourcemanager.network.models.NetworkSecurityGroup)1 GenericResource (com.azure.resourcemanager.resources.models.GenericResource)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1