|
| 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 has 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 has 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 | + # Loading stub cmdlets |
| 32 | + Import-Module -Name "$PSScriptRoot/../../Stubs/SqlServerStub.psm1" -Force |
| 33 | + |
| 34 | + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName |
| 35 | + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName |
| 36 | + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName |
| 37 | +} |
| 38 | + |
| 39 | +AfterAll { |
| 40 | + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') |
| 41 | + $PSDefaultParameterValues.Remove('Mock:ModuleName') |
| 42 | + $PSDefaultParameterValues.Remove('Should:ModuleName') |
| 43 | + |
| 44 | + # Unload the module being tested so that it doesn't impact any other tests. |
| 45 | + Get-Module -Name $script:dscModuleName -All | Remove-Module -Force |
| 46 | +} |
| 47 | + |
| 48 | +Describe 'Get-SqlDscLogin' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 49 | + BeforeAll { |
| 50 | + $mockInstanceName = $env:COMPUTERNAME |
| 51 | + $mockSqlCredential = $null |
| 52 | + $mockConnectTimeout = 30 |
| 53 | + $mockEncryptConnection = $true |
| 54 | + |
| 55 | + if ($env:INTEGRATION_TESTS_SQLSERVER_CREDENTIAL) |
| 56 | + { |
| 57 | + <# |
| 58 | + This is set by the pipeline so we can run the tests towards the |
| 59 | + SQL Server instance on the AppVeyor build worker. |
| 60 | + #> |
| 61 | + $mockSqlCredential = (ConvertFrom-Json -InputObject $env:INTEGRATION_TESTS_SQLSERVER_CREDENTIAL) |
| 62 | + } |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName $mockInstanceName -Credential $mockSqlCredential -ConnectTimeout $mockConnectTimeout -EncryptConnection $mockEncryptConnection |
| 67 | + } |
| 68 | + catch |
| 69 | + { |
| 70 | + Set-ItResult -Skip -Because 'Could not connect to SQL Server instance' |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Context 'When getting all SQL Server logins' { |
| 75 | + It 'Should return an array of Login objects' { |
| 76 | + $result = Get-SqlDscLogin -ServerObject $serverObject |
| 77 | + |
| 78 | + <# |
| 79 | + Casting to array to ensure we get the count on Windows PowerShell |
| 80 | + when there is only one login. |
| 81 | + #> |
| 82 | + @($result).Count | Should -BeGreaterOrEqual 1 |
| 83 | + $result[0] | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' |
| 84 | + } |
| 85 | + |
| 86 | + It 'Should return system logins including sa' { |
| 87 | + $result = Get-SqlDscLogin -ServerObject $serverObject |
| 88 | + |
| 89 | + $result.Name | Should -Contain 'sa' |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Context 'When getting a specific SQL Server login' { |
| 94 | + It 'Should return the specified login when it exists' { |
| 95 | + $result = Get-SqlDscLogin -ServerObject $serverObject -Name 'sa' |
| 96 | + |
| 97 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' |
| 98 | + $result.Name | Should -Be 'sa' |
| 99 | + $result.LoginType | Should -Be 'SqlLogin' |
| 100 | + } |
| 101 | + |
| 102 | + It 'Should throw an error when the login does not exist' { |
| 103 | + { Get-SqlDscLogin -ServerObject $serverObject -Name 'NonExistentLogin' -ErrorAction 'Stop' } | |
| 104 | + Should -Throw -ExpectedMessage "There is no login with the name 'NonExistentLogin'." |
| 105 | + } |
| 106 | + |
| 107 | + It 'Should return null when the login does not exist and error action is SilentlyContinue' { |
| 108 | + $result = Get-SqlDscLogin -ServerObject $serverObject -Name 'NonExistentLogin' -ErrorAction 'SilentlyContinue' |
| 109 | + |
| 110 | + $result | Should -BeNullOrEmpty |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + Context 'When using the Refresh parameter' { |
| 115 | + It 'Should return the same results with and without Refresh' { |
| 116 | + $resultWithoutRefresh = Get-SqlDscLogin -ServerObject $serverObject |
| 117 | + $resultWithRefresh = Get-SqlDscLogin -ServerObject $serverObject -Refresh |
| 118 | + |
| 119 | + $resultWithoutRefresh.Count | Should -Be $resultWithRefresh.Count |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + Context 'When using pipeline input' { |
| 124 | + It 'Should accept ServerObject from pipeline' { |
| 125 | + $result = $serverObject | Get-SqlDscLogin -Name 'sa' |
| 126 | + |
| 127 | + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Login' |
| 128 | + $result.Name | Should -Be 'sa' |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments