-
Notifications
You must be signed in to change notification settings - Fork 225
/
MSFT_xSqlAlias.Tests.ps1
145 lines (113 loc) · 6.21 KB
/
MSFT_xSqlAlias.Tests.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
$script:DSCModuleName = 'xSQLServer'
$script:DSCResourceName = 'MSFT_xSqlAlias'
#region HEADER
# Unit Test Template Version: 1.1.0
[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}
Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceName `
-TestType Unit
#endregion HEADER
try
{
#region Pester Test Initialization
#endregion Pester Test Initialization
#region Get-TargetResource
Describe 'Get-TargetResource' {
Mock -CommandName Get-ItemProperty -MockWith {
return 'DBMSSOCN,localhost,1433'
} -ModuleName $script:DSCResourceName
$SqlAlias = Get-TargetResource -Name 'localhost' -Servername 'localhost'
It 'Should return hashtable with Key Protocol'{
$SqlAlias.ContainsKey('Protocol') | Should Be $true
}
It 'Should return hashtable with Value that matches "TCP"'{
$SqlAlias.Protocol = 'TCP'
}
}
#end region Get-TargetResource
#region Set-TargetResource
Describe 'Set-TargetResource' {
Mock -CommandName New-ItemProperty -MockWith {} -ModuleName $script:DSCResourceName
Mock -CommandName Set-ItemProperty -MockWith {} -ModuleName $script:DSCResourceName
Mock -CommandName Remove-ItemProperty -MockWith {} -ModuleName $script:DSCResourceName
Mock -CommandName Test-Path -MockWith {
return $true
} -ModuleName $script:DSCResourceName
Mock -CommandName Get-WmiObject -MockWith {
return @{
Class = 'win32_OperatingSystem'
OSArchitecture = '64-bit'
}
} -ModuleName $script:DSCResourceName
It 'Should call New-ItemProperty when value is not set' {
Mock -CommandName Get-ItemProperty -MockWith {
return $null
} -ModuleName $script:DSCResourceName
Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Present'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 2 -Scope It
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 0 -Scope It
}
Mock -CommandName Get-ItemProperty -MockWith {
return 'DBMSSOCN,localhost,52002'
} -ModuleName $script:DSCResourceName
It 'Should not call any *-ItemProperty when value is already set' {
Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Present'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 0 -Scope It
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 0 -Scope It
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 0 -Scope It
}
It 'Should call Set-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys) when server name is different' {
Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'newserver' -TCPPort 52002 -Ensure 'Present'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 0 -Scope It
}
It 'Should call Set-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys) when TCP port is different' {
Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 1433 -Ensure 'Present'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 2 -Scope It
}
It 'Should call any Remove-ItemProperty exactly 2 times (1 for 32bit and 1 for 64 bit reg keys) when alias should be absent' {
Set-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName 'localhost' -TCPPort 52002 -Ensure 'Absent'
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Set-ItemProperty -Exactly 0 -Scope It
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName New-ItemProperty -Exactly 0 -Scope It
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Remove-ItemProperty -Exactly 2 -Scope It
}
}
#end region Set-TargetResource
#region Test-TargetResource
Describe 'Test-TargetResource' {
Mock -CommandName Test-Path -MockWith {
return $true
} -ModuleName $script:DSCResourceName
Mock -CommandName Get-ItemProperty -MockWith {
return @{
myServerAlias = 'DBMSSOCN,localhost,1433'
}
} -ModuleName $script:DSCResourceName
Mock -CommandName Get-WmiObject -MockWith {
return @{
Class = 'win32_OperatingSystem'
OSArchitecture = '64-bit'
}
} -ModuleName $script:DSCResourceName
It 'Should return $true when Test is passed as Alias thats already set'{
Test-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName localhost -TCPPort 1433 -Ensure 'Present' | Should Be $true
}
It 'Should return $false when Test is passed as Alias that is not set'{
Test-TargetResource -Name 'myServerAlias' -Protocol 'TCP' -ServerName localhost -TCPPort 52002 -Ensure 'Present' | Should Be $false
}
}
#end region Test-TargetResource
}
finally
{
#region FOOTER
Restore-TestEnvironment -TestEnvironment $TestEnvironment
#endregion
}