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());
}
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");
}
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()));
}
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());
}
}
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());
}
}
Aggregations