Test/PackageManagement/PackageManagement.Get.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
#
# 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 SetupPackageManagementTest
#
 
# Calling the setup function
SetupPackageManagementTest

$AdditionalParameters = @{"Destination" = $DestinationPath}
$AdditionalParameterCimInstanceArray = ConvertHashtableToArryCimInstance $AdditionalParameters

Describe -Name "PackageManagement Get-TargetResource Basic Test" -Tags "BVT" {

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

    AfterEach {
 
    }     

    Context "PackageManagement Get-TargetResource BVT" {

        #Mock Set-TargetResource/PackageManagement DSC Resource. The tests under this context use the below mock function
        
        Mock Set-TargetResource  {

            #Nuget package folder name format: MyTestPackage.12.0.1, i.e., name + version
            $package = $name + "." + $RequiredVersion+ ".nupkg"

            #MyTestPackage.12.0.1.1
            $path = "$($DestinationPath)\$($name).$($RequiredVersion)" 

            if ($Ensure -ieq "Present") {
         
                if (!(Test-Path -path $path)) {New-Item $path -Type Directory}


                #Copy the $package folder to your destination folder
                Copy-Item -Path "$($LocalRepositoryPath)\$package" -Destination "$($path)" -Recurse -Force
            }
            else {

                #Delete the $package folder
                Remove-Item "$($path)\$package" -Recurse -Force  -ErrorAction SilentlyContinue
            }            
        }

        It "Get-TargetResource with the Mandatory Parameters: Check Absent" {

            # Calling Get-TargetResource in the NugetPackage resource
            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -Verbose

            # Validate the result
            $result.Ensure | should be "Absent"
        }

        It "Get-TargetResource with the Mandatory Parameters: Check Present" {
         
            Set-TargetResource -name "MyTestPackage" -RequiredVersion "12.0.1" -Ensure "Present" -AdditionalParameters $AdditionalParameterCimInstanceArray -Verbose

            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray  -ErrorVariable ev

            #Validate the returned results
            $result.Ensure | should be "Present"
            $result.Name | should be "MyTestPackage"
            $result.ProviderName | should be "NuGet"
            $result.RequiredVersion | should be "12.0.1"
            ($result.Source).StartsWith($DestinationPath) | should be $true
        }

        It "Get-TargetResource with RequiredVersion: Check Present" {
            
            Set-TargetResource -name "MyTestPackage" -RequiredVersion "12.0.1" -Ensure "Present" -AdditionalParameters $AdditionalParameterCimInstanceArray -Verbose

            #provide a req version that exists, expect ensure=Present
            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "12.0.1" -ErrorVariable ev

            #Validate the returned results
            $result.Ensure | should be "Present"
            $result.Name | should be "MyTestPackage"
            $result.RequiredVersion | should be "12.0.1"    
        }

        It "Get-TargetResource with Non-exist RequiredVersion: Check Absent" {
            
            Set-TargetResource -name "MyTestPackage" -RequiredVersion "12.0.1" -Ensure "Present" -Verbose -AdditionalParameters $AdditionalParameterCimInstanceArray

            #Provide a req version does not exist, expect Ensure=Absent
            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -RequiredVersion "10.11.12" -ErrorVariable ev -AdditionalParameters $AdditionalParameterCimInstanceArray

            #Validate the returned results
            $result.Ensure | should be "Absent"  
        }

        It "Get-TargetResource with MaximumVersion: Check Present" {
            
            Set-TargetResource -name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "12.0.1.1" -Ensure "Present" -Verbose
            Set-TargetResource -name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "12.0.1" -Ensure "Present" -Verbose
            Set-TargetResource -name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "15.2.1" -Ensure "Present" -Verbose

            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray  -MaximumVersion "19.9" -ErrorVariable ev

            $result.Ensure | should be "Present"
            $result.RequiredVersion | should be "15.2.1"  #1.8.2 is the only package -le maximumversion 1.9.9
        }

        It "Get-TargetResource MinimumVersion: Check Present" {
            
            Set-TargetResource -name "MyTestPackage" -RequiredVersion "12.0.1.1" -Ensure "Present" -Verbose -AdditionalParameters $AdditionalParameterCimInstanceArray
            Set-TargetResource -name "MyTestPackage" -RequiredVersion "12.0.1" -Ensure "Present" -Verbose -AdditionalParameters $AdditionalParameterCimInstanceArray
            Set-TargetResource -name "MyTestPackage" -RequiredVersion "15.2.1" -Ensure "Present" -Verbose -AdditionalParameters $AdditionalParameterCimInstanceArray

            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray  -MinimumVersion "12.0.1"

            $result.Ensure | should be "Present"
            $result.RequiredVersion | should be "15.2.1"  #Get-package will return the latest version
        }

        It "Get-TargetResource MinimumVersion and MaximumVersion: Check Present" {
            
            Set-TargetResource -name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "12.0.1.1" -Ensure "Present" -Verbose
            Set-TargetResource -name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "12.0.1" -Ensure "Present" -Verbose
            Set-TargetResource -name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray -RequiredVersion "15.2.1" -Ensure "Present" -Verbose

            #will return the latest, ie 15.2.1
            $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray  -MinimumVersion "15.0"  -MaximumVersion "19.0"

            $result.Ensure | should be "Present"
            $result.RequiredVersion | should be "15.2.1"  
        }
        
    }#context
}#Describe

Describe -Name "PackageManagement Get-Dscconfiguration Error Cases" -Tags "RI" {


    BeforeEach {

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

    AfterEach {

    }

    #Mock Set-TargetResource/NugetPackage DSC Resource
    Mock Set-TargetResource  {

        #Nuget package folder name format: MyTestPackage.12.0.1, i.e., name + version
        $package = $Name + "." + $RequiredVersion

        if ($Ensure -ieq "Present") {

            #Copy the $package folder to your destination folder
            Copy-Item -Path "$($LocalRepositoryPath)\$package" -Destination "$($DestinationPath)\$package" -Recurse -Force
        }
        else {

            #Delete the $package folder
            Remove-Item "$($DestinationPath)\$package" -Recurse -Force -ErrorAction SilentlyContinue
        }
            
    }

    It "Get-TargetResource with Max, Req and Min Verion: Check Error" {

       $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray  `
                           -MinimumVersion "2.1.3" -RequiredVersion "1.1.1" -MaximumVersion "2.3.5" `
                           -ErrorVariable ev

      ($ev -ne $null) | should be $true
      $ev[0].FullyQualifiedErrorId | should be "VersionRangeAndRequiredVersionCannotBeSpecifiedTogether,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage"
    }
    
    It "Get-TargetResource with Max and Min Verion: Check Error" {
    
      $result = MSFT_PackageManagement\Get-TargetResource -Name "MyTestPackage" -AdditionalParameters $AdditionalParameterCimInstanceArray  `
                          -MinimumVersion "5.0" -MaximumVersion "2.5" `
                          -ErrorVariable ev

      ($ev -ne $null) | should be $true
      $ev[0].FullyQualifiedErrorId | should be "NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage"
    }
}