forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssert-SqlDscAgentOperator.Integration.Tests.ps1
More file actions
92 lines (72 loc) · 3.98 KB
/
Assert-SqlDscAgentOperator.Integration.Tests.ps1
File metadata and controls
92 lines (72 loc) · 3.98 KB
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
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
param ()
BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
}
# If the dependencies have not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
}
}
BeforeAll {
$script:moduleName = 'SqlServerDsc'
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
$env:SqlServerDscCI = $true
# Integration tests are run on the DSCSQLTEST instance
$script:sqlServerInstance = 'DSCSQLTEST'
}
AfterAll {
Remove-Item -Path 'Env:\SqlServerDscCI' -ErrorAction 'SilentlyContinue'
# Unload the module being tested so that it doesn't impact any other tests.
Get-Module -Name $script:moduleName -All | Remove-Module -Force
}
Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
BeforeAll {
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
# Connect to the SQL Server instance
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
# Create a test operator for assertion tests
$script:sqlServerObject | New-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -EmailAddress 'assert@contoso.com' -ErrorAction Stop
}
AfterAll {
$script:sqlServerObject | Remove-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -Force -ErrorAction 'SilentlyContinue'
# Disconnect from the SQL Server
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
}
Context 'When operator exists' {
It 'Should not throw when asserting existing operator' {
$null = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_AssertOperator' -ErrorAction Stop
}
It 'Should not throw when asserting persistent operator created by New-SqlDscAgentOperator' {
$null = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'SqlDscIntegrationTestOperator_Persistent' -ErrorAction Stop
}
It 'Should not return anything when operator exists' {
$result = Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_AssertOperator' -ErrorAction Stop
$result | Should -BeNullOrEmpty
}
}
Context 'When operator does not exist' {
It 'Should throw terminating error when operator does not exist' {
{ Assert-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'NonExistentOperator' -ErrorAction Stop } | Should -Throw
}
}
Context 'When using pipeline input' {
It 'Should accept ServerObject from pipeline' {
$null = $script:sqlServerObject | Assert-SqlDscAgentOperator -Name 'IntegrationTest_AssertOperator' -ErrorAction 'Stop'
}
}
}