use of com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask 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));
}
use of com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask 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));
}
}
}
use of com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask 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()));
}
Aggregations