Search in sources :

Example 1 with AzureVMCloud

use of com.microsoft.azure.vmagent.AzureVMCloud in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method uploadCustomScript.

private void uploadCustomScript(String uploadFileName, String writtenData) {
    AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
    when(templateMock.getStorageAccountName()).thenReturn(testEnv.azureStorageAccountName);
    when(templateMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
    when(templateMock.getLocation()).thenReturn(testEnv.azureLocation);
    when(templateMock.getInitScript()).thenReturn(writtenData);
    when(templateMock.getTerminateScript()).thenReturn(writtenData);
    when(templateMock.getStorageAccountType()).thenReturn(SkuName.STANDARD_LRS.toString());
    when(templateMock.getResourceGroupReferenceType()).thenReturn(testEnv.azureResourceGroupReferenceType);
    AzureVMCloud cloudMock = mock(AzureVMCloud.class);
    when(cloudMock.getCloudName()).thenReturn("testCloudName");
    when(templateMock.retrieveAzureCloudReference()).thenReturn(cloudMock);
    try {
        delegate.uploadCustomScript(templateMock, uploadFileName);
        final String downloadedData = downloadFromAzure(testEnv.azureResourceGroup, testEnv.azureStorageAccountName, Constants.CONFIG_CONTAINER_NAME, uploadFileName);
        /*Data padded before upload to Page Blob so we need to use strip*/
        Assert.assertEquals(StringUtils.strip(writtenData), StringUtils.strip(downloadedData));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException)

Example 2 with AzureVMCloud

use of com.microsoft.azure.vmagent.AzureVMCloud in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method setVirtualMachineDetailsCommonVerification.

private void setVirtualMachineDetailsCommonVerification(String vmName, String fqdn, String privateIP, String publicIp) throws Exception {
    AzureVMAgent agentMock = mock(AzureVMAgent.class);
    AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
    AzureVMCloud cloudMock = mock(AzureVMCloud.class);
    when(templateMock.retrieveAzureCloudReference()).thenReturn(cloudMock);
    when(templateMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
    when(agentMock.getNodeName()).thenReturn(vmName);
    delegate.setVirtualMachineDetails(agentMock, templateMock);
    verify(agentMock).setPublicDNSName(fqdn);
    verify(agentMock).setSshPort(Constants.DEFAULT_SSH_PORT);
    verify(agentMock).setPublicIP(publicIp);
    verify(agentMock).setPrivateIP(privateIP);
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate)

Example 3 with AzureVMCloud

use of com.microsoft.azure.vmagent.AzureVMCloud in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMAgentCleanUpTask method cleanLeakedResourcesRemovesVM.

@Test
public void cleanLeakedResourcesRemovesVM() {
    final String vmName = "tstleak";
    final String tagName = Constants.AZURE_RESOURCES_TAG_NAME;
    final AzureUtil.DeploymentTag tagValue = new AzureUtil.DeploymentTag("some_value/123");
    final AzureUtil.DeploymentTag nonMatchingTagValue1 = new AzureUtil.DeploymentTag("some_value/124");
    final AzureUtil.DeploymentTag nonMatchingTagValue2 = new AzureUtil.DeploymentTag("some_other_value/9999123");
    final AzureUtil.DeploymentTag matchingTagValue = new AzureUtil.DeploymentTag("some_value/9999123");
    final String cloudName = "some_cloud_name";
    final List<String> emptyValidVMsList = Collections.emptyList();
    AzureVMCloud cloud = mock(AzureVMCloud.class);
    when(cloud.getCloudName()).thenReturn(cloudName);
    when(cloud.getAzureClient()).thenReturn(azureClient);
    when(cloud.getServiceDelegate()).thenReturn(delegate);
    AzureVMAgentCleanUpTask cleanUpTask = spy(AzureVMAgentCleanUpTask.class);
    when(cleanUpTask.getValidVMs()).thenReturn(emptyValidVMsList);
    final DeploymentRegistrar deploymentRegistrarMock_nonMatching1 = mock(DeploymentRegistrar.class);
    when(deploymentRegistrarMock_nonMatching1.getDeploymentTag()).thenReturn(nonMatchingTagValue1);
    final DeploymentRegistrar deploymentRegistrarMock_nonMatching2 = mock(DeploymentRegistrar.class);
    when(deploymentRegistrarMock_nonMatching2.getDeploymentTag()).thenReturn(nonMatchingTagValue2);
    final DeploymentRegistrar deploymentRegistrarMock_matching = mock(DeploymentRegistrar.class);
    when(deploymentRegistrarMock_matching.getDeploymentTag()).thenReturn(matchingTagValue);
    createAzureVM(vmName, tagName, tagValue.get());
    cleanUpTask.cleanLeakedResources(cloud, testEnv.azureResourceGroup, deploymentRegistrarMock_nonMatching1);
    Assert.assertNotNull(azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName));
    cleanUpTask.cleanLeakedResources(cloud, testEnv.azureResourceGroup, deploymentRegistrarMock_nonMatching2);
    Assert.assertNotNull(azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName));
    cleanUpTask.cleanLeakedResources(cloud, testEnv.azureResourceGroup, deploymentRegistrarMock_matching);
    ManagementException managementException = assertThrows(ManagementException.class, () -> azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName));
    assertThat(managementException.getResponse().getStatusCode(), equalTo(404));
}
Also used : DeploymentRegistrar(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar) ManagementException(com.azure.core.management.exception.ManagementException) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) Test(org.junit.Test)

