ImportCommand.cs (6614B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="ImportCommand.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace AppInstallerCLIE2ETests 8 { 9 using System.IO; 10 using AppInstallerCLIE2ETests.Helpers; 11 using NUnit.Framework; 12 13 /// <summary> 14 /// Import command tests. 15 /// </summary> 16 public class ImportCommand : BaseCommand 17 { 18 /// <summary> 19 /// Set up. 20 /// </summary> 21 [SetUp] 22 public void Setup() 23 { 24 this.CleanupTestExe(); 25 } 26 27 /// <summary> 28 /// Test import v1. 29 /// </summary> 30 [Test] 31 public void ImportSuccessful_1_0() 32 { 33 var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Good.1.0.json")); 34 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 35 Assert.True(this.VerifyTestExeInstalled()); 36 this.UninstallTestExe(); 37 } 38 39 /// <summary> 40 /// Test import v2. 41 /// </summary> 42 [Test] 43 public void ImportSuccessful_2_0() 44 { 45 var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Good.2.0.json")); 46 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 47 Assert.True(this.VerifyTestExeInstalled()); 48 this.UninstallTestExe(); 49 } 50 51 /// <summary> 52 /// Test import invalid file. 53 /// </summary> 54 [Test] 55 public void ImportInvalidFile() 56 { 57 // Verify failure when trying to import with an invalid file 58 var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-Invalid.json")); 59 Assert.AreEqual(Constants.ErrorCode.ERROR_JSON_INVALID_FILE, result.ExitCode); 60 Assert.True(result.StdOut.Contains("JSON file is not valid")); 61 } 62 63 /// <summary> 64 /// Test import from an unknown source. 65 /// </summary> 66 [Test] 67 public void ImportUnknownSource() 68 { 69 // Verify failure when trying to import from an unknown source 70 var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-UnknownSource.json")); 71 Assert.AreEqual(Constants.ErrorCode.ERROR_SOURCE_NAME_DOES_NOT_EXIST, result.ExitCode); 72 Assert.True(result.StdOut.Contains("Source required for import is not installed")); 73 } 74 75 /// <summary> 76 /// Test import for an unknown package. 77 /// </summary> 78 [Test] 79 public void ImportUnavailablePackage() 80 { 81 // Verify failure when trying to import an unavailable package 82 var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-UnknownPackage.json")); 83 Assert.AreEqual(Constants.ErrorCode.ERROR_NOT_ALL_QUERIES_FOUND_SINGLE, result.ExitCode); 84 Assert.True(result.StdOut.Contains("Package not found: MissingPackage")); 85 } 86 87 /// <summary> 88 /// Test import when the package version is not present. 89 /// </summary> 90 [Test] 91 public void ImportUnavailableVersion() 92 { 93 // Verify failure when trying to import an unavailable package 94 var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-UnknownPackageVersion.json")); 95 Assert.AreEqual(Constants.ErrorCode.ERROR_NOT_ALL_QUERIES_FOUND_SINGLE, result.ExitCode); 96 Assert.True(result.StdOut.Contains("Search failed for: AppInstallerTest.TestExeInstaller")); 97 } 98 99 /// <summary> 100 /// Test import when the package is already installed. 101 /// </summary> 102 [Test] 103 public void ImportAlreadyInstalled() 104 { 105 // Verify success with message when trying to import a package that is already installed 106 var installDir = TestCommon.GetRandomTestDir(); 107 TestCommon.RunAICLICommand("install", $"AppInstallerTest.TestExeInstaller -l {installDir}"); 108 var result = TestCommon.RunAICLICommand("import", $"{this.GetTestImportFile("ImportFile-Good.1.0.json")}"); 109 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 110 Assert.True(result.StdOut.Contains("Package is already installed")); 111 Assert.False(this.VerifyTestExeInstalled()); 112 this.UninstallTestExe(); 113 } 114 115 /// <summary> 116 /// Test Import with an exported file. 117 /// </summary> 118 [Test] 119 public void ImportExportedFile() 120 { 121 // Verify success when importing an exported list of packages. 122 // First install the test package to ensure it is exported. 123 TestCommon.RunAICLICommand("install", Constants.ExeInstallerPackageId); 124 125 var jsonFile = TestCommon.GetRandomTestFile(".json"); 126 TestCommon.RunAICLICommand("export", $"{jsonFile} -s TestSource"); 127 128 // Uninstall the package to ensure we can install it again 129 this.UninstallTestExe(); 130 131 // Import the file 132 var result = TestCommon.RunAICLICommand("import", jsonFile); 133 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 134 Assert.True(this.VerifyTestExeInstalled()); 135 this.UninstallTestExe(); 136 } 137 138 private string GetTestImportFile(string importFileName) 139 { 140 return TestCommon.GetTestDataFile(Path.Combine("ImportFiles", importFileName)); 141 } 142 143 private bool VerifyTestExeInstalled(string installDir = null) 144 { 145 if (string.IsNullOrEmpty(installDir)) 146 { 147 // Default location used by installer 148 installDir = Path.GetTempPath(); 149 } 150 151 return File.Exists(Path.Combine(installDir, Constants.TestExeInstalledFileName)); 152 } 153 154 private void UninstallTestExe() 155 { 156 TestCommon.RunAICLICommand("uninstall", Constants.ExeInstallerPackageId); 157 } 158 159 private void CleanupTestExe() 160 { 161 this.UninstallTestExe(); 162 File.Delete(Path.Combine(Path.GetTempPath(), Constants.TestExeInstalledFileName)); 163 File.Delete(Path.Combine(Path.GetTempPath(), Constants.TestExeUninstallerFileName)); 164 } 165 } 166 }