Skip to content

Commit

Permalink
SqlSetup,SqlWindowsFirewall: Fix duplicate function (#1608)
Browse files Browse the repository at this point in the history
- SqlServerDsc
  - Document changes in the file `build.yml`.
- SqlSetup
  - Duplicate function Get-SqlMajorVersion was removed and instead the
    helper function Get-FilePathMajorVersion from the helper module
    SqlServerDsc.Common is used (issue #1178).
- SqlWindowsFirewall
  - Duplicate function Get-SqlMajorVersion was removed and instead the
    helper function Get-FilePathMajorVersio` from the helper module
    SqlServerDsc.Common is used (issue #1178).
- SqlServerDsc.Common
  - Function Get-FilePathMajorVersion was added. The function Get-SqlMajorVersion
    from the resources SqlSetup and SqlWindowsFirewall was moved and
    renamed without any functional changes (issue #1178).
  • Loading branch information
johlju authored Aug 11, 2020
1 parent 3056173 commit 877ea96
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 98 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SqlDatabaseObjectPermission
- New integration tests to verify scenarios when passing a single permission.

### Changed

- SqlServerDsc
- Minor document changes in the file `build.yml`.
- SqlSetup
- Duplicate function Get-SqlMajorVersion was removed and instead the
helper function `Get-FilePathMajorVersion` from the helper module
SqlServerDsc.Common is used ([issue #1178](https://github.com/PowerShell/SqlServerDsc/issues/1178)).
- SqlWindowsFirewall
- Duplicate function Get-SqlMajorVersion was removed and instead the
helper function `Get-FilePathMajorVersion` from the helper module
SqlServerDsc.Common is used ([issue #1178](https://github.com/PowerShell/SqlServerDsc/issues/1178)).
- SqlServerDsc.Common
- Function `Get-FilePathMajorVersion` was added. The function `Get-SqlMajorVersion`
from the resources _SqlSetup_ and _SqlWindowsFirewall_ was moved and
renamed without any functional changes ([issue #1178](https://github.com/PowerShell/SqlServerDsc/issues/1178)).

### Fixed

- SqlServerDsc
Expand Down
73 changes: 38 additions & 35 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
---
####################################################
# ModuleBuilder Configuration #
# Pipeline Build Task Configuration (Invoke-Build) #
####################################################
BuildWorkflow:
'.':
- build
- test

build:
- Clean
- Build_Module_ModuleBuilder
- Build_NestedModules_ModuleBuilder
- Create_Changelog_Release_Output
- Generate_Conceptual_Help
- Generate_Wiki_Content

pack:
- build
- package_module_nupkg

hqrmtest:
- DscResource_Tests_Stop_On_Fail

test:
- Pester_Tests_Stop_On_Fail
- Pester_If_Code_Coverage_Under_Threshold

publish:
- Publish_Release_To_GitHub
- Publish_Module_To_gallery
- Publish_GitHub_Wiki_Content

####################################################
# ModuleBuilder Configuration #
####################################################
CopyPaths:
- DSCResources
Expand All @@ -27,7 +59,7 @@ TaskHeader: |
""
####################################################
# ModuleBuilder Dependent Modules Configuration #
# Dependent Modules Configuration (Sampler) #
####################################################
NestedModule:
DscResource.Common:
Expand All @@ -37,39 +69,7 @@ NestedModule:
Exclude: PSGetModuleInfo.xml

####################################################
# Pipeline Configuration #
####################################################
BuildWorkflow:
'.':
- build
- test

build:
- Clean
- Build_Module_ModuleBuilder
- Build_NestedModules_ModuleBuilder
- Create_Changelog_Release_Output
- Generate_Conceptual_Help
- Generate_Wiki_Content

pack:
- build
- package_module_nupkg

hqrmtest:
- DscResource_Tests_Stop_On_Fail

test:
- Pester_Tests_Stop_On_Fail
- Pester_If_Code_Coverage_Under_Threshold

publish:
- Publish_Release_To_GitHub
- Publish_Module_To_gallery
- Publish_GitHub_Wiki_Content

####################################################
# PESTER Configuration #
# Pester Configuration (Sampler) #
####################################################
Pester:
OutputFormat: NUnitXML
Expand All @@ -88,6 +88,9 @@ Pester:
CodeCoverageOutputFile: JaCoCo_coverage.xml
CodeCoverageOutputFileEncoding: ascii

####################################################
# Pester Configuration (DscResource.Test) #
####################################################
DscTest:
ExcludeTag:
- "Common Tests - New Error-Level Script Analyzer Rules"
Expand Down
26 changes: 3 additions & 23 deletions source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function Get-TargetResource

Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)

$sqlVersion = Get-SqlMajorVersion -Path $pathToSetupExecutable
$sqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable

if ($SourceCredential)
{
Expand Down Expand Up @@ -984,7 +984,7 @@ function Set-TargetResource

Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)

$sqlVersion = Get-SqlMajorVersion -Path $pathToSetupExecutable
$sqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable

# Determine features to install
$featuresToInstall = ''
Expand Down Expand Up @@ -2192,7 +2192,7 @@ function Test-TargetResource

if ($getTargetResourceParameters.Action -eq 'Upgrade')
{
$installerSqlVersion = Get-SqlMajorVersion -Path (Join-Path -Path $sourcePath -ChildPath 'setup.exe')
$installerSqlVersion = Get-FilePathMajorVersion -Path (Join-Path -Path $sourcePath -ChildPath 'setup.exe')
$instanceSqlVersion = Get-SQLInstanceMajorVersion -InstanceName $InstanceName

if ($installerSQLVersion -gt $instanceSqlVersion)
Expand All @@ -2208,26 +2208,6 @@ function Test-TargetResource
return $result
}

<#
.SYNOPSIS
Returns the SQL Server major version from the setup.exe executable provided in the Path parameter.
.PARAMETER Path
String containing the path to the SQL Server setup.exe executable.
#>
function Get-SqlMajorVersion
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$Path
)

(Get-Item -Path $Path).VersionInfo.ProductVersion.Split('.')[0]
}

<#
.SYNOPSIS
Returns the first item value in the registry location provided in the Path parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function Get-TargetResource
$script:localizedData.UsingPath -f $pathToSetupExecutable
)

$sqlVersion = Get-SqlMajorVersion -Path $pathToSetupExecutable
$sqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable

Write-Verbose -Message (
$script:localizedData.MajorVersion -f $sqlVersion
Expand Down Expand Up @@ -401,7 +401,7 @@ function Set-TargetResource
$script:localizedData.UsingPath -f $pathToSetupExecutable
)

$sqlVersion = Get-SqlMajorVersion -Path $pathToSetupExecutable
$sqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable

Write-Verbose -Message (
$script:localizedData.MajorVersion -f $sqlVersion
Expand Down Expand Up @@ -864,25 +864,4 @@ function Test-IsFirewallRuleInDesiredState
return $isRuleInDesiredState
}

<#
.SYNOPSIS
Returns the SQL Server major version from the setup.exe executable provided
in the Path parameter.
.PARAMETER Path
String containing the path to the SQL Server setup.exe executable.
#>
function Get-SqlMajorVersion
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$path
)

(Get-Item -Path $path).VersionInfo.ProductVersion.Split('.')[0]
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'Import-Assembly'
'Set-PSModulePath'
'ConvertTo-ServerInstanceName'
'Get-FilePathMajorVersion'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
21 changes: 21 additions & 0 deletions source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2374,3 +2374,24 @@ function ConvertTo-ServerInstanceName

return $serverInstance
}

<#
.SYNOPSIS
Returns the SQL Server major version from the setup.exe executable provided
in the Path parameter.
.PARAMETER Path
String containing the path to the SQL Server setup.exe executable.
#>
function Get-FilePathMajorVersion
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$Path
)

(Get-Item -Path $Path).VersionInfo.ProductVersion.Split('.')[0]
}
6 changes: 3 additions & 3 deletions tests/Unit/DSC_SqlSetup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ try

# General mocks
Mock -CommandName Get-PSDrive
Mock -CommandName Get-SqlMajorVersion -MockWith $mockGetSqlMajorVersion
Mock -CommandName Get-FilePathMajorVersion -MockWith $mockGetSqlMajorVersion

Mock -CommandName Get-RegistryPropertyValue -ParameterFilter {
$Name -eq 'ImagePath'
Expand Down Expand Up @@ -1610,7 +1610,7 @@ try
}
}

Mock -CommandName Get-SqlMajorVersion -MockWith {
Mock -CommandName Get-FilePathMajorVersion -MockWith {
return '15'
}

Expand Down Expand Up @@ -2071,7 +2071,7 @@ try
# General mocks
Mock -CommandName Get-PSDrive
Mock -CommandName Import-SQLPSModule
Mock -CommandName Get-SqlMajorVersion -MockWith $mockGetSqlMajorVersion
Mock -CommandName Get-FilePathMajorVersion -MockWith $mockGetSqlMajorVersion

# Mocking SharedDirectory and SharedWowDirectory (when not previously installed)
Mock -CommandName Get-ItemProperty
Expand Down
19 changes: 5 additions & 14 deletions tests/Unit/DSC_SqlWindowsFirewall.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,9 @@ try
}
$mockSetNetFirewallRule = $mockNewNetFirewallRule

$mockGetItem_SqlMajorVersion = {
return New-Object -TypeName Object |
Add-Member -MemberType ScriptProperty -Name VersionInfo -Value {
return New-Object -TypeName Object |
Add-Member -MemberType NoteProperty -Name 'ProductVersion' -Value ('{0}.0.0000.00000' -f $mockCurrentSqlMajorVersion) -PassThru -Force
} -PassThru -Force
$mockGetSqlMajorVersion = {
return $mockCurrentSqlMajorVersion
}

$mockGetItem_SqlMajorVersion_ParameterFilter = {
$Path -eq $mockCurrentPathToSetupExecutable
}

#endregion Function mocks

# Default parameters that are used for the It-blocks
Expand All @@ -438,7 +429,7 @@ try

BeforeEach {
# General mocks
Mock -CommandName Get-Item -ParameterFilter $mockGetItem_SqlMajorVersion_ParameterFilter -MockWith $mockGetItem_SqlMajorVersion -Verifiable
Mock -CommandName Get-FilePathMajorVersion -MockWith $mockGetSqlMajorVersion -Verifiable

# Mock SQL Server Database Engine registry for Instance ID.
Mock -CommandName Get-ItemProperty `
Expand Down Expand Up @@ -1028,7 +1019,7 @@ try

BeforeEach {
# General mocks
Mock -CommandName Get-Item -ParameterFilter $mockGetItem_SqlMajorVersion_ParameterFilter -MockWith $mockGetItem_SqlMajorVersion -Verifiable
Mock -CommandName Get-FilePathMajorVersion -MockWith $mockGetSqlMajorVersion -Verifiable

# Mock SQL Server Database Engine registry for Instance ID.
Mock -CommandName Get-ItemProperty `
Expand Down Expand Up @@ -1323,7 +1314,7 @@ try

BeforeEach {
# General mocks
Mock -CommandName Get-Item -ParameterFilter $mockGetItem_SqlMajorVersion_ParameterFilter -MockWith $mockGetItem_SqlMajorVersion -Verifiable
Mock -CommandName Get-FilePathMajorVersion -MockWith $mockGetSqlMajorVersion -Verifiable

# Mock SQL Server Database Engine registry for Instance ID.
Mock -CommandName Get-ItemProperty `
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/SqlServerDsc.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3446,4 +3446,24 @@ InModuleScope $script:subModuleName {
$result | Should -BeExactly ('{0}\{1}' -f $env:COMPUTERNAME, 'MyInstance')
}
}

Describe 'SqlServerDsc.Common\Get-FilePathMajorVersion' {
BeforeAll {
$mockGetItem_SqlMajorVersion = {
return New-Object -TypeName Object |
Add-Member -MemberType ScriptProperty -Name VersionInfo -Value {
return New-Object -TypeName Object |
Add-Member -MemberType NoteProperty -Name 'ProductVersion' -Value '10.0.0000.00000' -PassThru -Force
} -PassThru -Force
}

Mock -CommandName Get-Item -MockWith $mockGetItem_SqlMajorVersion
}

It 'Should return correct version' {
$result = Get-FilePathMajorVersion -Path 'C:\AnyPath\Setup.exe'

$result | Should -Be '10'
}
}
}

0 comments on commit 877ea96

Please sign in to comment.