|
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] |
| 2 | +param () |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + try |
| 6 | + { |
| 7 | + if (-not (Get-Module -Name 'DscResource.Test')) |
| 8 | + { |
| 9 | + # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. |
| 10 | + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) |
| 11 | + { |
| 12 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 13 | + & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null |
| 14 | + } |
| 15 | + |
| 16 | + # If the dependencies have not been resolved, this will throw an error. |
| 17 | + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' |
| 18 | + } |
| 19 | + } |
| 20 | + catch [System.IO.FileNotFoundException] |
| 21 | + { |
| 22 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +BeforeAll { |
| 27 | + $script:dscModuleName = 'SqlServerDsc' |
| 28 | + |
| 29 | + Import-Module -Name $script:dscModuleName |
| 30 | +} |
| 31 | + |
| 32 | +Describe 'Get-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 33 | + BeforeAll { |
| 34 | + # Starting the named instance SQL Server service prior to running tests. |
| 35 | + Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' |
| 36 | + |
| 37 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 38 | + $script:mockComputerName = Get-ComputerName |
| 39 | + |
| 40 | + $mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 41 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 42 | + |
| 43 | + $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) |
| 44 | + |
| 45 | + $script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential |
| 46 | + } |
| 47 | + |
| 48 | + AfterAll { |
| 49 | + # Stop the named instance SQL Server service to save memory on the build worker. |
| 50 | + Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop' |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + Context 'When getting all SQL Server logins' { |
| 55 | + It 'Should return an array of Login objects' { |
| 56 | + $result = Get-SqlDscLogin -ServerObject $script:serverObject |
| 57 | + |
| 58 | + <# |
| 59 | + Casting to array to ensure we get the count on Windows PowerShell |
| 60 | + when there is only one login. |
| 61 | + #> |
| 62 | + @($result).Count | Should -BeGreaterOrEqual 1 |
| 63 | + @($result)[0] | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' |
| 64 | + } |
| 65 | + |
| 66 | + It 'Should return system logins including sa' { |
| 67 | + $result = Get-SqlDscLogin -ServerObject $script:serverObject |
| 68 | + |
| 69 | + $result.Name | Should -Contain 'sa' |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Context 'When getting a specific SQL Server login' { |
| 74 | + It 'Should return the specified login when it exists' { |
| 75 | + $result = Get-SqlDscLogin -ServerObject $script:serverObject -Name 'sa' |
| 76 | + |
| 77 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' |
| 78 | + $result.Name | Should -Be 'sa' |
| 79 | + $result.LoginType | Should -Be 'SqlLogin' |
| 80 | + } |
| 81 | + |
| 82 | + It 'Should throw an error when the login does not exist' { |
| 83 | + { Get-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin' -ErrorAction 'Stop' } | |
| 84 | + Should -Throw -ExpectedMessage 'There is no login with the name ''NonExistentLogin''.' |
| 85 | + } |
| 86 | + |
| 87 | + It 'Should return null when the login does not exist and error action is SilentlyContinue' { |
| 88 | + $result = Get-SqlDscLogin -ServerObject $script:serverObject -Name 'NonExistentLogin' -ErrorAction 'SilentlyContinue' |
| 89 | + |
| 90 | + $result | Should -BeNullOrEmpty |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Context 'When using the Refresh parameter' { |
| 95 | + It 'Should return the same results with and without Refresh' { |
| 96 | + $resultWithoutRefresh = Get-SqlDscLogin -ServerObject $script:serverObject |
| 97 | + $resultWithRefresh = Get-SqlDscLogin -ServerObject $script:serverObject -Refresh |
| 98 | + |
| 99 | + @($resultWithoutRefresh).Count | Should -Be @($resultWithRefresh).Count |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Context 'When using pipeline input' { |
| 104 | + It 'Should accept ServerObject from pipeline' { |
| 105 | + $result = $script:serverObject | Get-SqlDscLogin -Name 'sa' |
| 106 | + |
| 107 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' |
| 108 | + $result.Name | Should -Be 'sa' |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments