public void LegalHoldTest() { if (isResourceMode) { string containerName = Utility.GenNameString("container"); string[] legalhold = new string[] { "tag1", "tag2", "tag3", "tag4" }; string[] legalhold2Remove = new string[] { "tag2", "tag3" }; string[] legalholdLeft = new string[] { "tag1", "tag4" }; //Creat container Test.Assert(CommandAgent.NewAzureRmStorageContainer(resourceGroupName, accountName, containerName, PublicAccess: PSPublicAccess.Container), "Create Container should success."); PSContainer con = GetContainer(containerName); ValidateContainer(con, resourceGroupName, accountName, containerName, publicaccess: PSPublicAccess.Container); //Add Container LegalHold Test.Assert(CommandAgent.AddAzureRmStorageContainerLegalHold(resourceGroupName, accountName, containerName, legalhold), "Add Container LegalHold should success."); PSLegalHold outputLegalhold = GetLegalholdFromOutput(); CompareLegalhold(legalhold, outputLegalhold); //Remove Container LegalHold Test.Assert(CommandAgent.RemoveAzureRmStorageContainerLegalHold(resourceGroupName, accountName, containerName, legalhold2Remove), "Remove Container LegalHold should success."); outputLegalhold = GetLegalholdFromOutput(); CompareLegalhold(legalholdLeft, outputLegalhold); //Remove Container fail when legalhold exist Test.Assert(!CommandAgent.RemoveAzureRmStorageContainer(resourceGroupName, accountName, containerName), "Remove Container with legalhold should fail."); ExpectedContainErrorMessage(string.Format("The storage account {0} container {1} is protected from deletion due to LegalHold.", accountName, containerName)); //Remove Container LegalHold Test.Assert(CommandAgent.RemoveAzureRmStorageContainerLegalHold(resourceGroupName, accountName, containerName, legalholdLeft), "Remove Container LegalHold should success."); outputLegalhold = GetLegalholdFromOutput(); CompareLegalhold(null, outputLegalhold); //Remove container Test.Assert(CommandAgent.RemoveAzureRmStorageContainer(resourceGroupName, accountName, containerName), "Remove Container should success."); } }
public void GetContainer_withLegalHoldAndImmutabilityPolicy() { if (isResourceMode) { string containerName = Utility.GenNameString("container"); //Creat container Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript( string.Format("Get-AzureRMStorageAccount -ResourceGroupName {0} -Name {1} | New-AzureRmStorageContainer -ContainerName {2}", resourceGroupName, accountName, containerName)), string.Format("Create container for storage account {0} in the resource group {1} should success.", accountName, resourceGroupName)); PSContainer con = GetContainerFromServer(containerName); ValidateContainer(con, resourceGroupName, accountName, containerName, publicaccess: PSPublicAccess.None); //Set ImmutabilityPolicy int immutabilityPeriod = 5; Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript( string.Format("Get-AzureRMStorageAccount -ResourceGroupName {0} -Name {1} | Set-AzureRmStorageContainerImmutabilityPolicy -ContainerName {2} -ImmutabilityPeriod {3}", resourceGroupName, accountName, containerName, immutabilityPeriod)), string.Format("Set Container ImmutabilityPolicy should success.")); Test.Assert(CommandAgent.GetAzureRmStorageContainerImmutabilityPolicy(resourceGroupName, accountName, containerName), "Get Container ImmutabilityPolicy should success."); PSImmutabilityPolicy immuPolicy = GetImmutabilityPolicyFromOutput(); ValidateImmutabilityPolicy(immuPolicy, immutabilityPeriod, "Unlocked"); //Lock ImmutabilityPolicy Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript( string.Format("Get-AzureRmStorageContainer -ResourceGroupName {0} -StorageAccountName {1} -Name {2} | Lock-AzureRmStorageContainerImmutabilityPolicy -Etag '{3}' -Force", resourceGroupName, accountName, containerName, immuPolicy.Etag)), string.Format("Lock Container ImmutabilityPolicy should success.")); Test.Assert(CommandAgent.GetAzureRmStorageContainerImmutabilityPolicy(resourceGroupName, accountName, containerName), "Get Container ImmutabilityPolicy should success."); immuPolicy = GetImmutabilityPolicyFromOutput(); ValidateImmutabilityPolicy(immuPolicy, immutabilityPeriod, "Locked"); //Extend ImmutabilityPolicy int immutabilityPeriod2 = 10; Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript( string.Format("Get-AzureRmStorageContainerImmutabilityPolicy -ResourceGroupName {0} -StorageAccountName {1} -ContainerName {2} | Set-AzureRmStorageContainerImmutabilityPolicy -ImmutabilityPeriod {3} -ExtendPolicy", resourceGroupName, accountName, containerName, immutabilityPeriod2)), string.Format("Extend Container ImmutabilityPolicy should success.")); Test.Assert(CommandAgent.GetAzureRmStorageContainerImmutabilityPolicy(resourceGroupName, accountName, containerName), "Get Container ImmutabilityPolicy should success."); immuPolicy = GetImmutabilityPolicyFromOutput(); ValidateImmutabilityPolicy(immuPolicy, immutabilityPeriod2, "Locked"); //Add Container LegalHold string[] legalhold = new string[] { "legalholdtag1", "legalholdtag2" }; Test.Assert(CommandAgent.AddAzureRmStorageContainerLegalHold(resourceGroupName, accountName, containerName, legalhold), "Add Container LegalHold should success."); PSLegalHold outputLegalhold = GetLegalholdFromOutput(); CompareLegalhold(legalhold, outputLegalhold); //get Container and validate legalhold, ImmutabilityPolicy con = GetContainerFromServer(containerName); ValidateContainer(con, resourceGroupName, accountName, containerName, publicaccess: PSPublicAccess.None); Test.Assert(con.HasImmutabilityPolicy.Value, "Should has HasImmutabilityPolicy = true"); Test.Assert(con.HasLegalHold.Value, "Should has HasLegalHold = true"); Test.AssertEqual(immutabilityPeriod2, con.ImmutabilityPolicy.ImmutabilityPeriodSinceCreationInDays); Test.AssertEqual("Locked", con.ImmutabilityPolicy.State); Test.AssertEqual(3, con.ImmutabilityPolicy.UpdateHistory.Length); Test.AssertEqual("put", con.ImmutabilityPolicy.UpdateHistory[0].Update); Test.AssertEqual(immutabilityPeriod, con.ImmutabilityPolicy.UpdateHistory[0].ImmutabilityPeriodSinceCreationInDays.Value); Test.AssertEqual("lock", con.ImmutabilityPolicy.UpdateHistory[1].Update); Test.AssertEqual(immutabilityPeriod, con.ImmutabilityPolicy.UpdateHistory[1].ImmutabilityPeriodSinceCreationInDays.Value); Test.AssertEqual("extend", con.ImmutabilityPolicy.UpdateHistory[2].Update); Test.AssertEqual(immutabilityPeriod2, con.ImmutabilityPolicy.UpdateHistory[2].ImmutabilityPeriodSinceCreationInDays.Value); Test.AssertEqual(legalhold.Length, con.LegalHold.Tags.Length); if (legalhold.Length == con.LegalHold.Tags.Length) { for (int i = 0; i < legalhold.Length; i++) { Test.AssertEqual(legalhold[i], con.LegalHold.Tags[i].Tag); } } //Remove Legalhold and Container //Test.Assert(CommandAgent.RemoveAzureRmStorageContainerLegalHold(resourceGroupName, accountName, containerName, legalhold), "Remove Container LegalHold should success."); Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript( string.Format("Get-AzureRMStorageAccount -ResourceGroupName {0} -Name {1} | Remove-AzureRmStorageContainerLegalHold -Name {2} -Tag {3}", resourceGroupName, accountName, containerName, legalhold[0] + "," + legalhold[1])), string.Format("Remove Container LegalHold should success.")); //outputLegalhold = GetLegalholdFromOutput(); //CompareLegalhold(null, outputLegalhold); //Test.Assert(CommandAgent.RemoveAzureRmStorageContainer(resourceGroupName, accountName, containerName), "Remove Container should success."); Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript( string.Format("Get-AzureRmStorageContainer -ResourceGroupName {0} -StorageAccountName {1} -Name {2} | Remove-AzureRmStorageContainer -Force", resourceGroupName, accountName, containerName)), string.Format("Remove Container should success.")); } }