Skip to content

Commit

Permalink
Start on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Jan 14, 2025
1 parent 4799ddf commit bddd14b
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// -----------------------------------------------------------------------------
// <copyright file="ConfigurationEnvironmentData.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace Microsoft.Management.Configuration.UnitTests.Helpers
{
using System;
using System.Collections.Generic;

/// <summary>
/// Contains the data defining a configuration environment.
/// </summary>
internal class ConfigurationEnvironmentData
{
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationEnvironmentData"/> class.
/// </summary>
internal ConfigurationEnvironmentData() { }

/// <summary>
/// Gets or sets the security context.
/// </summary>
internal SecurityContext Context { get; set; } = SecurityContext.Current;

/// <summary>
/// Gets or sets the processor identifier.
/// </summary>
internal string ProcessorIdentifier { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the processor properties.
/// </summary>
internal Dictionary<string, string> ProcessorProperties { get; set; } = new ();

/// <summary>
/// Applies this environment to the given unit.
/// </summary>
/// <param name="unit">The unit to apply to.</param>
/// <returns>The given unit.</returns>
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;
}

/// <summary>
/// Tests whether the given properties match this object's properties.
/// </summary>
/// <param name="properties">The properties to test.</param>
/// <returns>True if the properties match; false if not.</returns>
internal bool PropertiesEqual(IReadOnlyDictionary<string, string> properties)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// <copyright file="ConfigurationExtensions.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,5 +131,62 @@ public void ConfigurationSetSerializeNestedValueSets()
string yamlOutput = this.ReadStream(stream);
Assert.NotNull(yamlOutput);
}

/// <summary>
/// Test for unique unit evironment calculation.

Check warning on line 136 in src/Microsoft.Management.Configuration.UnitTests/Tests/ConfigurationSetAuthoringTests.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`evironment` is not a recognized word. (unrecognized-spelling)
/// </summary>
[Fact]
public void ConfigurationSet_UnitEnvironments()
{
ConfigurationSet testSet = this.ConfigurationSet();

Dictionary<string, string> firstProperty = new Dictionary<string, string>();
firstProperty.Add("property", "value1");

Dictionary<string, string> secondProperty = new Dictionary<string, string>();
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}");
}
}
}
}

0 comments on commit bddd14b

Please sign in to comment.