Example 4 with AzureVMCloud

use of com.microsoft.azure.vmagent.AzureVMCloud 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)

Example 5 with AzureVMCloud

use of com.microsoft.azure.vmagent.AzureVMCloud in project azure-vm-agents-plugin by jenkinsci.

the class IntegrationTest method createDefaultDeployment.

protected AzureVMDeploymentInfo createDefaultDeployment(int numberOfAgents, boolean usePrivateIP, boolean enableMSI, boolean enableUAMI, boolean ephemeralOSDisk, String nsgName, AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar) throws Exception {
    final String templateName = "t" + TestEnvironment.GenerateRandomString(7);
    final String osType = OS_TYPE;
    final String initScript = "echo \"" + UUID.randomUUID().toString() + "\"";
    final String terminateScript = "echo \"" + UUID.randomUUID().toString() + "\"";
    final String launchMethod = Constants.LAUNCH_METHOD_SSH;
    final String vmUser = "tstVmUser";
    final Secret vmPassword = Secret.fromString(TestEnvironment.GenerateRandomString(16) + "AA@@12345@#$%^&*-_!+=[]{}|\\:`,.?/~\\\"();\'");
    final String storageType = SkuName.STANDARD_LRS.toString();
    StandardUsernamePasswordCredentials vmCredentials = mock(StandardUsernamePasswordCredentials.class);
    when(vmCredentials.getUsername()).thenReturn(vmUser);
    when(vmCredentials.getPassword()).thenReturn(vmPassword);
    if (deploymentRegistrar == null) {
        deploymentRegistrar = AzureVMAgentCleanUpTask.DeploymentRegistrar.getInstance();
    }
    AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
    AzureVMCloud cloudMock = mock(AzureVMCloud.class);
    when(cloudMock.getCloudName()).thenReturn("testCloud");
    when(cloudMock.getCloudTags()).thenReturn(testEnv.customTags);
    when(templateMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
    when(templateMock.getResourceGroupReferenceType()).thenReturn(testEnv.azureResourceGroupReferenceType);
    when(templateMock.getStorageAccountName()).thenReturn(testEnv.azureStorageAccountName);
    when(templateMock.getLocation()).thenReturn(testEnv.azureLocation);
    when(templateMock.getAvailabilityType()).thenReturn(new AzureVMAgentTemplate.AvailabilityTypeClass(testEnv.availabilitySet));
    when(templateMock.getTemplateName()).thenReturn(templateName);
    when(templateMock.getOsType()).thenReturn(osType);
    when(templateMock.getInitScript()).thenReturn(initScript);
    when(templateMock.getTerminateScript()).thenReturn(terminateScript);
    when(templateMock.getAgentLaunchMethod()).thenReturn(launchMethod);
    when(templateMock.getImageTopLevelType()).thenReturn(Constants.IMAGE_TOP_LEVEL_ADVANCED);
    when(templateMock.isTopLevelType(Constants.IMAGE_TOP_LEVEL_BASIC)).thenReturn(false);
    when(templateMock.getTags()).thenReturn(testEnv.templateTags);
    when(templateMock.getImageReference()).thenReturn(new AzureVMAgentTemplate.ImageReferenceTypeClass(null, testEnv.azureImageId, testEnv.azureImagePublisher, testEnv.azureImageOffer, testEnv.azureImageSku, testEnv.azureImageVersion, null, null, null, null, null));
    when(templateMock.getVirtualMachineSize()).thenReturn(testEnv.azureImageSize);
    when(templateMock.getVMCredentials()).thenReturn(vmCredentials);
    when(templateMock.retrieveAzureCloudReference()).thenReturn(cloudMock);
    when(templateMock.getUsePrivateIP()).thenReturn(!usePrivateIP);
    when(templateMock.getNsgName()).thenReturn(nsgName);
    when(templateMock.getStorageAccountType()).thenReturn(storageType);
    when(templateMock.getOsDiskStorageAccountType()).thenReturn(storageType);
    when(templateMock.getDiskType()).thenReturn(Constants.DISK_MANAGED);
    when(templateMock.getOsDiskSize()).thenReturn(testEnv.osDiskSize);
    when(templateMock.isPreInstallSsh()).thenReturn(true);
    when(templateMock.isEnableMSI()).thenReturn(enableMSI);
    when(templateMock.isEnableUAMI()).thenReturn(enableUAMI);
    when(templateMock.isEphemeralOSDisk()).thenReturn(ephemeralOSDisk);
    AzureVMDeploymentInfo ret = delegate.createDeployment(templateMock, numberOfAgents, deploymentRegistrar);
    List<String> vmNames = new ArrayList<>();
    for (int i = 0; i < numberOfAgents; i++) {
        vmNames.add(ret.getVmBaseName() + i);
    }
    // wait for deployment to complete
    // wait 10 minutes
    final int maxTries = 20;
    for (int i = 0; i < maxTries; i++) {
        if (areAllVMsDeployed(vmNames)) {
            return ret;
        }
        try {
            Thread.sleep(30 * 1000);
        } catch (InterruptedException ex) {
        // ignore
        }
    }
    throw new Exception("Deployment is not completed after 10 minutes");
}
Also used : AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate) ArrayList(java.util.ArrayList) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) ManagementException(com.azure.core.management.exception.ManagementException) IOException(java.io.IOException) Secret(hudson.util.Secret) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud)

Aggregations

AzureVMCloud (com.microsoft.azure.vmagent.AzureVMCloud)13 AzureVMAgentTemplate (com.microsoft.azure.vmagent.AzureVMAgentTemplate)10 Test (org.junit.Test)10 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)6 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)6 ManagementException (com.azure.core.management.exception.ManagementException)5 AzureVMAgent (com.microsoft.azure.vmagent.AzureVMAgent)4 IOException (java.io.IOException)4 AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)3 DeploymentRegistrar (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar)3 PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)2 AzureTagPair (com.microsoft.azure.vmagent.AzureTagPair)2 AzureVMCloudRetensionStrategy (com.microsoft.azure.vmagent.AzureVMCloudRetensionStrategy)2 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)2 ProvisioningActivity (org.jenkinsci.plugins.cloudstats.ProvisioningActivity)2 GenericResource (com.azure.resourcemanager.resources.models.GenericResource)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 Node (hudson.model.Node)1 TaskListener (hudson.model.TaskListener)1 Secret (hudson.util.Secret)1