forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-SqlDscAgentOperator.Integration.Tests.ps1
More file actions
96 lines (74 loc) · 4.33 KB
/
Get-SqlDscAgentOperator.Integration.Tests.ps1
File metadata and controls
96 lines (74 loc) · 4.33 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
93
94
95
96
[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 'Get-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 test operators for getting
$script:sqlServerObject | New-SqlDscAgentOperator -Name 'IntegrationTest_GetOperator1' -EmailAddress 'operator1@contoso.com' -Force -ErrorAction 'Stop'
$script:sqlServerObject | New-SqlDscAgentOperator -Name 'IntegrationTest_GetOperator2' -EmailAddress 'operator2@contoso.com' -Force -ErrorAction 'Stop'
}
AfterAll {
$script:sqlServerObject | Remove-SqlDscAgentOperator -Name 'IntegrationTest_GetOperator1' -Force -ErrorAction 'SilentlyContinue'
$script:sqlServerObject | Remove-SqlDscAgentOperator -Name 'IntegrationTest_GetOperator2' -Force -ErrorAction 'SilentlyContinue'
# Disconnect from the SQL Server
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
}
It 'Should get all operators' {
$operators = $script:sqlServerObject | Get-SqlDscAgentOperator -ErrorAction 'Stop'
$operators | Should -Not -BeNullOrEmpty
@($operators)[0] | Should -BeOfType [Microsoft.SqlServer.Management.Smo.Agent.Operator]
}
It 'Should get specific operator by name' {
$operator = $script:sqlServerObject | Get-SqlDscAgentOperator -Name 'IntegrationTest_GetOperator1' -ErrorAction 'Stop'
$operator | Should -Not -BeNullOrEmpty
$operator | Should -BeOfType [Microsoft.SqlServer.Management.Smo.Agent.Operator]
$operator.Name | Should -Be 'IntegrationTest_GetOperator1'
$operator.EmailAddress | Should -Be 'operator1@contoso.com'
}
It 'Should return nothing when operator does not exist' {
$operator = $script:sqlServerObject | Get-SqlDscAgentOperator -Name 'NonExistentOperator' -ErrorAction 'Stop'
$operator | Should -BeNullOrEmpty
}
It 'Should get operator using ServerObject parameter directly' {
$operator = Get-SqlDscAgentOperator -ServerObject $script:sqlServerObject -Name 'IntegrationTest_GetOperator2' -ErrorAction 'Stop'
$operator | Should -Not -BeNullOrEmpty
$operator | Should -BeOfType [Microsoft.SqlServer.Management.Smo.Agent.Operator]
$operator.Name | Should -Be 'IntegrationTest_GetOperator2'
}
}