Moc.Tests.ps1

ipmo .\moc.psm1 -force

BeforeAll {
    $global:config = @{}
    $global:config[$global:MocModule] = @{}
}

Describe 'Set-MocConfig' {
    It 'Should be able to create ssh configuration and set-mocconfig without IP' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test"
        $sshConfig.Name | should -Be "test"
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $false
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -BeNullOrEmpty
        (Get-MocConfig).sshIPAddresses | should -BeNullOrEmpty
    }

    It 'Should be able to create ssh configuration and set-mocconfig with IP Address' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -ipaddresses @('127.0.0.1')
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $false
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -BeNullOrEmpty
        (Get-MocConfig).sshIPAddresses | should -Be "127.0.0.1"
    }

    It 'Should be able to create ssh configuration and set-mocconfig with IP Addresses' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -ipaddresses @('127.0.0.1', '127.0.0.2')
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $false
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -BeNullOrEmpty
        (Get-MocConfig).sshIPAddresses | should -Be "127.0.0.1,127.0.0.2"
    }

    It 'Should be able to create ssh configuration and set-mocconfig with CIDR' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -cidr '127.0.0.1/25'
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $false
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -Be "127.0.0.1/25"
        (Get-MocConfig).sshIPAddresses | should -BeNullOrEmpty
    }

    It 'Should be able to create ssh configuration and set-mocconfig with sshRestriction' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -cidr '127.0.0.1/25' -restrictSSHCommands
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $true
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -Be "127.0.0.1/25"
        (Get-MocConfig).sshIPAddresses | should -BeNullOrEmpty
    }

    It 'Should be able to create ssh configuration and set-mocconfig with public & private key' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        Set-Content -value "1234" -path "C:\test\temp.pub"
        Set-Content -value "4567" -path "C:\test\temp"
        $sshConfig = New-SSHConfiguration -name "test" -cidr '127.0.0.1/25' -restrictSSHCommands -sshPublicKey "C:\test\temp.pub" -sshPrivateKey "C:\test\temp"
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $true
        (Get-MocConfig).sshPublicKey | should -Be "C:\test\temp.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\test\temp"
        (Get-MocConfig).sshCidr | should -Be "127.0.0.1/25"
        (Get-MocConfig).sshIPAddresses | should -BeNullOrEmpty
    }

    It 'Get-SSHAuthConfig with public & private key' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        Set-Content -value "1234" -path "C:\test\temp.pub"
        Set-Content -value "4567" -path "C:\test\temp"
        $sshConfig = New-SSHConfiguration -name "test" -cidr '127.0.0.1/25' -restrictSSHCommands -sshPublicKey "C:\test\temp.pub" -sshPrivateKey "C:\test\temp"
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $true
        (Get-MocConfig).sshPublicKey | should -Be "C:\test\temp.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\test\temp"
        (Get-MocConfig).sshCidr | should -Be "127.0.0.1/25"
        (Get-MocConfig).sshIPAddresses | should -BeNullOrEmpty
        Get-SSHAuthConfig | should -Be "from=""127.0.0.1/25"" 1234"
    }

    It 'Get-SSHAuthConfig without public & private key' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -cidr '127.0.0.1/25' -restrictSSHCommands
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $true
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -Be "127.0.0.1/25"
        (Get-MocConfig).sshIPAddresses | should -BeNullOrEmpty
        $content = get-content -path  "C:\AksHci\.ssh\akshci_rsa.pub"
        Get-SSHAuthConfig | should -Be "from=""127.0.0.1/25"" $content"
    }

    It 'Get-SSHAuthConfig with ip Address' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -ipaddresses @('127.0.0.1')
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $false
        (Get-MocConfig).sshPublicKey | should -Be "C:\AksHci\.ssh\akshci_rsa.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\AksHci\.ssh\akshci_rsa"
        (Get-MocConfig).sshCidr | should -BeNullOrEmpty
        (Get-MocConfig).sshIPAddresses | should -Be "127.0.0.1"
        $content = get-content -path  "C:\AksHci\.ssh\akshci_rsa.pub"
        Get-SSHAuthConfig | should -Be "from=""127.0.0.1"" $content"
    }

    It 'Get-SSHAuthConfig with ip Addresses' {
        Save-ConfigurationDirectory -moduleName $global:AksHciModule -workingDir "C:\test"
        $sshConfig = New-SSHConfiguration -name "test" -ipaddresses @('127.0.0.1','127.0.0.2') -sshPublicKey "C:\test\temp.pub" -sshPrivateKey "C:\test\temp"
        Set-Content -value "1234" -path "C:\test\temp.pub"
        Set-Content -value "4567" -path "C:\test\temp"
        $vnet =  [VirtualNetwork]::new("defaultswitch", "Default Switch" , "", "", $(), "", 0,  "", "",  "", "")
        Set-MocConfig -ssh $sshConfig -vnet $vnet -skipHostLimitChecks -skipRemotingChecks -skipValidationCheck
        (Get-MocConfig).sshConfigName | should -Be "test"
        (Get-MocConfig).sshRestrictCommands | should -Be $false
        (Get-MocConfig).sshPublicKey | should -Be "C:\test\temp.pub"
        (Get-MocConfig).sshPrivateKey | should -Be "C:\test\temp"
        (Get-MocConfig).sshCidr | should -BeNullOrEmpty
        (Get-MocConfig).sshIPAddresses | should -Be "127.0.0.1,127.0.0.2"
        Get-SSHAuthConfig | should -Be "from=""127.0.0.1,127.0.0.2"" 1234"
    }
}