Skip to content

Commit

Permalink
Convert tests from rspec to pester (#290)
Browse files Browse the repository at this point in the history
We've had a lot of trouble fighting with ruby - mainly around rubygems. It's just not fun.
Combine that with unfamiliarity and a "why are we using ruby for tests when the rest of the code is in powershell" disconnect, it makes a lot of sense to use Pester as our test framework instead.
  • Loading branch information
matt-richardson authored Nov 3, 2021
1 parent fddf416 commit fc9cfd5
Show file tree
Hide file tree
Showing 78 changed files with 3,432 additions and 1,549 deletions.
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

1 change: 0 additions & 1 deletion E2ETests/.rspec

This file was deleted.

7 changes: 0 additions & 7 deletions E2ETests/Gemfile

This file was deleted.

64 changes: 0 additions & 64 deletions E2ETests/Gemfile.lock

This file was deleted.

6 changes: 0 additions & 6 deletions E2ETests/Rakefile

This file was deleted.

11 changes: 6 additions & 5 deletions E2ETests/Scenarios/Server_Scenario_01_Install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ Configuration Server_Scenario_01_Install
$user = $repository.Users.GetCurrent()
$createApiKeyResult = $repository.Users.CreateApiKey($user, "Octopus DSC Testing")

#save it to enviornment variables for tests to use
[environment]::SetEnvironmentVariable("OctopusServerUrl", "http://localhost:81", "User")
[environment]::SetEnvironmentVariable("OctopusServerUrl", "http://localhost:81", "Machine")
[environment]::SetEnvironmentVariable("OctopusApiKey", $createApiKeyResult.ApiKey, "User")
[environment]::SetEnvironmentVariable("OctopusApiKey", $createApiKeyResult.ApiKey, "Machine")
#save it to file for tests to use
$content = @{
"OctopusServerUrl" = "http://localhost:81";
"OctopusApiKey" = $createApiKeyResult.ApiKey;
}
set-content "c:\temp\octopus-configured.marker" ($content | ConvertTo-Json)
}
TestScript = {
Add-Type -Path "${env:ProgramFiles}\Octopus Deploy\Octopus\Newtonsoft.Json.dll"
Expand Down
11 changes: 6 additions & 5 deletions E2ETests/Scenarios/Server_Scenario_07_Reinstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ Configuration Server_Scenario_07_Reinstall

write-verbose "setting OctopusApiKey to $($createApiKeyResult.ApiKey)"

#save it to enviornment variables for tests to use
[environment]::SetEnvironmentVariable("OctopusServerUrl", "http://localhost:81", "User")
[environment]::SetEnvironmentVariable("OctopusServerUrl", "http://localhost:81", "Machine")
[environment]::SetEnvironmentVariable("OctopusApiKey", $createApiKeyResult.ApiKey, "User")
[environment]::SetEnvironmentVariable("OctopusApiKey", $createApiKeyResult.ApiKey, "Machine")
#save it to file for tests to use
$content = @{
"OctopusServerUrl" = "http://localhost:81";
"OctopusApiKey" = $createApiKeyResult.ApiKey;
}
set-content "c:\temp\octopus-configured.marker" ($content | ConvertTo-Json)
}
TestScript = {
Add-Type -Path "${env:ProgramFiles}\Octopus Deploy\Octopus\Newtonsoft.Json.dll"
Expand Down
29 changes: 29 additions & 0 deletions E2ETests/Spec/TestHelpers/Get-EnvironmentDetails.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Get-EnvironmentDetails {
[CmdletBinding()]
[OutputType([HashTable])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$EnvironmentName,
[Parameter(Mandatory=$false)]
[string]
$SpaceId
)

$serverSupportsSpaces = Test-ServerSupportsSpaces $OctopusServerUrl
if ($serverSupportsSpaces -and (-not [string]::IsNullOrEmpty($SpaceId))) {
$spaceFragment = "$SpaceId/"
}

[PSCustomObject]$environment = Get-EnvironmentViaApi -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -EnvironmentName $EnvironmentName -SpaceFragment $spaceFragment

return @{
Exists = $null -ne $environment;
}
}
26 changes: 26 additions & 0 deletions E2ETests/Spec/TestHelpers/Get-SpaceDetails.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Get-SpaceDetails {
[CmdletBinding()]
[OutputType([HashTable])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$SpaceName
)

[PSCustomObject]$space = Get-SpaceViaApi -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -SpaceName $SpaceName
$exists = $null -ne $space
$description = ""
if ($exists) {
$description = $space.Description
}
return @{
Exists = $exists;
Description = $description;
}
}
56 changes: 56 additions & 0 deletions E2ETests/Spec/TestHelpers/Get-TentacleDetails.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function Get-TentacleDetails {
[CmdletBinding()]
[OutputType([HashTable])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$InstanceName,
[Parameter(Mandatory=$false)]
[string]
$SpaceId
)

[bool]$exists = Test-Path "c:\program files\Octopus Deploy\Tentacle\Tentacle.exe"
[PSCustomObject]$machine = @{}
[string[]]$environments = @()
[string[]]$tenants = @()
[string]$machinePolicy = $null
[bool]$isOnline = $false
[string]$communicationStyle = $Null

if ($exists) {
$thumbprint = Get-Thumbprint $InstanceName
$serverSupportsSpaces = Test-ServerSupportsSpaces $OctopusServerUrl
if ($serverSupportsSpaces -and (-not [string]::IsNullOrEmpty($SpaceId))) {
$spaceFragment = "$SpaceId/"
}
$machine = Get-MachineViaApi -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -Thumbprint $thumbprint -SpaceFragment $spaceFragment
$communicationStyle = $machine.Endpoint.CommunicationStyle
$environments = (Get-Environments -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -SpaceFragment $spaceFragment) | Where-Object { $machine.EnvironmentIds -contains $_.Id } | Select-Object -ExpandProperty Name
$tenants = (Get-Tenants -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -SpaceFragment $spaceFragment) | Where-Object { $machine.TenantIds -contains $_.Id } | Select-Object -ExpandProperty Name
$machinePolicy = (Get-MachinePolicies -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -SpaceFragment $spaceFragment) | Where-Object { $machine.MachinePolicyId -contains $_.Id } | Select-Object -ExpandProperty Name
$isOnline = Test-IsTentacleOnline -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -thumbprint $thumbprint -SpaceFragment $spaceFragment
}

return @{
Exists = $exists;
IsRegisteredWithTheServer = $null -ne $machine;
IsOnline = $isOnline
IsListening = $communicationStyle -eq "TentaclePassive"
IsPolling = $communicationStyle -eq "TentacleActive"
Environments = $environments
Roles = $machine.Roles
DisplayName = $machine.Name
Tenants = $tenants
TenantTags = $machine.TenantTags
Policy = $machinePolicy
TenantedDeploymentParticipation = $machine.TenantedDeploymentParticipation
Endpoint = $machine.Uri
}
}
43 changes: 43 additions & 0 deletions E2ETests/Spec/TestHelpers/Get-WorkerDetails.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Get-WorkerDetails {
[CmdletBinding()]
[OutputType([HashTable])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$InstanceName,
[Parameter(Mandatory=$false)]
[string]
$SpaceId
)

[bool]$exists = Test-Path "c:\program files\Octopus Deploy\Tentacle\Tentacle.exe"
[PSCustomObject]$worker = @{}
[bool]$isOnline = $false
[string]$communicationStyle = $Null

if ($exists) {
$thumbprint = Get-Thumbprint $InstanceName
$serverSupportsSpaces = Test-ServerSupportsSpaces $OctopusServerUrl
if ($serverSupportsSpaces -and (-not [string]::IsNullOrEmpty($SpaceId))) {
$spaceFragment = "$SpaceId/"
}
$worker = Get-WorkerViaApi -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -Thumbprint $thumbprint -SpaceFragment $spaceFragment
$communicationStyle = $worker.Endpoint.CommunicationStyle
$isOnline = Test-IsWorkerOnline -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -thumbprint $thumbprint -SpaceFragment $spaceFragment
}

return @{
Exists = $exists;
IsRegisteredWithTheServer = $null -ne $worker;
IsOnline = $isOnline
IsListening = $communicationStyle -eq "TentaclePassive"
IsPolling = $communicationStyle -eq "TentacleActive"
DisplayName = $worker.Name
}
}
29 changes: 29 additions & 0 deletions E2ETests/Spec/TestHelpers/Get-WorkerPoolDetails.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Get-WorkerPoolDetails {
[CmdletBinding()]
[OutputType([HashTable])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$WorkerPoolName,
[Parameter(Mandatory=$false)]
[string]
$SpaceId
)

$serverSupportsSpaces = Test-ServerSupportsSpaces $OctopusServerUrl
if ($serverSupportsSpaces -and (-not [string]::IsNullOrEmpty($SpaceId))) {
$spaceFragment = "$SpaceId/"
}

[PSCustomObject]$workerPool = Get-WorkerPoolViaApi -OctopusServerUrl $OctopusServerUrl -OctopusApiKey $OctopusApiKey -WorkerPoolName $WorkerPoolName -SpaceFragment $spaceFragment

return @{
Exists = $null -ne $workerPool;
}
}
25 changes: 25 additions & 0 deletions E2ETests/Spec/TestHelpers/Private/Get-EnvironmentViaApi.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Get-EnvironmentViaApi {
[CmdletBinding()]
[OutputType([PSCustomObject])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$EnvironmentName,
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]
$spaceFragment
)

$url = "$OctopusServerUrl/api/$($spaceFragment)environments/all?api-key=$OctopusApiKey"
$response = Invoke-RestMethod $url
$environment = $response | Where-Object { $_.Name -eq $EnvironmentName }

return $environment
}
19 changes: 19 additions & 0 deletions E2ETests/Spec/TestHelpers/Private/Get-Environments.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Get-Environments {
[CmdletBinding()]
[OutputType([PSCustomObject])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]
$spaceFragment
)

