Skip to content

Commit

Permalink
Rework Update-SteamServer (#82)
Browse files Browse the repository at this point in the history
* test if curent user is admin

* Refactor Pester tests

* refactor & pester tests

* typo

* add end of line
  • Loading branch information
hjorslev authored May 13, 2024
1 parent 7ce8993 commit 62f8ada
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 38 deletions.
24 changes: 21 additions & 3 deletions SteamPS/Public/Server/Update-SteamServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

[Parameter(Mandatory = $false)]
[Alias('LogLocation')]
[string]$LogPath = "C:\DedicatedServers\Logs",
[string]$LogPath = 'C:\DedicatedServers\Logs',

[Parameter(Mandatory = $false)]
[string]$DiscordWebhookUri,
Expand All @@ -103,13 +103,31 @@

begin {
if ($null -eq (Get-SteamPath)) {
throw 'SteamCMD could not be found in the env:Path. Have you executed Install-SteamCMD?'
$Exception = [Exception]::new('SteamCMD could not be found in the env:Path. Have you executed Install-SteamCMD?')
$ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
$Exception,
'SteamCMDNotInstalled',
[System.Management.Automation.ErrorCategory]::NotInstalled,
(Test-Admin)
)
$PSCmdlet.ThrowTerminatingError($ErrorRecord)
}

if ((Test-Admin) -eq $false) {
$Exception = [Exception]::new('The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.')
$ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
$Exception,
'MissingUserPermissions',
[System.Management.Automation.ErrorCategory]::PermissionDenied,
(Test-Admin)
)
$PSCmdlet.ThrowTerminatingError($ErrorRecord)
}

# Log settings
$PSFLoggingProvider = @{
Name = 'logfile'
InstanceName = '<taskname>'
InstanceName = "Update game server $ServiceName"
FilePath = "$LogPath\$ServiceName\$ServiceName-%Date%.csv"
Enabled = $true
LogRotatePath = "$LogPath\$ServiceName\$ServiceName-*.csv"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,3 @@
}
}
}

Describe 'SteamCMD cmdlets' {
BeforeAll {
. "$($SteamPSModulePath)\Private\Server\Add-EnvPath.ps1"
Add-EnvPath -Path 'TestDrive:\Test\SteamCMD'

Install-SteamCMD -InstallPath 'TestDrive:\Test' -Force
}

It 'Finds steamcmd.exe' {
Test-Path -Path "$TestDrive\Test\SteamCMD\steamcmd.exe" | Should -BeTrue
}

Context 'Update-SteamApp' {
It 'Installs Ground Branch Dedicated Server using AppID' {
Update-SteamApp -AppID 476400 -Path "$TestDrive\GB-AppID" -Force
Test-Path -Path "$TestDrive\GB-AppID\GroundBranchServer.exe" | Should -BeTrue
}

It 'Installs Ground Branch Dedicated Server using Application Name' {
Update-SteamApp -ApplicationName 'Ground Branch D' -Path "$TestDrive\GB-AppName" -Force
Test-Path -Path "$TestDrive\GB-AppName\GroundBranchServer.exe" | Should -BeTrue
}

It 'Passes custom argument and installs testing branch of Ground Branch Dedicated Server' {
Update-SteamApp -AppID 476400 -Path "$TestDrive\GB-TestingBranch" -Arguments "-beta testing" -Force
Test-Path -Path "$TestDrive\GB-TestingBranch\GroundBranchServer.exe" | Should -BeTrue
}
}

AfterAll {
# Wait for the process steamerrorreporter to be closed - else test folder wont be deleted.
Wait-Process -Name 'steamerrorreporter' -ErrorAction SilentlyContinue
}
}
22 changes: 22 additions & 0 deletions Tests/Integration/Public/Install-SteamCMD.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Describe 'Install-SteamCMD Tests' -Tag 'Integration' {
BeforeAll {
. "$($SteamPSModulePath)\Private\Server\Add-EnvPath.ps1"
Add-EnvPath -Path 'TestDrive:\Test\SteamCMD'
}

Context 'When executing Install-SteamCMD' {
It 'Should install SteamCMD' {
Install-SteamCMD -InstallPath 'TestDrive:\Test' -Force
}

It 'Should find steamcmd.exe' {
Test-Path -Path "$TestDrive\Test\SteamCMD\steamcmd.exe" | Should -BeTrue
}
}

AfterAll {
# Wait for the process steamerrorreporter to be closed - else test folder wont be deleted.
Wait-Process -Name 'steamerrorreporter' -ErrorAction SilentlyContinue
}
}
33 changes: 33 additions & 0 deletions Tests/Integration/Public/Update-SteamApp.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

