Test/PackageManagement/PackageManagement.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
#
# 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"

if (-not (IsAdmin))
{
  throw "This test script requires to be run from an elevated PowerShell session. Launch an elevated PowerShell session and try again."
}

#
# Pre-Requisite: MyTestModule 1.1, 1.1.2, 3.2.1 modules are available under the $LocalRepositoryPath for testing purpose only.
# It's been taken care of by SetupPackageManagementTest
#
 
# Calling the setup function
SetupPackageManagementTest -SetupPSModuleRepository
 
Describe -Name  "PackageManagement Set-TargetResource Basic Test" -Tags "BVT" {

    BeforeAll {
        $script:OriginalRepository = CleanupRepository
    }
 
    BeforeEach {

        #Remove all left over files if exists
        Remove-Item "$PSModuleBase\MyTestModule" -Recurse -Force  -ErrorAction SilentlyContinue      
        Remove-Item "$PSModuleBase\MyTestModule" -Recurse -Force  -ErrorAction SilentlyContinue     
    }

    AfterEach {
        
    }

    AfterAll {
        # Remove all left over files if exists
        Remove-Item "$PSModuleBase\MyTestModule" -Recurse -Force  -ErrorAction SilentlyContinue      
        Remove-Item "$PSModuleBase\MyTestModule" -Recurse -Force  -ErrorAction SilentlyContinue     
     
        RestoreRepository $script:OriginalRepository
    }

    Context "PackageManagement Set-TargetResource Basic Test" {

        It "Set, Test-TargetResource with Trusted Source, No Versions Specified: Check Installed" {
           
            #Register a local module repository to make the test run faster
            RegisterRepository -Name "LocalRepository" -InstallationPolicy Trusted -Ensure Present

            # 'BeforeEach' removes all specific modules under the $module path, so it is expected Set-Target* should success in the installation
            MSFT_PackageManagement\Set-TargetResource -name "MyTestModule" -Source $LocalRepository  -Ensure Present -Verbose

            # Validate the module is installed
            Test-Path -Path "$PSModuleBase\MyTestModule\3.2.1" | should be $true
            
            # Uninstalling the module
            MSFT_PackageManagement\Set-TargetResource -name "MyTestModule" -Source $LocalRepository  -Ensure Absent -Verbose

            # Validate the module is uninstalled
            $result = MSFT_PackageManagement\Test-TargetResource -name "MyTestModule" -Source $LocalRepository  -Ensure Absent
            $result| should be $true

            Test-Path -Path "$PSModuleBase\MyTestModule\3.2.1" | should be $false
        }

        It "Set, Test-TargetResource with Trusted Source, No respository Specified: Check Installed" {
           
            #Register a local module repository to make the test run faster
            RegisterRepository -Name "LocalRepository" -InstallationPolicy Trusted -Ensure Present

            # 'BeforeEach' removes all specific modules under the $module path, so it is expected Set-Target* should success in the installation
            MSFT_PackageManagement\Set-TargetResource -name "MyTestModule" -Ensure Present -Verbose

            # Validate the module is installed
            Test-Path -Path "$PSModuleBase\MyTestModule\3.2.1" | should be $true

            # Uninstalling the module
            MSFT_PackageManagement\Set-TargetResource -name "MyTestModule" -Ensure Absent -Verbose

            # Validate the module is uninstalled
            $result = MSFT_PackageManagement\Test-TargetResource -name "MyTestModule" -Ensure Absent

            $result| should be $true
        }

        It "Set, Test-TargetResource with multiple sources and versions of a modules: Check Installed" {
           
            # Registering multiple source

            $returnVal = $null

            try
            {
                $returnVal = CleanupRepository
                
                RegisterRepository -Name "LocalRepository1" -InstallationPolicy Untrusted -Ensure Present -SourceLocation $LocalRepositoryPath1 -PublishLocation $LocalRepositoryPath1

                RegisterRepository -Name "LocalRepository2" -InstallationPolicy Trusted -Ensure Present -SourceLocation $LocalRepositoryPath2 -PublishLocation $LocalRepositoryPath2

                RegisterRepository -Name "LocalRepository3" -InstallationPolicy Untrusted -Ensure Present -SourceLocation $LocalRepositoryPath3 -PublishLocation $LocalRepositoryPath3
                
                # User's installation policy is untrusted
                MSFT_PackageManagement\Set-TargetResource -name "MyTestModule" -Ensure "Present" -Verbose -Source "LocalRepository2"

                # The module from the trusted source should be installed
                Get-InstalledModule MyTestModule | % Repository | should be "LocalRepository2"
            }
            finally
            {
                RestoreRepository -RepositoryInfo $returnVal
                # Unregistering the repository sources
            
                RegisterRepository -Name "LocalRepository1" -Ensure Absent -SourceLocation $LocalRepositoryPath1 -PublishLocation $LocalRepositoryPath1

                RegisterRepository -Name "LocalRepository2" -Ensure Absent -SourceLocation $LocalRepositoryPath2 -PublishLocation $LocalRepositoryPath2

                RegisterRepository -Name "LocalRepository3" -Ensure Absent -SourceLocation $LocalRepositoryPath3 -PublishLocation $LocalRepositoryPath3
            }
        }  
                    
    }#context

    
    Context "PackageManagement Set-TargetResource Error Cases" {

        #Register a local module repository to make the test run faster
        RegisterRepository -Name "LocalRepository" -InstallationPolicy Trusted -Ensure Present

        It "Set-TargetResource with module not found for the install: Check Error" {

            try
            {
                # The module does not exist
                MSFT_PackageManagement\Set-TargetResource -name "NonExistModule" -Ensure Present -ErrorAction SilentlyContinue  2>&1
            }
            catch
            {
                #Expect fail to install.
                $_.FullyQualifiedErrorId | should be "NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage"
                return
            }
   
            Throw "Expected 'ModuleNotFoundInRepository' exception did not happen"  
        }

        
        It "Set , Test-TargetResource: Check Absent and False" {

            # Calling Set-TargetResource to uninstall the MyTestModule module
            try
            {
                MSFT_PackageManagement\Set-TargetResource -name "MyTestModule" -Source $LocalRepository -RequiredVersion "1.1.2" -Ensure "Absent" -Verbose
            }
            catch
            {
                if ($_.FullyQualifiedErrorId -ieq "NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.UninstallPackage")
                {
                    #The module is not installed. Ignore the error
                }
                else
                {
                    throw
                }
            }

            # Calling Get-TargetResource in the PSModule resource
            $result = MSFT_PackageManagement\Test-TargetResource -Name "MyTestModule" -Source $LocalRepository -RequiredVersion "1.1.2"

            # Validate the result
            $result | should be $false

        }
       
    }#context
}#Describe