Search in sources :

Example 6 with AzureVMDeploymentInfo

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

the class ITAzureVMManagementServiceDelegate method createDeploymentWithSpecificOsDiskSize.

@Test
public void createDeploymentWithSpecificOsDiskSize() throws Exception {
    int osDiskSize = 100;
    testEnv.osDiskSize = osDiskSize;
    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);
    Assert.assertEquals(osDiskSize, actualVM.osDiskSize());
}
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) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 7 with AzureVMDeploymentInfo

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

Example 8 with AzureVMDeploymentInfo

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

the class ITAzureVMAgentCleanUpTask method cleanDeploymentsTest.

@Test
public void cleanDeploymentsTest() throws Exception {
    final AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, null);
    final String cloudName = "fake_cloud_name";
    final DeploymentRegistrar deploymentRegistrar = DeploymentRegistrar.getInstance();
    deploymentRegistrar.registerDeployment(cloudName, testEnv.azureResourceGroup, deploymentInfo.getDeploymentName(), null);
    AzureVMAgentCleanUpTask cleanUpMock = spy(AzureVMAgentCleanUpTask.class);
    AzureVMCloud cloudMock = mock(AzureVMCloud.class);
    when(cloudMock.getCloudName()).thenReturn(cloudName);
    when(cloudMock.getAzureClient()).thenReturn(azureClient);
    when(cloudMock.getServiceDelegate()).thenReturn(delegate);
    when(cleanUpMock.getCloud(cloudName)).thenReturn(cloudMock);
    doNothing().when(cleanUpMock).execute(any(TaskListener.class));
    // should be a no-op, the timeout is 1 day
    cleanUpMock.cleanDeployments(60 * 24, -1);
    Assert.assertTrue(azureClient.deployments().checkExistence(testEnv.azureResourceGroup, deploymentInfo.getDeploymentName()));
    // should delete all deployments
    cleanUpMock.cleanDeployments(-1, -1);
    // give time for azure to realize that the deployment was deleted
    Thread.sleep(10 * 1000);
    Assert.assertFalse(azureClient.deployments().checkExistence(testEnv.azureResourceGroup, deploymentInfo.getDeploymentName()));
}
Also used : DeploymentRegistrar(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) TaskListener(hudson.model.TaskListener) Test(org.junit.Test)

Example 9 with AzureVMDeploymentInfo

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

the class ITAzureVMCloud method createProvisionedAgentWorksWhenDeploymentExists.

