Test/PackageManagementSource/OneGetSource.Get.Set.Test.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# Copyright (c) Microsoft Corporation.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
$CurrentDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path

.  "$CurrentDirectory\..\OneGetTestHelper.ps1"

#Calling the setup function
SetupOneGetSourceTest

Describe -Name  "PackageManagementSource Get.Set.Test-TargetResource Basic Test" -Tags "BVT" {

    BeforeEach {

        #Unregister the source if already registered
        UnRegisterSource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath
    }

    AfterEach {
    }     
    
    Context "PackageManagementSource Get.Set.Test-TargetResource Basic Test" {

        It "Get.Set.Test-TargetResource: Check Present" {
            
            #Register the package source
            MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Ensure Present -Verbose

            #Test it to make sure Set-TargetResource is successfully register the source
            $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Verbose

            $testResult | should be $true

            #Validate the returned Get results
            $getResult = MSFT_PackageManagementSource\Get-TargetResource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Verbose

            $getResult.Ensure | should be "Present"
            $getResult.Name | should be "MyNuget"
            $getResult.SourceUri | should be $LocalRepositoryPath
            $getResult.InstallationPolicy | should be "Untrusted"  #default is untrusted
            $getResult.Providername | should be "Nuget"  
        }

        
        It "Get.Set.Test-TargetResource: Check Absent" {
            
 
            #Test it to make sure the source is unregistered
            $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Verbose

            $testResult | should be $false

            #Validate the returned Get results
            $getResult = MSFT_PackageManagementSource\Get-TargetResource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Verbose

            $getResult.Ensure | should be "Absent"
            $getResult.Name | should be "MyNuget"
            $getResult.Providername | should be "Nuget" 
            $getResult.SourceUri | should BeNullOrEmpty
            $getResult.InstallationPolicy | should BeNullOrEmpty 
        }

     
        It "Get.Set.Test-TargetResource with the multiple Sources" {

            #Unregister the source if already registered
            UnRegisterSource -Name "MyNuget1" -providerName "Nuget" -SourceUri $LocalRepositoryPath1
            UnRegisterSource -Name "MyNuget2" -providerName "Nuget" -SourceUri $LocalRepositoryPath2

            Try
            {
                #Register the package source
                MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Ensure Present -Verbose
                MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget1" -providerName "Nuget" -SourceUri $LocalRepositoryPath1 -Ensure Present -Verbose
                MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget2" `
                                                     -providerName "Nuget" `
                                                     -SourceUri $LocalRepositoryPath2 `
                                                     -Ensure Present `
                                                     -InstallationPolicy Trusted `
                                                     -Verbose                                                 

            
                $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" `
                                                     -providerName "Nuget"`
                                                     -SourceUri $LocalRepositoryPath `
                                                     -InstallationPolicy Trusted `
                                                     -Verbose
                                                    
                #We registered a source with untrusted installation policy but test-targetresource uses trusted, so it's a false
                $testResult | should be $false

                $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" `
                                                     -providerName "Nuget"`
                                                     -SourceUri $LocalRepositoryPath1 `
                                                     -InstallationPolicy Untrusted `
                                                     -Verbose
                                                    
                #We registered a source with $LocalRepositoryPath but test-targetresource uses $LocalRepositoryPath1 , so it's a false
                $testResult | should be $false

                $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget2" `
                                                     -providerName "Nuget"`
                                                     -SourceUri $LocalRepositoryPath2 `
                                                     -InstallationPolicy Trusted `
                                                     -Verbose
                                                    
                # The properties in Test and Set all match, should return true
                $testResult | should be $true
            }
            finally
            {
                #Unregister the source if already registered
                UnRegisterSource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath
                UnRegisterSource -Name "MyNuget1" -providerName "Nuget" -SourceUri $LocalRepositoryPath1
                UnRegisterSource -Name "MyNuget2" -providerName "Nuget" -SourceUri $LocalRepositoryPath2
            }
        }
       
               
        It "Get.Set.Test-TargetResource with SourceCredential: Check Registered" {
           
            $credential = (CreateCredObject -Name ".\Administrator" -PSCode "MassRules!")

            MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget" `
                                                     -providerName "Nuget" `
                                                     -SourceUri $LocalRepositoryPath `
                                                     -Ensure Present `
                                                     -InstallationPolicy Trusted `
                                                     -SourceCredential $credential `
                                                     -Verbose  


            # Validate the package is installed
            $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" `
                                                     -providerName "Nuget" `
                                                     -SourceUri $LocalRepositoryPath `
                                                     -Ensure Present `
                                                     -InstallationPolicy Trusted `
                                                     -SourceCredential $credential `
                                                     -Verbose 
                                                    
            # The properties in Test and Set all match, should return true
            $testResult | should be $true

        }

        It "Set-TargetResource to change installationpolicy from untrusted to trusted: Check Installed" {
            
            #Register the package source
            MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Ensure Present -InstallationPolicy Untrusted -Verbose 

            #Test it to make sure Set-TargetResource is successfully unregister the source
            $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath

            $testResult | should be $true
                    
            
            #register it with the same name but different source uri
            MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Ensure Present -InstallationPolicy Trusted -Verbose 
            
            $testResult = MSFT_PackageManagementSource\Test-TargetResource -Name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -InstallationPolicy Trusted

            $testResult | should be $true
                          
        }
        
    }#context
   
    Context "PackageManagementSource Get.Set.Test-TargetResource Error Case" {  

        It "Get-TargetResource to unregistered a source that does not exist: Check Error" {

            try
            {
                MSFT_PackageManagementSource\Set-TargetResource -name "MyNuget" -providerName "Nuget" -SourceUri $LocalRepositoryPath -Ensure Absent -Verbose 2>&1
            }
            catch
            {
                $_.FullyQualifiedErrorId -ieq "UnRegisterFailed" | should be $true
                return
            }
            
            Throw "Expected Error 'UnRegisterFailed' does not happen"
        }

     } #context

}#Describe