ConfigurationSetAuthoringTests.cs (10919B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="ConfigurationSetAuthoringTests.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Tests 8 { 9 using System; 10 using System.Collections.Generic; 11 using Microsoft.Management.Configuration.UnitTests.Fixtures; 12 using Microsoft.Management.Configuration.UnitTests.Helpers; 13 using Microsoft.VisualBasic; 14 using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces; 15 using Windows.Foundation.Collections; 16 using Windows.Storage.Streams; 17 using Xunit; 18 using Xunit.Abstractions; 19 20 /// <summary> 21 /// Unit tests for configuration set authoring (creating objects). 22 /// </summary> 23 [Collection("UnitTestCollection")] 24 [InProc] 25 [OutOfProc] 26 public class ConfigurationSetAuthoringTests : ConfigurationProcessorTestBase 27 { 28 /// <summary> 29 /// Initializes a new instance of the <see cref="ConfigurationSetAuthoringTests"/> class. 30 /// </summary> 31 /// <param name="fixture">Unit test fixture.</param> 32 /// <param name="log">Log helper.</param> 33 public ConfigurationSetAuthoringTests(UnitTestFixture fixture, ITestOutputHelper log) 34 : base(fixture, log) 35 { 36 } 37 38 /// <summary> 39 /// Creates a configuration set and sets all available properties. 40 /// </summary> 41 [Fact] 42 public void ConfigurationSetAndProperties() 43 { 44 string testName = "Test Name"; 45 string testOrigin = "Test Origin"; 46 string testPath = "TestPath.ext"; 47 48 ConfigurationSet testSet = this.ConfigurationSet(); 49 50 testSet.Name = testName; 51 Assert.Equal(testName, testSet.Name); 52 testSet.Origin = testOrigin; 53 Assert.Equal(testOrigin, testSet.Origin); 54 testSet.Path = testPath; 55 Assert.Equal(testPath, testSet.Path); 56 57 Assert.NotEqual(Guid.Empty, testSet.InstanceIdentifier); 58 Assert.Equal(ConfigurationSetState.Unknown, testSet.State); 59 60 Assert.Empty(testSet.Units); 61 testSet.Units = new ConfigurationUnit[] { this.ConfigurationUnit() }; 62 Assert.Equal(1, testSet.Units.Count); 63 64 Assert.NotEqual(string.Empty, testSet.SchemaVersion); 65 } 66 67 /// <summary> 68 /// Creates a configuration unit and sets all available properties. 69 /// </summary> 70 [Fact] 71 public void ConfigurationUnitAndProperties() 72 { 73 string testName = "Test Name"; 74 string testIdentifier = "Test Identifier"; 75 ConfigurationUnitIntent testIntent = ConfigurationUnitIntent.Assert; 76 77 ConfigurationUnit testUnit = this.ConfigurationUnit(); 78 testUnit.Type = testName; 79 Assert.Equal(testName, testUnit.Type); 80 testUnit.Identifier = testIdentifier; 81 Assert.Equal(testIdentifier, testUnit.Identifier); 82 83 Assert.NotEqual(Guid.Empty, testUnit.InstanceIdentifier); 84 85 Assert.Equal(ConfigurationUnitIntent.Apply, testUnit.Intent); 86 testUnit.Intent = testIntent; 87 Assert.Equal(testIntent, testUnit.Intent); 88 89 Assert.Empty(testUnit.Dependencies); 90 testUnit.Dependencies = new string[] { "dependency1", "dependency2" }; 91 Assert.Equal(2, testUnit.Dependencies.Count); 92 93 Assert.Empty(testUnit.Metadata); 94 Assert.Empty(testUnit.Settings); 95 Assert.Null(testUnit.Details); 96 97 Assert.Equal(ConfigurationUnitState.Unknown, testUnit.State); 98 99 Assert.Null(testUnit.ResultInformation); 100 101 Assert.True(testUnit.IsActive); 102 testUnit.IsActive = false; 103 Assert.False(testUnit.IsActive); 104 } 105 106 /// <summary> 107 /// Basic sanity check to verify that nested value sets can be serialized successfully. 108 /// </summary> 109 [Fact] 110 public void ConfigurationSetSerializeNestedValueSets() 111 { 112 ConfigurationSet testSet = this.ConfigurationSet(); 113 114 testSet.SchemaVersion = "0.2"; 115 ConfigurationUnit testUnit = this.ConfigurationUnit(); 116 string testName = "Test Name"; 117 string testIdentifier = "Test Identifier"; 118 testUnit.Type = testName; 119 testUnit.Identifier = testIdentifier; 120 121 ValueSet innerValueSet = new ValueSet(); 122 innerValueSet.Add("innerKey", "innerValue"); 123 124 ValueSet outerValueSet = new ValueSet(); 125 outerValueSet.Add("outerKey", innerValueSet); 126 testUnit.Metadata = outerValueSet; 127 testSet.Units.Add(testUnit); 128 129 InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); 130 testSet.Serialize(stream); 131 132 string yamlOutput = this.ReadStream(stream); 133 Assert.NotNull(yamlOutput); 134 } 135 136 /// <summary> 137 /// Test for unique unit environment calculation. 138 /// </summary> 139 [Fact] 140 public void ConfigurationSet_UnitEnvironments() 141 { 142 ConfigurationSet testSet = this.ConfigurationSet(); 143 144 Dictionary<string, string> firstProperty = new Dictionary<string, string>(); 145 firstProperty.Add("property", "value1"); 146 147 Dictionary<string, string> secondProperty = new Dictionary<string, string>(); 148 secondProperty.Add("property", "value2"); 149 150 Helpers.ConfigurationEnvironmentData[] environments = new Helpers.ConfigurationEnvironmentData[] 151 { 152 new () { ProcessorIdentifier = "dscv3" }, 153 new () { ProcessorIdentifier = "pwsh" }, 154 new () { ProcessorIdentifier = "dscv3", Context = SecurityContext.Elevated }, 155 new () { ProcessorIdentifier = "pwsh", Context = SecurityContext.Restricted }, 156 new () { ProcessorIdentifier = "dscv3", ProcessorProperties = firstProperty }, 157 new () { ProcessorIdentifier = "pwsh", ProcessorProperties = firstProperty }, 158 new () { ProcessorIdentifier = "pwsh", ProcessorProperties = secondProperty }, 159 new () { ProcessorIdentifier = "dscv3", Context = SecurityContext.Restricted, ProcessorProperties = firstProperty }, 160 new () { ProcessorIdentifier = "pwsh", Context = SecurityContext.Elevated, ProcessorProperties = firstProperty }, 161 }; 162 163 foreach (int index in new int[] { 0, 1, 1, 2, 3, 5, 4, 6, 7, 8, 2, 7, 7, 7 }) 164 { 165 Assert.True(index < environments.Length); 166 testSet.Units.Add(environments[index].ApplyToUnit(this.ConfigurationUnit())); 167 } 168 169 var uniqueEnvironments = testSet.GetUnitEnvironments(); 170 this.EnsureEnvironmentEquivalence(environments, uniqueEnvironments); 171 } 172 173 /// <summary> 174 /// Test for unique unit environment calculation with group units. 175 /// </summary> 176 [Fact] 177 public void ConfigurationSet_GroupUnitEnvironments() 178 { 179 ConfigurationSet testSet = this.ConfigurationSet(); 180 181 Dictionary<string, string> firstProperty = new Dictionary<string, string>(); 182 firstProperty.Add("property", "value1"); 183 184 Dictionary<string, string> secondProperty = new Dictionary<string, string>(); 185 secondProperty.Add("property", "value2"); 186 187 Helpers.ConfigurationEnvironmentData[] environments = new Helpers.ConfigurationEnvironmentData[] 188 { 189 new () { ProcessorIdentifier = "dscv3" }, 190 new () { ProcessorIdentifier = "pwsh" }, 191 new () { ProcessorIdentifier = "dscv3", Context = SecurityContext.Elevated }, 192 new () { ProcessorIdentifier = "pwsh", Context = SecurityContext.Restricted }, 193 new () { ProcessorIdentifier = "dscv3", ProcessorProperties = firstProperty }, 194 new () { ProcessorIdentifier = "pwsh", ProcessorProperties = firstProperty }, 195 new () { ProcessorIdentifier = "pwsh", ProcessorProperties = secondProperty }, 196 new () { ProcessorIdentifier = "dscv3", Context = SecurityContext.Restricted, ProcessorProperties = firstProperty }, 197 new () { ProcessorIdentifier = "pwsh", Context = SecurityContext.Elevated, ProcessorProperties = firstProperty }, 198 new (), // The default environment for the group unit 199 }; 200 201 foreach (int index in new int[] { 0, 1, 1, 3, 5, 4, 6, 8 }) 202 { 203 Assert.True(index < environments.Length); 204 testSet.Units.Add(environments[index].ApplyToUnit(this.ConfigurationUnit())); 205 } 206 207 var groupUnit = this.ConfigurationUnit(); 208 groupUnit.IsGroup = true; 209 210 foreach (int index in new int[] { 7, 5, 2 }) 211 { 212 Assert.True(index < environments.Length); 213 groupUnit.Units.Add(environments[index].ApplyToUnit(this.ConfigurationUnit())); 214 } 215 216 testSet.Units.Add(groupUnit); 217 218 var uniqueEnvironments = testSet.GetUnitEnvironments(); 219 this.EnsureEnvironmentEquivalence(environments, uniqueEnvironments); 220 } 221 222 private void EnsureEnvironmentEquivalence(Helpers.ConfigurationEnvironmentData[] expectedEnvironments, IList<ConfigurationEnvironment>? actualEnvironments) 223 { 224 Assert.NotNull(actualEnvironments); 225 Assert.Equal(expectedEnvironments.Length, actualEnvironments.Count); 226 227 bool[] foundEnvironments = new bool[expectedEnvironments.Length]; 228 foreach (var actual in actualEnvironments) 229 { 230 for (int i = 0; i < expectedEnvironments.Length; i++) 231 { 232 var expected = expectedEnvironments[i]; 233 if (actual.Context == expected.Context && actual.ProcessorIdentifier == expected.ProcessorIdentifier && expected.PropertiesEqual(actual.ProcessorProperties)) 234 { 235 foundEnvironments[i] = true; 236 break; 237 } 238 } 239 } 240 241 for (int i = 0; i < foundEnvironments.Length; i++) 242 { 243 Assert.True(foundEnvironments[i], $"Found expected environment: {i}"); 244 } 245 } 246 } 247 }