Test/NugetPackage/NugetPackage.Set.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#
# 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"

#
# Pre-Requisite: MyTestPackage.12.0.1.1, MyTestPackage.12.0.1, MyTestPackage.15.2.1 packages are available under the $LocalRepositoryPath.
# It's been taken care of by SetupNugetTest
#
 
# Calling the setup function
SetupNugetTest
 
Describe -Name  "NugetPackage Set-TargetResource Basic Test" -Tags "BVT" {

    BeforeEach {

        #Remove all left over files if exists
        Remove-Item "$($DestinationPath)" -Recurse -Force -ErrorAction SilentlyContinue

    }

    AfterEach {

    }

    Context "NugetPackage Set-TargetResource Basic Test" {


        It "Set-TargetResource with RequiredVersion: Check Installed & UnInstalled" {

            RegisterPackageSource -Name "NugetTestSourceName" -InstallationPolicy Trusted  -Ensure Present -SourceUri $LocalRepositoryPath

            $result = MSFT_NugetPackage\Set-TargetResource  -Name "MyTestPackage"  `
                                                            -DestinationPath $DestinationPath  `
                                                            -RequiredVersion "12.0.1" `
                                                            -Ensure Present

            # Validate the package is installed
            Test-Path -Path "$($DestinationPath)\MyTestPackage.12.0.1" | should be $true


            # Calling Set-TargetResource in the NugetPackage resource to uninstall it
            $result = MSFT_NugetPackage\Set-TargetResource  -Name "MyTestPackage"  `
                                                            -DestinationPath $DestinationPath  `
                                                            -RequiredVersion "12.0.1" `
                                                            -Ensure Absent

            # Package should not be there
            Test-Path -Path "$($DestinationPath)\MyTestPackage.12.0.1" | should be $false
        }

        It "Set-TargetResource with Trusted Source, No Versions Specified: Check Installed" {
           
            # Calling Set-TargetResource in the NugetPackage resource with trusted policy

             RegisterPackageSource -Name "NugetTestSourceName" -InstallationPolicy Trusted  -Ensure Present -SourceUri $LocalRepositoryPath

            # User's installation policy is untrusted by default
            $result = MSFT_NugetPackage\Set-TargetResource -Name "MyTestPackage" -DestinationPath $DestinationPath -Source "NugetTestSourceName"

            # Validate the package is installed. 2.1.3 is the latest in the local source

            Test-Path -Path "$($DestinationPath)\MyTestPackage.15.2.1" | should be $false
        }
    
 

        It "Set-TargetResource with untrusted Source and trusted user policy: Check Warning & Installed" {
           
            # Calling Set-TargetResource in the NugetPackage resource with untrusted policy

             RegisterPackageSource -Name "NugetTestSourceName" -Ensure Present -SourceUri $LocalRepositoryPath

            # User's installation policy is trusted
            $result = MSFT_NugetPackage\Set-TargetResource  -Name "MyTestPackage" `
                                                            -DestinationPath $DestinationPath `
                                                            -Source "NugetTestSourceName" `
                                                            -InstallationPolicy Trusted `
                                                            -WarningVariable wv


            if ($wv)
            {
                # Check the warning message
                $wv -imatch "untrusted repository"
                               
                # The package should be installed
                $result = MSFT_NugetPackage\Test-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath -Ensure "Present" -Source "NugetTestSourceName"
                
                $result| should be $true

                return
            }
           
            Throw "Expecting InstallationPolicyWarning but not happen" 
 
        }

               
        It "Set-TargetResource with mulitple sources containing the same package: Check Installed" {
           
            try
            {
                # registering multiple source
                RegisterPackageSource -Name "NugetTestSourceName10"   -Ensure Present -SourceUri $LocalRepositoryPath 
                RegisterPackageSource -Name "NugetTestSourceName20"   -Ensure Present -SourceUri $LocalRepositoryPath -InstallationPolicy Trusted
                RegisterPackageSource -Name "NugetTestSourceName30"   -Ensure Present -SourceUri $LocalRepositoryPath 

                # User's installation policy is untrusted
                $result = MSFT_NugetPackage\Set-TargetResource -Name "MyTestPackage" -DestinationPath $DestinationPath

                # The package should be installed
                MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $destinationPath -Source "NugetTestSourceName20" | should be $true
            }
            finally
            {
                #unregister them
                RegisterPackageSource -Name "NugetTestSourceName10"   -Ensure Absent -SourceUri $LocalRepositoryPath 
                RegisterPackageSource -Name "NugetTestSourceName20"   -Ensure Absent -SourceUri $LocalRepositoryPath -InstallationPolicy Trusted
                RegisterPackageSource -Name "NugetTestSourceName30"   -Ensure Absent -SourceUri $LocalRepositoryPath 
            }

        }
       
        It "Set-TargetResource with SourceCredential: Check Installed" {
           
            $credential = (CreateCredObject -Name ".\Administrator" -PSCode "MassRules!")

            # Calling Set-TargetResource in the NugetPackage resource with SourceCredential
            $result = MSFT_NugetPackage\Set-TargetResource  -Name "MyTestPackage" `
                                                            -DestinationPath $DestinationPath `
                                                            -Source $LocalRepositoryPath `
                                                            -SourceCredential $credential `
                                                            -InstallationPolicy Trusted

            # Validate the package is installed
            MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $destinationPath -Source "NugetTestSourceName" | should be $true
        }
        
    }#context

}#Describe

