Tests/PemFile/PemFile.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 |
. (Join-Path $PSScriptRoot '../TestCommon.ps1') Describe "Validation - No Default EC2 PemFile" { $fakeInstance = 'i-123456' $fakeRegion = 'us-east-1' $emptyFile = Resolve-Path(Join-Path $PSScriptRoot '../PemFile/empty.txt') function Test-PemFileValidation([string]$FunctionName) { Context $FunctionName { it "should throw error if not specified" { $test = { . $FunctionName -InstanceId $fakeInstance -Region $fakeRegion } $test | Should Throw $exceptionText } $exceptionText = 'Provide an argument that is not null or empty' foreach($case in @($null, '')) { it "should throw '$exceptionText' if '$case' passed" { $test = { . $FunctionName -InstanceId $fakeInstance -Region $fakeRegion -PemFile $case } $test | Should Throw $exceptionText } } $exceptionText = 'Please provide the path to a valid PemFile.' foreach($case in @('x:\unlikelytoexists',$emptyFile)) { it "should throw '$exceptionText' if '$case' passed" { $test = { . $FunctionName -InstanceId $fakeInstance -Region $fakeRegion -PemFile $case } $test | Should Throw $exceptionText } } } } Clear-DefaultEC2PemFile @( 'Get-Ec2Credential' 'New-EC2PSSession' 'Enter-EC2PSSession' 'Enter-EC2RDPSession' ) | ForEach-Object { Test-PemFileValidation -FunctionName $_ } } Describe "Validation - With Default EC2 PemFile set" { $fakeInstance = 'i-123456' $fakeRegion = 'us-east-1' $notemptyFile = Resolve-Path(Join-Path $PSScriptRoot '../PemFile/notempty.txt') function Test-DefaultPemFile([string]$FunctionName) { Context $FunctionName { $exceptionText = 'Please provide the path to a valid PemFile.' it "$FunctionName should NOT throw '$exceptionText' if not specified" { $test = { . $FunctionName -InstanceId $fakeInstance -Region $fakeRegion } $test | Should Not Throw $exceptionText } } } Set-DefaultEC2PemFile -PemFile $notemptyFile @( 'Get-Ec2Credential' 'New-EC2PSSession' 'Enter-EC2PSSession' 'Enter-EC2RDPSession' ) | ForEach-Object { Test-DefaultPemFile -FunctionName $_ } } |