AzureArtifactsPowerShellModuleHelper.IntegrationTests.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# These are Integration tests (not unit tests).
# This means that these tests will actually reach out to the specified $FeedUrl and connect/authenticate against it.
# In order for these tests to run successfully:
# - You need to use a real Azure Artifacts $FeedUrl and a real module to import from it.
# Ideally we would mock out any external/infrastructure dependencies; I just haven't had time to yet so for now hit the real dependencies.

param
(
    [Parameter(Mandatory = $false, HelpMessage = 'The Personal Access Token to use to connect to the Azure Artifacts feed.')]
    [string] $AzureArtifactsPersonalAccessToken = 'YourPatGoesHereButDoNotCommitItToSourceControl'
)

Set-StrictMode -Version Latest
[string] $THIS_SCRIPTS_PATH = $PSCommandPath
[string] $moduleFilePathToTest = $THIS_SCRIPTS_PATH.Replace('.IntegrationTests.ps1', '.psm1') | Resolve-Path
Write-Verbose "Importing the module file '$moduleFilePathToTest' to run tests against it." -Verbose
Import-Module -Name $moduleFilePathToTest -Force
[string] $ModuleNameBeingTested = ((Split-Path -Path $moduleFilePathToTest -Leaf) -split '\.')[0] # Filename without the extension.

###########################################################
# You will need to update the following variables with info to pull a real package down from a real feed.
###########################################################
# [string] $FeedUrl = 'https://pkgs.dev.azure.com/Organization/_packaging/Feed/nuget/v2'
[string] $FeedUrl = 'https://pkgs.dev.azure.com/iqmetrix/_packaging/iqmetrix/nuget/v2'
[string] $PowerShellModuleName = 'IQ.DataCenter.ServerConfiguration'
[string] $ValidOlderModuleVersionThatExists = '1.0.40'
[string] $InvalidModuleVersionThatDoesNotExist = '1.0.99999'
[string] $ValidModulePrereleaseVersionThatExists = '1.0.66-ci20191121T214736'
[System.Security.SecureString] $SecurePersonalAccessToken = ($AzureArtifactsPersonalAccessToken | ConvertTo-SecureString -AsPlainText -Force)
[PSCredential] $Credential = New-Object System.Management.Automation.PSCredential 'Username@DoesNotMatter.com', $SecurePersonalAccessToken
[System.Version] $MinimumRequiredPowerShellGetModuleVersion = [System.Version]::Parse('2.2.1')

function Remove-PsRepository([string] $feedUrl)
{
    Get-PSRepository | Where-Object { $_.SourceLocation -ieq $feedUrl } | Unregister-PSRepository
    Get-PSRepository | Where-Object { $_.SourceLocation -ieq $feedUrl } | Should -BeNullOrEmpty
}

function Remove-PowerShellModule([string] $powerShellModuleName)
{
    Remove-Module -Name $powerShellModuleName -Force -ErrorAction SilentlyContinue
    Get-Module -Name $powerShellModuleName | Should -BeNullOrEmpty
}

function Uninstall-PowerShellModule([string] $powerShellModuleName)
{
    Remove-PowerShellModule -powerShellModuleName $powerShellModuleName
    Uninstall-Module -Name $powerShellModuleName -AllVersions -Force
    Get-Module -Name $powerShellModuleName -ListAvailable | Should -BeNullOrEmpty
}