Describe -Name "NugetPackage Set-TargetResource Error Cases" -Tags "RI" {

    BeforeEach {

        #Remove all left over files if exists
        Remove-Item "$($DestinationPath)" -Recurse -Force -ErrorAction SilentlyContinue

        RegisterPackageSource -Name "NugetTestSourceName" -Ensure Present -SourceUri $LocalRepositoryPath -InstallationPolicy Trusted
    }

    AfterEach {
        RegisterPackageSource -Name "NugetTestSourceName" -Ensure Absent -SourceUri $LocalRepositoryPath
    }


    Context "NugetPackage Set-TargetResource Error Cases" {
            
        It "Set-TargetResource with package not found for the install: Check Error" {

            #every slow need mock
            try
            {
                # None-exist package for install
                $result = MSFT_NugetPackage\Set-TargetResource -Name "MyTestPackageyyyy" -DestinationPath $DestinationPath -Ensure Present -ErrorAction SilentlyContinue
                
            }
            Catch
            {
                #Expect fail to install.
                $_.FullyQualifiedErrorId | should be "PackageNotFoundInRepository"
                return
            }
   
            Throw "Expecting PackageNotFoundInRepository but not happen" 
        }

          
        It "Set-TargetResource with package not found for the uninstall: Check Error" {
           
            # Create a folder that is mimicking the package is installed
            if (-not (Test-Path -Path $DestinationPath))
            {
                New-Item -Path $DestinationPath
            } 

            try
            {
                # None-exist package for uninstall
                $result = MSFT_NugetPackage\Set-TargetResource -Name "MyTestPackageyyyy" -DestinationPath $DestinationPath -Ensure Absent -ErrorAction SilentlyContinue
            }
            Catch
            {
                #Expect fail to install.
                $_.FullyQualifiedErrorId | should be "PackageNotFound"
                return
            } 
            
            Throw "Expecting PackageNotFound but not happen"  
        }                   
     
        It "Set-TargetResource with Untrusted User InstallationPolicy and Source: Check Error" {
            
            # No install will happen if both user and source are untrusted

            RegisterPackageSource -Name "NugetTestSourceName"  -Ensure Present -SourceUri $LocalRepositoryPath

            Try
            {
                # User's installation policy is untrusted.
                $result = MSFT_NugetPackage\Set-TargetResource -Name "MyTestPackage" `
                                                                -DestinationPath $DestinationPath `
                                                                -Source "NugetTestSourceName" `
                                                                -InstallationPolicy Untrusted `
                                                                -ErrorVariable ev

            }
            Catch
            {
                #Expect fail to install.
                $_.FullyQualifiedErrorId | should be "InstallationPolicyFailed"
                return
            }

            
            Throw "Expecting InstallationPolicyFailed but not happen"            
        } 

    }#context
}#Describe