|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +$modPath = "$psscriptroot/../PSGetTestUtils.psm1" |
| 5 | +Write-Verbose -Verbose -Message "PSGetTestUtils path: $modPath" |
| 6 | +Import-Module $modPath -Force -Verbose |
| 7 | + |
| 8 | +Describe "Test MAR Repository Registration" -tags 'CI' { |
| 9 | + BeforeEach { |
| 10 | + $MARName = Get-MARName |
| 11 | + $MARUri = Get-MARLocation |
| 12 | + $PSGalleryName = Get-PSGalleryName |
| 13 | + $PSGalleryUri = Get-PSGalleryLocation |
| 14 | + Get-NewPSResourceRepositoryFile |
| 15 | + } |
| 16 | + AfterEach { |
| 17 | + Get-RevertPSResourceRepositoryFile |
| 18 | + } |
| 19 | + |
| 20 | + Context "MAR is auto-registered with expected values" { |
| 21 | + It "MAR repository should be present with expected default values" { |
| 22 | + $res = Get-PSResourceRepository -Name $MARName |
| 23 | + $res | Should -Not -BeNullOrEmpty |
| 24 | + $res.Name | Should -Be $MARName |
| 25 | + $res.Uri | Should -Be "$MARUri/" |
| 26 | + $res.Trusted | Should -Be True |
| 27 | + $res.Priority | Should -Be 40 |
| 28 | + $res.ApiVersion | Should -Be 'ContainerRegistry' |
| 29 | + } |
| 30 | + |
| 31 | + It "MAR repository should have lower priority number (higher priority) than PSGallery" { |
| 32 | + $mar = Get-PSResourceRepository -Name $MARName |
| 33 | + $psGallery = Get-PSResourceRepository -Name $PSGalleryName |
| 34 | + $mar.Priority | Should -BeLessThan $psGallery.Priority |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + Context "MAR name protection" { |
| 39 | + It "should not allow registering MAR with -Name parameter" { |
| 40 | + { Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" |
| 41 | + } |
| 42 | + |
| 43 | + It "should not allow registering MAR (case insensitive) with -Name parameter" { |
| 44 | + { Register-PSResourceRepository -Name "mar" -Uri "https://mcr.microsoft.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" |
| 45 | + } |
| 46 | + |
| 47 | + It "should not allow registering MAR with -Name parameter in hashtable" { |
| 48 | + Unregister-PSResourceRepository -Name $MARName |
| 49 | + $hashtable = @{Name = "MAR"; Uri = "https://mcr.microsoft.com"} |
| 50 | + Register-PSResourceRepository -Repository $hashtable -ErrorVariable err -ErrorAction SilentlyContinue |
| 51 | + $err.Count | Should -BeGreaterThan 0 |
| 52 | + $err[0].FullyQualifiedErrorId | Should -BeExactly "MARProvidedAsNameRepoPSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" |
| 53 | + } |
| 54 | + |
| 55 | + It "should not allow setting Uri for MAR repository" { |
| 56 | + { Set-PSResourceRepository -Name $MARName -Uri "https://example.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" |
| 57 | + } |
| 58 | + |
| 59 | + It "should not allow setting CredentialInfo for MAR repository" { |
| 60 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 61 | + $credentialInfo = New-Object Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret) |
| 62 | + { Set-PSResourceRepository -Name $MARName -CredentialInfo $credentialInfo -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" |
| 63 | + } |
| 64 | + |
| 65 | + It "should allow setting Trusted for MAR repository" { |
| 66 | + Set-PSResourceRepository -Name $MARName -Trusted:$false |
| 67 | + $res = Get-PSResourceRepository -Name $MARName |
| 68 | + $res.Trusted | Should -Be False |
| 69 | + } |
| 70 | + |
| 71 | + It "should allow setting Priority for MAR repository" { |
| 72 | + Set-PSResourceRepository -Name $MARName -Priority 10 |
| 73 | + $res = Get-PSResourceRepository -Name $MARName |
| 74 | + $res.Priority | Should -Be 10 |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Context "Reset repository store includes MAR" { |
| 79 | + It "Reset-PSResourceRepository should register MAR alongside PSGallery" { |
| 80 | + Reset-PSResourceRepository -Force |
| 81 | + $res = Get-PSResourceRepository -Name $MARName |
| 82 | + $res | Should -Not -BeNullOrEmpty |
| 83 | + $res.Name | Should -Be $MARName |
| 84 | + $res.Uri | Should -Be "$MARUri/" |
| 85 | + $res.Trusted | Should -Be True |
| 86 | + $res.Priority | Should -Be 40 |
| 87 | + $res.ApiVersion | Should -Be 'ContainerRegistry' |
| 88 | + |
| 89 | + $psGallery = Get-PSResourceRepository -Name $PSGalleryName |
| 90 | + $psGallery | Should -Not -BeNullOrEmpty |
| 91 | + } |
| 92 | + |
| 93 | + It "Reset-PSResourceRepository should restore MAR after unregistration" { |
| 94 | + Unregister-PSResourceRepository -Name $MARName |
| 95 | + $res = Get-PSResourceRepository -Name $MARName -ErrorAction SilentlyContinue |
| 96 | + $res | Should -BeNullOrEmpty |
| 97 | + |
| 98 | + Reset-PSResourceRepository -Force |
| 99 | + $res = Get-PSResourceRepository -Name $MARName |
| 100 | + $res | Should -Not -BeNullOrEmpty |
| 101 | + $res.Name | Should -Be $MARName |
| 102 | + $res.Uri | Should -Be "$MARUri/" |
| 103 | + $res.Trusted | Should -Be True |
| 104 | + $res.Priority | Should -Be 40 |
| 105 | + } |
| 106 | + |
| 107 | + It "Reset-PSResourceRepository should restore both PSGallery and MAR" { |
| 108 | + Unregister-PSResourceRepository -Name $MARName |
| 109 | + Unregister-PSResourceRepository -Name $PSGalleryName |
| 110 | + Reset-PSResourceRepository -Force |
| 111 | + |
| 112 | + $mar = Get-PSResourceRepository -Name $MARName |
| 113 | + $mar | Should -Not -BeNullOrEmpty |
| 114 | + $mar.Priority | Should -Be 40 |
| 115 | + $mar.Trusted | Should -Be True |
| 116 | + $mar.ApiVersion | Should -Be 'ContainerRegistry' |
| 117 | + |
| 118 | + $psGallery = Get-PSResourceRepository -Name $PSGalleryName |
| 119 | + $psGallery | Should -Not -BeNullOrEmpty |
| 120 | + $psGallery.Priority | Should -Be 50 |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Context "MAR first due to higher priority" { |
| 125 | + It "Find-PSResource Az.Accounts module from MAR" { |
| 126 | + $res = Find-PSResource -Name "Az.Accounts" |
| 127 | + $res | Should -Not -BeNullOrEmpty |
| 128 | + $res.Name | Should -Be "Az.Accounts" |
| 129 | + $res.Repository | Should -Be $MARName |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments