forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnect-SqlDscDatabaseEngine.Integration.Tests.ps1
More file actions
134 lines (108 loc) · 5.55 KB
/
Connect-SqlDscDatabaseEngine.Integration.Tests.ps1
File metadata and controls
134 lines (108 loc) · 5.55 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
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
[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'
}
# cSpell: ignore DSCSQLTEST
Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
# $previouslyErrorViewPreference = $ErrorView
# $ErrorView = 'DetailedView'
# $Error.Clear()
}
# AfterAll {
# $ErrorView = $previouslyErrorViewPreference
# Write-Verbose -Message ('Error count: {0}' -f $Error.Count) -Verbose
# Write-Verbose -Message ($Error | Out-String) -Verbose
# }
Context 'When connecting to the default instance impersonating a Windows user' {
BeforeAll {
# Starting the default instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
}
AfterAll {
# Stop the default instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
}
It 'Should have the default instance SQL Server service started' {
$getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop'
$getServiceResult.Status | Should -Be 'Running'
}
Context 'When impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$connectSqlDscDatabaseEngineParameters = @{
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}
}
Context 'When connecting to a named instance' {
It 'Should have the named instance SQL Server service started' {
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'
$getServiceResult.Status | Should -Be 'Running'
}
Context 'When impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}
Context 'When using a SQL login' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'sa'
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
LoginType = 'SqlLogin'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}
}
}