@Ignore
@Test
public void createProvisionedAgentWorksWhenDeploymentExists() {
    try {
        final AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, null);
        final String vmName = deploymentInfo.getVmBaseName() + "0";
        final String vmDNS = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).getPrimaryPublicIPAddress().fqdn();
        final String deploymentName = deploymentInfo.getDeploymentName();
        final String templateName = "createTemplate";
        final String templateDesc = "createTemplateDesc";
        final String agentWorkspace = "createTemplateAgentWorkspace";
        final int noOfParallelJobs = 2;
        final Node.Mode useAgentAlwaysIfAvail = Node.Mode.NORMAL;
        final String templateLabels = "label1 label2";
        final String credentialsId = "cred_id";
        final String jvmOptions = "";
        final boolean isShutdownOnIdle = false;
        final int retentionTimeInMin = 30;
        final String initScript = "";
        final String terminateScript = "";
        final String agentLaunchMethod = Constants.LAUNCH_METHOD_SSH;
        final boolean executeInitScriptAsRoot = true;
        final boolean doNotUseMachineIfInitFails = true;
        final boolean enableMSI = false;
        final boolean enableUAMI = false;
        final boolean ephemeralOSDisk = false;
        final ProvisioningActivity.Id provisioningId = new ProvisioningActivity.Id(vmName, deploymentName);
        AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
        AzureVMCloud cloudMock = spy(new AzureVMCloud("", credentialsId, "42", "30", "new", testEnv.azureResourceGroup, null, null));
        when(templateMock.retrieveAzureCloudReference()).thenReturn(cloudMock);
        when(templateMock.getTemplateName()).thenReturn(templateName);
        when(templateMock.getTemplateDesc()).thenReturn(templateDesc);
        when(templateMock.getAgentWorkspace()).thenReturn(agentWorkspace);
        when(templateMock.getNoOfParallelJobs()).thenReturn(noOfParallelJobs);
        when(templateMock.getUsageMode()).thenReturn(useAgentAlwaysIfAvail);
        when(templateMock.getLabels()).thenReturn(templateLabels);
        when(templateMock.getCredentialsId()).thenReturn(credentialsId);
        when(templateMock.getJvmOptions()).thenReturn(jvmOptions);
        when(templateMock.isShutdownOnIdle()).thenReturn(isShutdownOnIdle);
        when(templateMock.getRetentionTimeInMin()).thenReturn(retentionTimeInMin);
        when(templateMock.getInitScript()).thenReturn(initScript);
        when(templateMock.getTerminateScript()).thenReturn(terminateScript);
        when(templateMock.getAgentLaunchMethod()).thenReturn(agentLaunchMethod);
        when(templateMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
        when(templateMock.getExecuteInitScriptAsRoot()).thenReturn(executeInitScriptAsRoot);
        when(templateMock.getDoNotUseMachineIfInitFails()).thenReturn(doNotUseMachineIfInitFails);
        when(templateMock.isEnableMSI()).thenReturn(enableMSI);
        when(templateMock.isEnableUAMI()).thenReturn(enableUAMI);
        when(templateMock.isEphemeralOSDisk()).thenReturn(ephemeralOSDisk);
        AzureVMAgent newAgent = cloudMock.createProvisionedAgent(provisioningId, templateMock, vmName, deploymentName);
        Assert.assertEquals(vmDNS, newAgent.getPublicDNSName());
        Assert.assertEquals(Constants.DEFAULT_SSH_PORT, newAgent.getSshPort());
        Assert.assertEquals(templateName, newAgent.getTemplateName());
        Assert.assertEquals(templateDesc, newAgent.getNodeDescription());
        Assert.assertEquals(noOfParallelJobs, newAgent.getNumExecutors());
        Assert.assertEquals(useAgentAlwaysIfAvail, newAgent.getMode());
        Assert.assertEquals(templateLabels, newAgent.getLabelString());
        Assert.assertEquals(jvmOptions, newAgent.getJvmOptions());
        Assert.assertEquals(retentionTimeInMin, newAgent.getRetentionTimeInMin());
        Assert.assertEquals(initScript, newAgent.getInitScript());
        Assert.assertEquals(terminateScript, newAgent.getTerminateScript());
        Assert.assertEquals(agentLaunchMethod, newAgent.getAgentLaunchMethod());
        Assert.assertEquals(executeInitScriptAsRoot, newAgent.getExecuteInitScriptAsRoot());
        Assert.assertEquals(doNotUseMachineIfInitFails, newAgent.getDoNotUseMachineIfInitFails());
        Assert.assertEquals(enableMSI, newAgent.isEnableMSI());
        Assert.assertEquals(enableUAMI, newAgent.isEnableUAMI());
        Assert.assertEquals(ephemeralOSDisk, newAgent.isEphemeralOSDisk());
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) Node(hudson.model.Node) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) ProvisioningActivity(org.jenkinsci.plugins.cloudstats.ProvisioningActivity) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with AzureVMDeploymentInfo

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

the class ITAzureVMManagementServiceDelegate method createDeploymentWithExistingNSG.

@Test
public void createDeploymentWithExistingNSG() {
    try {
        final String nsgName = TestEnvironment.GenerateRandomString(12);
        NetworkSecurityGroup nsg = azureClient.networkSecurityGroups().define(nsgName).withRegion(testEnv.azureLocation).withNewResourceGroup(testEnv.azureResourceGroup).create();
        AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
        when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
        AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, nsgName, deploymentRegistrar);
        VirtualMachine deployedVM = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, deploymentInfo.getVmBaseName() + "0");
        final String actualNSGId = deployedVM.getPrimaryNetworkInterface().getNetworkSecurityGroup().id();
        Assert.assertEquals(nsg.id(), actualNSGId);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : NetworkSecurityGroup(com.azure.resourcemanager.network.models.NetworkSecurityGroup) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) 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