From b60a96ad05c2eba24f1beb88f8febe1807f9edb2 Mon Sep 17 00:00:00 2001 From: Justin Smith Date: Thu, 2 Mar 2017 14:48:48 -0800 Subject: [PATCH] SQL tests --- Public/Sql.ps1 | 51 ++++++++++++++++++++++++++++++++++++++++ Tests/poshspec.Tests.ps1 | 13 ++++++++++ poshspecdemo.ps1 | 4 ++++ 3 files changed, 68 insertions(+) create mode 100644 Public/Sql.ps1 diff --git a/Public/Sql.ps1 b/Public/Sql.ps1 new file mode 100644 index 0000000..a2687e1 --- /dev/null +++ b/Public/Sql.ps1 @@ -0,0 +1,51 @@ +<# +.SYNOPSIS + Test sql connections +.DESCRIPTION + This function will test connectivity to a SQL server. It does not run any select statements. +.PARAMETER Target + The database computer name to test. Include the instance, if needed. +.PARAMETER Qualifier + The connection string uri +.PARAMETER Property + Specifies a property of the database to test +.PARAMETER Should + A Script Block defining a Pester Assertion. +.EXAMPLE + Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" AccessToken { Should Be $null } +.EXAMPLE + Sql "server60" $ConnectionString State { Should Match "Closed" } +.NOTES + Assertions: Be, BeExactly, Match, Exist + Hint: The connection string can be stored as variable in the declaration file. + $connectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" + Sql "server60" $ConnectionString AccessToken { Should Be $null } +#> + +function Sql { + [CmdletBinding()] + param( + [Parameter(Mandatory, Position=1)] + [Alias('Computername','Computername\Instance')] + [string]$Target, + + [PARAMETER(Position=2)] + [Alias('ConnectionString')] + [string]$Qualifier, + + [PARAMETER(Position=3)] + [ValidateSet('AccessToken', 'ClientConnectionId', 'ConnectionString', 'ConnectionTimeout', 'Container', 'Credential', 'Database', 'DataSource', 'FireInfoMessageEventOnUserErrors', 'PacketSize', 'ServerVersion', 'Site', 'State', 'StatisticsEnabled', 'WorkstationId')] + [string]$Property, + + [Parameter(Mandatory,Position=4)] + [scriptblock]$Should + ) + + $ConnectionString = $Qualifier + + $expression = {New-Object System.Data.SqlClient.SqlConnection '$ConnectionString'} + + $params = Get-PoshspecParam -TestName Sql -TestExpression $expression @PSBoundParameters + + Invoke-PoshspecExpression @params +} \ No newline at end of file diff --git a/Tests/poshspec.Tests.ps1 b/Tests/poshspec.Tests.ps1 index 87a2c70..42fb79b 100644 --- a/Tests/poshspec.Tests.ps1 +++ b/Tests/poshspec.Tests.ps1 @@ -648,6 +648,19 @@ Describe 'Test Functions' { $results.Expression | Should Be "GetFeature -Name Telnet-Client | Select-Object -ExpandProperty 'Installed' | Should be `$false" } } + + Context 'Sql' { + + $results = Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportDB;Data Source=server60" State { Should Match "Closed" } + + It "Should return the correct test Name" { + $results.Name | Should Be "Sql property 'State' for 'server60' at 'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportDB;Data Source=server60' Should Match `"Closed`"" + } + + It "Should return the correct test Expression" { + $results.Expression | Should Be "New-Object System.Data.SqlClient.SqlConnection 'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportDB;Data Source=server60' | Select-Object -ExpandProperty 'State' | Should Match `"Closed`"" + } + } } } diff --git a/poshspecdemo.ps1 b/poshspecdemo.ps1 index d705db3..c6a61fd 100644 --- a/poshspecdemo.ps1 +++ b/poshspecdemo.ps1 @@ -45,3 +45,7 @@ Describe 'Volume' { Volume 'ShouldNotExist' { should BeNullOrEmpty} } +Describe 'Sql' { + Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" Database { Should Be "ReportingDB" } + Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" State { Should Match "Closed" } +}