tests/Remove-SSHSession.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
Import-Module .\Posh-SSH.psd1

Describe "Remove-SSHSession" {
    Context "Parameters" { 
                                 
    }

    Context "Action" { 
        
        $Global:SshSessions = New-Object System.Collections.ArrayList
        $session = New-Object SSH.SshSession
        $session.host = "dummy"
        $session.SessionId = 0
        $session.Session = New-Object Renci.SshNet.SshClient -ArgumentList "localhost","user","pass"
        $Global:SshSessions.Add($session)

        It "Should remove a session by index" {
            Remove-SSHSession -Index 0
            $sessions = Get-SSHSession
            $sessions | should be $null
        }

        It "Removing a non-existing session should not throw" {
            { Remove-SSHSession -SessionId 0 } | Should not throw
        }

        $Global:SshSessions.Add($session)

        It "Should remove a session submitted through the pipeline" {
            $session | Remove-SSHSession

            $sessions = Get-SSHSession
            $sessions | Should be $null
        }

        $Global:SshSessions.Add($session)

        It "Should remove a session by parameter" {
            Remove-SSHSession -SSHSession $session
            $sessions = Get-SSHSession
            $sessions | Should be $null
        }
                             
    }
}

Remove-Module Posh-SSH -ErrorAction SilentlyContinue
Remove-Variable sshsessions -Scope Global -ErrorAction SilentlyContinue