forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertTo-SqlDscDataFile.Integration.Tests.ps1
More file actions
148 lines (111 loc) · 6.5 KB
/
ConvertTo-SqlDscDataFile.Integration.Tests.ps1
File metadata and controls
148 lines (111 loc) · 6.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
[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'
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
# PowerShell class types to get new identities, breaking type comparisons.
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
# Import the SMO module to ensure real SMO types are available
Import-SqlDscPreferredModule
}
Describe 'ConvertTo-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022', 'Integration_SQL2025') {
BeforeAll {
$script:mockInstanceName = 'DSCSQLTEST'
$script:mockComputerName = Get-ComputerName
$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)
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
}
AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
}
Context 'When converting a DatabaseFileSpec to a DataFile with real SMO types' {
BeforeAll {
$script:testDatabaseName = 'TestDB_{0}' -f (Get-Random)
# Create a test database for the file group context
$script:testDatabase = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Confirm:$false
}
AfterAll {
# Clean up the test database
if ($script:testDatabase)
{
Remove-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Confirm:$false
}
}
BeforeEach {
$script:mockFileGroup = New-SqlDscFileGroup -Database $script:testDatabase -Name 'TestFileGroup' -Confirm:$false
}
It 'Should convert a minimal DatabaseFileSpec to a DataFile object' {
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.ndf' -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $script:mockFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DataFile]
$result.Name | Should -Be 'TestFile'
$result.FileName | Should -Be 'C:\Data\TestFile.ndf'
}
It 'Should convert a DatabaseFileSpec with Size to a DataFile object' {
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.ndf' -Size 100 -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $script:mockFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result.Size | Should -Be 100
}
It 'Should convert a DatabaseFileSpec with MaxSize to a DataFile object' {
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.ndf' -MaxSize 1000 -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $script:mockFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result.MaxSize | Should -Be 1000
}
It 'Should convert a DatabaseFileSpec with Growth to a DataFile object' {
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.ndf' -Growth 10 -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $script:mockFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result.Growth | Should -Be 10
}
It 'Should convert a DatabaseFileSpec with GrowthType to a DataFile object' {
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.ndf' -GrowthType 'Percent' -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $script:mockFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result.GrowthType | Should -Be 'Percent'
}
It 'Should convert a DatabaseFileSpec with IsPrimaryFile to a DataFile object' {
# IsPrimaryFile can only be set for files in the PRIMARY filegroup
$primaryFileGroup = $script:testDatabase.FileGroups['PRIMARY']
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.mdf' -IsPrimaryFile -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $primaryFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result.IsPrimaryFile | Should -Be $true
}
It 'Should convert a DatabaseFileSpec with all optional properties to a DataFile object' {
$fileSpec = New-SqlDscDataFile -Name 'TestFile' -FileName 'C:\Data\TestFile.ndf' -Size 100 -MaxSize 1000 -Growth 10 -GrowthType 'Percent' -AsSpec
$result = ConvertTo-SqlDscDataFile -FileGroupObject $script:mockFileGroup -DataFileSpec $fileSpec
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be 'TestFile'
$result.FileName | Should -Be 'C:\Data\TestFile.ndf'
$result.Size | Should -Be 100
$result.MaxSize | Should -Be 1000
$result.Growth | Should -Be 10
$result.GrowthType | Should -Be 'Percent'
}
}
}