forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssert-SqlDscLogin.Integration.Tests.ps1
More file actions
95 lines (78 loc) · 3.91 KB
/
Assert-SqlDscLogin.Integration.Tests.ps1
File metadata and controls
95 lines (78 loc) · 3.91 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
[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 build" first.'
}
}
BeforeAll {
$script:dscModuleName = 'SqlServerDsc'
Import-Module -Name $script:dscModuleName -Force -ErrorAction 'Stop'
}
Describe 'Assert-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
$script:instanceName = 'DSCSQLTEST'
$script:computerName = Get-ComputerName
}
AfterAll {
# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}
Context 'When connecting to SQL Server instance' {
BeforeAll {
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$script:sqlAdminCredential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:instanceName -Credential $script:sqlAdminCredential
}
AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
}
Context 'When a login exists' {
It 'Should not throw an error for sa login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'sa' } | Should -Not -Throw
}
It 'Should not throw an error when using pipeline' {
{ $script:serverObject | Assert-SqlDscLogin -Name 'sa' } | Should -Not -Throw
}
It 'Should not throw an error for NT AUTHORITY\SYSTEM login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'NT AUTHORITY\SYSTEM' } | Should -Not -Throw
}
It 'Should not throw an error for SqlAdmin login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name ('{0}\SqlAdmin' -f $script:computerName) } | Should -Not -Throw
}
}
Context 'When a login does not exist' {
It 'Should throw a terminating error for non-existent login' {
{ Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin123' } | Should -Throw -ExpectedMessage "*does not exist as a login*"
}
It 'Should throw an error with ObjectNotFound category' {
try
{
Assert-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin123'
}
catch
{
$_.CategoryInfo.Category | Should -Be 'ObjectNotFound'
}
}
}
}
}