From bddd14bc31010a5aed261cc7f55b65042737e7cf Mon Sep 17 00:00:00 2001 From: John McPherson Date: Mon, 13 Jan 2025 17:34:57 -0800 Subject: [PATCH] Start on tests --- .../Helpers/ConfigurationEnvironmentData.cs | 67 +++++++++++++++++++ .../Helpers/ConfigurationExtensions.cs | 2 +- .../Tests/ConfigurationSetAuthoringTests.cs | 58 ++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationEnvironmentData.cs diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationEnvironmentData.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationEnvironmentData.cs new file mode 100644 index 0000000000..a06678de66 --- /dev/null +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationEnvironmentData.cs @@ -0,0 +1,67 @@ +// ----------------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT License. +// +// ----------------------------------------------------------------------------- + +namespace Microsoft.Management.Configuration.UnitTests.Helpers +{ + using System; + using System.Collections.Generic; + + /// + /// Contains the data defining a configuration environment. + /// + internal class ConfigurationEnvironmentData + { + /// + /// Initializes a new instance of the class. + /// + internal ConfigurationEnvironmentData() { } + + /// + /// Gets or sets the security context. + /// + internal SecurityContext Context { get; set; } = SecurityContext.Current; + + /// + /// Gets or sets the processor identifier. + /// + internal string ProcessorIdentifier { get; set; } = string.Empty; + + /// + /// Gets or sets the processor properties. + /// + internal Dictionary ProcessorProperties { get; set; } = new (); + + /// + /// Applies this environment to the given unit. + /// + /// The unit to apply to. + /// The given unit. + internal ConfigurationUnit ApplyToUnit(ConfigurationUnit unit) + { + var environment = unit.Environment; + + environment.Context = this.Context; + environment.ProcessorIdentifier = this.ProcessorIdentifier; + environment.ProcessorProperties.Clear(); + foreach (var property in environment.ProcessorProperties) + { + environment.ProcessorProperties.Add(property.Key, property.Value); + } + + return unit; + } + + /// + /// Tests whether the given properties match this object's properties. + /// + /// The properties to test. + /// True if the properties match; false if not. + internal bool PropertiesEqual(IReadOnlyDictionary properties) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationExtensions.cs b/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationExtensions.cs index dc7a924248..e6409d5090 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationExtensions.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Helpers/ConfigurationExtensions.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // diff --git a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs index 4590af2aad..49f4843e81 100644 --- a/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs +++ b/src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs @@ -7,6 +7,7 @@ namespace Microsoft.Management.Configuration.UnitTests.Tests { using System; + using System.Collections.Generic; using Microsoft.Management.Configuration.UnitTests.Fixtures; using Microsoft.Management.Configuration.UnitTests.Helpers; using Microsoft.VisualBasic; @@ -130,5 +131,62 @@ public void ConfigurationSetSerializeNestedValueSets() string yamlOutput = this.ReadStream(stream); Assert.NotNull(yamlOutput); } + + /// + /// Test for unique unit evironment calculation. + /// + [Fact] + public void ConfigurationSet_UnitEnvironments() + { + ConfigurationSet testSet = this.ConfigurationSet(); + + Dictionary firstProperty = new Dictionary(); + firstProperty.Add("property", "value1"); + + Dictionary secondProperty = new Dictionary(); + secondProperty.Add("property", "value2"); + + Helpers.ConfigurationEnvironmentData[] environments = new Helpers.ConfigurationEnvironmentData[] + { + new () { ProcessorIdentifier = "dsc3" }, + new () { ProcessorIdentifier = "pwsh" }, + new () { ProcessorIdentifier = "dsc3", Context = SecurityContext.Elevated }, + new () { ProcessorIdentifier = "pwsh", Context = SecurityContext.Restricted }, + new () { ProcessorIdentifier = "dsc3", ProcessorProperties = firstProperty }, + new () { ProcessorIdentifier = "pwsh", ProcessorProperties = firstProperty }, + new () { ProcessorIdentifier = "pwsh", ProcessorProperties = secondProperty }, + new () { ProcessorIdentifier = "dsc3", Context = SecurityContext.Restricted, ProcessorProperties = firstProperty }, + new () { ProcessorIdentifier = "pwsh", Context = SecurityContext.Elevated, ProcessorProperties = firstProperty }, + }; + + foreach (int index in new int[] { 0, 1, 1, 2, 3, 5, 4, 6, 7, 8, 2, 7, 7, 7 }) + { + Assert.True(index < environments.Length); + testSet.Units.Add(environments[index].ApplyToUnit(this.ConfigurationUnit())); + } + + var uniqueEnvironments = testSet.GetUnitEnvironments(); + + Assert.Equal(environments.Length, uniqueEnvironments.Count); + + bool[] foundEnvironments = new bool[environments.Length]; + foreach (var actual in uniqueEnvironments) + { + for (int i = 0; i < environments.Length; i++) + { + var expected = environments[i]; + if (actual.Context == expected.Context && actual.ProcessorIdentifier == expected.ProcessorIdentifier && expected.PropertiesEqual(actual.ProcessorPropertiesView)) + { + foundEnvironments[i] = true; + break; + } + } + } + + for (int i = 0; i < foundEnvironments.Length; i++) + { + Assert.True(foundEnvironments[i], $"Found expected environment: {i}"); + } + } } }