Search in sources :

Example 11 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method removeStorageBlobKeepsContainerIfNotEmpty.

@Test
public void removeStorageBlobKeepsContainerIfNotEmpty() {
    try {
        final String fileName1 = "abc1.txt";
        final String fileName2 = "abc2.txt";
        final String data = "5gadfgbsdafsdg";
        final String containerName = "jnkshouldgetdeleted";
        azureClient.resourceGroups().define(testEnv.azureResourceGroup).withRegion(testEnv.azureLocation).create();
        StorageAccount storageAccount = azureClient.storageAccounts().define(testEnv.azureStorageAccountName).withRegion(testEnv.azureLocation).withExistingResourceGroup(testEnv.azureResourceGroup).withSku(StorageAccountSkuType.STANDARD_LRS).create();
        final String blobToBeDeleted = uploadFile(storageAccount, fileName1, data, containerName);
        final String notDeletedBlob = uploadFile(storageAccount, fileName2, data, containerName);
        delegate.removeStorageBlob(new URI(blobToBeDeleted), testEnv.azureResourceGroup);
        Assert.assertTrue(containerExists(blobToBeDeleted));
        Assert.assertFalse(blobExists(blobToBeDeleted));
        Assert.assertTrue(blobExists(notDeletedBlob));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) URI(java.net.URI) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.Test)

Example 12 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method removeStorageBlobRemovesEmptyJenkinsContainers.

@Test
public void removeStorageBlobRemovesEmptyJenkinsContainers() {
    try {
        final String fileName = "abc.txt";
        final String data = "5gadfgbsdafsdg";
        // we deploy the init script in containers starting with jnk
        final String containerName_from_jenkins = "jnkshouldgetdeleted";
        // we shouldn't delete containers not deployed by us
        final String containerName_from_user = "notstartingwithjnk";
        azureClient.resourceGroups().define(testEnv.azureResourceGroup).withRegion(testEnv.azureLocation).create();
        StorageAccount storageAccount = azureClient.storageAccounts().define(testEnv.azureStorageAccountName).withRegion(testEnv.azureLocation).withExistingResourceGroup(testEnv.azureResourceGroup).withSku(StorageAccountSkuType.STANDARD_LRS).create();
        final String deletedContainerBlobURI = uploadFile(storageAccount, fileName, data, containerName_from_jenkins);
        final String existingContainerBlobURI = uploadFile(storageAccount, fileName, data, containerName_from_user);
        delegate.removeStorageBlob(new URI(deletedContainerBlobURI), testEnv.azureResourceGroup);
        delegate.removeStorageBlob(new URI(existingContainerBlobURI), testEnv.azureResourceGroup);
        // both container and blob are missing
        Assert.assertFalse(containerExists(deletedContainerBlobURI));
        // the container is there, but the blob is missing
        Assert.assertTrue(containerExists(existingContainerBlobURI));
        Assert.assertFalse(blobExists(existingContainerBlobURI));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) URI(java.net.URI) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.Test)

Example 13 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method getBlobEndpointSuffixForArmTemplateForGlobal.

@Test
public // Add Test for global first, will add test for mooncake later
void getBlobEndpointSuffixForArmTemplateForGlobal() {
    try {
        azureClient.storageAccounts().define(testEnv.azureStorageAccountName).withRegion(testEnv.azureLocation).withNewResourceGroup(testEnv.azureResourceGroup).create();
        StorageAccount storageAccount = azureClient.storageAccounts().getByResourceGroup(testEnv.azureResourceGroup, testEnv.azureStorageAccountName);
        String endSuffix = AzureVMManagementServiceDelegate.getBlobEndpointSuffixForTemplate(storageAccount);
        Assert.assertEquals(endSuffix, testEnv.blobEndpointSuffixForTemplate.get(TestEnvironment.AZUREPUBLIC));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) Test(org.junit.Test)

Example 14 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method canDeployMultipleTimes.

@Test
public void canDeployMultipleTimes() {
    try {
        AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
        when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
        AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar2 = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
        when(deploymentRegistrar2.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("other_tag/123"));
        AzureVMDeploymentInfo firstDeployment = createDefaultDeployment(1, deploymentRegistrar);
        AzureVMDeploymentInfo secondDeployment = createDefaultDeployment(1, deploymentRegistrar);
        AzureVMDeploymentInfo thirdDeployment = createDefaultDeployment(1, deploymentRegistrar2);
        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 List<String> baseVMNames = Arrays.asList(firstDeployment.getVmBaseName(), secondDeployment.getVmBaseName(), thirdDeployment.getVmBaseName());
        for (String base : baseVMNames) {
            final String baseName = base + "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);
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
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 15 with StorageAccount

use of com.azure.resourcemanager.storage.models.StorageAccount in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method createDeploymentWithMSI.

@Test
public void createDeploymentWithMSI() throws Exception {
    AzureVMAgentCleanUpTask.DeploymentRegistrar deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
    when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
    AzureVMDeploymentInfo deploymentInfo;
    deploymentInfo = createDefaultDeployment(1, true, true, false, false, "", 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.assertTrue(actualVM.isManagedServiceIdentityEnabled());
}
Also used : PublicIpAddress(com.azure.resourcemanager.network.models.PublicIpAddress) AzureVMAgentCleanUpTask(com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask) AzureVMDeploymentInfo(com.microsoft.azure.vmagent.AzureVMDeploymentInfo) StorageAccount(com.azure.resourcemanager.storage.models.StorageAccount) Network(com.azure.resourcemanager.network.models.Network) AzureUtil(com.microsoft.azure.vmagent.util.AzureUtil) 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) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Aggregations

StorageAccount (com.azure.resourcemanager.storage.models.StorageAccount)17 ManagementException (com.azure.core.management.exception.ManagementException)16 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)16 IOException (java.io.IOException)16 Test (org.junit.Test)11 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)7 Network (com.azure.resourcemanager.network.models.Network)7 NetworkInterface (com.azure.resourcemanager.network.models.NetworkInterface)7 PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)7 AzureVMAgentCleanUpTask (com.microsoft.azure.vmagent.AzureVMAgentCleanUpTask)7 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)7 AzureUtil (com.microsoft.azure.vmagent.util.AzureUtil)7 StorageAccountKey (com.azure.resourcemanager.storage.models.StorageAccountKey)4 BlobServiceClient (com.azure.storage.blob.BlobServiceClient)4 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)3 BlobServiceClientBuilder (com.azure.storage.blob.BlobServiceClientBuilder)3 StorageSharedKeyCredential (com.azure.storage.common.StorageSharedKeyCredential)3 FormException (hudson.model.Descriptor.FormException)3 URI (java.net.URI)3 BlobUrlParts (com.azure.storage.blob.BlobUrlParts)2