-
Notifications
You must be signed in to change notification settings - Fork 225
/
MSFT_xSQLServerScript.Test.ps1
134 lines (101 loc) · 5.63 KB
/
MSFT_xSQLServerScript.Test.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
<#
.Synopsis
Automated unit test for MSFT_xSQLServerScript DSC Resource
#>
$Script:DSCModuleName = 'MSFT_xSQLServerScript'
$Script:DSCResourceName = 'MSFT_xSQLServerScript'
#region HEADER
# Unit Test Template Version: 1.1.0
[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
}
Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $DSCModuleName `
-DSCResourceName $DSCResourceName `
-TestType Unit
#endregion HEADER
# Begin Testing
try
{
InModuleScope $DSCResourceName {
#region Pester Test Initialization
Function script:Invoke-SqlCmd {}
#endregion Pester Test Initialization
#region Function Get-TargetResource
Describe "$($DSCResourceName)\Get-TargetResource" {
It "Should throw if SQLPS module cannot be found" {
$throwMessage = "Failed to find module"
Mock -CommandName Import-Module -MockWith { Throw $throwMessage }
{ Get-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" } | should throw $throwMessage
}
It "Should throw if Invoke-SqlCmd throws" {
$throwMessage = "Failed to run SQL Script"
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Invoke-SqlCmd -MockWith { Throw $throwMessage }
{ Get-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" } | should throw $throwMessage
}
It "Should return a hashtable if the Get SQL script returns" {
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Invoke-SqlCmd -MockWith { "" }
$result = Get-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql"
$result.ServerInstance | should be $env:COMPUTERNAME
$result.SetFilePath | should be "set.sql"
$result.GetFilePath | should be "get.sql"
$result.TestFilePath | should be "test.sql"
$result.GetType() | should be "hashtable"
}
}
#endregion Function Get-TargetResource
#region Function Test-TargetResource
Describe "$($DSCResourceName)\Test-TargetResource" {
It "Should throw if SQLPS module cannot be found" {
$throwMessage = "Failed to find module"
Mock -CommandName Import-Module -MockWith { Throw $throwMessage }
{ Test-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" } | should throw $throwMessage
}
It "Should return false if Invoke-SqlCmd throws" {
$throwMessage = "Failed to run SQL Script"
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Invoke-SqlCmd -MockWith { Throw $throwMessage }
Test-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" | should be $false
}
It "Should return true if Invoke-SqlCmd returns null" {
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Invoke-SqlCmd -MockWith {}
Test-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" | should be $true
}
}
#endregion Function Test-TargetResource
#region Function Set-TargetResource
Describe "$($DSCResourceName)\Set-TargetResource" {
It "Should throw if SQLPS module cannot be found" {
$throwMessage = "Failed to find module"
Mock -CommandName Import-Module -MockWith { Throw $throwMessage }
{ Set-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" } | should throw $throwMessage
}
It "Should throw if Invoke-SqlCmd throws" {
$throwMessage = "Failed to run SQL Script"
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Invoke-SqlCmd -MockWith { Throw $throwMessage }
{ Set-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql" } | should throw $throwMessage
}
It "Should always attempt to execute Invoke-SqlCmd" {
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Invoke-SqlCmd -MockWith {} -Verifiable
$result = Set-TargetResource -ServerInstance $env:COMPUTERNAME -SetFilePath "set.sql" -GetFilePath "get.sql" -TestFilePath "test.sql"
Assert-MockCalled -CommandName Invoke-SqlCmd -Times 1
}
}
#endregion Function Set-TargetResource
}
}
finally
{
#region FOOTER
Restore-TestEnvironment -TestEnvironment $TestEnvironment
#endregion
}