PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/AddAzureVhdSASUriTest.cs

https://github.com/IrisClasson/azure-sdk-tools
C# | 664 lines | 555 code | 58 blank | 51 comment | 32 complexity | eca385369d8b0dc6682e9afdf77d0e1e MD5 | raw file
Possible License(s): Apache-2.0
  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. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests
  15. {
  16. using System;
  17. using System.IO;
  18. using System.Reflection;
  19. using Microsoft.VisualStudio.TestTools.UnitTesting;
  20. using Microsoft.WindowsAzure.Storage.Auth;
  21. using Microsoft.WindowsAzure.Storage.Blob;
  22. [TestClass]
  23. public class AddAzureVhdSASUriTest : AzureVhdTest
  24. {
  25. [TestInitialize]
  26. public void Initialize()
  27. {
  28. SetTestSettings();
  29. if (defaultAzureSubscription.Equals(null))
  30. {
  31. Assert.Inconclusive("No Subscription is selected!");
  32. }
  33. pass = false;
  34. testStartTime = DateTime.Now;
  35. storageAccountKey = vmPowershellCmdlets.GetAzureStorageAccountKey(defaultAzureSubscription.CurrentStorageAccountName);
  36. try
  37. {
  38. vmPowershellCmdlets.RunPSScript("Get-AzureStorageContainer -Name " + vhdContainerName);
  39. }
  40. catch
  41. {
  42. // Create a container.
  43. vmPowershellCmdlets.RunPSScript("New-AzureStorageContainer -Name " + vhdContainerName);
  44. }
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  50. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\upload_VHD.csv", "upload_VHD#csv", DataAccessMethod.Sequential)]
  51. public void UploadDiskSasUri()
  52. {
  53. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  54. // Choose the vhd file from local machine
  55. var vhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  56. var vhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + vhdName);
  57. Assert.IsTrue(File.Exists(vhdLocalPath.FullName), "VHD file not exist={0}", vhdLocalPath);
  58. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  59. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  60. int i = 0;
  61. while (i < 16)
  62. {
  63. string destinationSasUri2 = CreateSasUriWithPermission(vhdName, i);
  64. try
  65. {
  66. Console.WriteLine("uploads {0} to {1}", vhdName, destinationSasUri2);
  67. vmPowershellCmdlets.RemoveAzureSubscriptions();
  68. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2);
  69. ReImportSubscription();
  70. Console.WriteLine("Finished uploading: {0}", destinationSasUri2);
  71. // Verify the upload.
  72. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, vhdLocalPath, vhdUploadContext, md5hash);
  73. Console.WriteLine("Test success with permission: {0}", i);
  74. i++;
  75. }
  76. catch (Exception e)
  77. {
  78. if (e.ToString().Contains("already running"))
  79. {
  80. Console.WriteLine(e.ToString());
  81. continue;
  82. }
  83. if (i != 3 && i != 7 && i != 11 && i != 15)
  84. {
  85. Console.WriteLine("Error as expected. Permission: {0}", i);
  86. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  87. i++;
  88. continue;
  89. }
  90. else
  91. {
  92. Assert.Fail("Test failed Permission: {0} \n {1}", i, e.ToString());
  93. }
  94. }
  95. }
  96. DateTime testEndTime = DateTime.Now;
  97. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  98. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  99. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0} {1},{2}", testName, vhdName, (testEndTime - testStartTime).TotalSeconds) });
  100. pass = true;
  101. }
  102. private string CreateSasUriWithPermission(string vhdName, int p)
  103. {
  104. // Set the destination
  105. string vhdBlobName = string.Format("{0}/{1}.vhd", vhdContainerName, Utilities.GetUniqueShortName(Path.GetFileNameWithoutExtension(vhdName)));
  106. string httpsBlobUrlRoot = string.Format("https:{0}", blobUrlRoot.Substring(blobUrlRoot.IndexOf('/')));
  107. string vhdDestUri = httpsBlobUrlRoot + vhdBlobName;
  108. var destinationBlob2 = new CloudPageBlob(new Uri(vhdDestUri), new StorageCredentials(storageAccountKey.StorageAccountName, storageAccountKey.Primary));
  109. var policy2 = new SharedAccessBlobPolicy()
  110. {
  111. Permissions = (SharedAccessBlobPermissions)p,
  112. SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromHours(1)
  113. };
  114. var destinationBlobToken2 = destinationBlob2.GetSharedAccessSignature(policy2);
  115. vhdDestUri += destinationBlobToken2;
  116. return vhdDestUri;
  117. }
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  122. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\overwrite_VHD.csv", "overwrite_VHD#csv", DataAccessMethod.Sequential)]
  123. public void UploadDiskOverwriteSasUri()
  124. {
  125. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  126. // Choose the vhd file from local machine
  127. var vhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  128. var vhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + vhdName);
  129. Assert.IsTrue(File.Exists(vhdLocalPath.FullName), "VHD file not exist={0}", vhdLocalPath);
  130. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  131. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  132. int i = 0;
  133. while (i < 16)
  134. {
  135. string destinationSasUri2 = CreateSasUriWithPermission(vhdName, i);
  136. try
  137. {
  138. Console.WriteLine("uploads {0} to {1}", vhdName, destinationSasUri2);
  139. vmPowershellCmdlets.RemoveAzureSubscriptions();
  140. vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2);
  141. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2, true);
  142. ReImportSubscription();
  143. Console.WriteLine("Finished uploading: {0}", destinationSasUri2);
  144. // Verify the upload.
  145. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, vhdLocalPath, vhdUploadContext, md5hash);
  146. Console.WriteLine("Test success with permission: {0}", i);
  147. i++;
  148. }
  149. catch (Exception e)
  150. {
  151. if (e.ToString().Contains("already running"))
  152. {
  153. Console.WriteLine(e.ToString());
  154. continue;
  155. }
  156. if (i != 7 && i != 15)
  157. {
  158. Console.WriteLine("Error as expected. Permission: {0}", i);
  159. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  160. i++;
  161. continue;
  162. }
  163. else
  164. {
  165. Assert.Fail("Test failed Permission: {0} \n {1}", i, e.ToString());
  166. }
  167. }
  168. }
  169. DateTime testEndTime = DateTime.Now;
  170. pass = true;
  171. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  172. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  173. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  179. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\overwrite_VHD.csv", "overwrite_VHD#csv", DataAccessMethod.Sequential)]
  180. public void UploadDiskOverwriteNonExistSasUri()
  181. {
  182. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  183. // Choose the vhd file from local machine
  184. var vhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  185. var vhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + vhdName);
  186. Assert.IsTrue(File.Exists(vhdLocalPath.FullName), "VHD file not exist={0}", vhdLocalPath);
  187. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  188. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  189. int i = 0;
  190. while (i < 16)
  191. {
  192. string destinationSasUri2 = CreateSasUriWithPermission(vhdName, i);
  193. try
  194. {
  195. Console.WriteLine("uploads {0} to {1}", vhdName, destinationSasUri2);
  196. vmPowershellCmdlets.RemoveAzureSubscriptions();
  197. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2, true);
  198. ReImportSubscription();
  199. Console.WriteLine("Finished uploading: {0}", destinationSasUri2);
  200. // Verify the upload.
  201. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, vhdLocalPath, vhdUploadContext, md5hash);
  202. Console.WriteLine("Test success with permission: {0}", i);
  203. i++;
  204. }
  205. catch (Exception e)
  206. {
  207. if (e.ToString().Contains("already running"))
  208. {
  209. Console.WriteLine(e.ToString());
  210. continue;
  211. }
  212. if (i != 7 && i != 15)
  213. {
  214. Console.WriteLine("Error as expected. Permission: {0}", i);
  215. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  216. i++;
  217. continue;
  218. }
  219. else
  220. {
  221. Assert.Fail("Test failed Permission: {0} \n {1}", i, e.ToString());
  222. }
  223. }
  224. }
  225. DateTime testEndTime = DateTime.Now;
  226. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  227. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  228. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  229. pass = true;
  230. }
  231. /// <summary>
  232. ///
  233. /// </summary>
  234. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  235. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\overwrite_VHD.csv", "overwrite_VHD#csv", DataAccessMethod.Sequential)]
  236. public void UploadDiskSecondWithoutOverwriteSasUri()
  237. {
  238. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  239. // Choose the vhd file from local machine
  240. var vhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  241. var vhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + vhdName);
  242. Assert.IsTrue(File.Exists(vhdLocalPath.FullName), "VHD file not exist={0}", vhdLocalPath);
  243. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  244. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  245. for (int i = 0; i < 16; i++)
  246. {
  247. string destinationSasUri2 = CreateSasUriWithPermission(vhdName, i);
  248. try
  249. {
  250. Console.WriteLine("uploads {0} to {1}", vhdName, destinationSasUri2);
  251. vmPowershellCmdlets.RemoveAzureSubscriptions();
  252. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2);
  253. try
  254. {
  255. Console.WriteLine("uploads {0} to {1} second times", vhdName, destinationSasUri2);
  256. vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2);
  257. pass = false;
  258. }
  259. catch (Exception e)
  260. {
  261. Console.WriteLine("Failed as expected while uploading {0} second time without overwrite: {1}", vhdLocalPath.Name, e.InnerException.Message);
  262. }
  263. // Verify the upload.
  264. ReImportSubscription();
  265. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, vhdLocalPath, vhdUploadContext, md5hash);
  266. Console.WriteLine("Test success with permission: {0}", i);
  267. }
  268. catch (Exception e)
  269. {
  270. if (i != 3 && i != 7 && i != 11 && i != 15)
  271. {
  272. Console.WriteLine("Error as expected. Permission: {0}", i);
  273. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  274. continue;
  275. }
  276. else
  277. {
  278. Assert.Fail("Test failed. Permission: {0}", i);
  279. }
  280. }
  281. }
  282. pass = true;
  283. DateTime testEndTime = DateTime.Now;
  284. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  285. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  286. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  287. }
  288. /// <summary>
  289. ///
  290. /// </summary>
  291. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  292. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\thread_VHD.csv", "thread_VHD#csv", DataAccessMethod.Sequential)]
  293. public void UploadDiskThreadNumberSasUri()
  294. {
  295. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  296. // Choose the vhd file from local machine
  297. var vhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  298. var vhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + vhdName);
  299. Assert.IsTrue(File.Exists(vhdLocalPath.FullName), "VHD file not exist={0}", vhdLocalPath);
  300. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  301. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  302. int i = 0;
  303. while (i < 16)
  304. {
  305. string destinationSasUri2 = CreateSasUriWithPermission(vhdName, i);
  306. try
  307. {
  308. Console.WriteLine("uploads {0} to {1}", vhdName, destinationSasUri2);
  309. vmPowershellCmdlets.RemoveAzureSubscriptions();
  310. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2, 16);
  311. ReImportSubscription();
  312. Console.WriteLine("uploading completed: {0}", vhdName);
  313. // Verify the upload.
  314. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, vhdLocalPath, vhdUploadContext, md5hash);
  315. Console.WriteLine("Test success with permission: {0}", i);
  316. i++;
  317. }
  318. catch (Exception e)
  319. {
  320. if (e.ToString().Contains("already running"))
  321. {
  322. Console.WriteLine(e.ToString());
  323. continue;
  324. }
  325. if (i != 3 && i != 7 && i != 11 && i != 15)
  326. {
  327. Console.WriteLine("Error as expected. Permission: {0}", i);
  328. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  329. i++;
  330. continue;
  331. }
  332. else
  333. {
  334. Assert.Fail("Test failed Permission: {0} \n {1}", i, e.ToString());
  335. }
  336. }
  337. }
  338. DateTime testEndTime = DateTime.Now;
  339. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  340. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  341. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  342. pass = true;
  343. }
  344. /// <summary>
  345. ///
  346. /// </summary>
  347. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  348. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\thread_VHD.csv", "thread_VHD#csv", DataAccessMethod.Sequential)]
  349. public void UploadDiskThreadNumberOverwriteSasUri()
  350. {
  351. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  352. // Choose the vhd file from local machine
  353. var vhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  354. var vhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + vhdName);
  355. Assert.IsTrue(File.Exists(vhdLocalPath.FullName), "VHD file not exist={0}", vhdLocalPath);
  356. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  357. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  358. for (int i = 0; i < 16; i++)
  359. {
  360. string destinationSasUri2 = CreateSasUriWithPermission(vhdName, i);
  361. try
  362. {
  363. Console.WriteLine("uploads {0} to {1}", vhdName, destinationSasUri2);
  364. vmPowershellCmdlets.RemoveAzureSubscriptions();
  365. vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2);
  366. Console.WriteLine("uploaded: {0}", vhdName);
  367. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(vhdLocalPath, destinationSasUri2, 16, true);
  368. Console.WriteLine("uploading overwrite completed: {0}", vhdName);
  369. // Verify the upload.
  370. ReImportSubscription();
  371. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, vhdLocalPath, vhdUploadContext, md5hash);
  372. Console.WriteLine("Test success with permission: {0}", i);
  373. }
  374. catch (Exception e)
  375. {
  376. if (i != 7 && i != 15)
  377. {
  378. Console.WriteLine("Error as expected. Permission: {0}", i);
  379. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  380. continue;
  381. }
  382. else
  383. {
  384. Assert.Fail("Test failed Permission: {0} \n {1}", i, e.ToString());
  385. }
  386. }
  387. }
  388. pass = true;
  389. DateTime testEndTime = DateTime.Now;
  390. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  391. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  392. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  393. }
  394. /// <summary>
  395. ///
  396. /// </summary>
  397. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  398. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\patch_VHD.csv", "patch_VHD#csv", DataAccessMethod.Sequential)]
  399. public void PatchFirstLevelDifferencingDiskSasUri()
  400. {
  401. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  402. // Choose the base vhd file from local machine
  403. var baseVhdName = Convert.ToString(TestContext.DataRow["baseImage"]);
  404. var baseVhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + baseVhdName);
  405. Assert.IsTrue(File.Exists(baseVhdLocalPath.FullName), "VHD file not exist={0}", baseVhdLocalPath);
  406. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  407. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  408. string md5hashBase = Convert.ToString(TestContext.DataRow["MD5hashBase"]);
  409. // Choose the child vhd file from the local machine
  410. var childVhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  411. var childVhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + childVhdName);
  412. Assert.IsTrue(File.Exists(childVhdLocalPath.FullName), "VHD file not exist={0}", childVhdLocalPath);
  413. for (int i = 0; i < 16; i++)
  414. {
  415. string destinationSasUri2 = CreateSasUriWithPermission(baseVhdName, i);
  416. string destinationSasUri3 = CreateSasUriWithPermission(childVhdName, i);
  417. try
  418. {
  419. Console.WriteLine("uploads {0} to {1}", baseVhdName, destinationSasUri2);
  420. vmPowershellCmdlets.RemoveAzureSubscriptions();
  421. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(baseVhdLocalPath, destinationSasUri2, true);
  422. Console.WriteLine("uploading completed: {0}", baseVhdName);
  423. // Verify the upload.
  424. ReImportSubscription();
  425. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri2, baseVhdLocalPath, vhdUploadContext, md5hashBase, false);
  426. Console.WriteLine("uploads {0} to {1}", childVhdName, destinationSasUri3);
  427. vmPowershellCmdlets.RemoveAzureSubscriptions();
  428. var patchVhdUploadContext = vmPowershellCmdlets.AddAzureVhd(childVhdLocalPath, destinationSasUri3, destinationSasUri2);
  429. Console.WriteLine("uploading completed: {0}", childVhdName);
  430. // Verify the upload.
  431. ReImportSubscription();
  432. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUri3, childVhdLocalPath, patchVhdUploadContext, md5hash);
  433. Console.WriteLine("Test success with permission: {0}", i);
  434. }
  435. catch (Exception e)
  436. {
  437. Console.WriteLine("Error as expected. Permission: {0}", i);
  438. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  439. continue;
  440. }
  441. }
  442. pass = true;
  443. DateTime testEndTime = DateTime.Now;
  444. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  445. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  446. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  447. }
  448. /// <summary>
  449. /// This test is ignored until patching scenario is available for SAS Uri.
  450. /// </summary>
  451. [TestMethod(), TestCategory("Functional"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  452. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\patch_VHD.csv", "patch_VHD#csv", DataAccessMethod.Sequential)]
  453. [Ignore]
  454. public void PatchSasUriNormalBaseShouldFail()
  455. {
  456. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  457. // Choose the base vhd file from local machine
  458. var baseVhdName = Convert.ToString(TestContext.DataRow["baseImage"]);
  459. var baseVhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + baseVhdName);
  460. Assert.IsTrue(File.Exists(baseVhdLocalPath.FullName), "VHD file not exist={0}", baseVhdLocalPath);
  461. // Set the destination
  462. string vhdBlobName = string.Format("{0}/{1}.vhd", vhdContainerName, Utilities.GetUniqueShortName(Path.GetFileNameWithoutExtension(baseVhdName)));
  463. string vhdDestUri = blobUrlRoot + vhdBlobName;
  464. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  465. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  466. string md5hashBase = Convert.ToString(TestContext.DataRow["MD5hashBase"]);
  467. Console.WriteLine("uploads {0} to {1}", baseVhdName, vhdDestUri);
  468. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(baseVhdLocalPath, vhdDestUri, true);
  469. Console.WriteLine("uploading the parent vhd completed: {0}", baseVhdName);
  470. // Choose the child vhd file from the local machine
  471. var childVhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  472. var childVhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + childVhdName);
  473. Assert.IsTrue(File.Exists(childVhdLocalPath.FullName), "VHD file not exist={0}", childVhdLocalPath);
  474. for (int i = 0; i < 16; i++)
  475. {
  476. string destinationSasUriParent = CreateSasUriWithPermission(baseVhdName, i);
  477. string destinationSasUriChild = CreateSasUriWithPermission(childVhdName, i);
  478. try
  479. {
  480. Console.WriteLine("uploads {0} to {1} with patching from {2}", childVhdName, destinationSasUriChild, vhdDestUri);
  481. var patchVhdUploadContext = vmPowershellCmdlets.AddAzureVhd(childVhdLocalPath, destinationSasUriChild, vhdDestUri);
  482. Console.WriteLine("uploading the child vhd completed: {0}", childVhdName);
  483. // Verify the upload.
  484. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUriChild, childVhdLocalPath, patchVhdUploadContext, md5hash);
  485. Console.WriteLine("Test success with permission: {0}", i);
  486. }
  487. catch (Exception e)
  488. {
  489. Console.WriteLine("Error as expected. Permission: {0}", i);
  490. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  491. continue;
  492. }
  493. }
  494. DateTime testEndTime = DateTime.Now;
  495. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  496. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  497. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  498. }
  499. /// <summary>
  500. ///
  501. /// </summary>
  502. [TestMethod(), TestCategory("Sequential"), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet (Add-AzureVhd)")]
  503. [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\patch_VHD.csv", "patch_VHD#csv", DataAccessMethod.Sequential)]
  504. public void PatchNormalSasUriBase()
  505. {
  506. StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);
  507. // Choose the base vhd file from local machine
  508. var baseVhdName = Convert.ToString(TestContext.DataRow["baseImage"]);
  509. var baseVhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + baseVhdName);
  510. Assert.IsTrue(File.Exists(baseVhdLocalPath.FullName), "VHD file not exist={0}", baseVhdLocalPath);
  511. // Get the pre-calculated MD5 hash of the fixed vhd that was converted from the original vhd.
  512. string md5hash = Convert.ToString(TestContext.DataRow["MD5hash"]);
  513. string md5hashBase = Convert.ToString(TestContext.DataRow["MD5hashBase"]);
  514. // Choose the child vhd file from the local machine
  515. var childVhdName = Convert.ToString(TestContext.DataRow["vhdName"]);
  516. var childVhdLocalPath = new FileInfo(Directory.GetCurrentDirectory() + "\\" + childVhdName);
  517. Assert.IsTrue(File.Exists(childVhdLocalPath.FullName), "VHD file not exist={0}", childVhdLocalPath);
  518. int i = 0;
  519. while (i < 16)
  520. {
  521. string destinationSasUriParent = CreateSasUriWithPermission(baseVhdName, i); // the destination of the parent vhd is a Sas Uri
  522. // Set the destination of child vhd
  523. string vhdBlobName = string.Format("{0}/{1}.vhd", vhdContainerName, Utilities.GetUniqueShortName(Path.GetFileNameWithoutExtension(childVhdName)));
  524. string vhdDestUri = blobUrlRoot + vhdBlobName;
  525. try
  526. {
  527. // Upload the parent vhd using Sas Uri
  528. Console.WriteLine("uploads {0} to {1}", baseVhdName, destinationSasUriParent);
  529. vmPowershellCmdlets.RemoveAzureSubscriptions();
  530. var vhdUploadContext = vmPowershellCmdlets.AddAzureVhd(baseVhdLocalPath, destinationSasUriParent, true);
  531. Console.WriteLine("uploading completed: {0}", baseVhdName);
  532. // Verify the upload.
  533. ReImportSubscription();
  534. AssertUploadContextAndContentMD5UsingSaveVhd(destinationSasUriParent, baseVhdLocalPath, vhdUploadContext, md5hashBase, false);
  535. Console.WriteLine("uploads {0} to {1} with patching from {2}", childVhdName, vhdDestUri, destinationSasUriParent);
  536. var patchVhdUploadContext = vmPowershellCmdlets.AddAzureVhd(childVhdLocalPath, vhdDestUri, destinationSasUriParent);
  537. Console.WriteLine("uploading the child vhd completed: {0}", childVhdName);
  538. // Verify the upload.
  539. AssertUploadContextAndContentMD5UsingSaveVhd(vhdDestUri, childVhdLocalPath, patchVhdUploadContext, md5hash);
  540. Console.WriteLine("Test success with permission: {0}", i);
  541. i++;
  542. }
  543. catch (Exception e)
  544. {
  545. if (i != 3 && i != 7 && i != 11 && i != 15)
  546. {
  547. Console.WriteLine("Error as expected. Permission: {0}", i);
  548. Console.WriteLine("Error message: {0}", e.InnerException.Message);
  549. i++;
  550. continue;
  551. }
  552. else
  553. {
  554. Assert.Fail("Test failed Permission: {0} \n {1}", i, e.ToString());
  555. }
  556. }
  557. }
  558. pass = true;
  559. DateTime testEndTime = DateTime.Now;
  560. Console.WriteLine("{0} test passed at {1}.", testName, testEndTime);
  561. Console.WriteLine("Duration of the test pass: {0} seconds", (testEndTime - testStartTime).TotalSeconds);
  562. System.IO.File.AppendAllLines(perfFile, new string[] { String.Format("{0},{1}", testName, (testEndTime - testStartTime).TotalSeconds) });
  563. }
  564. [TestCleanup]
  565. public virtual void CleanUp()
  566. {
  567. Console.WriteLine("Test {0}", pass ? "passed" : "failed");
  568. ReImportSubscription();
  569. }
  570. }
  571. }