Microsoft.WinGet.DSC.Tests.ps1 (10775B)
1 # Copyright (c) Microsoft Corporation. All rights reserved. 2 # Licensed under the MIT License. 3 4 <# 5 .Synopsis 6 Pester tests related to the Microsoft.WinGet.DSC PowerShell module. 7 The tests require the localhost web server to be running and serving the test data. 8 'Invoke-Pester' should be called in an admin PowerShell window. 9 #> 10 11 BeforeAll { 12 Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck 13 Import-Module Microsoft.WinGet.Client 14 Import-Module Microsoft.WinGet.DSC 15 16 # Helper function for calling Invoke-DscResource on the Microsoft.WinGet.DSC module. 17 function InvokeWinGetDSC() { 18 param ( 19 [Parameter()] 20 [string]$Name, 21 22 [Parameter()] 23 [string]$Method, 24 25 [Parameter()] 26 [hashtable]$Property 27 ) 28 29 return Invoke-DscResource -Name $Name -ModuleName Microsoft.WinGet.DSC -Method $Method -Property $Property 30 } 31 } 32 33 Describe 'List available DSC resources'{ 34 It 'Shows DSC Resources'{ 35 $expectedDSCResources = "WinGetAdminSettings", "WinGetPackage", "WinGetPackageManager", "WinGetSource", "WinGetUserSettings" 36 $availableDSCResources = (Get-DscResource -Module Microsoft.WinGet.DSC).Name 37 $availableDSCResources.length | Should -Be 5 38 $availableDSCResources | Where-Object {$expectedDSCResources -notcontains $_} | Should -BeNullOrEmpty -ErrorAction Stop 39 } 40 } 41 42 Describe 'WinGetAdminSettings' { 43 44 BeforeAll { 45 $initialAdminSettings = (Get-WinGetSetting).adminSettings 46 $adminSettingsHash = @{ 47 BypassCertificatePinningForMicrosoftStore = !$initialAdminSettings.BypassCertificatePinningForMicrosoftStore; 48 InstallerHashOverride = !$initialAdminSettings.InstallerHashOverride; 49 LocalManifestFiles = !$initialAdminSettings.LocalManifestFiles; 50 LocalArchiveMalwareScanOverride = !$initialAdminSettings.LocalArchiveMalwareScanOverride; 51 } 52 } 53 54 It 'Get admin settings' { 55 $result = InvokeWinGetDSC -Name WinGetAdminSettings -Method Get -Property @{ Settings = $adminSettingsHash } 56 $adminSettings = $result.Settings 57 $adminSettings.BypassCertificatePinningForMicrosoftStore | Should -Be $initialAdminSettings.BypassCertificatePinningForMicrosoftStore 58 $adminSettings.InstallerHashOverride | Should -Be $initialAdminSettings.InstallerHashOverride 59 $adminSettings.LocalManifestFiles | Should -Be $initialAdminSettings.LocalManifestFiles 60 $adminSettings.LocalArchiveMalwareScanOverride | Should -Be $initialAdminSettings.LocalArchiveMalwareScanOverride 61 } 62 63 It 'Test admin settings' { 64 $result = InvokeWinGetDSC -Name WinGetAdminSettings -Method Test -Property @{ Settings = $adminSettingsHash } 65 $result.InDesiredState | Should -Be $false 66 } 67 68 It 'Set admin settings' { 69 InvokeWinGetDSC -Name WinGetAdminSettings -Method Set -Property @{ Settings = $adminSettingsHash } 70 71 # Verify settings were applied. 72 $result = InvokeWinGetDSC -Name WinGetAdminSettings -Method Get -Property @{ Settings = $adminSettingsHash } 73 $adminSettings = $result.Settings 74 $adminSettings.BypassCertificatePinningForMicrosoftStore | Should -Not -Be $initialAdminSettings.BypassCertificatePinningForMicrosoftStore 75 $adminSettings.InstallerHashOverride | Should -Not -Be $initialAdminSettings.InstallerHashOverride 76 $adminSettings.LocalManifestFiles | Should -Not -Be $initialAdminSettings.LocalManifestFiles 77 $adminSettings.LocalArchiveMalwareScanOverride | Should -Not -Be $initialAdminSettings.LocalArchiveMalwareScanOverride 78 79 $testResult = InvokeWinGetDSC -Name WinGetAdminSettings -Method Test -Property @{ Settings = $adminSettingsHash } 80 $testResult | Should -Be $true 81 } 82 83 AfterAll { 84 InvokeWinGetDSC -Name WinGetAdminSettings -Method Set -Property @{ Settings = $initialAdminSettings } 85 } 86 } 87 88 Describe 'WinGetUserSettings' { 89 BeforeAll { 90 # Delete existing user settings file. 91 $settingsFilePath = (Get-WinGetSetting).userSettingsFile 92 $backupSettingsFilePath = $settingsFilePath + ".backup" 93 94 if (Test-Path -Path $settingsFilePath) 95 { 96 Remove-Item $settingsFilePath 97 } 98 99 if (Test-Path -Path $backupSettingsFilePath) 100 { 101 Remove-Item $backupSettingsFilePath 102 } 103 104 $userSettingsHash = @{ 105 experimentalFeatures = @{ directMSI = $true }; 106 installBehavior = @{ Preferences = @{ Scope = 'User' }} 107 } 108 } 109 110 It 'Get user settings' { 111 $result = InvokeWinGetDSC -Name WinGetUserSettings -Method Get -Property @{ Settings = $userSettingsHash } 112 $result.Settings.Count | Should -Be 0 113 } 114 115 It 'Test user settings' { 116 $result = InvokeWinGetDSC -Name WinGetUserSettings -Method Test -Property @{ Settings = $userSettingsHash } 117 $result.InDesiredState | Should -Be $false 118 } 119 120 It 'Set user settings' { 121 InvokeWinGetDSC -Name WinGetUserSettings -Method Set -Property @{ Settings = $userSettingsHash } 122 123 # Verify user settings were applied. 124 $result = InvokeWinGetDSC -Name WinGetUserSettings -Method Get -Property @{ Settings = $userSettingsHash } 125 $userSettings = $result.Settings 126 $userSettings.experimentalFeatures.directMSI | Should -Be $true 127 $userSettings.installBehavior.Preferences.Scope | Should -Be 'User' 128 } 129 } 130 131 Describe 'WinGetSource' { 132 BeforeAll { 133 $testSourceName = 'TestSource' 134 $testSourceArg = 'https://localhost:5001/TestKit/' 135 $testSourceType = 'Microsoft.PreIndexed.Package' 136 137 InvokeWinGetDSC -Name WinGetSource -Method Set -Property @{ Ensure = 'Absent'; Name = $testSourceName } 138 } 139 140 It 'Get WinGet source' { 141 $result = InvokeWinGetDSC -Name WinGetSource -Method Get -Property @{ Name = $testSourceName } 142 $result.Ensure | Should -Be 'Absent' 143 } 144 145 It 'Test WinGet source' { 146 $result = InvokeWinGetDSC -Name WinGetSource -Method Test -Property @{ Ensure='Present'; Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType } 147 $result.InDesiredState | Should -Be $false 148 } 149 150 It 'Set WinGet source' { 151 InvokeWinGetDSC -Name WinGetSource -Method Set -Property @{ Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType; TrustLevel = 'Trusted'; Explicit = $true } 152 153 $result = InvokeWinGetDSC -Name WinGetSource -Method Test -Property @{ Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType } 154 $result.InDesiredState | Should -Be $true 155 156 $result = InvokeWinGetDSC -Name WinGetSource -Method Get -Property @{ Name = $testSourceName } 157 $result.Name | Should -Be $testSourceName 158 $result.Type | Should -Be $testSourceType 159 $result.Argument | Should -Be $testSourceArg 160 $result.TrustLevel | Should -Be 'Trusted' 161 $result.Explicit | Should -Be $true 162 } 163 } 164 165 Describe 'WinGetPackage' { 166 BeforeAll { 167 $testSourceName = 'TestSource' 168 $testSourceArg = 'https://localhost:5001/TestKit/' 169 $testSourceType = 'Microsoft.PreIndexed.Package' 170 171 $testPackageId = 'AppInstallerTest.TestExeInstaller' 172 $testPackageVersion = '1.0.0.0' 173 174 InvokeWinGetDSC -Name WinGetSource -Method Set -Property @{ Name = $testSourceName; Argument = $testSourceArg; Type = $testSourceType; TrustLevel = 'Trusted' } 175 } 176 177 It 'Get WinGetPackage' { 178 $result = InvokeWinGetDSC -Name WinGetPackage -Method Get -Property @{ Id = $testPackageId; Version = $testPackageVersion } 179 $result.Ensure | Should -Be 'Absent' 180 } 181 182 It 'Test WinGetPackage' { 183 $result = InvokeWinGetDSC -Name WinGetPackage -Method Test -Property @{ Id = $testPackageId; Version = $testPackageVersion } 184 $result.InDesiredState | Should -Be $false 185 } 186 187 It 'Install WinGetPackage' { 188 InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Id = $testPackageId; Version = $testPackageVersion } 189 190 # Verify package installed. 191 $result = InvokeWinGetDSC -Name WinGetPackage -Method Get -Property @{ Id = $testPackageId; Version = $testPackageVersion } 192 $result.Ensure | Should -Be 'Present' 193 $result.UseLatest | Should -Be $false 194 $result.Version | Should -Be $testPackageVersion 195 } 196 197 It 'Update WinGetPackage' { 198 $testResult = InvokeWinGetDSC -Name WinGetPackage -Method Test -Property @{ Id = $testPackageId; UseLatest = $true } 199 $testResult.InDesiredState | Should -Be $false 200 201 InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Id = $testPackageId; UseLatest = $true } 202 203 # Verify package updated. 204 $result = InvokeWinGetDSC -Name WinGetPackage -Method Get -Property @{ Id = $testPackageId; UseLatest = $true } 205 $result.Ensure | Should -Be 'Present' 206 $result.UseLatest | Should -Be $true 207 $result.Version | Should -Not -Be $testPackageVersion 208 } 209 210 It 'Uninstall WinGetPackage' { 211 InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Id = $testPackageId; UseLatest = $true } 212 213 $testResult = InvokeWinGetDSC -Name WinGetPackage -Method Test -Property @{ Ensure = 'Absent'; Id = $testPackageId } 214 $testResult.InDesiredState | Should -Be $false 215 216 InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Ensure = 'Absent'; Id = $testPackageId } 217 218 # Verify package uninstalled. 219 $result = InvokeWinGetDSC -Name WinGetPackage -Method Get -Property @{ Ensure = 'Absent'; Id = $testPackageId } 220 $result.Ensure | Should -Be 'Absent' 221 } 222 223 AfterAll { 224 InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Ensure = 'Absent'; Id = $testPackageId} 225 } 226 } 227 228 Describe 'WinGetPackageManager' { 229 It 'Get WinGet version' { 230 $result = InvokeWinGetDSC -Name WinGetPackageManager -Method Get -Property @{} 231 $result.Version | Should -Not -Be $null 232 } 233 234 It 'Test WinGet version' { 235 $result = InvokeWinGetDSC -Name WinGetPackageManager -Method Test -Property @{ Version = "1.2.3.4" } 236 $result.InDesiredState | Should -Be $false 237 238 $currentVersion = Get-WinGetVersion 239 $result = InvokeWinGetDSC -Name WinGetPackageManager -Method Test -Property @{ Version = $currentVersion } 240 $result.InDesiredState | Should -Be $true 241 } 242 243 # TODO: Add test to verify Set method for WinGetPackageManager 244 }