diff --git a/windows/Install-IPerf3.ps1 b/windows/Install-IPerf3.ps1 index 436804d..fb83c12 100644 --- a/windows/Install-IPerf3.ps1 +++ b/windows/Install-IPerf3.ps1 @@ -42,8 +42,7 @@ begin { $ServerServiceInstalled = $true Write-Host 'Server service already installed.' } else { - if (-not (Test-Path 'C:\Program Files\nssm') ) { - # if ('nssm' -notin $env:PATH) { + if (-not (Get-Command 'nssm.exe') ) { throw 'NSSM is not installed. Please install NSSM before attempting to install the server service.' } } @@ -75,7 +74,7 @@ process { Expand-Archive -Path $ArchivePath -DestinationPath $DestinationPath -Force # Add the DestinationPath to Path system environment variable - if ($DestinationPath -notin $env:PATH) { [Environment]::SetEnvironmentVariable("Path", $env:Path + ";$DestinationPath\", [System.EnvironmentVariableTarget]::Machine) } + if ($DestinationPath -notin $env:PATH) { $env:PATH = $env:PATH + ";$DestinationPath\"; [Environment]::SetEnvironmentVariable("Path", $env:PATH, [System.EnvironmentVariableTarget]::Machine) } # Add firewall rule allowing application executable if ('Allow iPerf3' -notin (Get-NetFirewallRule).DisplayName) { New-NetFirewallRule -DisplayName 'Allow iPerf3' -Direction Inbound -Program $ExecutablePath -RemoteAddress Any -Profile Any -Action Allow } diff --git a/windows/Install-WinAcme.Tests.ps1 b/windows/Install-WinAcme.Tests.ps1 new file mode 100644 index 0000000..c095b8f --- /dev/null +++ b/windows/Install-WinAcme.Tests.ps1 @@ -0,0 +1,51 @@ +Describe 'Install-WinAcme' { + BeforeAll { + function Uninstall-WinAcme { + param ( + [string]$DestinationPath + ) + + # Remove the installation directory + if (Test-Path -Path $DestinationPath) { + Remove-Item -Path $DestinationPath -Recurse -Force + } + + # Remove from PATH + $env:PATH = $env:PATH -replace [regex]::Escape("$DestinationPath\"), '' + [Environment]::SetEnvironmentVariable("Path", $env:PATH, [System.EnvironmentVariableTarget]::Machine) + } + + # Clean up any existing win-acme installation + Uninstall-WinAcme -DestinationPath 'C:\Program Files\win-acme' + + # Load the Install-WinAcme function + Set-Item function:Install-WinAcme ([ScriptBlock]::Create((Get-Content -Raw 'windows\Install-WinAcme.ps1'))) + } + + It 'Has the correct parameters' { + $params = (Get-Command Install-WinAcme).Parameters + + $params.ContainsKey('DestinationPath') | Should -Be $true + $params['DestinationPath'].ParameterType.Name | Should -Be 'String' + } + + It 'Should download and install win-acme without errors' { + { Install-WinAcme -DestinationPath 'C:\Program Files\win-acme' } | Should -Not -Throw + } + + It 'Should add win-acme to the system PATH' { + $env:PATH | Should -Match 'C:\\Program Files\\win-acme\\' + } + + It 'Should create the win-acme installation directory' { + Test-Path -Path 'C:\Program Files\win-acme' | Should -Be $true + } + + It 'Should create the wacs executable in the installation directory' { + Test-Path -Path 'C:\Program Files\win-acme\wacs.exe' | Should -Be $true + } + + AfterAll { + Uninstall-WinAcme -DestinationPath 'C:\Program Files\win-acme' + } +} \ No newline at end of file diff --git a/windows/Install-WinAcme.ps1 b/windows/Install-WinAcme.ps1 index 4932d96..9f669a2 100644 --- a/windows/Install-WinAcme.ps1 +++ b/windows/Install-WinAcme.ps1 @@ -18,7 +18,7 @@ param ( begin { # Prep - $Releases = (Invoke-WebRequest 'https://api.github.com/repos/win-acme/win-acme/releases').Content | ConvertFrom-Json | Where-Object { $_.prerelease -eq $false -and $_.draft -eq $false } + $Releases = (Invoke-WebRequest 'https://api.github.com/repos/win-acme/win-acme/releases' -UseBasicParsing).Content | ConvertFrom-Json | Where-Object { $_.prerelease -eq $false -and $_.draft -eq $false } $ReleaseTag = ($Releases | Select-Object -First 1).tag_name # $InstallerUrl = 'https://github.com/win-acme/win-acme/releases/download/v2.2.9.1701/win-acme.v2.2.9.1701.x64.pluggable.zip' @@ -50,7 +50,7 @@ process { Expand-Archive -Path $ArchivePath -DestinationPath $DestinationPath -Force # Add the DestinationPath to Path system environment variable - if ($DestinationPath -notin $env:PATH) { [Environment]::SetEnvironmentVariable("Path", $env:Path + ";$DestinationPath\", [System.EnvironmentVariableTarget]::Machine) } + if ($DestinationPath -notin $env:PATH) { $env:PATH = $env:PATH + ";$DestinationPath\"; [Environment]::SetEnvironmentVariable("Path", $env:PATH, [System.EnvironmentVariableTarget]::Machine) } } catch { throw "Failed installing application. $_" } diff --git a/windows/Install-iPerf3.Tests.ps1 b/windows/Install-iPerf3.Tests.ps1 index d42f8a6..9ea3b49 100644 --- a/windows/Install-iPerf3.Tests.ps1 +++ b/windows/Install-iPerf3.Tests.ps1 @@ -1,5 +1,35 @@ Describe 'Install-IPerf3' { BeforeAll { + function Uninstall-IPerf3 { + param ( + [string]$DestinationPath = 'C:\Program Files\iPerf3' + ) + + # Stop and remove the service if it exists + if (Get-Service -Name iperf3server -ErrorAction SilentlyContinue) { + Stop-Service -Name iperf3server -Force + Remove-Service -Name iperf3server + } + + # Remove the installation directory + if (Test-Path -Path $DestinationPath) { + Remove-Item -Path $DestinationPath -Recurse -Force + } + + # Remove firewall rule + if ('Allow iPerf3' -in (Get-NetFirewallRule).DisplayName) { + Remove-NetFirewallRule -DisplayName 'Allow iPerf3' + } + + # Remove from PATH + $env:PATH = $env:PATH -replace [regex]::Escape("$DestinationPath\"), '' + [Environment]::SetEnvironmentVariable("Path", $env:PATH, [System.EnvironmentVariableTarget]::Machine) + } + + # Clean up any existing iPerf3 installation + Uninstall-IPerf3 -DestinationPath 'C:\Program Files\iPerf3' + + # Load the Install-iPerf3 function Set-Item function:Install-IPerf3 ([ScriptBlock]::Create((Get-Content -Raw 'windows\Install-iPerf3.ps1'))) } @@ -34,16 +64,6 @@ Describe 'Install-IPerf3' { $firewallRule = Get-NetFirewallRule -DisplayName 'Allow iPerf3' -ErrorAction SilentlyContinue $firewallRule | Should -Not -Be $null } - - AfterAll { - # Clean up any existing iPerf3 installation - if (Get-Service -Name iperf3server -ErrorAction SilentlyContinue) { - Stop-Service -Name iperf3server -Force - Remove-Service -Name iperf3server - } - Remove-Item -Path 'C:\Program Files\iPerf3' -Recurse -Force -ErrorAction SilentlyContinue - Remove-NetFirewallRule -DisplayName 'Allow iPerf3' -ErrorAction SilentlyContinue - } } Context 'With ServerService parameter' { @@ -53,15 +73,9 @@ Describe 'Install-IPerf3' { $service | Should -Not -Be $null $service.Status | Should -Be 'Running' } + } - AfterAll { - # Clean up any existing iPerf3 installation - if (Get-Service -Name iperf3server -ErrorAction SilentlyContinue) { - Stop-Service -Name iperf3server -Force - Remove-Service -Name iperf3server - } - Remove-Item -Path 'C:\Program Files\iPerf3' -Recurse -Force -ErrorAction SilentlyContinue - Remove-NetFirewallRule -DisplayName 'Allow iPerf3' -ErrorAction SilentlyContinue - } + AfterAll { + Uninstall-IPerf3 -DestinationPath 'C:\Program Files\iPerf3' } } \ No newline at end of file