From 62eea1878af00d1347cd4639d1f1809189bd6b89 Mon Sep 17 00:00:00 2001 From: Alexander Krasnukhin Date: Tue, 28 Jun 2016 01:36:56 +0200 Subject: [PATCH] Add tests for ps1 --- appveyor.yml | 32 ++++++++++++++ tests/runtests.ps1 | 105 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 appveyor.yml create mode 100644 tests/runtests.ps1 diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..6c7b57b --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,32 @@ +platform: Any CPU + +environment: + matrix: + - bootstrap_args: + test_args: -master salt -runservice + + - bootstrap_args: -runservice false + test_args: -master salt -noservice + + - bootstrap_args: -minion a-minion + test_args: -minion a-minion -master salt -runservice + + - bootstrap_args: -minion a-minion -master a-master + test_args: -minion a-minion -master a-master -runservice + +build_script: + - ps: | + $Path = (Get-Location).Path | Join-Path -ChildPath bootstrap-salt.ps1 + Invoke-Expression "$Path -verbose $env:bootstrap_args" + +before_test: + - SET PATH=%PATH%;C:\salt + - salt-call --version + +test_script: + - ps: | + $Path = (Get-Location).Path | Join-Path -ChildPath tests\runtests.ps1 + Invoke-Expression "$Path -verbose $env:test_args" + +matrix: + fast_finish: true diff --git a/tests/runtests.ps1 b/tests/runtests.ps1 new file mode 100644 index 0000000..3d83b85 --- /dev/null +++ b/tests/runtests.ps1 @@ -0,0 +1,105 @@ +<# +.SYNOPSIS + A simple Powershell script to test installed salt minion on windows. + +.PARAMETER version + Salt version installed. + +.PARAMETER runservice + Boolean flag whenever to test if service is running. + +.PARAMETER noservice + Boolean flag whenever to test if service is not running. + +.PARAMETER minion + Name of the minion installed on this host. + +.PARAMETER master + Name of the master configured on this host. + +.EXAMPLE + ./runtests.ps1 + Runs without any parameters. Uses all the default values/settings. +#> + +#=============================================================================== +# Commandlet Binding +#=============================================================================== +[CmdletBinding()] +Param( + [Parameter(Mandatory=$False,ValueFromPipeline=$True)] + [string]$version = $null, + + [Parameter(Mandatory=$False,ValueFromPipeline=$True)] + [switch]$runservice, + + [Parameter(Mandatory=$False,ValueFromPipeline=$True)] + [switch]$noservice, + + [Parameter(Mandatory=$False,ValueFromPipeline=$True)] + [string]$minion = $null, + + [Parameter(Mandatory=$False,ValueFromPipeline=$True)] + [string]$master = $null +) + +#=============================================================================== +# Script Functions +#=============================================================================== +function Get-Grains ([string]$Name) { + $Command = "salt-call --local --out json --out-indent -1 grains.get $Name" + $Result = iex $Command | Out-String | ConvertFrom-Json + + Write-Verbose "salt-call grains.get ${Name}:`n${Result}" + return $Result."local" +} + +function Get-Service-Status([string]$Name) { + $Service = Get-Service $Name -ErrorAction Stop + $Status = $Service.Status + + Write-Verbose "${Name}: ${Status}" + return $Status +} + +function Assert-Equal { + [CmdletBinding()] + Param ( + [Parameter(Mandatory=$True,ValueFromPipeline=$True)] + [string]$Actual, + + [Parameter(Mandatory=$True,ValueFromPipeline=$True)] + [string]$Expected + ) + + If ($Actual -ne $Expected) { + throw "Assert: $Actual != $Expected" + } +} + +#=============================================================================== +# Do enabled checks +#=============================================================================== +if ($True) { + Get-Grains -Name os_family | Assert-Equal -Expected "Windows" +} + +if ($version) { + Get-Grains -Name saltversion | Assert-Equal -Expected $version +} + +if ($master) { + Get-Grains -Name master | Assert-Equal -Expected $master +} + +if ($minion) { + Get-Grains -Name id | Assert-Equal -Expected $minion +} + +if ($runservice) { + Get-Service-Status salt-minion | Assert-Equal -Expected "Running" +} + +if ($noservice) { + Get-Service-Status salt-minion | Assert-Equal -Expected "Stopped" +}