winget-cli

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

ConfigurationDetailsTests.cs (10747B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ConfigurationDetailsTests.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 System.Linq;
     12     using System.Management.Automation;
     13     using Microsoft.Management.Configuration.Processor.Helpers;
     14     using Microsoft.Management.Configuration.Processor.PowerShell.DscResourcesInfo;
     15     using Microsoft.Management.Configuration.Processor.PowerShell.Helpers;
     16     using Microsoft.Management.Configuration.Processor.Unit;
     17     using Microsoft.Management.Configuration.UnitTests.Fixtures;
     18     using Microsoft.Management.Configuration.UnitTests.Helpers;
     19     using Windows.Security.Cryptography.Certificates;
     20     using Xunit;
     21     using Xunit.Abstractions;
     22 
     23     /// <summary>
     24     /// Tests for ConfigurationUnitProcessorDetails and ConfigurationUnitSettingDetails.
     25     /// </summary>
     26     [Collection("UnitTestCollection")]
     27     [InProc]
     28     public class ConfigurationDetailsTests
     29     {
     30         private readonly UnitTestFixture fixture;
     31         private readonly ITestOutputHelper log;
     32 
     33         /// <summary>
     34         /// Initializes a new instance of the <see cref="ConfigurationDetailsTests"/> class.
     35         /// </summary>
     36         /// <param name="fixture">Fixture.</param>
     37         /// <param name="log">log.</param>
     38         public ConfigurationDetailsTests(UnitTestFixture fixture, ITestOutputHelper log)
     39         {
     40             this.fixture = fixture;
     41             this.log = log;
     42         }
     43 
     44         /// <summary>
     45         /// Tests creating ConfigurationUnitProcessorDetails and ConfigurationUnitSettingDetails with different inputs.
     46         /// </summary>
     47         /// <param name="hasDscInfo">Has dsc info.</param>
     48         /// <param name="hasPSModuleInfo">Has ps module info.</param>
     49         /// <param name="hasGetModuleInfo">Has get module info.</param>
     50         /// <param name="hasCerts">Has certs.</param>
     51         [Theory]
     52         [InlineData(false, false, false, false)]
     53         [InlineData(false, false, false, true)]
     54         [InlineData(false, false, true, false)]
     55         [InlineData(false, false, true, true)]
     56         [InlineData(false, true, false, false)]
     57         [InlineData(false, true, false, true)]
     58         [InlineData(false, true, true, false)]
     59         [InlineData(false, true, true, true)]
     60         [InlineData(true, false, false, false)]
     61         [InlineData(true, false, false, true)]
     62         [InlineData(true, false, true, false)]
     63         [InlineData(true, false, true, true)]
     64         [InlineData(true, true, false, false)]
     65         [InlineData(true, true, false, true)]
     66         [InlineData(true, true, true, false)]
     67         [InlineData(true, true, true, true)]
     68         public void ConfigurationUnitProcessorDetails_CreationTest(bool hasDscInfo, bool hasPSModuleInfo, bool hasGetModuleInfo, bool hasCerts)
     69         {
     70             List<Certificate>? certsInput = null;
     71             if (hasCerts)
     72             {
     73                 certsInput = new List<Certificate>();
     74             }
     75 
     76             if (!hasDscInfo && !hasPSModuleInfo && !hasGetModuleInfo)
     77             {
     78                 Assert.Throws<ArgumentException>(
     79                     () => Factory.CreateUnitProcessorDetails("unitName", null, null, null, certsInput));
     80             }
     81             else
     82             {
     83                 var unit = this.CreateConfigurationUnit();
     84                 var (dscResourceInfo, psModuleInfo) = this.GetResourceAndModuleInfo(unit);
     85 
     86                 DscResourceInfoInternal? dscResourceInfoInput = null;
     87                 if (hasDscInfo)
     88                 {
     89                     dscResourceInfoInput = dscResourceInfo;
     90                 }
     91 
     92                 PSModuleInfo? psModuleInfoInput = null;
     93                 if (hasPSModuleInfo)
     94                 {
     95                     psModuleInfoInput = psModuleInfo;
     96                 }
     97 
     98                 PSObject? getModuleInfo = null;
     99                 if (hasGetModuleInfo)
    100                 {
    101                     getModuleInfo = this.CreateGetModuleInfo();
    102                 }
    103 
    104                 var details = Factory.CreateUnitProcessorDetails(unit.Type, dscResourceInfoInput, psModuleInfoInput, getModuleInfo, certsInput);
    105 
    106                 Assert.Equal(unit.Type, details.UnitType);
    107 
    108                 if (hasDscInfo)
    109                 {
    110                     Assert.True(details.IsLocal);
    111                     Assert.Equal("xSimpleTestResource", details.ModuleName);
    112                     Assert.Equal("0.0.0.1", details.Version);
    113                     Assert.NotNull(details.Settings);
    114                     Assert.True(details.Settings.Count == 5);
    115 
    116                     var pathSetting = details.Settings.Where(s => s.Identifier == "Path").FirstOrDefault();
    117                     Assert.NotNull(pathSetting);
    118                     Assert.Equal(Windows.Foundation.PropertyType.String, pathSetting.Type);
    119                     Assert.True(pathSetting.IsRequired);
    120                     Assert.Equal(string.Empty, pathSetting.Schema);
    121 
    122                     var contentSetting = details.Settings.Where(s => s.Identifier == "Content").FirstOrDefault();
    123                     Assert.NotNull(contentSetting);
    124                     Assert.Equal(Windows.Foundation.PropertyType.String, contentSetting.Type);
    125                     Assert.False(contentSetting.IsRequired);
    126                     Assert.Equal(string.Empty, contentSetting.Schema);
    127 
    128                     var dependsOnSetting = details.Settings.Where(s => s.Identifier == "DependsOn").FirstOrDefault();
    129                     Assert.NotNull(dependsOnSetting);
    130                     Assert.Equal(Windows.Foundation.PropertyType.StringArray, dependsOnSetting.Type);
    131                     Assert.False(dependsOnSetting.IsRequired);
    132                     Assert.Equal(string.Empty, dependsOnSetting.Schema);
    133 
    134                     var ensureSetting = details.Settings.Where(s => s.Identifier == "Ensure").FirstOrDefault();
    135                     Assert.NotNull(ensureSetting);
    136                     Assert.Equal(Windows.Foundation.PropertyType.String, ensureSetting.Type);
    137                     Assert.False(ensureSetting.IsRequired);
    138                     Assert.Equal(string.Empty, ensureSetting.Schema);
    139 
    140                     var psDscRunAsCredentialSetting = details.Settings.Where(s => s.Identifier == "PsDscRunAsCredential").FirstOrDefault();
    141                     Assert.NotNull(psDscRunAsCredentialSetting);
    142                     Assert.Equal(Windows.Foundation.PropertyType.Inspectable, psDscRunAsCredentialSetting.Type);
    143                     Assert.False(psDscRunAsCredentialSetting.IsRequired);
    144                     Assert.Equal(string.Empty, psDscRunAsCredentialSetting.Schema);
    145                 }
    146 
    147                 if (hasPSModuleInfo)
    148                 {
    149                     Assert.Equal(new Uri("https://www.contoso.com/help"), details.ModuleDocumentationUri);
    150                     Assert.Equal(new Uri("https://www.contoso.com/icons/icon.png"), details.UnitIconUri);
    151                     Assert.Equal("xSimpleTestResource", details.ModuleName);
    152                     Assert.Equal(ModuleType.Script.ToString(), details.ModuleType);
    153                     Assert.Equal("PowerShell module with DSC resources for unit tests", details.ModuleDescription);
    154                     Assert.Equal(new Uri("https://github.com/microsoft/winget-cli"), details.PublishedModuleUri);
    155                     Assert.Equal("0.0.0.1", details.Version);
    156                     Assert.Equal("Luffytaro", details.Author);
    157                     Assert.Equal("Microsoft Corporation", details.Publisher);
    158                 }
    159 
    160                 if (hasGetModuleInfo)
    161                 {
    162                     Assert.Equal("PSGallery", details.ModuleSource);
    163                     Assert.Equal(new DateTimeOffset(new DateTime(2017, 12, 10)), details.PublishedDate);
    164                     Assert.Equal(new Uri("https://www.contoso.com/icons/icon.png"), details.UnitIconUri);
    165                     Assert.Equal("xSimpleTestResource", details.ModuleName);
    166                     Assert.Equal("PowerShell module with DSC resources for unit tests", details.ModuleDescription);
    167                     Assert.Equal("0.0.0.1", details.Version);
    168                     Assert.Equal("Luffytaro", details.Author);
    169                     Assert.Equal("Microsoft Corporation", details.Publisher);
    170                     Assert.True(details.IsPublic);
    171                 }
    172 
    173                 if (hasCerts)
    174                 {
    175                     Assert.NotNull(details.SigningInformation);
    176                 }
    177             }
    178         }
    179 
    180         private ConfigurationUnit CreateConfigurationUnit()
    181         {
    182             var unit = new ConfigurationUnit();
    183             unit.Type = "SimpleFileResource";
    184             unit.Metadata.Add("module", "xSimpleTestResource");
    185             unit.Metadata.Add("version", "0.0.0.1");
    186 
    187             return unit;
    188         }
    189 
    190         private (DscResourceInfoInternal dscResourceInfo, PSModuleInfo psModuleInfo) GetResourceAndModuleInfo(ConfigurationUnit unit)
    191         {
    192             // This is easier than trying to mock sealed class from external code...
    193             var testEnv = this.fixture.PrepareTestProcessorEnvironment(true);
    194 
    195             var dscResourceInfo = testEnv.GetDscResource(new ConfigurationUnitAndModule(unit, string.Empty));
    196             var psModuleInfo = testEnv.GetAvailableModule(PowerShellHelpers.CreateModuleSpecification("xSimpleTestResource", "0.0.0.1"));
    197 
    198             if (dscResourceInfo is null || psModuleInfo is null)
    199             {
    200                 throw new ArgumentNullException("Test processor environment not set correctly");
    201             }
    202 
    203             return (dscResourceInfo, psModuleInfo);
    204         }
    205 
    206         private PSObject CreateGetModuleInfo()
    207         {
    208             return new PSObject(new
    209             {
    210                 Repository = "PSGallery",
    211                 PublishedDate = new DateTime(2017, 12, 10),
    212                 IconUri = "https://www.contoso.com/icons/icon.png",
    213                 Name = "xSimpleTestResource",
    214                 Description = "PowerShell module with DSC resources for unit tests",
    215                 RepositorySourceLocation = "https://www.powershellgallery.com/api/v2",
    216                 Version = "0.0.0.1",
    217                 Author = "Luffytaro",
    218                 CompanyName = "Microsoft Corporation",
    219             });
    220         }
    221     }
    222 }