PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Storage/Commands.Storage.ScenarioTest/CLIBlobFunc.cs

https://gitlab.com/jslee1/azure-powershell
C# | 270 lines | 165 code | 38 blank | 67 comment | 10 complexity | 22497a4643242d83d115fa7007d1f8c2 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;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using Commands.Storage.ScenarioTest.Common;
  20. using Commands.Storage.ScenarioTest.Util;
  21. using Microsoft.VisualStudio.TestTools.UnitTesting;
  22. using Microsoft.WindowsAzure.Storage;
  23. using Microsoft.WindowsAzure.Storage.Blob;
  24. using MS.Test.Common.MsTestLib;
  25. using StorageTestLib;
  26. namespace Commands.Storage.ScenarioTest
  27. {
  28. /// <summary>
  29. /// this class contains all the functional test cases for PowerShell Blob cmdlets
  30. /// </summary>
  31. [TestClass]
  32. class CLIBlobFunc
  33. {
  34. private static CloudStorageAccount StorageAccount;
  35. private static CloudBlobHelper BlobHelper;
  36. private static string BlockFilePath;
  37. private static string PageFilePath;
  38. private TestContext testContextInstance;
  39. /// <summary>
  40. ///Gets or sets the test context which provides
  41. ///information about and functionality for the current test run.
  42. ///</summary>
  43. public TestContext TestContext
  44. {
  45. get
  46. {
  47. return testContextInstance;
  48. }
  49. set
  50. {
  51. testContextInstance = value;
  52. }
  53. }
  54. #region Additional test attributes
  55. //
  56. //You can use the following additional attributes as you write your tests:
  57. //
  58. //Use ClassInitialize to run code before running the first test in the class
  59. [ClassInitialize()]
  60. public static void MyClassInitialize(TestContext testContext)
  61. {
  62. Trace.WriteLine("ClassInit");
  63. Test.FullClassName = testContext.FullyQualifiedTestClassName;
  64. StorageAccount = TestBase.GetCloudStorageAccountFromConfig();
  65. //init the blob helper for blob related operations
  66. BlobHelper = new CloudBlobHelper(StorageAccount);
  67. // import module
  68. string moduleFilePath = Test.Data.Get("ModuleFilePath");
  69. if (moduleFilePath.Length > 0)
  70. PowerShellAgent.ImportModule(moduleFilePath);
  71. // $context = New-AzureStorageContext -ConnectionString ...
  72. PowerShellAgent.SetStorageContext(StorageAccount.ToString(true));
  73. BlockFilePath = Path.Combine(Test.Data.Get("TempDir"), FileUtil.GetSpecialFileName());
  74. PageFilePath = Path.Combine(Test.Data.Get("TempDir"), FileUtil.GetSpecialFileName());
  75. FileUtil.CreateDirIfNotExits(Path.GetDirectoryName(BlockFilePath));
  76. FileUtil.CreateDirIfNotExits(Path.GetDirectoryName(PageFilePath));
  77. // Generate block file and page file which are used for uploading
  78. Helper.GenerateMediumFile(BlockFilePath, 1);
  79. Helper.GenerateMediumFile(PageFilePath, 1);
  80. }
  81. //
  82. //Use ClassCleanup to run code after all tests in a class have run
  83. [ClassCleanup()]
  84. public static void MyClassCleanup()
  85. {
  86. Trace.WriteLine("ClasssCleanup");
  87. }
  88. //Use TestInitialize to run code before running each test
  89. [TestInitialize()]
  90. public void MyTestInitialize()
  91. {
  92. Trace.WriteLine("TestInit");
  93. Test.Start(TestContext.FullyQualifiedTestClassName, TestContext.TestName);
  94. }
  95. //Use TestCleanup to run code after each test has run
  96. [TestCleanup()]
  97. public void MyTestCleanup()
  98. {
  99. Trace.WriteLine("TestCleanup");
  100. // do not clean up the blobs here for investigation
  101. // every test case should do cleanup in its init
  102. Test.End(TestContext.FullyQualifiedTestClassName, TestContext.TestName);
  103. }
  104. #endregion
  105. [TestMethod]
  106. [TestCategory(Tag.Function)]
  107. public void RootBlobOperations()
  108. {
  109. string DownloadDirPath = Test.Data.Get("DownloadDir");
  110. RootBlobOperations(new PowerShellAgent(), BlockFilePath, DownloadDirPath, Microsoft.WindowsAzure.Storage.Blob.BlobType.BlockBlob);
  111. RootBlobOperations(new PowerShellAgent(), PageFilePath, DownloadDirPath, Microsoft.WindowsAzure.Storage.Blob.BlobType.PageBlob);
  112. }
  113. [TestMethod]
  114. [TestCategory(Tag.Function)]
  115. public void GetNonExistingBlob()
  116. {
  117. GetNonExistingBlob(new PowerShellAgent());
  118. }
  119. [TestMethod]
  120. [TestCategory(Tag.Function)]
  121. public void RemoveNonExistingBlob()
  122. {
  123. RemoveNonExistingBlob(new PowerShellAgent());
  124. }
  125. /// <summary>
  126. /// Functional Cases:
  127. /// 1. Upload a new blob file in the root container (Set-AzureStorageBlobContent Positive 2)
  128. /// 2. Get an existing blob in the root container (Get-AzureStorageBlob Positive 2)
  129. /// 3. Download an existing blob in the root container (Get-AzureStorageBlobContent Positive 2)
  130. /// 4. Remove an existing blob in the root container (Remove-AzureStorageBlob Positive 2)
  131. /// </summary>
  132. internal void RootBlobOperations(Agent agent, string UploadFilePath, string DownloadDirPath, Microsoft.WindowsAzure.Storage.Blob.BlobType Type)
  133. {
  134. const string ROOT_CONTAINER_NAME = "$root";
  135. string blobName = Path.GetFileName(UploadFilePath);
  136. string downloadFilePath = Path.Combine(DownloadDirPath, blobName);
  137. Collection<Dictionary<string, object>> comp = new Collection<Dictionary<string, object>>();
  138. Dictionary<string, object> dic = Utility.GenComparisonData(StorageObjectType.Blob, blobName);
  139. dic["BlobType"] = Type;
  140. comp.Add(dic);
  141. // create the container
  142. CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetRootContainerReference();
  143. container.CreateIfNotExists();
  144. //--------------Upload operation--------------
  145. Test.Assert(agent.SetAzureStorageBlobContent(UploadFilePath, ROOT_CONTAINER_NAME, Type), Utility.GenComparisonData("SendAzureStorageBlob", true));
  146. ICloudBlob blob = BlobHelper.QueryBlob(ROOT_CONTAINER_NAME, blobName);
  147. blob.FetchAttributes();
  148. // Verification for returned values
  149. CloudBlobUtil.PackBlobCompareData(blob, dic);
  150. agent.OutputValidation(comp);
  151. Test.Assert(blob.Exists(), "blob " + blobName + " should exist!");
  152. // validate the ContentType value for GetAzureStorageBlob operation
  153. if (Type == Microsoft.WindowsAzure.Storage.Blob.BlobType.BlockBlob)
  154. {
  155. dic["ContentType"] = "application/octet-stream";
  156. }
  157. //--------------Get operation--------------
  158. Test.Assert(agent.GetAzureStorageBlob(blobName, ROOT_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageBlob", true));
  159. // Verification for returned values
  160. agent.OutputValidation(comp);
  161. //--------------Download operation--------------
  162. downloadFilePath = Path.Combine(DownloadDirPath, blobName);
  163. Test.Assert(agent.GetAzureStorageBlobContent(blobName, downloadFilePath, ROOT_CONTAINER_NAME),
  164. Utility.GenComparisonData("GetAzureStorageBlobContent", true));
  165. // Verification for returned values
  166. agent.OutputValidation(comp);
  167. Test.Assert(Helper.CompareTwoFiles(downloadFilePath, UploadFilePath),
  168. String.Format("File '{0}' should be bit-wise identicial to '{1}'", downloadFilePath, UploadFilePath));
  169. //--------------Remove operation--------------
  170. Test.Assert(agent.RemoveAzureStorageBlob(blobName, ROOT_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageBlob", true));
  171. blob = BlobHelper.QueryBlob(ROOT_CONTAINER_NAME, blobName);
  172. Test.Assert(blob == null, "blob {0} should not exist!", blobName);
  173. }
  174. /// <summary>
  175. /// Negative Functional Cases : for Get-AzureStorageBlob
  176. /// 1. Get a non-existing blob (Negative 1)
  177. /// </summary>
  178. internal void GetNonExistingBlob(Agent agent)
  179. {
  180. string CONTAINER_NAME = Utility.GenNameString("upload-");
  181. // create the container
  182. CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetContainerReference(CONTAINER_NAME);
  183. container.CreateIfNotExists();
  184. try
  185. {
  186. string BLOB_NAME = Utility.GenNameString("nonexisting");
  187. // Delete the blob if it exists
  188. ICloudBlob blob = BlobHelper.QueryBlob(CONTAINER_NAME, BLOB_NAME);
  189. if (blob != null)
  190. blob.DeleteIfExists();
  191. //--------------Get operation--------------
  192. Test.Assert(!agent.GetAzureStorageBlob(BLOB_NAME, CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageBlob", false));
  193. // Verification for returned values
  194. Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
  195. Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Can not find blob '{0}' in container '{1}'.", BLOB_NAME, CONTAINER_NAME)), agent.ErrorMessages[0]);
  196. }
  197. finally
  198. {
  199. // cleanup
  200. container.DeleteIfExists();
  201. }
  202. }
  203. /// <summary>
  204. /// Negative Functional Cases : for Remove-AzureStorageBlob
  205. /// 1. Remove a non-existing blob (Negative 2)
  206. /// </summary>
  207. internal void RemoveNonExistingBlob(Agent agent)
  208. {
  209. string CONTAINER_NAME = Utility.GenNameString("upload-");
  210. string BLOB_NAME = Utility.GenNameString("nonexisting");
  211. // create the container
  212. CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetContainerReference(CONTAINER_NAME);
  213. container.CreateIfNotExists();
  214. try
  215. {
  216. // Delete the blob if it exists
  217. ICloudBlob blob = BlobHelper.QueryBlob(CONTAINER_NAME, BLOB_NAME);
  218. if (blob != null)
  219. blob.DeleteIfExists();
  220. //--------------Remove operation--------------
  221. Test.Assert(!agent.RemoveAzureStorageBlob(BLOB_NAME, CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageBlob", false));
  222. // Verification for returned values
  223. Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
  224. Test.Assert(agent.ErrorMessages[0].Equals(String.Format("Can not find blob '{0}' in container '{1}'.", BLOB_NAME, CONTAINER_NAME)), agent.ErrorMessages[0]);
  225. }
  226. finally
  227. {
  228. container.DeleteIfExists();
  229. }
  230. }
  231. }
  232. }