Search in sources :

Example 6 with AzureVMCloud

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

the class AzureVMAgentTemplateReadResolveTest method readResolve.

@Test
@LocalData
public void readResolve() {
    Jenkins.CloudList clouds = j.jenkins.clouds;
    assertThat(clouds, hasSize(1));
    AzureVMCloud cloud = (AzureVMCloud) clouds.get(0);
    assertThat(cloud.getCloudName(), is("myAzure"));
    assertThat(cloud.getVmTemplates(), hasSize(1));
    AzureVMAgentTemplate template = cloud.getVmTemplates().get(0);
    AzureVMAgentTemplate.ImageReferenceTypeClass reference = template.getImageReference();
    assertThat(reference.getPublisher(), is("Canonical"));
    assertThat(reference.getOffer(), is("UbuntuServer"));
    assertThat(reference.getSku(), is("16.04-LTS"));
    assertThat(reference.getVersion(), is("latest"));
    assertThat(reference.getType(), is(ImageReferenceType.REFERENCE));
}
Also used : Jenkins(jenkins.model.Jenkins) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate) LocalData(org.jvnet.hudson.test.recipes.LocalData) Test(org.junit.Test)

Example 7 with AzureVMCloud

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

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

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

the class ITAzureVMCloud method createProvisionedAgentThrowsExceptionWhenNoDeployments.

@Test
public void createProvisionedAgentThrowsExceptionWhenNoDeployments() {
    try {
        final String vmName = "fakeVM";
        final String deploymentName = "fakeDeployment";
        final ProvisioningActivity.Id provisioningId = new ProvisioningActivity.Id(vmName, deploymentName);
        AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
        AzureVMCloud cloudMock = spy(new AzureVMCloud("", "xyz", "42", "0", "new", testEnv.azureResourceGroup, null, null));
        when(templateMock.retrieveAzureCloudReference()).thenReturn(cloudMock);
        Assert.assertThrows(AzureCloudException.class, () -> cloudMock.createProvisionedAgent(provisioningId, templateMock, vmName, deploymentName));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : ProvisioningActivity(org.jenkinsci.plugins.cloudstats.ProvisioningActivity) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) Test(org.junit.Test)

Example 10 with AzureVMCloud

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

the class ITAzureVMManagementServiceDelegate method attachPublicIP.

@Test
public void attachPublicIP() {
    try {
        final AzureVMDeploymentInfo deploymentInfo = createDefaultDeployment(1, false, null);
        final String nodeName = deploymentInfo.getVmBaseName() + "0";
        final String privateIP = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, nodeName).getPrimaryNetworkInterface().primaryPrivateIP();
        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(templateMock.getLocation()).thenReturn(testEnv.azureLocation);
        when(agentMock.getNodeName()).thenReturn(nodeName);
        delegate.attachPublicIP(agentMock, templateMock);
        final PublicIpAddress publicIP = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, nodeName).getPrimaryPublicIPAddress();
        Assert.assertNotNull(publicIP);
        verify(agentMock).setPublicDNSName(publicIP.fqdn());
        verify(agentMock).setSshPort(Constants.DEFAULT_SSH_PORT);
        verify(agentMock).setPublicIP(publicIP.ipAddress());
        verify(agentMock).setPrivateIP(privateIP);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) 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) Test(org.junit.Test)

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