Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed Azure DevOps pipeline conditions that were preventing DSC resource
integration tests from running when they should by removing incorrect quotes
around boolean values.
- `SqlAgentAlert`
- Minor fix in `source/Classes/020.SqlAgentAlert.ps1` to correct `ExcludeDscProperties`
formatting (added missing delimiter).

### Added

Expand Down Expand Up @@ -57,6 +60,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Refactored GitHub Copilot workflow setup to be module-agnostic via MODULE_NAME
environment variable, includes full-history detection, uses idempotent .NET
tool install, and adds Linux dependency handling ([issue #2127](https://github.com/dsccommunity/SqlServerDsc/issues/2127)).
- `SqlAgentAlert`
- Added additional unit tests covering MessageId-based alerts, the hidden
`Modify()` method behavior, and `AssertProperties()` validation scenarios.
- Minor fix in `source/Classes/020.SqlAgentAlert.ps1` to correct `ExcludeDscProperties`
formatting (added missing delimiter).
- Module now outputs a verbose message instead of a warning when the SMO
dependency module is missing during import to work around a DSC v3 issue.
- VS Code tasks configuration was improved to support AI.
Expand Down
2 changes: 1 addition & 1 deletion source/Classes/020.SqlAgentAlert.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class SqlAgentAlert : SqlResourceBase
$this.ExcludeDscProperties = @(
'InstanceName',
'ServerName',
'Credential'
'Credential',
'Name'
)
}
Expand Down
112 changes: 112 additions & 0 deletions tests/Unit/Classes/SqlAgentAlert.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,36 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
$result.Ensure | Should -Be 'Absent'
}
}

It 'Should return current state when alert exists with MessageId' {
InModuleScope -ScriptBlock {
$script:mockAlertObjectWithMessageId = New-Object -TypeName 'PSCustomObject'
$script:mockAlertObjectWithMessageId | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
$script:mockAlertObjectWithMessageId | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 0
$script:mockAlertObjectWithMessageId | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 50001

Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
return $script:mockAlertObjectWithMessageId
}

$instance = [SqlAgentAlert] @{
Name = 'TestAlert'
InstanceName = 'MSSQLSERVER'
MessageId = 50002
} |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
return $script:mockServerObject
} -PassThru

$result = $instance.Get()

$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be 'TestAlert'
$result.Ensure | Should -Be 'Present'
$result.MessageId | Should -Be 50001
$result.Severity | Should -BeNullOrEmpty
}
}
}

Context 'When using the Test() method' {
Expand Down Expand Up @@ -269,6 +299,27 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
}
}

It 'Should create alert with MessageId' {
InModuleScope -ScriptBlock {
$instance = [SqlAgentAlert] @{
Name = 'TestAlert'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'
MessageId = 50001
} |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
return $script:mockServerObject
} -PassThru

$null = $instance.Set()

Should -Invoke -CommandName 'New-SqlDscAgentAlert' -ParameterFilter {
$MessageId -eq 50001
} -Exactly -Times 1 -Scope It
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
}
}
}

Context 'When it exists and Ensure is Absent' {
Expand Down Expand Up @@ -320,6 +371,67 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
Mock -CommandName 'Set-SqlDscAgentAlert'
}


Context 'When Ensure is Present and alert does not exist' {
It 'Should create alert with Severity' {
InModuleScope -ScriptBlock {
Mock -CommandName 'Get-SqlDscAgentAlert'

$instance = [SqlAgentAlert] @{
Name = 'TestAlert'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'
Severity = 16
} |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
return $script:mockServerObject
} -PassThru

$properties = @{
Severity = 16
}

$null = $instance.Modify($properties)

Should -Invoke -CommandName 'Get-SqlDscAgentAlert' -Exactly -Times 1 -Scope It
Should -Invoke -CommandName 'New-SqlDscAgentAlert' -ParameterFilter {
$Severity -eq 16
} -Exactly -Times 1 -Scope It
Should -Invoke -CommandName 'Set-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
}
}

It 'Should create alert with MessageId' {
InModuleScope -ScriptBlock {
Mock -CommandName 'Get-SqlDscAgentAlert'

$instance = [SqlAgentAlert] @{
Name = 'TestAlert'
InstanceName = 'MSSQLSERVER'
Ensure = 'Present'
MessageId = 50001
} |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
return $script:mockServerObject
} -PassThru

$properties = @{
MessageId = 50001
}

$null = $instance.Modify($properties)

Should -Invoke -CommandName 'Get-SqlDscAgentAlert' -Exactly -Times 1 -Scope It
Should -Invoke -CommandName 'New-SqlDscAgentAlert' -ParameterFilter {
$MessageId -eq 50001
} -Exactly -Times 1 -Scope It
Should -Invoke -CommandName 'Set-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
}
}
}

Context 'When Ensure is Present and alert exists' {
It 'Should update alert when Severity property differs' {
InModuleScope -ScriptBlock {
Expand Down
Loading