winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

DSCv3ProcessorTests.cs (12781B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="DSCv3ProcessorTests.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.Collections.Generic;
     10     using System.Linq;
     11     using Microsoft.Management.Configuration.Processor;
     12     using Microsoft.Management.Configuration.Processor.DSCv3.Model;
     13     using Microsoft.Management.Configuration.Processor.Exceptions;
     14     using Microsoft.Management.Configuration.UnitTests.Fixtures;
     15     using Microsoft.Management.Configuration.UnitTests.Helpers;
     16     using Windows.Foundation.Collections;
     17     using Xunit;
     18     using Xunit.Abstractions;
     19 
     20     /// <summary>
     21     /// Tests for the DSCv3 processor.
     22     /// </summary>
     23     [Collection("UnitTestCollection")]
     24     [InProc]
     25     public class DSCv3ProcessorTests : ConfigurationProcessorTestBase
     26     {
     27         private readonly UnitTestFixture fixture;
     28         private readonly ITestOutputHelper log;
     29 
     30         /// <summary>
     31         /// Initializes a new instance of the <see cref="DSCv3ProcessorTests"/> class.
     32         /// </summary>
     33         /// <param name="fixture">Unit test fixture.</param>
     34         /// <param name="log">Log helper.</param>
     35         public DSCv3ProcessorTests(UnitTestFixture fixture, ITestOutputHelper log)
     36             : base(fixture, log)
     37         {
     38             this.fixture = fixture;
     39             this.log = log;
     40         }
     41 
     42         /// <summary>
     43         /// Tests for the unit details caching.
     44         /// </summary>
     45         [Fact]
     46         public void Set_UnitPropertyDetailsCached()
     47         {
     48             var (factory, dsc) = CreateTestFactory();
     49             var set = this.ConfigurationSet();
     50             string type1 = "Type1";
     51             string type2 = "Type2";
     52             var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
     53             var unit2 = this.ConfigurationUnit().Assign(new { Type = type2 });
     54 
     55             var setProcessor = factory.CreateSetProcessor(set);
     56 
     57             // Initially, no details
     58             var details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
     59             Assert.Null(details);
     60 
     61             // Null result not cached
     62             dsc.GetResourceByTypeResult = new TestResourceListItem() { Type = type1 };
     63             details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
     64             Assert.NotNull(details);
     65             Assert.Equal(type1, details.UnitType);
     66 
     67             // Not-null result cached
     68             dsc.GetResourceByTypeResult = null;
     69             dsc.GetResourceByTypeDelegate = s => throw new System.Exception("Shouldn't be called");
     70             details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
     71             Assert.NotNull(details);
     72             Assert.Equal(type1, details.UnitType);
     73 
     74             // Different type, no details
     75             dsc.GetResourceByTypeDelegate = null;
     76             details = setProcessor.GetUnitProcessorDetails(unit2, ConfigurationUnitDetailFlags.Local);
     77             Assert.Null(details);
     78 
     79             // Null result not cached
     80             dsc.GetResourceByTypeResult = new TestResourceListItem() { Type = type2 };
     81             details = setProcessor.GetUnitProcessorDetails(unit2, ConfigurationUnitDetailFlags.Local);
     82             Assert.NotNull(details);
     83             Assert.Equal(type2, details.UnitType);
     84 
     85             // First type is still first type
     86             dsc.GetResourceByTypeResult = null;
     87             dsc.GetResourceByTypeDelegate = s => throw new System.Exception("Shouldn't be called");
     88             details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
     89             Assert.NotNull(details);
     90             Assert.Equal(type1, details.UnitType);
     91         }
     92 
     93         /// <summary>
     94         /// Test for unit processor creation requiring resource to be found.
     95         /// </summary>
     96         [Fact(Skip = "Disable this test while we have the bypass in place")]
     97         public void Set_ResourceNotFoundIsError()
     98         {
     99             var (factory, dsc) = CreateTestFactory();
    100             var set = this.ConfigurationSet();
    101             string type1 = "Type1";
    102             var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
    103 
    104             var setProcessor = factory.CreateSetProcessor(set);
    105 
    106             // Not found is error
    107             Assert.Throws<FindDscResourceNotFoundException>(() => setProcessor.CreateUnitProcessor(unit1));
    108 
    109             // Found is not error
    110             dsc.GetResourceByTypeResult = new TestResourceListItem() { Type = type1 };
    111             var unitProcessor = setProcessor.CreateUnitProcessor(unit1);
    112             Assert.NotNull(unitProcessor);
    113             Assert.Equal(type1, unitProcessor.Unit.Type);
    114         }
    115 
    116         /// <summary>
    117         /// Test for settings export.
    118         /// </summary>
    119         [Fact]
    120         public void GetAllSettings_Expected()
    121         {
    122             var (factory, dsc) = CreateTestFactory();
    123             var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
    124 
    125             string type1 = "Type1";
    126             var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
    127 
    128             dsc.GetResourceByTypeDelegate = (type) =>
    129             {
    130                 Assert.Equal(type1, type);
    131                 return new TestResourceListItem() { Type = type1 };
    132             };
    133 
    134             ValueSet set1 = new ValueSet();
    135             set1.Add("key1", "val1");
    136 
    137             ValueSet set2 = new ValueSet();
    138             set2.Add("key2", "val2");
    139 
    140             dsc.ExportResourceResult = new List<IResourceExportItem>()
    141             {
    142                 new TestResourceExportItem() { Type = type1, Name = "1", Settings = set1 },
    143                 new TestResourceExportItem() { Type = type1, Name = "2", Settings = set2 },
    144             };
    145 
    146             var result = processor.GetAllUnitSettings(unit1);
    147 
    148             Assert.NotNull(result);
    149             Assert.NotNull(result.ResultInformation);
    150             Assert.Null(result.ResultInformation.ResultCode);
    151 
    152             Assert.Equal(2, result.Settings.Count);
    153             Assert.NotNull(result.Settings.Single(set => set.Contains(set1.First())));
    154             Assert.NotNull(result.Settings.Single(set => set.Contains(set2.First())));
    155         }
    156 
    157         /// <summary>
    158         /// Test for settings export with differing types.
    159         /// </summary>
    160         [Fact]
    161         public void GetAllSettings_DifferentType()
    162         {
    163             var (factory, dsc) = CreateTestFactory();
    164             var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
    165 
    166             string type1 = "Type1";
    167             string type2 = "Type2";
    168             var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
    169 
    170             dsc.GetResourceByTypeDelegate = (type) =>
    171             {
    172                 Assert.Equal(type1, type);
    173                 return new TestResourceListItem() { Type = type1 };
    174             };
    175 
    176             ValueSet set1 = new ValueSet();
    177             set1.Add("key1", "val1");
    178 
    179             ValueSet set2 = new ValueSet();
    180             set2.Add("key2", "val2");
    181 
    182             dsc.ExportResourceResult = new List<IResourceExportItem>()
    183             {
    184                 new TestResourceExportItem() { Type = type1, Name = "1", Settings = set1 },
    185                 new TestResourceExportItem() { Type = type2, Name = "2", Settings = set2 },
    186             };
    187 
    188             var result = processor.GetAllUnitSettings(unit1);
    189 
    190             Assert.NotNull(result);
    191             Assert.NotNull(result.ResultInformation);
    192             Assert.NotNull(result.ResultInformation.ResultCode);
    193             Assert.Equal(ErrorCodes.WinGetConfigUnitUnsupportedType, result.ResultInformation.ResultCode.HResult);
    194         }
    195 
    196         /// <summary>
    197         /// Test for unit export.
    198         /// </summary>
    199         [Fact]
    200         public void GetAllUnits_Simple()
    201         {
    202             var (factory, dsc) = CreateTestFactory();
    203             var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
    204 
    205             string type1 = "Type1";
    206             var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
    207 
    208             dsc.GetResourceByTypeDelegate = (type) =>
    209             {
    210                 Assert.Equal(type1, type);
    211                 return new TestResourceListItem() { Type = type1 };
    212             };
    213 
    214             ValueSet set1 = new ValueSet();
    215             set1.Add("key1", "val1");
    216 
    217             ValueSet set2 = new ValueSet();
    218             set2.Add("key2", "val2");
    219 
    220             dsc.ExportResourceResult = new List<IResourceExportItem>()
    221             {
    222                 new TestResourceExportItem() { Type = type1, Name = "1", Settings = set1 },
    223                 new TestResourceExportItem() { Type = type1, Name = "2", Settings = set2 },
    224             };
    225 
    226             var result = processor.GetAllUnits(unit1);
    227 
    228             Assert.NotNull(result);
    229             Assert.NotNull(result.ResultInformation);
    230             Assert.Null(result.ResultInformation.ResultCode);
    231 
    232             Assert.Equal(2, result.Units.Count);
    233 
    234             foreach (var unit in result.Units)
    235             {
    236                 Assert.Equal(type1, unit.Type);
    237                 Assert.NotEmpty(unit.Identifier);
    238             }
    239 
    240             Assert.NotEqual(result.Units[0].Identifier, result.Units[1].Identifier);
    241 
    242             Assert.NotNull(result.Units.Single(unit => unit.Settings.Contains(set1.First())));
    243             Assert.NotNull(result.Units.Single(unit => unit.Settings.Contains(set2.First())));
    244         }
    245 
    246         /// <summary>
    247         /// Test for unit export with complex data.
    248         /// </summary>
    249         [Fact]
    250         public void GetAllUnits_Complex()
    251         {
    252             var (factory, dsc) = CreateTestFactory();
    253             var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
    254 
    255             string type1 = "Type1";
    256             string type2 = "Type2";
    257 
    258             string name1 = "1";
    259             string name2 = "2";
    260 
    261             var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
    262 
    263             dsc.GetResourceByTypeDelegate = (type) =>
    264             {
    265                 Assert.Equal(type1, type);
    266                 return new TestResourceListItem() { Type = type1 };
    267             };
    268 
    269             ValueSet set1 = new ValueSet();
    270             set1.Add("key1", "val1");
    271 
    272             ValueSet metadata1 = new ValueSet();
    273             metadata1.Add("met1", "val11");
    274 
    275             ValueSet set2 = new ValueSet();
    276             set2.Add("key2", "val2");
    277 
    278             List<string> dependencies2 = new List<string>();
    279             dependencies2.Add(name1);
    280 
    281             dsc.ExportResourceResult = new List<IResourceExportItem>()
    282             {
    283                 new TestResourceExportItem() { Type = type1, Name = name1, Settings = set1, Metadata = metadata1 },
    284                 new TestResourceExportItem() { Type = type2, Name = name2, Settings = set2, Dependencies = dependencies2 },
    285             };
    286 
    287             var result = processor.GetAllUnits(unit1);
    288 
    289             Assert.NotNull(result);
    290             Assert.NotNull(result.ResultInformation);
    291             Assert.Null(result.ResultInformation.ResultCode);
    292 
    293             Assert.Equal(2, result.Units.Count);
    294 
    295             var result1 = result.Units.Single(unit => unit.Identifier == name1);
    296             var result2 = result.Units.Single(unit => unit.Identifier == name2);
    297 
    298             Assert.Equal(type1, result1.Type);
    299             Assert.Equal(type2, result2.Type);
    300 
    301             Assert.Contains(set1.First(), result1.Settings);
    302             Assert.Contains(set2.First(), result2.Settings);
    303 
    304             Assert.Contains(metadata1.First(), result1.Metadata);
    305             Assert.Empty(result2.Metadata);
    306 
    307             Assert.Empty(result1.Dependencies);
    308             Assert.Contains(dependencies2.First(), result2.Dependencies);
    309         }
    310 
    311         private static (DSCv3ConfigurationSetProcessorFactory, TestDSCv3) CreateTestFactory()
    312         {
    313             DSCv3ConfigurationSetProcessorFactory factory = new DSCv3ConfigurationSetProcessorFactory();
    314             TestDSCv3 dsc = new TestDSCv3();
    315             factory.Settings.DSCv3 = dsc;
    316             factory.Settings.DscExecutablePath = "Test-Path-Not-Used.txt";
    317 
    318             return (factory, dsc);
    319         }
    320     }
    321 }