Search in sources :

Example 1 with DeploymentRegistrar

use of com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar 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 2 with DeploymentRegistrar

use of com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar 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 3 with DeploymentRegistrar

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

Aggregations

AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)3 DeploymentRegistrar (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask.DeploymentRegistrar)3 AzureVMCloud (com.microsoft.azure.vmagent.AzureVMCloud)3 Test (org.junit.Test)3 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)2 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)2 ManagementException (com.azure.core.management.exception.ManagementException)1 GenericResource (com.azure.resourcemanager.resources.models.GenericResource)1 TaskListener (hudson.model.TaskListener)1