Describe 'Update-SteamApp Tests' -Tag 'Integration' {
BeforeAll {
. "$($SteamPSModulePath)\Private\Server\Add-EnvPath.ps1"
Add-EnvPath -Path 'TestDrive:\Test\SteamCMD'

if ((Test-Path -Path "$TestDrive\Test\SteamCMD\steamcmd.exe") -eq $false) {
Install-SteamCMD -InstallPath 'TestDrive:\Test' -Force
}
}

Context 'When executing Update-SteamApp' {
It 'Installs Ground Branch Dedicated Server using AppID' {
Update-SteamApp -AppID 476400 -Path "$TestDrive\GB-AppID" -Force
Test-Path -Path "$TestDrive\GB-AppID\GroundBranchServer.exe" | Should -BeTrue
}

It 'Installs Ground Branch Dedicated Server using Application Name' {
Update-SteamApp -ApplicationName 'Ground Branch D' -Path "$TestDrive\GB-AppName" -Force
Test-Path -Path "$TestDrive\GB-AppName\GroundBranchServer.exe" | Should -BeTrue
}

It 'Passes custom argument and installs testing branch of Ground Branch Dedicated Server' {
Update-SteamApp -AppID 476400 -Path "$TestDrive\GB-TestingBranch" -Arguments "-beta testing" -Force
Test-Path -Path "$TestDrive\GB-TestingBranch\GroundBranchServer.exe" | Should -BeTrue
}
}

AfterAll {
# Wait for the process steamerrorreporter to be closed - else test folder wont be deleted.
Wait-Process -Name 'steamerrorreporter' -ErrorAction SilentlyContinue
}
}
39 changes: 39 additions & 0 deletions Tests/Unit/Public/Update-SteamServer.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Describe 'Update-SteamServer Tests' -Tag 'Unit' {
Context 'When executing Update-SteamServer without SteamCMD installed' {
BeforeAll {
Mock -CommandName Test-Admin -ModuleName SteamPS -MockWith {
return $true
}
Mock -CommandName Get-SteamPath -ModuleName SteamPS -MockWith {
return $null
}
Mock -CommandName Get-Service -ModuleName SteamPS -MockWith {
return @{ Name = 'GB-PG10' }
}
}
It 'Should throw an error' {
{ Update-SteamServer -AppID 476400 -ServiceName 'GB-PG10' -IPAddress '1.1.1.1' -Port 27015 } | Should -Throw 'SteamCMD could not be found in the env:Path. Have you executed Install-SteamCMD?'
}
}

Context 'When executing Update-SteamServer with insufficient permissions' {
BeforeAll {
Mock -CommandName Test-Admin -ModuleName SteamPS -MockWith {
return $false
}
Mock -CommandName Get-SteamPath -ModuleName SteamPS -MockWith {
return [PSCustomObject]@{
'Path' = 'C:\Program Files\'
'Executable' = 'C:\Program Files\steamcmd.exe'
}
}
Mock -CommandName Get-Service -ModuleName SteamPS -MockWith {
return @{ Name = 'GB-PG10' }
}
}
It 'Should throw an error' {
{ Update-SteamServer -AppID 476400 -ServiceName 'GB-PG10' -IPAddress '1.1.1.1' -Port 27015 } | Should -Throw 'The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.'
}
}
}

0 comments on commit 62f8ada

Please sign in to comment.