|
| 1 | +// ----------------------------------------------------------------------------- |
| 2 | +// <copyright file="DSCv3ResourceTestBase.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; |
| 10 | + using System.Collections.Generic; |
| 11 | + using System.IO; |
| 12 | + using System.Text.Json; |
| 13 | + using System.Text.Json.Serialization; |
| 14 | + using AppInstallerCLIE2ETests.Helpers; |
| 15 | + using NUnit.Framework; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Provides common functionality for DSC v3 resource tests. |
| 19 | + /// </summary> |
| 20 | + public class DSCv3ResourceTestBase |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// The string for the `get` function. |
| 24 | + /// </summary> |
| 25 | + public const string GetFunction = "get"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The string for the `test` function. |
| 29 | + /// </summary> |
| 30 | + public const string TestFunction = "test"; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The string for the `set` function. |
| 34 | + /// </summary> |
| 35 | + public const string SetFunction = "set"; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// The string for the `export` function. |
| 39 | + /// </summary> |
| 40 | + public const string ExportFunction = "export"; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// The string for the `_exist` property name. |
| 44 | + /// </summary> |
| 45 | + public const string ExistPropertyName = "_exist"; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// The string for the `_inDesiredState` property name. |
| 49 | + /// </summary> |
| 50 | + public const string InDesiredStatePropertyName = "_inDesiredState"; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Write the resource manifest out to the WindowsApps alias directory. |
| 54 | + /// </summary> |
| 55 | + /// <param name="resource">The resource manifest to write.</param> |
| 56 | + public static void EnsureTestResourcePresence(string resource) |
| 57 | + { |
| 58 | + string outputDirectory = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\WindowsApps"); |
| 59 | + Assert.IsNotEmpty(outputDirectory); |
| 60 | + |
| 61 | + var result = TestCommon.RunAICLICommand($"dscv3 {resource}", $"--manifest -o {outputDirectory}\\microsoft.winget.{resource}.dsc.resource.json"); |
| 62 | + Assert.AreEqual(0, result.ExitCode); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Runs a DSC v3 resource command. |
| 67 | + /// </summary> |
| 68 | + /// <param name="resource">The resource to target.</param> |
| 69 | + /// <param name="function">The resource function to run.</param> |
| 70 | + /// <param name="input">Input for the function; supports null, direct string, or JSON serialization of complex objects.</param> |
| 71 | + /// <param name="timeOut">The maximum time to wait in milliseconds.</param> |
| 72 | + /// <param name="throwOnTimeout">Whether to throw on a timeout or simply return the incomplete result.</param> |
| 73 | + /// <returns>A RunCommandResult containing the process exit code and output and error streams.</returns> |
| 74 | + protected static TestCommon.RunCommandResult RunDSCv3Command(string resource, string function, object input, int timeOut = 60000, bool throwOnTimeout = true) |
| 75 | + { |
| 76 | + return TestCommon.RunAICLICommand($"dscv3 {resource}", $"--{function}", ConvertToJSON(input), timeOut, throwOnTimeout); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Asserts that a RunCommandResult contains a success for a DSC v3 resource command run. |
| 81 | + /// </summary> |
| 82 | + /// <param name="result">The result of a DSC v3 resource command run.</param> |
| 83 | + protected static void AssertSuccessfulResourceRun(ref TestCommon.RunCommandResult result) |
| 84 | + { |
| 85 | + Assert.AreEqual(0, result.ExitCode); |
| 86 | + Assert.IsNotEmpty(result.StdOut); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Gets the output as lines. |
| 91 | + /// </summary> |
| 92 | + /// <param name="output">The output stream from a DSC v3 resource command.</param> |
| 93 | + /// <returns>The lines of the output.</returns> |
| 94 | + protected static string[] GetOutputLines(string output) |
| 95 | + { |
| 96 | + return output.TrimEnd().Split(Environment.NewLine); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Asserts the the output is a single line and deserializes that line as JSON. |
| 101 | + /// </summary> |
| 102 | + /// <typeparam name="T">The type to deserialize from JSON.</typeparam> |
| 103 | + /// <param name="output">The output stream from a DSC v3 resource command.</param> |
| 104 | + /// <returns>The object as deserialized.</returns> |
| 105 | + protected static T GetSingleOutputLineAs<T>(string output) |
| 106 | + { |
| 107 | + string[] lines = GetOutputLines(output); |
| 108 | + Assert.AreEqual(1, lines.Length); |
| 109 | + |
| 110 | + return JsonSerializer.Deserialize<T>(lines[0], GetDefaultJsonOptions()); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Asserts the the output is two lines and deserializes them as a JSON object and JSON string array. |
| 115 | + /// </summary> |
| 116 | + /// <typeparam name="T">The type to deserialize from JSON.</typeparam> |
| 117 | + /// <param name="output">The output stream from a DSC v3 resource command.</param> |
| 118 | + /// <returns>The object as deserialized and the contents of the string array.</returns> |
| 119 | + protected static (T, List<string>) GetSingleOutputLineAndDiffAs<T>(string output) |
| 120 | + { |
| 121 | + string[] lines = GetOutputLines(output); |
| 122 | + Assert.AreEqual(2, lines.Length); |
| 123 | + |
| 124 | + var options = GetDefaultJsonOptions(); |
| 125 | + return (JsonSerializer.Deserialize<T>(lines[0], options), JsonSerializer.Deserialize<List<string>>(lines[1], options)); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Deserializes all lines as JSON objects. |
| 130 | + /// </summary> |
| 131 | + /// <typeparam name="T">The type to deserialize from JSON.</typeparam> |
| 132 | + /// <param name="output">The output stream from a DSC v3 resource command.</param> |
| 133 | + /// <returns>A List of objects as deserialized.</returns> |
| 134 | + protected static List<T> GetOutputLinesAs<T>(string output) |
| 135 | + { |
| 136 | + List<T> result = new List<T>(); |
| 137 | + string[] lines = GetOutputLines(output); |
| 138 | + var options = GetDefaultJsonOptions(); |
| 139 | + |
| 140 | + foreach (string line in lines) |
| 141 | + { |
| 142 | + result.Add(JsonSerializer.Deserialize<T>(line, options)); |
| 143 | + } |
| 144 | + |
| 145 | + return result; |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Requires that the diff from a resource command contain the same set of strings as expected. |
| 150 | + /// </summary> |
| 151 | + /// <param name="diff">The diff from a resource command.</param> |
| 152 | + /// <param name="expected">The expected strings.</param> |
| 153 | + protected static void AssertDiffState(List<string> diff, IList<string> expected) |
| 154 | + { |
| 155 | + Assert.IsNotNull(diff); |
| 156 | + Assert.AreEqual(expected.Count, diff.Count); |
| 157 | + |
| 158 | + foreach (string item in expected) |
| 159 | + { |
| 160 | + Assert.Contains(item, diff); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static JsonSerializerOptions GetDefaultJsonOptions() |
| 165 | + { |
| 166 | + return new JsonSerializerOptions() |
| 167 | + { |
| 168 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 169 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 170 | + Converters = |
| 171 | + { |
| 172 | + new JsonStringEnumConverter(), |
| 173 | + }, |
| 174 | + }; |
| 175 | + } |
| 176 | + |
| 177 | + private static string ConvertToJSON(object value) => value switch |
| 178 | + { |
| 179 | + string s => s, |
| 180 | + null => null, |
| 181 | + _ => JsonSerializer.Serialize(value, GetDefaultJsonOptions()), |
| 182 | + }; |
| 183 | + } |
| 184 | +} |
0 commit comments