Test/NugetPackage/NugetPackage.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
#
# 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 Test-TargetResource Basic Tests" -Tags "BVT"{

    BeforeEach {

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

    AfterEach {

    }

      
    Context "NugetPackage Test-TargetResource with Mandatory Parameters" {

       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 "Test-TargetResource: Check False" {

            # Because 'BeforeEach' removes all packages, there is no package left in the $DestinationPath.
            # It is expected Test-Target* returns false for ensure='Present'
            #
            # Calling Test-TargetResource in the NugetPackage resource
            $result = MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $DestinationPath 

            # Validate the result
            $result | should be $false
        }

        It "Test-TargetResource: Check True" {

            # Calling Test-TargetResource in the NugetPackage resource
            $result = MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $DestinationPath -Ensure Absent 

            # Validate the result
            $result | should be $true
        }

        It "Test-TargetResource with RequiredVersion: Check True" {
            
            Set-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath  -RequiredVersion "12.0.1" -Ensure "Present" -Verbose

            $result = MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $destinationPath -RequiredVersion "12.0.1" 

            #Validate the returned results
            $result | should be $true
        }

        
        It "Test-TargetResource with InstalledVersion 12.0.1 but RequiredVersion 12.0.1.1: Check False" {
            
            Set-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath  -RequiredVersion "12.0.1" -Ensure "Present" -Verbose

            #The requiredVersion does not exist, expect Ensure=Absent
            $result = MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $destinationPath -RequiredVersion "12.0.1.1" 

            #Validate the returned results
            $result | should be $false 
        }

        It "Test-TargetResource with InstalledVersion 12.0.1.1 but RequiredVersion 12.0.1: Check False" {
            
            Set-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath  -RequiredVersion "12.0.1.1" -Ensure "Present" -Verbose

            #Provide a req version does not exist, expect Ensure=Absent
            $result = MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $destinationPath -RequiredVersion "12.0.1" 

            #Validate the returned results
            $result | should be $false 
        }

        #Skip for the next WMF release for the bug fixes around MaximumVersion
        It -Skip "Test-TargetResource with MaximumVersion: Check True" {
            
            Set-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath -RequiredVersion "12.0.1.1" -Ensure "Present" -Verbose
            Set-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath -RequiredVersion "12.0.1" -Ensure "Present" -Verbose
            Set-TargetResource -name "MyTestPackage" -DestinationPath $DestinationPath -RequiredVersion "15.2.1" -Ensure "Present" -Verbose

            $result = MSFT_NugetPackage\Test-TargetResource -Name "MyTestPackage" -DestinationPath $destinationPath  -MaximumVersion "12.9.9"

            $result | should be $true
        }
    }#context

}#Describe