Describe 'Registering an Azure Artifacts PS Repository' {
    Context 'When relying on retrieving the Azure Artifacts PAT from the environment variable that exists' {
        Mock Get-SecurePersonalAccessTokenFromEnvironmentVariable { return $SecurePersonalAccessToken } -ModuleName $ModuleNameBeingTested

        It 'Should register a new PS repository properly when relying in PAT from environmental variable' {
            # Arrange.
            [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
            Remove-PsRepository -feedUrl $FeedUrl

            # Act.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl -Repository $expectedRepository

            # Assert.
            $repository | Should -Be $expectedRepository
            Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
        }

        It 'Should return an existing PS repository properly when no Repository is specified' {
            # Arrange.
            [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
            Remove-PsRepository -feedUrl $FeedUrl
            Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl -Repository $expectedRepository

            # Act.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl

            # Assert.
            $repository | Should -Be $expectedRepository
            Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
        }

        It 'Should return an existing PS repository properly when a different Repository is specified' {
            # Arrange.
            [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
            Remove-PsRepository -feedUrl $FeedUrl
            Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl -Repository $expectedRepository

            # Act.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl -Repository 'NameThatShouldNotEndUpInThePSRepositories'

            # Assert.
            $repository | Should -Be $expectedRepository
            Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
        }

        It 'Should register a new PS repository properly when piping in the Feed URL' {
            # Arrange.
            [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
            Remove-PsRepository -feedUrl $FeedUrl

            # Act.
            [string] $repository = ($FeedUrl | Register-AzureArtifactsPSRepository -Repository $expectedRepository)

            # Assert.
            $repository | Should -Be $expectedRepository
            Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
        }

        It 'Should register a new PS repository properly when piping in the all of the parameters by property name' {
            # Arrange.
            [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
            [PSCustomObject] $params = [PSCustomObject]@{
                FeedUrl = $FeedUrl
                Repository = $expectedRepository
                Credential = $Credential
                Scope = 'CurrentUser'
            }
            Remove-PsRepository -feedUrl $FeedUrl

            # Act.
            [string] $repository = ($params | Register-AzureArtifactsPSRepository)

            # Assert.
            $repository | Should -Be $expectedRepository
            Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
        }

        It 'Should import the PowerShellGet module properly when it is not imported yet' {
            # Arrange.
            Remove-PowerShellModule -powerShellModuleName PowerShellGet

            # Act.
            Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl

            # Assert.
            $powerShellGetModuleImported = Get-Module -Name PowerShellGet
            $powerShellGetModuleImported | Should -Not -BeNullOrEmpty
            $powerShellGetModuleImported.Version | Should -BeGreaterOrEqual $MinimumRequiredPowerShellGetModuleVersion
        }

        It 'Should remove the existing too-low PowerShellGet module and import a newer version properly' {
            # Arrange.
            [System.Version] $notHighEnoughPowerShellGetModuleVersion = [System.Version]::Parse('2.0.4')
            Remove-PowerShellModule -powerShellModuleName PowerShellGet
            Install-Module -Name PowerShellGet -RequiredVersion $notHighEnoughPowerShellGetModuleVersion -Force
            Import-Module -Name PowerShellGet -RequiredVersion $notHighEnoughPowerShellGetModuleVersion -Force

            # Act.
            Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl

            # Assert.
            $powerShellGetModuleImported = Get-Module -Name PowerShellGet
            $powerShellGetModuleImported | Should -Not -BeNullOrEmpty
            $powerShellGetModuleImported.Version | Should -BeGreaterOrEqual $MinimumRequiredPowerShellGetModuleVersion
        }

        It 'Should import the PowerShellGet module properly when a high enough version is already imported' {
            # Arrange.
            Remove-PowerShellModule -powerShellModuleName PowerShellGet
            Install-Module -Name PowerShellGet -MinimumVersion $MinimumRequiredPowerShellGetModuleVersion -Force
            Import-Module -Name PowerShellGet -MinimumVersion $MinimumRequiredPowerShellGetModuleVersion

            # Act.
            Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl

            # Assert.
            $powerShellGetModuleImported = Get-Module -Name PowerShellGet
            $powerShellGetModuleImported | Should -Not -BeNullOrEmpty
            $powerShellGetModuleImported.Version | Should -BeGreaterOrEqual $MinimumRequiredPowerShellGetModuleVersion
        }
    }

    It 'Should register a new PS repository properly when passing in a valid Credential' {
        # Arrange.
        [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
        Remove-PsRepository -feedUrl $FeedUrl

        # Act.
        [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl -Repository $expectedRepository -Credential $Credential

        # Assert.
        $repository | Should -Be $expectedRepository
        Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
    }

    Context 'When connecting to a feed without using a Credential' {
        Mock Get-AzureArtifactsCredential { return $null } -ModuleName $ModuleNameBeingTested

        It 'Should not throw an error when credentials are not found. (Assumes the FeedUrl allows you to register it without a Credential)' {
            # Arrange.
            [string] $expectedRepository = 'AzureArtifactsPowerShellFeed'
            Remove-PsRepository -feedUrl $FeedUrl

            # Act.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl -Repository $expectedRepository

            # Assert.
            $repository | Should -Be $expectedRepository
            Get-PSRepository -Name $repository | Should -Not -BeNullOrEmpty
        }
    }
}

# Describe 'Importing a PowerShell module from Azure Artifacts' {
# It 'Should import the module properly' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = { Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # Act and Assert.
# $action | Should -Not -Throw
# Get-Module -Name $PowerShellModuleName | Should -Not -BeNullOrEmpty
# }

# It 'Should import the module properly when forced' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = { Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Force }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # Act and Assert.
# $action | Should -Not -Throw
# Get-Module -Name $PowerShellModuleName | Should -Not -BeNullOrEmpty
# }

# It 'Should import the module properly when a specific version is requested' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = { Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Version $ValidOlderModuleVersionThatExists }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # Act and Assert.
# $action | Should -Not -Throw
# $module = Get-Module -Name $PowerShellModuleName
# $module | Should -Not -BeNullOrEmpty
# $module.Version | Should -Be $ValidOlderModuleVersionThatExists
# }

# # Could not get this one to work, as it complains that the module is in use so it's not able to uninstall it to do a proper test.
# # It 'Should throw an error when trying to import a version that does not exist and no different version exists' {
# # # Arrange.
# # [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# # [ScriptBlock] $action = { Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Version $InvalidModuleVersionThatDoesNotExist }
# # Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName
# # Uninstall-Module -Name $PowerShellModuleName -Force -AllVersions
# # Write-Host "Versions: " + (Get-Module -Name $PowerShellModuleName -ListAvailable | Format-Table | Out-String)
# # Get-Module -Name $PowerShellModuleName -ListAvailable | Should -BeNullOrEmpty

# # # Act and Assert.
# # $action | Should -Not -Throw
# # }

# It 'Should write an error and continue when trying to import a version that does not exist, but a different version exists' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository
# Get-Module -Name $PowerShellModuleName -ListAvailable | Should -Not -BeNullOrEmpty

# # Act
# Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Version $InvalidModuleVersionThatDoesNotExist -ErrorAction SilentlyContinue -ErrorVariable err

# # Assert.
# $err.Count | Should -BeGreaterThan 0
# [string] $errors = $err | ForEach-Object { $_.ToString() }
# $errors | Should -Match 'is already installed and will be imported instead.'
# }

# It 'Should throw an error when trying to import a module that does not exist' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = { Import-AzureArtifactsModule -Name 'InvalidModuleName' -Repository $repository }

# # Act and Assert.
# $action | Should -Throw "The PowerShell module 'InvalidModuleName' could not be found in the PSRepository"
# }

# It 'Should write an error and continue when an invalid Repository is specified, but the module is already installed' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository
# Get-Module -Name $PowerShellModuleName -ListAvailable | Should -Not -BeNullOrEmpty

# # Act.
# Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository 'InvalidRepositoryName' -ErrorAction SilentlyContinue -ErrorVariable err

# # Act and Assert.
# $err.Count | Should -BeGreaterThan 0
# [string] $errors = $err | ForEach-Object { $_.ToString() }
# $errors | Should -Match "Version '.+?' is installed on computer '.+?' though so it will be used.*"
# }

# It 'Should throw an error if the Credential is invalid' {
# # Arrange.
# [System.Security.SecureString] $invalidPat = 'InvalidPat' | ConvertTo-SecureString -AsPlainText -Force
# [PSCredential] $invalidCredential = New-Object System.Management.Automation.PSCredential 'Username@DoesNotMatter.com', $invalidPat
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl

# # Act.
# Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Credential $invalidCredential -ErrorAction SilentlyContinue -ErrorVariable err

# # Assert.
# $err.Count | Should -BeGreaterThan 0
# [string] $errors = $err | ForEach-Object { $_.ToString() }
# $errors | Should -Match "Perhaps the credentials used are not valid."
# }

# It 'Should not import module Prerelease versions when the Prerelease switch is not provided' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = { Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Version $ValidModulePrereleaseVersionThatExists }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # Act and Assert.
# $action | Should -Throw "The '-AllowPrerelease' parameter must be specified when using the Prerelease string"
# Get-Module -Name $PowerShellModuleName | Should -BeNullOrEmpty
# }

# It 'Should import module Prerelease versions properly' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = { Import-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Version $ValidModulePrereleaseVersionThatExists -AllowPrerelease }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # PowerShell is weird about the way it supports prerelease versions.
# # The directory it installs to and the version it gives it is just the version with the prerelease portion removed.
# # So we need to strip off the prerelease portion of the version number. i.e. what comes after the hyphen.
# [string] $prereleaseVersionsStablePortion = ($ValidModulePrereleaseVersionThatExists -split '-')[0]

# # Act and Assert.
# $action | Should -Not -Throw
# $module = Get-Module -Name $PowerShellModuleName
# $module | Should -Not -BeNullOrEmpty
# $module.Version | Should -Be $prereleaseVersionsStablePortion
# }

# It 'Should import the module properly when piping in the Repository Name' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [ScriptBlock] $action = {
# $repository | Import-AzureArtifactsModule -Name $PowerShellModuleName
# }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # Act and Assert.
# $action | Should -Not -Throw
# Get-Module -Name $PowerShellModuleName | Should -Not -BeNullOrEmpty
# }

# It 'Should import the module properly when piping in all of the parameters by property name' {
# # Arrange.
# [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
# [PSCustomObject] $params = [PSCustomObject]@{
# Name = $PowerShellModuleName
# Version = $null
# AllowPrerelease = $false
# Repository = $repository
# Credential = $Credential
# Force = $false
# Scope = 'CurrentUser'
# }
# [ScriptBlock] $action = {
# $params | Import-AzureArtifactsModule
# }
# Remove-PowerShellModule -powerShellModuleName $PowerShellModuleName

# # Act and Assert.
# $action | Should -Not -Throw
# Get-Module -Name $PowerShellModuleName | Should -Not -BeNullOrEmpty
# }
# }

Describe 'Finding a PowerShell module from Azure Artifacts' {
    Context 'When relying on retrieving the Azure Artifacts PAT from the environment variable that exists' {
        Mock Get-SecurePersonalAccessTokenFromEnvironmentVariable { return $SecurePersonalAccessToken } -ModuleName $ModuleNameBeingTested

        It 'Should find the module properly' {
            # Arrange.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
            [ScriptBlock] $action = { Find-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository }

            # Act and Assert.
            $action | Should -Not -BeNullOrEmpty
        }
    }

    Context 'When connecting to a feed without using a Credential' {
        Mock Get-AzureArtifactsCredential { return $null } -ModuleName $ModuleNameBeingTested

        It 'Should throw an exception' {
            # Arrange.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
            [scriptblock] $action = { Find-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -ErrorAction Stop }

            # Act and Assert.
            $action | Should -Throw "Unable to find repository"
        }
    }

    It 'Should throw an exception when the Credential is invalid' {
        # Arrange.
        [System.Security.SecureString] $invalidPat = 'InvalidPat' | ConvertTo-SecureString -AsPlainText -Force
        [PSCredential] $invalidCredential = New-Object System.Management.Automation.PSCredential 'Username@DoesNotMatter.com', $invalidPat
        [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
        [scriptblock] $action = { Find-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Credential $invalidCredential -ErrorAction Stop }

        # Act and Assert.
        $action | Should -Throw "No match was found for the specified search criteria and module name"
    }
}

Describe 'Installing a PowerShell module from Azure Artifacts' {
    Context 'When relying on retrieving the Azure Artifacts PAT from the environment variable that exists' {
        Mock Get-SecurePersonalAccessTokenFromEnvironmentVariable { return $SecurePersonalAccessToken } -ModuleName $ModuleNameBeingTested

        It 'Should install the module properly' {
            # Arrange.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
            [ScriptBlock] $action = { Install-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -Force -ErrorAction Stop }
            Uninstall-PowerShellModule -powerShellModuleName $PowerShellModuleName -ErrorAction 'SilentlyContinue'

            # Act and Assert.
            $action | Should -Not -Throw
            Get-Module -Name $PowerShellModuleName -ListAvailable | Should -Not -BeNullOrEmpty
        }
    }
}

Describe 'Updating a PowerShell module from Azure Artifacts' {
    Context 'When relying on retrieving the Azure Artifacts PAT from the environment variable that exists' {
        Mock Get-SecurePersonalAccessTokenFromEnvironmentVariable { return $SecurePersonalAccessToken } -ModuleName $ModuleNameBeingTested

        It 'Should update the module properly resulting in 2 versions being installed' {
            # Arrange.
            [string] $repository = Register-AzureArtifactsPSRepository -FeedUrl $FeedUrl
            [ScriptBlock] $action = { Update-AzureArtifactsModule -Name $PowerShellModuleName -Force -ErrorAction Stop }
            Uninstall-PowerShellModule -powerShellModuleName $PowerShellModuleName -ErrorAction 'SilentlyContinue'
            Install-AzureArtifactsModule -Name $PowerShellModuleName -Repository $repository -RequiredVersion $ValidOlderModuleVersionThatExists

            # Act and Assert.
            $action | Should -Not -Throw
            $modulesInstalled = Get-Module -Name $PowerShellModuleName -ListAvailable
            $modulesInstalled | Should -Not -BeNullOrEmpty
            $modulesInstalled.Count | Should -BeGreaterThan 1
        }
    }
}