PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Storage/Commands.Storage.ScenarioTest/BVT/HTTPS/AnonymousBVT.cs

https://gitlab.com/jslee1/azure-powershell
C# | 256 lines | 183 code | 28 blank | 45 comment | 4 complexity | 9e6d0c0f1be7bbb12296f02e6027baa3 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.IO;
  17. using Commands.Storage.ScenarioTest.Common;
  18. using Commands.Storage.ScenarioTest.Util;
  19. using Microsoft.VisualStudio.TestTools.UnitTesting;
  20. using MS.Test.Common.MsTestLib;
  21. using StorageTestLib;
  22. using StorageBlob = Microsoft.WindowsAzure.Storage.Blob;
  23. namespace Commands.Storage.ScenarioTest.BVT.HTTPS
  24. {
  25. /// <summary>
  26. /// bvt cases for anonymous storage account
  27. /// </summary>
  28. [TestClass]
  29. class AnonymousBVT : TestBase
  30. {
  31. protected static string downloadDirRoot;
  32. private static string ContainerPrefix = "anonymousbvt";
  33. protected static string StorageAccountName;
  34. protected static string StorageEndPoint;
  35. protected static bool useHttps;
  36. [ClassInitialize()]
  37. public static void AnonymousBVTClassInitialize(TestContext testContext)
  38. {
  39. TestBase.TestClassInitialize(testContext);
  40. CLICommonBVT.SaveAndCleanSubScriptionAndEnvConnectionString();
  41. StorageAccountName = Test.Data.Get("StorageAccountName");
  42. StorageEndPoint = Test.Data.Get("StorageEndPoint").Trim();
  43. useHttps = true;
  44. PowerShellAgent.SetAnonymousStorageContext(StorageAccountName, useHttps, StorageEndPoint);
  45. downloadDirRoot = Test.Data.Get("DownloadDir");
  46. SetupDownloadDir();
  47. }
  48. [ClassCleanup()]
  49. public static void AnonymousBVTClassCleanup()
  50. {
  51. FileUtil.CleanDirectory(downloadDirRoot);
  52. CLICommonBVT.RestoreSubScriptionAndEnvConnectionString();
  53. TestBase.TestClassCleanup();
  54. }
  55. /// <summary>
  56. /// create download dir
  57. /// </summary>
  58. //TODO remove code redundancy
  59. protected static void SetupDownloadDir()
  60. {
  61. if (!Directory.Exists(downloadDirRoot))
  62. {
  63. Directory.CreateDirectory(downloadDirRoot);
  64. }
  65. FileUtil.CleanDirectory(downloadDirRoot);
  66. }
  67. [TestMethod]
  68. [TestCategory(Tag.BVT)]
  69. public void ListContainerWithContianerPermission()
  70. {
  71. string containerName = Utility.GenNameString(ContainerPrefix);
  72. StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Container);
  73. try
  74. {
  75. Test.Assert(agent.GetAzureStorageContainer(containerName), Utility.GenComparisonData("GetAzureStorageContainer", true));
  76. Dictionary<string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, containerName);
  77. Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>> { dic };
  78. //remove the permssion information for anonymous storage account
  79. CloudBlobUtil.PackContainerCompareData(container, dic);
  80. dic["PublicAccess"] = null;
  81. dic["Permission"] = null;
  82. // Verification for returned values
  83. agent.OutputValidation(comp);
  84. //check the http or https usage
  85. StorageBlob.CloudBlobContainer retrievedContainer = (StorageBlob.CloudBlobContainer)agent.Output[0]["CloudBlobContainer"]; ;
  86. string uri = retrievedContainer.Uri.ToString();
  87. string uriPrefix = string.Empty;
  88. if (useHttps)
  89. {
  90. uriPrefix = "https";
  91. }
  92. else
  93. {
  94. uriPrefix = "http";
  95. }
  96. Test.Assert(uri.ToString().StartsWith(uriPrefix), string.Format("The prefix of container uri should be {0}, actually it's {1}", uriPrefix, uri));
  97. }
  98. finally
  99. {
  100. blobUtil.RemoveContainer(containerName);
  101. }
  102. }
  103. /// <summary>
  104. /// list blobs when container's public access level is public
  105. /// </summary>
  106. [TestMethod]
  107. [TestCategory(Tag.BVT)]
  108. public void ListBlobsWithBlobPermission()
  109. {
  110. string containerName = Utility.GenNameString(ContainerPrefix);
  111. StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Blob);
  112. try
  113. {
  114. string pageBlobName = Utility.GenNameString("pageblob");
  115. string blockBlobName = Utility.GenNameString("blockblob");
  116. StorageBlob.ICloudBlob blockBlob = blobUtil.CreateBlockBlob(container, blockBlobName);
  117. StorageBlob.ICloudBlob pageBlob = blobUtil.CreatePageBlob(container, pageBlobName);
  118. Test.Assert(agent.GetAzureStorageBlob(blockBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
  119. agent.OutputValidation(new List<StorageBlob.ICloudBlob> { blockBlob });
  120. Test.Assert(agent.GetAzureStorageBlob(pageBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
  121. agent.OutputValidation(new List<StorageBlob.ICloudBlob> { pageBlob });
  122. }
  123. finally
  124. {
  125. blobUtil.RemoveContainer(containerName);
  126. }
  127. }
  128. /// <summary>
  129. /// download blob when container's public access level is container
  130. /// </summary>
  131. [TestMethod]
  132. [TestCategory(Tag.BVT)]
  133. public void GetBlobContentWithContainerPermission()
  134. {
  135. string containerName = Utility.GenNameString(ContainerPrefix);
  136. StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Container);
  137. try
  138. {
  139. DownloadBlobFromContainerTest(container);
  140. }
  141. finally
  142. {
  143. blobUtil.RemoveContainer(containerName);
  144. }
  145. }
  146. /// <summary>
  147. /// download blob when container's public access level is blob
  148. /// </summary>
  149. [TestMethod]
  150. [TestCategory(Tag.BVT)]
  151. public void GetBlobContentWithBlobPermission()
  152. {
  153. string containerName = Utility.GenNameString(ContainerPrefix);
  154. StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Blob);
  155. try
  156. {
  157. DownloadBlobFromContainerTest(container);
  158. }
  159. finally
  160. {
  161. blobUtil.RemoveContainer(containerName);
  162. }
  163. }
  164. /// <summary>
  165. /// download test in specified container
  166. /// </summary>
  167. /// <param name="container">CloudBlobContainer object</param>
  168. private void DownloadBlobFromContainerTest(StorageBlob.CloudBlobContainer container)
  169. {
  170. DownloadBlobFromContainer(container, StorageBlob.BlobType.BlockBlob);
  171. DownloadBlobFromContainer(container, StorageBlob.BlobType.PageBlob);
  172. }
  173. /// <summary>
  174. /// download specified blob
  175. /// </summary>
  176. /// <param name="container"></param>
  177. /// <param name="blob"></param>
  178. private void DownloadBlobFromContainer(StorageBlob.CloudBlobContainer container, StorageBlob.BlobType type)
  179. {
  180. string blobName = Utility.GenNameString("blob");
  181. StorageBlob.ICloudBlob blob = blobUtil.CreateBlob(container, blobName, type);
  182. string filePath = Path.Combine(downloadDirRoot, blob.Name);
  183. Test.Assert(agent.GetAzureStorageBlobContent(blob.Name, filePath, container.Name, true), "download blob should be successful");
  184. string localMd5 = Helper.GetFileContentMD5(filePath);
  185. Test.Assert(localMd5 == blob.Properties.ContentMD5, string.Format("local content md5 should be {0}, and actualy it's {1}", blob.Properties.ContentMD5, localMd5));
  186. agent.OutputValidation(new List<StorageBlob.ICloudBlob> { blob });
  187. }
  188. [TestMethod()]
  189. [TestCategory(Tag.BVT)]
  190. public void MakeSureBvtUsingAnonymousContext()
  191. {
  192. //TODO EnvKey is not empty since we called SaveAndCleanSubScriptionAndEnvConnectionString when initializing
  193. string key = System.Environment.GetEnvironmentVariable(CLICommonBVT.EnvKey);
  194. Test.Assert(string.IsNullOrEmpty(key), string.Format("env connection string {0} should be null or empty", key));
  195. Test.Assert(PowerShellAgent.Context != null, "PowerShell context should be not null when running bvt against Anonymous storage account");
  196. }
  197. /// <summary>
  198. /// Anonymous storage context should work with specified end point
  199. /// </summary>
  200. [TestMethod()]
  201. [TestCategory(Tag.BVT)]
  202. public void AnonymousContextWithEndPoint()
  203. {
  204. string containerName = Utility.GenNameString(ContainerPrefix);
  205. StorageBlob.CloudBlobContainer container = blobUtil.CreateContainer(containerName, StorageBlob.BlobContainerPublicAccessType.Blob);
  206. try
  207. {
  208. string pageBlobName = Utility.GenNameString("pageblob");
  209. string blockBlobName = Utility.GenNameString("blockblob");
  210. StorageBlob.ICloudBlob blockBlob = blobUtil.CreateBlockBlob(container, blockBlobName);
  211. StorageBlob.ICloudBlob pageBlob = blobUtil.CreatePageBlob(container, pageBlobName);
  212. agent.UseContextParam = false;
  213. string cmd = string.Format("new-azurestoragecontext -StorageAccountName {0} " +
  214. "-Anonymous -EndPoint {1}", StorageAccountName, StorageEndPoint);
  215. ((PowerShellAgent)agent).AddPipelineScript(cmd);
  216. Test.Assert(agent.GetAzureStorageBlob(blockBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
  217. agent.OutputValidation(new List<StorageBlob.ICloudBlob> { blockBlob });
  218. ((PowerShellAgent)agent).AddPipelineScript(cmd);
  219. Test.Assert(agent.GetAzureStorageBlob(pageBlobName, containerName), Utility.GenComparisonData("Get-AzureStorageBlob", true));
  220. agent.OutputValidation(new List<StorageBlob.ICloudBlob> { pageBlob });
  221. }
  222. finally
  223. {
  224. blobUtil.RemoveContainer(containerName);
  225. }
  226. }
  227. }
  228. }