From 3c19f2aa0e23f84c28972aed45a8e769c7a760c2 Mon Sep 17 00:00:00 2001 From: Vivian Thiebaut Date: Mon, 14 Nov 2022 16:50:56 -0500 Subject: [PATCH] Change the way current platform is checked to use True automatic variable --- .../Authorized_keys_fileperm.Tests.ps1 | 6 ++-- regress/pesterTests/CommonUtils.psm1 | 34 ++----------------- .../pesterTests/FileBasedLogging.tests.ps1 | 4 +-- .../pesterTests/Hostkey_fileperm.Tests.ps1 | 7 ++-- regress/pesterTests/Log_fileperm.Tests.ps1 | 5 ++- .../pesterTests/PlatformAbstractLayer.psm1 | 17 +--------- regress/pesterTests/PortForwarding.Tests.ps1 | 3 +- regress/pesterTests/SCP.Tests.ps1 | 2 +- regress/pesterTests/SFTP.Tests.ps1 | 3 +- regress/pesterTests/SSH.Tests.ps1 | 3 +- regress/pesterTests/SSHDConfig.tests.ps1 | 4 +-- 11 files changed, 19 insertions(+), 69 deletions(-) diff --git a/regress/pesterTests/Authorized_keys_fileperm.Tests.ps1 b/regress/pesterTests/Authorized_keys_fileperm.Tests.ps1 index f129d3516..098957bf0 100644 --- a/regress/pesterTests/Authorized_keys_fileperm.Tests.ps1 +++ b/regress/pesterTests/Authorized_keys_fileperm.Tests.ps1 @@ -32,8 +32,7 @@ Describe "Tests for authorized_keys file permission" -Tags "CI" { #skip when the task schedular (*-ScheduledTask) cmdlets does not exist $ts = (get-command get-ScheduledTask -ErrorAction SilentlyContinue) $skip = $ts -eq $null - $platform = Get-Platform - if(($platform -eq [PlatformType]::Windows) -and ([Environment]::OSVersion.Version.Major -le 6)) + if($IsWindows -and ([Environment]::OSVersion.Version.Major -le 6)) { #suppress the firewall blocking dialogue on win7 netsh advfirewall firewall add rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any action=allow dir=in @@ -43,8 +42,7 @@ Describe "Tests for authorized_keys file permission" -Tags "CI" { AfterEach { $tI++ } AfterAll { - $platform = Get-Platform - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { netsh advfirewall firewall delete rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any dir=in } diff --git a/regress/pesterTests/CommonUtils.psm1 b/regress/pesterTests/CommonUtils.psm1 index eff6779d3..109dc5543 100644 --- a/regress/pesterTests/CommonUtils.psm1 +++ b/regress/pesterTests/CommonUtils.psm1 @@ -9,33 +9,6 @@ Add-Type -TypeDefinition @" } "@ -function Get-Platform { - # Use the .NET Core APIs to determine the current platform; if a runtime - # exception is thrown, we are on FullCLR, not .NET Core. - try { - $Runtime = [System.Runtime.InteropServices.RuntimeInformation] - $OSPlatform = [System.Runtime.InteropServices.OSPlatform] - - $IsLinux = $Runtime::IsOSPlatform($OSPlatform::Linux) - $IsOSX = $Runtime::IsOSPlatform($OSPlatform::OSX) - $IsWindows = $Runtime::IsOSPlatform($OSPlatform::Windows) - } catch { - try { - $IsLinux = $false - $IsOSX = $false - $IsWindows = $true - } - catch { } - } - if($IsOSX) { - [PlatformType]::OSX - } elseif($IsLinux) { - [PlatformType]::Linux - } else { - [PlatformType]::Windows - } -} - function Set-FilePermission { param( @@ -94,8 +67,7 @@ function Set-FilePermission function Add-PasswordSetting { param([string] $pass) - $platform = Get-Platform - if ($platform -eq [PlatformType]::Windows) { + if ($IsWindows) { if (-not($env:DISPLAY)) {$env:DISPLAY = 1} $askpass_util = Join-Path $PSScriptRoot "utilities\askpass_util\askpass_util.exe" $env:SSH_ASKPASS=$askpass_util @@ -157,8 +129,8 @@ function Stop-SSHDTestDaemon { foreach ($ps in $p) { $pss =$ps.ToString() -split "\s+"; - $pid = $pss[$pss.length -1] - Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue + $processid = $pss[$pss.length -1] + Stop-Process -Id $processid -Force -ErrorAction SilentlyContinue } #if still running, wait a little while for task to complete $num = 0 diff --git a/regress/pesterTests/FileBasedLogging.tests.ps1 b/regress/pesterTests/FileBasedLogging.tests.ps1 index dd065af07..aba0c938c 100644 --- a/regress/pesterTests/FileBasedLogging.tests.ps1 +++ b/regress/pesterTests/FileBasedLogging.tests.ps1 @@ -48,7 +48,7 @@ Describe "Tests for admin and non-admin file based logs" -Tags "CI" { { Stop-SSHDTestDaemon -Port $port } - if(($platform -eq [PlatformType]::Windows) -and ([Environment]::OSVersion.Version.Major -le 6)) + if($IsWindows -and ([Environment]::OSVersion.Version.Major -le 6)) { #suppress the firewall blocking dialogue on win7 netsh advfirewall firewall add rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any action=allow dir=in @@ -58,7 +58,7 @@ Describe "Tests for admin and non-admin file based logs" -Tags "CI" { AfterEach { $tI++ } AfterAll { - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { netsh advfirewall firewall delete rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any dir=in } diff --git a/regress/pesterTests/Hostkey_fileperm.Tests.ps1 b/regress/pesterTests/Hostkey_fileperm.Tests.ps1 index eb0e9fb34..a6408829e 100644 --- a/regress/pesterTests/Hostkey_fileperm.Tests.ps1 +++ b/regress/pesterTests/Hostkey_fileperm.Tests.ps1 @@ -21,9 +21,8 @@ Describe "Tests for host keys file permission" -Tags "CI" { $ssouser = $OpenSSHTestInfo["SSOUser"] $script:logNum = 0 Remove-Item -Path (Join-Path $testDir "*$logName") -Force -ErrorAction SilentlyContinue - $platform = Get-Platform - $skip = ($platform -eq [PlatformType]::Windows) -and ([Environment]::OSVersion.Version.Major -le 6) -and ([Environment]::OSVersion.Version.Minor -lt 2) - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + $skip = $IsWindows -and ([Environment]::OSVersion.Version.Major -le 6) -and ([Environment]::OSVersion.Version.Minor -lt 2) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { #suppress the firewall blocking dialogue on win7 netsh advfirewall firewall add rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any action=allow dir=in @@ -32,7 +31,7 @@ Describe "Tests for host keys file permission" -Tags "CI" { AfterEach { $tI++ } AfterAll { - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { netsh advfirewall firewall delete rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any dir=in } diff --git a/regress/pesterTests/Log_fileperm.Tests.ps1 b/regress/pesterTests/Log_fileperm.Tests.ps1 index 2edce87fa..1c00f200a 100644 --- a/regress/pesterTests/Log_fileperm.Tests.ps1 +++ b/regress/pesterTests/Log_fileperm.Tests.ps1 @@ -25,8 +25,7 @@ Describe "Tests for log file permission" -Tags "CI" { Remove-Item (Join-Path $testDir "*$logName") -Force -ErrorAction SilentlyContinue - $platform = Get-Platform - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { #suppress the firewall blocking dialogue on win7 netsh advfirewall firewall add rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any action=allow dir=in @@ -84,7 +83,7 @@ Describe "Tests for log file permission" -Tags "CI" { AfterEach {$tI++;} AfterAll { - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { netsh advfirewall firewall delete rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any dir=in } diff --git a/regress/pesterTests/PlatformAbstractLayer.psm1 b/regress/pesterTests/PlatformAbstractLayer.psm1 index a340d1653..5ea304d70 100644 --- a/regress/pesterTests/PlatformAbstractLayer.psm1 +++ b/regress/pesterTests/PlatformAbstractLayer.psm1 @@ -19,26 +19,11 @@ Enum PlatformType { function Set-Platform { # Use the .NET Core APIs to determine the current platform; if a runtime # exception is thrown, we are on FullCLR, not .NET Core. - try { - $Runtime = [System.Runtime.InteropServices.RuntimeInformation] - $OSPlatform = [System.Runtime.InteropServices.OSPlatform] - - $IsLinux = $Runtime::IsOSPlatform($OSPlatform::Linux) - $IsOSX = $Runtime::IsOSPlatform($OSPlatform::OSX) - $IsWindows = $Runtime::IsOSPlatform($OSPlatform::Windows) - } catch { - try { - $IsLinux = $false - $IsOSX = $false - $IsWindows = $true - } - catch { } - } if($IsOSX) { [PlatformType]::OSX } elseif($IsLinux) { [PlatformType]::Linux - } else { + } elseif($IsWindows) { [PlatformType]::Windows } } diff --git a/regress/pesterTests/PortForwarding.Tests.ps1 b/regress/pesterTests/PortForwarding.Tests.ps1 index 68f38d2f2..0fe4b5269 100644 --- a/regress/pesterTests/PortForwarding.Tests.ps1 +++ b/regress/pesterTests/PortForwarding.Tests.ps1 @@ -15,9 +15,8 @@ Describe "E2E scenarios for port forwarding" -Tags "CI" { { $null = New-Item $testDir -ItemType directory -Force -ErrorAction SilentlyContinue } - $platform = Get-Platform #skip on ps 2 becase non-interactive cmd require a ENTER before it returns on ps2 - $skip = ($platform -eq [PlatformType]::Windows) -and ($PSVersionTable.PSVersion.Major -le 2) + $skip = $IsWindows -and ($PSVersionTable.PSVersion.Major -le 2) } BeforeEach { diff --git a/regress/pesterTests/SCP.Tests.ps1 b/regress/pesterTests/SCP.Tests.ps1 index 54623c5a1..3648ac73d 100644 --- a/regress/pesterTests/SCP.Tests.ps1 +++ b/regress/pesterTests/SCP.Tests.ps1 @@ -206,7 +206,7 @@ Describe "Tests for scp command" -Tags "CI" { $equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir) (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ) -Property Name, Length).Length -eq 0 $equal | Should Be $true - if($Options.contains("-p ") -and ($platform -eq [PlatformType]::Windows) -and ($PSVersionTable.PSVersion.Major -gt 2)) + if($Options.contains("-p ") -and $IsWindows -and ($PSVersionTable.PSVersion.Major -gt 2)) { $equal = @(Compare-Object (Get-ChildItem -Recurse -path $SourceDir).LastWriteTime.DateTime (Get-ChildItem -Recurse -path (join-path $DestinationDir $SourceDirName) ).LastWriteTime.DateTime).Length -eq 0 $equal | Should Be $true diff --git a/regress/pesterTests/SFTP.Tests.ps1 b/regress/pesterTests/SFTP.Tests.ps1 index 8d8957e7b..a0a35f380 100644 --- a/regress/pesterTests/SFTP.Tests.ps1 +++ b/regress/pesterTests/SFTP.Tests.ps1 @@ -36,8 +36,7 @@ Describe "SFTP Test Cases" -Tags "CI" { Remove-item (Join-Path $rootDirectory "*.$batchFileName") -Force -ErrorAction SilentlyContinue Remove-item (Join-Path $rootDirectory "*.log") -Force -ErrorAction SilentlyContinue - $platform = Get-Platform - $skip = ($platform -eq [PlatformType]::Windows) -and ($PSVersionTable.PSVersion.Major -le 2) + $skip = $IsWindows -and ($PSVersionTable.PSVersion.Major -le 2) $testData1 = @( @{ diff --git a/regress/pesterTests/SSH.Tests.ps1 b/regress/pesterTests/SSH.Tests.ps1 index 618a925b2..f6ce8f9db 100644 --- a/regress/pesterTests/SSH.Tests.ps1 +++ b/regress/pesterTests/SSH.Tests.ps1 @@ -27,9 +27,8 @@ Describe "E2E scenarios for ssh client" -Tags "CI" { $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($ssouser, $rights, "ContainerInherit,Objectinherit", "None", "Allow") $acl.SetAccessRule($accessRule) Set-Acl -Path $testDir -AclObject $acl - $platform = Get-Platform #skip on ps 2 becase non-interactive cmd require a ENTER before it returns on ps2 - $skip = ($platform -eq [PlatformType]::Windows) -and ($PSVersionTable.PSVersion.Major -le 2) + $skip = $IsWindows -and ($PSVersionTable.PSVersion.Major -le 2) <#$testData = @( @{ diff --git a/regress/pesterTests/SSHDConfig.tests.ps1 b/regress/pesterTests/SSHDConfig.tests.ps1 index be1e2542f..47a825959 100644 --- a/regress/pesterTests/SSHDConfig.tests.ps1 +++ b/regress/pesterTests/SSHDConfig.tests.ps1 @@ -142,7 +142,7 @@ Match User matchuser { Stop-SSHDTestDaemon -Port $port } - if(($platform -eq [PlatformType]::Windows) -and ([Environment]::OSVersion.Version.Major -le 6)) + if($IsWindows -and ([Environment]::OSVersion.Version.Major -le 6)) { #suppress the firewall blocking dialogue on win7 netsh advfirewall firewall add rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any action=allow dir=in @@ -153,7 +153,7 @@ Match User matchuser AfterAll { $PrincipalContext.Dispose() - if(($platform -eq [PlatformType]::Windows) -and ($psversiontable.BuildVersion.Major -le 6)) + if($IsWindows -and ($psversiontable.BuildVersion.Major -le 6)) { netsh advfirewall firewall delete rule name="sshd" program="$($OpenSSHTestInfo['OpenSSHBinPath'])\sshd.exe" protocol=any dir=in }