public/GoldSourceRcon.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

Describe "GoldSourceRcon" -Tag 'Unit' {

    Context 'Runs' {

        $gameservers = [ordered]@{
            # Goldsource
            cstrike = @{
                Address = 'cs.startersclan.com'
                Port = 27815
            }
            czero = @{
                Address = 'cs.startersclan.com'
                Port = 27615
            }
            valve = @{
                Address = 'hl.startersclan.com'
                Port = 27915
            }
        }

        It 'Handles errors (error stream)' {
            $password = 'foo'
            $command = 'status'
            $ErrorActionPreference = 'Continue'
            Mock Write-Verbose {
                throw 'some error'
            }

            foreach ($game in $gameservers.Keys) {
                $params = $gameservers[$game]
                $err = GoldSourceRcon @params -Password $password -Command $command 2>&1
                $err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | % { $_.Exception.Message } | Should -Match 'some error'
            }
        }

        It 'Handles errors (exception)' {
            $password = 'foo'
            $command = 'status'
            $ErrorActionPreference = 'Stop'
            Mock Write-Verbose {
                throw 'some exception'
            }

            foreach ($game in $gameservers.Keys) {
                $params = $gameservers[$game]
                { GoldSourceRcon @params -Password $password -Command $command } | Should -Throw 'some exception'
            }
        }

        It 'Fails when rcon password is wrong' {
            $password = "$( Get-Random -Minimum 1 -Maximum 1000000 )"
            $command = 'status'
            $ErrorActionPreference = 'Stop'
            Mock Write-Verbose {}

            foreach ($game in $gameservers.Keys) {
                $params = $gameservers[$game]
                { GoldSourceRcon @params -Password $password -Command $command } | Should -Throw 'Bad rcon_password'
            }

        }

    }

}