$url = "$OctopusServerUrl/api/$($spaceFragment)environments/all?api-key=$OctopusApiKey"
return Invoke-RestMethod $url
}
19 changes: 19 additions & 0 deletions E2ETests/Spec/TestHelpers/Private/Get-MachinePolicies.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Get-MachinePolicies {
[CmdletBinding()]
[OutputType([PSCustomObject])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]
$spaceFragment
)

$url = "$OctopusServerUrl/api/$($spaceFragment)machinepolicies/all?api-key=$OctopusApiKey"
return Invoke-RestMethod $url
}
25 changes: 25 additions & 0 deletions E2ETests/Spec/TestHelpers/Private/Get-MachineViaApi.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Get-MachineViaApi {
[CmdletBinding()]
[OutputType([PSCustomObject])]
param (
[Parameter(Mandatory=$true)]
[string]
$OctopusServerUrl,
[Parameter(Mandatory=$true)]
[string]
$OctopusApiKey,
[Parameter(Mandatory=$true)]
[string]
$thumbprint,
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]
$spaceFragment
)

$url = "$OctopusServerUrl/api/$($spaceFragment)machines/all?api-key=$OctopusApiKey"
$response = Invoke-RestMethod $url
$machine = $response | Where-Object { $_.Thumbprint -eq $thumbprint }

return $machine
}
Loading

0 comments on commit fc9cfd5

Please sign in to comment.