|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +$modPath = "$psscriptroot/../PSGetTestUtils.psm1" |
| 5 | +Import-Module $modPath -Force -Verbose |
| 6 | + |
| 7 | +$psmodulePaths = $env:PSModulePath -split ';' |
| 8 | +Write-Verbose -Verbose "Current module search paths: $psmodulePaths" |
| 9 | + |
| 10 | + |
| 11 | +Describe 'Test Find-PSResource for MAR Repository' -tags 'Release' { |
| 12 | + BeforeAll { |
| 13 | + Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry" |
| 14 | + } |
| 15 | + |
| 16 | + AfterAll { |
| 17 | + Unregister-PSResourceRepository -Name "MAR" |
| 18 | + } |
| 19 | + |
| 20 | + It "Should find resource with wildcard in Name" { |
| 21 | + $res = Find-PSResource -Name "Az.App*" -Repository "MAR" |
| 22 | + $res | Should -Not -BeNullOrEmpty |
| 23 | + $res.Count | Should -BeGreaterThan 1 |
| 24 | + } |
| 25 | + |
| 26 | + It "Should find all resource with wildcard in Name" { |
| 27 | + $res = Find-PSResource -Name "*" -Repository "MAR" |
| 28 | + $res | Should -Not -BeNullOrEmpty |
| 29 | + $res.Count | Should -BeGreaterThan 1 |
| 30 | + } |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +Describe 'Test Find-PSResource for searching and looping through repositories' -tags 'Release' { |
| 36 | + |
| 37 | + BeforeAll{ |
| 38 | + $testModuleName = "test_module" |
| 39 | + $testModuleName2 = "test_module2" |
| 40 | + $testCmdDSCParentPkg = "myCmdDSCModule" |
| 41 | + $testScriptName = "test_script" |
| 42 | + |
| 43 | + $tag1 = "CommandsAndResource" |
| 44 | + $tag2 = "Tag-Required-Script1-2.5" |
| 45 | + |
| 46 | + $cmdName = "Get-TargetResource" |
| 47 | + $dscName = "SystemLocale" |
| 48 | + $tagsEscaped = @("'$tag1'", "'PSCommand_$cmdName'", "'PSDscResource_$dscName'") |
| 49 | + |
| 50 | + $cmdName2 = "Get-MyCommand" |
| 51 | + $dscName2 = "MyDSCResource" |
| 52 | + $tagsEscaped2 = @("'PSCommand_$cmdName2'", "'PSDscResource_$dscName2'") |
| 53 | + |
| 54 | + $PSGalleryName = "PSGallery" |
| 55 | + $NuGetGalleryName = "NuGetGallery" |
| 56 | + $localRepoName = "localRepo" |
| 57 | + |
| 58 | + Get-NewPSResourceRepositoryFile |
| 59 | + |
| 60 | + $localRepoUriAddress = Join-Path -Path $TestDrive -ChildPath "testdir" |
| 61 | + $null = New-Item $localRepoUriAddress -ItemType Directory -Force |
| 62 | + Register-PSResourceRepository -Name $localRepoName -Uri $localRepoUriAddress |
| 63 | + |
| 64 | + New-TestModule -moduleName $testModuleName -repoName localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags $tagsEscaped |
| 65 | + New-TestModule -moduleName $testCmdDSCParentPkg -repoName localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags $tagsEscaped2 |
| 66 | + } |
| 67 | + |
| 68 | + AfterAll { |
| 69 | + Get-RevertPSResourceRepositoryFile |
| 70 | + } |
| 71 | + |
| 72 | + It "find resources from all pattern matching repositories where it exists (-Repository with wildcard)" { |
| 73 | + # Package with CommandName "Get-TargetResource" exists in the following repositories: PSGallery, localRepo |
| 74 | + $res = Find-PSResource -CommandName $cmdName -Repository "*Gallery" -ErrorVariable err -ErrorAction SilentlyContinue |
| 75 | + $err | Should -HaveCount 0 |
| 76 | + $res.Count | Should -BeGreaterOrEqual 9 |
| 77 | + |
| 78 | + $pkgFoundFromLocalRepo = $false |
| 79 | + $pkgFoundFromPSGallery = $false |
| 80 | + |
| 81 | + foreach ($pkg in $res) |
| 82 | + { |
| 83 | + if ($pkg.ParentResource.Repository -eq $localRepoName) |
| 84 | + { |
| 85 | + $pkgFoundFromLocalRepo = $true |
| 86 | + } |
| 87 | + elseif ($pkg.ParentResource.Repository -eq $PSGalleryName) |
| 88 | + { |
| 89 | + $pkgFoundFromPSGallery = $true |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + $pkg.Names | Should -Be $cmdName |
| 94 | + $pkg.ParentResource.Includes.Command | Should -Contain $cmdName |
| 95 | + $pkgFoundFromLocalRepo | Should -BeFalse |
| 96 | + $pkgFoundFromPSGallery | Should -BeTrue |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'Release' { |
| 104 | + |
| 105 | + BeforeAll{ |
| 106 | + $PSGalleryName = Get-PSGalleryName |
| 107 | + $testModuleName = "test_module" |
| 108 | + $testScriptName = "test_script" |
| 109 | + $commandName = "Get-TargetResource" |
| 110 | + $dscResourceName = "SystemLocale" |
| 111 | + $parentModuleName = "SystemLocaleDsc" |
| 112 | + Get-NewPSResourceRepositoryFile |
| 113 | + } |
| 114 | + |
| 115 | + AfterAll { |
| 116 | + Get-RevertPSResourceRepositoryFile |
| 117 | + } |
| 118 | + |
| 119 | + It "find resource given CommandName" { |
| 120 | + $res = Find-PSResource -CommandName $commandName -Repository $PSGalleryName |
| 121 | + $res | Should -Not -BeNullOrEmpty |
| 122 | + foreach ($item in $res) { |
| 123 | + $item.Names | Should -Be $commandName |
| 124 | + $item.ParentResource.Includes.Command | Should -Contain $commandName |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + It "find resource given DscResourceName" { |
| 129 | + $res = Find-PSResource -DscResourceName $dscResourceName -Repository $PSGalleryName |
| 130 | + $res | Should -Not -BeNullOrEmpty |
| 131 | + foreach ($item in $res) { |
| 132 | + $item.Names | Should -Be $dscResourceName |
| 133 | + $item.ParentResource.Includes.DscResource | Should -Contain $dscResourceName |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments