From 476c82323dc3d8e47b193a07369a1272c09bc3f9 Mon Sep 17 00:00:00 2001 From: 030 Date: Fri, 4 Jul 2014 23:32:12 +0200 Subject: [PATCH 01/25] Work on #489. Install-ChocolateyService created. Entry function added to src/helpers/chocolateyInstaller.psm1 --- src/helpers/chocolateyInstaller.psm1 | 1 + .../functions/Install-ChocolateyService.ps1 | 106 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/helpers/functions/Install-ChocolateyService.ps1 diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index 4741fcf..cfa4fab 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -26,6 +26,7 @@ Export-ModuleMember -Function ` Install-ChocolateyExplorerMenuItem,` Install-ChocolateyFileAssociation,` Install-ChocolateyEnvironmentVariable,` + Install-ChocolateyService,` Install-ChocolateyVsixPackage,` Write-ChocolateySuccess,` Write-ChocolateyFailure,` diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 new file mode 100644 index 0000000..ca29d6c --- /dev/null +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -0,0 +1,106 @@ +function Install-ChocolateyService { +<# +.SYNOPSIS +Installs a service + +.DESCRIPTION +This will install a service + +.PARAMETER PackageName +The name of the package for whom the service will be installed. + +.PARAMETER ServiceName +The name of service which will be used to install and start the service. + +.PARAMETER CreateServiceCommand +The command which installs the service. + +.PARAMETER AvailablePort +The port which needs to be available in order to start the service. + +.EXAMPLE +Install-ChocolateyService 'PACKAGE_NAME' 'SERVICE_NAME' 'INSTALL_COMMAND' 'PORT' + +.OUTPUTS +None + +.NOTES +This helper reduces the number of lines one would have to write to install a service to 1 line. +This method has error handling built into it. + +.LINK +Get-ChocolateyWebFile +Get-ChocolateyUnzip +#> +param( + [string] $packageName, + [string] $serviceName, + [string] $createServiceCommand, + [int] $availablePort +) + Write-Debug "Running 'Install-ChocolateyService' for $packageName with url:`'$url`', unzipLocation: `'$unzipLocation`', url64bit: `'$url64bit`', specificFolder: `'$specificFolder`', checksum: `'$checksum`', checksumType: `'$checksumType`', checksum64: `'$checksum64`', checksumType64: `'$checksumType64`' "; + + if(!$packageName) { + Write-ChocolateyFailure "Install-ChocolateyService" "Missing PackageName input parameter." + return + } + + if(!$serviceName) { + Write-ChocolateyFailure "Install-ChocolateyService" "Missing ServiceName input parameter." + return + } + + if(!$createServiceCommand) { + Write-ChocolateyFailure "Install-ChocolateyService" "Missing CreateServiceCommand input parameter." + return + } + + if(!$availablePort) { + Write-ChocolateyFailure "Install-ChocolateyService" "Missing AvailablePort input parameter." + return + } + + function installService() { + if (serviceExists) { + Write-Host "$packageName service will be installed" + & $createServiceCommand install $serviceName + } + } + + function startService() { + if (portAvailable) { + Write-Host "Apache Tomcat service will be started" + start-service $serviceName + } else { + Write-ChocolateyFailure 'Apache Tomcat' "port $port is unavailable" + throw + } + } + + function portAvailable() { + Try { + $connection = (New-Object Net.Sockets.TcpClient) + $connection.Connect("127.0.0.1",$port) + return $FALSE + } Catch { + return $TRUE + } + } + + function serviceExists() { + $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue + if ($service.Length -gt 0) { + Write-Host "$serviceName service already exists and will be removed" + stop-service $serviceName + $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" + $service.delete() + return $TRUE + } else { + return $TRUE + } + } + + installService + + startService +} \ No newline at end of file From 61581b172a77399bd9133225fde7b821995fbc22 Mon Sep 17 00:00:00 2001 From: 030 Date: Sat, 5 Jul 2014 00:23:39 +0200 Subject: [PATCH 02/25] Work on #489. Creating Pester test for Install-ChocolateyService --- tests/unit/Install-ChocolateyService.Tests.ps1 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/unit/Install-ChocolateyService.Tests.ps1 diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 new file mode 100644 index 0000000..011ddb0 --- /dev/null +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -0,0 +1,17 @@ +$here = Split-Path -Parent $MyInvocation.MyCommand.Definition +$common = Join-Path (Split-Path -Parent $here) '_Common.ps1' +$base = Split-Path -parent (Split-Path -Parent $here) +. $common +. "$base\src\helpers\functions\Install-ChocolateyService.ps1" + +Describe "Install-ChocolateyService" { + Context "When no packageName parameter is passed to this function" { + Mock Write-ChocolateyFailure + + Install-ChocolateyService + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + } + } +} \ No newline at end of file From e6e507d4aed6df1bf98d1cf25c9d9b99487118de Mon Sep 17 00:00:00 2001 From: 030 Date: Sat, 5 Jul 2014 00:40:02 +0200 Subject: [PATCH 03/25] Work on #489. Creating Pester test for Install-ChocolateyService --- .../unit/Install-ChocolateyService.Tests.ps1 | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index 011ddb0..299300a 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -8,7 +8,47 @@ Describe "Install-ChocolateyService" { Context "When no packageName parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + } + } + + Context "When no serviceName parameter is passed to this function" { + Mock Write-ChocolateyFailure + + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing ServiceName input parameter." } + } + } + + Context "When no packageName parameter is passed to this function" { + Mock Write-ChocolateyFailure + + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + } + } + + Context "When no packageName parameter is passed to this function" { + Mock Write-ChocolateyFailure + + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + } + } + + Context "When availablePort parameter is passed in String format to this function" { + Mock Write-ChocolateyFailure + + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" It "should return an error" { Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } From 0a73276a9a1997159b16de3b1d5eb8a1198e880c Mon Sep 17 00:00:00 2001 From: 030 Date: Sat, 5 Jul 2014 12:24:49 +0200 Subject: [PATCH 04/25] Work on #489. Created tests to validate all input parameters --- .../functions/Install-ChocolateyService.ps1 | 29 ++++++--- .../unit/Install-ChocolateyService.Tests.ps1 | 61 +++++++++++++++---- 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index ca29d6c..ef2de58 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -63,18 +63,30 @@ param( function installService() { if (serviceExists) { Write-Host "$packageName service will be installed" + & $createServiceCommand install $serviceName + + + } else { + Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be installed" + throw } } function startService() { - if (portAvailable) { - Write-Host "Apache Tomcat service will be started" - start-service $serviceName - } else { - Write-ChocolateyFailure 'Apache Tomcat' "port $port is unavailable" - throw - } +# if (portAvailable) { +# Write-Host "$packageName service will be started" + $service6 = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" + if ($service6 -match "$serviceName") { + #start-service $serviceName + } else { + Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." + return + } + # } else { + # Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be started" + # throw + #} } function portAvailable() { @@ -100,7 +112,8 @@ param( } } - installService + # installService startService + } \ No newline at end of file diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index 299300a..619af93 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -8,7 +8,7 @@ Describe "Install-ChocolateyService" { Context "When no packageName parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + Install-ChocolateyService -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" It "should return an error" { Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } @@ -18,40 +18,77 @@ Describe "Install-ChocolateyService" { Context "When no serviceName parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + Install-ChocolateyService -packageName "TestTargetPath" -createServiceCommand "TestArguments" -availablePort "1" It "should return an error" { Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing ServiceName input parameter." } } } - Context "When no packageName parameter is passed to this function" { + Context "When no createServiceCommand parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -availablePort "1" It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing CreateServiceCommand input parameter." } } } - Context "When no packageName parameter is passed to this function" { + Context "When no availablePort parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing AvailablePort input parameter." } } } - Context "When availablePort parameter is passed in String format to this function" { + Context "When createService parameter is incorrect" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" + Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" -availablePort "135" It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service helloworld does not exist." +Write-host $failureMessage + } } - } + } + + + +# Context "When all parameters are passed with valid values" { +# $shortcutPath = "c:\test.lnk" +# $targetPath = "C:\test.txt" +# $workingDirectory = "C:\" +# $arguments = "args" +# $iconLocation = "C:\test.ico" +# $description = "Description" + +# Set-Content $targetPath -value "my test text." +# Set-Content $iconLocation -Value "icon" + +# Install-ChocolateyShortcut -shortcutFilePath $shortcutPath -targetPath $targetPath -workDirectory $workingDirectory -arguments $arguments -iconLocation $iconLocation -description $description +# Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\" -availablePort "1" + + # $result = Test-Path($shortcutPath) + +# It "should succeed." { +# } + + # Tidy up items that were created as part of this test + # if(Test-Path($shortcutPath)) { + # Remove-Item $shortcutPath + # } + + # if(Test-Path($targetPath)) { + # Remove-Item $targetPath + # } + +# if(Test-Path($iconLocation)) { +# Remove-Item $iconLocation +# } +# } } \ No newline at end of file From 100209ebebedb486a5dfcc78f3b896d9e701ae6b Mon Sep 17 00:00:00 2001 From: 030 Date: Sat, 5 Jul 2014 23:28:00 +0200 Subject: [PATCH 05/25] Work on #489. Multiple functions created --- .../functions/Install-ChocolateyService.ps1 | 130 ++++++++++++------ .../unit/Install-ChocolateyService.Tests.ps1 | 21 ++- 2 files changed, 108 insertions(+), 43 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index ef2de58..e187d09 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -19,7 +19,7 @@ The command which installs the service. The port which needs to be available in order to start the service. .EXAMPLE -Install-ChocolateyService 'PACKAGE_NAME' 'SERVICE_NAME' 'INSTALL_COMMAND' 'PORT' +Install-ChocolateyService 'PACKAGE_NAME' 'SERVICE_NAME' 'CREATE_SERVICE_COMMAND' 'PORT' .OUTPUTS None @@ -60,60 +60,110 @@ param( return } - function installService() { - if (serviceExists) { + $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" + + function createService() { + if (get-command $createServiceCommand -erroraction silentlycontinue) { Write-Host "$packageName service will be installed" - & $createServiceCommand install $serviceName - - } else { - Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be installed" - throw + Write-ChocolateyFailure 'Install-ChocolateyService' "createServiceCommand $createServiceCommand is incorrect." + return } } function startService() { -# if (portAvailable) { -# Write-Host "$packageName service will be started" - $service6 = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" - if ($service6 -match "$serviceName") { - #start-service $serviceName - } else { - Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." - return + if (serviceExist) { + Write-Host "$packageName service will be started" + start-service $serviceName + } else { + Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." + return + } } - # } else { - # Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be started" - # throw - #} + + function availablePort() { + $listeningStatePort = Get-NetTCPConnection -State Listen | Where-Object {$_.LocalAddress -eq "0.0.0.0" -and $_.LocalPort -eq "$availablePort"} + if (!$listeningStatePort) { + "$availablePort is AVAILABLE" + } else { + "$availablePort is in LISTENING state" + } } - function portAvailable() { - Try { - $connection = (New-Object Net.Sockets.TcpClient) - $connection.Connect("127.0.0.1",$port) - return $FALSE - } Catch { - return $TRUE - } + function serviceExist() { + $service } - function serviceExists() { - $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue - if ($service.Length -gt 0) { + function serviceDelete() { + if (serviceExist) { Write-Host "$serviceName service already exists and will be removed" stop-service $serviceName - $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" - $service.delete() - return $TRUE - } else { - return $TRUE - } - } + $service.delete() + } else { + + } + } - # installService - startService + + +# function installService() { +# if (serviceExists) { +# Write-Host "$packageName service will be installed" +# if ( get-command helloworld -erroraction silentlycontinue ) { +# & $createServiceCommand install $serviceName +# } else { +# Write-ChocolateyFailure 'Install-ChocolateyService' "createServiceCommand $createServiceCommand is incorrect" +# return +# } +# } else { +# Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be installed" +# return +# } +# } + +# function startService() { +# if (portAvailable) { +# Write-Host "$packageName service will be started" +# $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" +# if ($service -match "$serviceName") { +# start-service $serviceName +# } else { +# Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." +# return +# } +# } else { +# Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be started" +# return +# } +# } + +# function portAvailable() { +# Try { +# $connection = (New-Object Net.Sockets.TcpClient) +# $connection.Connect("127.0.0.1",$port) +# return $FALSE +# } Catch { +# return $TRUE +# } +# } + +# function serviceExists() { +# $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue +# if ($service.Length -gt 0) { +# Write-Host "$serviceName service already exists and will be removed" +# stop-service $serviceName +# $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" +# $service.delete() +# return $TRUE +# } else { +# return $TRUE +# } +# } + +# installService + +# startService } \ No newline at end of file diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index 619af93..190a211 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -45,20 +45,35 @@ Describe "Install-ChocolateyService" { } } - Context "When createService parameter is incorrect" { +# Context "When service does not exist" { +# Mock Write-ChocolateyFailure + +# Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" -availablePort "135" + +# It "should return an error" { +# Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service helloworld does not exist." +# #Write-host $failureMessage +# } +# } +# } + + Context "When createServiceCommand is incorrect" { Mock Write-ChocolateyFailure Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" -availablePort "135" It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service helloworld does not exist." -Write-host $failureMessage + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "createServiceCommand c:\helloworld is incorrect." +# Write-host $failureMessage } } } + + + # Context "When all parameters are passed with valid values" { # $shortcutPath = "c:\test.lnk" # $targetPath = "C:\test.txt" From 51906e256e81b918d1ebf242d757458324b58e1a Mon Sep 17 00:00:00 2001 From: 030 Date: Sat, 5 Jul 2014 23:59:53 +0200 Subject: [PATCH 06/25] (GH-489) Multiple functions created --- .../functions/Install-ChocolateyService.ps1 | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index e187d09..c0ed877 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -73,7 +73,7 @@ param( } function startService() { - if (serviceExist) { + if (serviceExist && availablePort) { Write-Host "$packageName service will be started" start-service $serviceName } else { @@ -95,17 +95,23 @@ param( $service } - function serviceDelete() { + function deleteService() { if (serviceExist) { Write-Host "$serviceName service already exists and will be removed" stop-service $serviceName $service.delete() } else { - + return $TRUE } } - + deleteService + createService + startService + + + + From 0b99c707ffbb6c5b0d48521d8179886e138bbae6 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 01:41:49 +0200 Subject: [PATCH 07/25] (GH-489) Port changed from required to optional --- .../functions/Install-ChocolateyService.ps1 | 26 +++++++++++-------- .../unit/Install-ChocolateyService.Tests.ps1 | 14 +++++----- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index c0ed877..5ad5f56 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -55,10 +55,10 @@ param( return } - if(!$availablePort) { - Write-ChocolateyFailure "Install-ChocolateyService" "Missing AvailablePort input parameter." - return - } +# if(!$availablePort) { +# Write-ChocolateyFailure "Install-ChocolateyService" "Missing AvailablePort input parameter." +# return +# } $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" @@ -73,7 +73,8 @@ param( } function startService() { - if (serviceExist && availablePort) { +# if (serviceExist -and availablePort) { + if (serviceExist) { Write-Host "$packageName service will be started" start-service $serviceName } else { @@ -83,12 +84,15 @@ param( } function availablePort() { - $listeningStatePort = Get-NetTCPConnection -State Listen | Where-Object {$_.LocalAddress -eq "0.0.0.0" -and $_.LocalPort -eq "$availablePort"} - if (!$listeningStatePort) { - "$availablePort is AVAILABLE" - } else { - "$availablePort is in LISTENING state" - } + if($availablePort) { + $listeningStatePort = Get-NetTCPConnection -State Listen | Where-Object {$_.LocalAddress -eq "0.0.0.0" -and $_.LocalPort -eq "$availablePort"} + if (!$listeningStatePort) { + return $TRUE + } else { + Write-ChocolateyFailure "Install-ChocolateyService" "$availablePort is in LISTENING state and not available." + return + } + } } function serviceExist() { diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index 190a211..e51e303 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -35,15 +35,15 @@ Describe "Install-ChocolateyService" { } } - Context "When no availablePort parameter is passed to this function" { - Mock Write-ChocolateyFailure +# Context "When no availablePort parameter is passed to this function" { +# Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" +# Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" - It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing AvailablePort input parameter." } - } - } +# It "should return an error" { +# Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing AvailablePort input parameter." } +# } +# } # Context "When service does not exist" { # Mock Write-ChocolateyFailure From 3df0334eb2ab693fb90a49dc6433ad8f7e0390fc Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 02:12:01 +0200 Subject: [PATCH 08/25] (GH-489) Functions inside function issue solved --- .../functions/Install-ChocolateyService.ps1 | 120 +++--------------- 1 file changed, 18 insertions(+), 102 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index 5ad5f56..7b6b1b2 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -54,36 +54,24 @@ param( Write-ChocolateyFailure "Install-ChocolateyService" "Missing CreateServiceCommand input parameter." return } - -# if(!$availablePort) { -# Write-ChocolateyFailure "Install-ChocolateyService" "Missing AvailablePort input parameter." -# return -# } $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" + + try { + if ($service) { + Write-Host "$serviceName service already exists and will be removed" + stop-service $serviceName + $service.delete() + } - function createService() { if (get-command $createServiceCommand -erroraction silentlycontinue) { Write-Host "$packageName service will be installed" & $createServiceCommand install $serviceName } else { Write-ChocolateyFailure 'Install-ChocolateyService' "createServiceCommand $createServiceCommand is incorrect." return - } - } + } - function startService() { -# if (serviceExist -and availablePort) { - if (serviceExist) { - Write-Host "$packageName service will be started" - start-service $serviceName - } else { - Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." - return - } - } - - function availablePort() { if($availablePort) { $listeningStatePort = Get-NetTCPConnection -State Listen | Where-Object {$_.LocalAddress -eq "0.0.0.0" -and $_.LocalPort -eq "$availablePort"} if (!$listeningStatePort) { @@ -92,88 +80,16 @@ param( Write-ChocolateyFailure "Install-ChocolateyService" "$availablePort is in LISTENING state and not available." return } - } - } - - function serviceExist() { - $service - } - - function deleteService() { - if (serviceExist) { - Write-Host "$serviceName service already exists and will be removed" - stop-service $serviceName - $service.delete() - } else { - return $TRUE + } + + if ($service) { + Write-Host "$packageName service will be started" + start-service $serviceName + } else { + Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." + return } + } catch { + Write-ChocolateyFailure "Install-ChocolateyService" "There were errors attempting to create the $packageName service. The error message was '$_'." } - - deleteService - createService - startService - - - - - - - -# function installService() { -# if (serviceExists) { -# Write-Host "$packageName service will be installed" -# if ( get-command helloworld -erroraction silentlycontinue ) { -# & $createServiceCommand install $serviceName -# } else { -# Write-ChocolateyFailure 'Install-ChocolateyService' "createServiceCommand $createServiceCommand is incorrect" -# return -# } -# } else { -# Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be installed" -# return -# } -# } - -# function startService() { -# if (portAvailable) { -# Write-Host "$packageName service will be started" -# $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -# if ($service -match "$serviceName") { -# start-service $serviceName -# } else { -# Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." -# return -# } -# } else { -# Write-ChocolateyFailure 'Install-ChocolateyService' "service $serviceName cannot be started" -# return -# } -# } - -# function portAvailable() { -# Try { -# $connection = (New-Object Net.Sockets.TcpClient) -# $connection.Connect("127.0.0.1",$port) -# return $FALSE -# } Catch { -# return $TRUE -# } -# } - -# function serviceExists() { -# $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue -# if ($service.Length -gt 0) { -# Write-Host "$serviceName service already exists and will be removed" -# stop-service $serviceName -# $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -# $service.delete() -# return $TRUE -# } else { -# return $TRUE -# } -# } - -# installService - -# startService } \ No newline at end of file From f5d1e8809d6d8d6054bab4140c8df9f96cea0bb0 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 02:18:56 +0200 Subject: [PATCH 09/25] (GH-489) Details about how to install services removed --- .../functions/Install-ChocolateyService.ps1 | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index 7b6b1b2..56b6802 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -1,37 +1,4 @@ function Install-ChocolateyService { -<# -.SYNOPSIS -Installs a service - -.DESCRIPTION -This will install a service - -.PARAMETER PackageName -The name of the package for whom the service will be installed. - -.PARAMETER ServiceName -The name of service which will be used to install and start the service. - -.PARAMETER CreateServiceCommand -The command which installs the service. - -.PARAMETER AvailablePort -The port which needs to be available in order to start the service. - -.EXAMPLE -Install-ChocolateyService 'PACKAGE_NAME' 'SERVICE_NAME' 'CREATE_SERVICE_COMMAND' 'PORT' - -.OUTPUTS -None - -.NOTES -This helper reduces the number of lines one would have to write to install a service to 1 line. -This method has error handling built into it. - -.LINK -Get-ChocolateyWebFile -Get-ChocolateyUnzip -#> param( [string] $packageName, [string] $serviceName, From 2f24256f8496cbf54494f68a8cf8550fbdc25b68 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 02:38:30 +0200 Subject: [PATCH 10/25] (GH-489) Created tests --- .../unit/Install-ChocolateyService.Tests.ps1 | 120 +++++------------- 1 file changed, 34 insertions(+), 86 deletions(-) diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index e51e303..b89b983 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -5,105 +5,53 @@ $base = Split-Path -parent (Split-Path -Parent $here) . "$base\src\helpers\functions\Install-ChocolateyService.ps1" Describe "Install-ChocolateyService" { - Context "When no packageName parameter is passed to this function" { - Mock Write-ChocolateyFailure - - Install-ChocolateyService -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" -availablePort "1" - - It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } - } + Context "When no packageName parameter is passed to this function" { + Mock Write-ChocolateyFailure + + Install-ChocolateyService -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } } + } - Context "When no serviceName parameter is passed to this function" { - Mock Write-ChocolateyFailure + Context "When no serviceName parameter is passed to this function" { + Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -createServiceCommand "TestArguments" -availablePort "1" + Install-ChocolateyService -packageName "TestTargetPath" -createServiceCommand "TestArguments" - It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing ServiceName input parameter." } - } + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing ServiceName input parameter." } } + } - Context "When no createServiceCommand parameter is passed to this function" { - Mock Write-ChocolateyFailure + Context "When no createServiceCommand parameter is passed to this function" { + Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -availablePort "1" + Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" - It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing CreateServiceCommand input parameter." } - } + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing CreateServiceCommand input parameter." } } - -# Context "When no availablePort parameter is passed to this function" { -# Mock Write-ChocolateyFailure - -# Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" - -# It "should return an error" { -# Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing AvailablePort input parameter." } -# } -# } + } -# Context "When service does not exist" { -# Mock Write-ChocolateyFailure - -# Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" -availablePort "135" + Context "When service does not exist" { + Mock Write-ChocolateyFailure -# It "should return an error" { -# Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service helloworld does not exist." -# #Write-host $failureMessage -# } -# } -# } - - Context "When createServiceCommand is incorrect" { - Mock Write-ChocolateyFailure + Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "notepad" - Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" -availablePort "135" - - It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "createServiceCommand c:\helloworld is incorrect." -# Write-host $failureMessage - } - } - } - - - - - + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service helloworld does not exist." } + } + } -# Context "When all parameters are passed with valid values" { -# $shortcutPath = "c:\test.lnk" -# $targetPath = "C:\test.txt" -# $workingDirectory = "C:\" -# $arguments = "args" -# $iconLocation = "C:\test.ico" -# $description = "Description" - -# Set-Content $targetPath -value "my test text." -# Set-Content $iconLocation -Value "icon" - -# Install-ChocolateyShortcut -shortcutFilePath $shortcutPath -targetPath $targetPath -workDirectory $workingDirectory -arguments $arguments -iconLocation $iconLocation -description $description -# Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\" -availablePort "1" - - # $result = Test-Path($shortcutPath) + Context "When createServiceCommand is incorrect" { + Mock Write-ChocolateyFailure -# It "should succeed." { -# } + Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" - # Tidy up items that were created as part of this test - # if(Test-Path($shortcutPath)) { - # Remove-Item $shortcutPath - # } - - # if(Test-Path($targetPath)) { - # Remove-Item $targetPath - # } - -# if(Test-Path($iconLocation)) { -# Remove-Item $iconLocation -# } -# } + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "createServiceCommand c:\helloworld is incorrect." } + } + } } \ No newline at end of file From 6c93cacddf50093bae3c7ef39b0cbe33fe1244bb Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 02:51:23 +0200 Subject: [PATCH 11/25] (GH-489) Moved Install-ChocolateyService parameter to the end of file according instructions CONTRIBUTING document --- src/helpers/chocolateyInstaller.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index cfa4fab..00587e3 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -26,7 +26,6 @@ Export-ModuleMember -Function ` Install-ChocolateyExplorerMenuItem,` Install-ChocolateyFileAssociation,` Install-ChocolateyEnvironmentVariable,` - Install-ChocolateyService,` Install-ChocolateyVsixPackage,` Write-ChocolateySuccess,` Write-ChocolateyFailure,` @@ -37,4 +36,5 @@ Export-ModuleMember -Function ` Update-SessionEnvironment,` Get-EnvironmentVariableNames,` Get-EnvironmentVariable,` - Set-EnvironmentVariable + Set-EnvironmentVariable,` + Install-ChocolateyService From e8a7bb534cb9363c24d3499f29ad0dd3a9e4349a Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 21:49:43 +0200 Subject: [PATCH 12/25] (GH-489) Function created to test whether service exists --- src/functions/Get-ServiceExistence.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/functions/Get-ServiceExistence.ps1 diff --git a/src/functions/Get-ServiceExistence.ps1 b/src/functions/Get-ServiceExistence.ps1 new file mode 100644 index 0000000..8cf0ca0 --- /dev/null +++ b/src/functions/Get-ServiceExistence.ps1 @@ -0,0 +1,6 @@ +function getServiceExistence { +param( + [string] $correctServiceName = '' +) + Get-WmiObject -Class Win32_Service -Filter "Name='$correctServiceName'" +} \ No newline at end of file From 8a5cccbead124065bf2319e62a8bbf5e7b09d5f0 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 21:52:56 +0200 Subject: [PATCH 13/25] (GH-489) Function created to test whether status is running or stopped --- src/functions/Get-ServiceStatus.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/functions/Get-ServiceStatus.ps1 diff --git a/src/functions/Get-ServiceStatus.ps1 b/src/functions/Get-ServiceStatus.ps1 new file mode 100644 index 0000000..69530b3 --- /dev/null +++ b/src/functions/Get-ServiceStatus.ps1 @@ -0,0 +1,7 @@ +function Get-ServiceStatus { +param( + [string] $correctServiceName = '' +) + $serviceStatus = Get-Service -Name $correctServiceName + $serviceStatus.Status +} \ No newline at end of file From f5a28734ea1fe6ffb47010ee87404eeb0ae20561 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 22:00:22 +0200 Subject: [PATCH 14/25] (GH-489) ServiceExistence and Status functions moved from functions to helpers directory --- src/functions/Get-ServiceExistence.ps1 | 6 ------ src/functions/Get-ServiceStatus.ps1 | 7 ------- src/helpers/functions/Get-ServiceExistence.ps1 | 6 ++++++ src/helpers/functions/Get-ServiceStatus.ps1 | 7 +++++++ 4 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 src/functions/Get-ServiceExistence.ps1 delete mode 100644 src/functions/Get-ServiceStatus.ps1 create mode 100644 src/helpers/functions/Get-ServiceExistence.ps1 create mode 100644 src/helpers/functions/Get-ServiceStatus.ps1 diff --git a/src/functions/Get-ServiceExistence.ps1 b/src/functions/Get-ServiceExistence.ps1 deleted file mode 100644 index 8cf0ca0..0000000 --- a/src/functions/Get-ServiceExistence.ps1 +++ /dev/null @@ -1,6 +0,0 @@ -function getServiceExistence { -param( - [string] $correctServiceName = '' -) - Get-WmiObject -Class Win32_Service -Filter "Name='$correctServiceName'" -} \ No newline at end of file diff --git a/src/functions/Get-ServiceStatus.ps1 b/src/functions/Get-ServiceStatus.ps1 deleted file mode 100644 index 69530b3..0000000 --- a/src/functions/Get-ServiceStatus.ps1 +++ /dev/null @@ -1,7 +0,0 @@ -function Get-ServiceStatus { -param( - [string] $correctServiceName = '' -) - $serviceStatus = Get-Service -Name $correctServiceName - $serviceStatus.Status -} \ No newline at end of file diff --git a/src/helpers/functions/Get-ServiceExistence.ps1 b/src/helpers/functions/Get-ServiceExistence.ps1 new file mode 100644 index 0000000..c3aafa7 --- /dev/null +++ b/src/helpers/functions/Get-ServiceExistence.ps1 @@ -0,0 +1,6 @@ +function Get-ServiceExistence { +param( + [string] $serviceName = '' +) + Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" +} \ No newline at end of file diff --git a/src/helpers/functions/Get-ServiceStatus.ps1 b/src/helpers/functions/Get-ServiceStatus.ps1 new file mode 100644 index 0000000..2ad4551 --- /dev/null +++ b/src/helpers/functions/Get-ServiceStatus.ps1 @@ -0,0 +1,7 @@ +function Get-ServiceStatus { +param( + [string] $serviceName = '' +) + $serviceStatus = Get-Service -Name $serviceName + $serviceStatus.Status +} \ No newline at end of file From 0685746e0a9f3b3df44b258f39f45df2a1f9e681 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 22:59:38 +0200 Subject: [PATCH 15/25] (GH-489) Service create to delete services --- src/helpers/functions/Delete-Service.ps1 | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/helpers/functions/Delete-Service.ps1 diff --git a/src/helpers/functions/Delete-Service.ps1 b/src/helpers/functions/Delete-Service.ps1 new file mode 100644 index 0000000..cd92d38 --- /dev/null +++ b/src/helpers/functions/Delete-Service.ps1 @@ -0,0 +1,11 @@ +function Delete-Service { +param( + [string] $serviceName = '' +) + if (Get-ServiceExistence -serviceName "$serviceName") { + Write-Host "$serviceName service already exists and will be removed" + stop-service $serviceName + $service = Get-ServiceExistence -serviceName "$serviceName" + $service.delete() + } +} \ No newline at end of file From 26011d9e48d15539d83116e317c219ff978e4a0a Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 23:17:52 +0200 Subject: [PATCH 16/25] (GH-489) Get-ServiceExistence, Service-Status and Delete-Service functions added to psm file --- src/helpers/chocolateyInstaller.psm1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index 00587e3..e15ed76 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -37,4 +37,7 @@ Export-ModuleMember -Function ` Get-EnvironmentVariableNames,` Get-EnvironmentVariable,` Set-EnvironmentVariable,` - Install-ChocolateyService + Install-ChocolateyService,` + Get-ServiceExistence,` + Get-ServiceStatus,` + Delete-Service \ No newline at end of file From 1658dd518f13d4dfe2bb640fdc299e820df3f199 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 23:21:20 +0200 Subject: [PATCH 17/25] (GH-489) Function created to test Install-ChocolateyService if valid parameters are passed --- ...ocolateyServiceCorrectParameters.Tests.ps1 | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/unit/Install-ChocolateyServiceCorrectParameters.Tests.ps1 diff --git a/tests/unit/Install-ChocolateyServiceCorrectParameters.Tests.ps1 b/tests/unit/Install-ChocolateyServiceCorrectParameters.Tests.ps1 new file mode 100644 index 0000000..fadd5a4 --- /dev/null +++ b/tests/unit/Install-ChocolateyServiceCorrectParameters.Tests.ps1 @@ -0,0 +1,21 @@ +function Install-ChocolateyServiceCorrectParameters.Tests { +param( + [string] $testDirectory, + [string] $serviceName = "installServiceTest", + [string] $createServiceCommand = "nssm install installServiceTest", + [int] $availablePort +) + $testServiceBatPath = "$testDirectory\testService.bat" + $testDirectoryExist = Test-Path $testDirectory + $createServiceCommandComplete = "$createServiceCommand `"$testServiceBatPath`"" + + `cinst NSSM` + + if (!$testDirectoryExist) { + Write-Host "$testDirectory directory does not exist and will be created" + New-Item -ItemType Directory -Path $testDirectory + Set-Content -Value "ping localhost" -Path $testServiceBatPath + } + + Install-ChocolateyService -packageName "$serviceName" -serviceName "$serviceName" -createServiceCommand "$createServiceCommandComplete" -availablePort "$availablePort" +} \ No newline at end of file From fb2236f22b5248ac0eb691df0a2acd0de3458aef Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 23:24:51 +0200 Subject: [PATCH 18/25] (GH-489) For loop created to start service 12 times with an interval of 5seconds if it is not able to start the first time to avoid incorrect error --- .../functions/Install-ChocolateyService.ps1 | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index 56b6802..c5aad8d 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -22,22 +22,17 @@ param( return } - $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" - try { - if ($service) { - Write-Host "$serviceName service already exists and will be removed" - stop-service $serviceName - $service.delete() - } + Delete-Service -serviceName "$serviceName" - if (get-command $createServiceCommand -erroraction silentlycontinue) { + try { Write-Host "$packageName service will be installed" - & $createServiceCommand install $serviceName - } else { - Write-ChocolateyFailure 'Install-ChocolateyService' "createServiceCommand $createServiceCommand is incorrect." - return - } + Write-Host $createServiceCommand + iex $createServiceCommand + } catch { + Write-ChocolateyFailure "Install-ChocolateyService" "The createServiceCommand is incorrect: '$_'." + return + } if($availablePort) { $listeningStatePort = Get-NetTCPConnection -State Listen | Where-Object {$_.LocalAddress -eq "0.0.0.0" -and $_.LocalPort -eq "$availablePort"} @@ -49,9 +44,20 @@ param( } } - if ($service) { + if (Get-ServiceExistence -serviceName "$serviceName") { Write-Host "$packageName service will be started" - start-service $serviceName + + for ($i=0;$i -lt 12; $i++) { + $serviceStatus = Get-Service -Name $serviceName + + start-service $serviceName + + if ($serviceStatus.Status -eq "running") { + Write-Host "$packageName service has been started" + return + } + Start-Sleep -s 5 + } } else { Write-ChocolateyFailure "Install-ChocolateyService" "service $serviceName does not exist." return From ee0bfa7a86d2aa76ed62ea966028d0587e103187 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 6 Jul 2014 23:28:16 +0200 Subject: [PATCH 19/25] (GH-489) Test created to test validity of installCommand and serviceStart. If optional port is specified then a check is done to see whether port is available. Creation of service mimiced by installing ping localhost bat file using NSSM as service. Test directory cleaned after finishing test --- .../unit/Install-ChocolateyService.Tests.ps1 | 73 ++++++++++++++++--- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index b89b983..cb9c0ea 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -3,12 +3,56 @@ $common = Join-Path (Split-Path -Parent $here) '_Common.ps1' $base = Split-Path -parent (Split-Path -Parent $here) . $common . "$base\src\helpers\functions\Install-ChocolateyService.ps1" +. "$base\src\helpers\functions\Get-ServiceExistence.ps1" +. "$base\src\helpers\functions\Get-ServiceStatus.ps1" +. "$base\tests\unit\Install-ChocolateyServiceCorrectParameters.Tests.ps1" + +$availablePort = "135" +$correctServiceName = "installServiceTest" +$unavailableServiceName = "unavailableServiceName" +$testDirectory = "C:\installChocolateyServiceTest" Describe "Install-ChocolateyService" { + Context "When provided parameters are correct the service should be created and started" { + Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" + + It "service creation should succeed" { + Get-ServiceExistence -serviceName "$correctServiceName" | should Be $true + } + + It "service start should succeed" { + Get-ServiceStatus -serviceName "$correctServiceName" -eq "running" | should Be $true + } + } + + Context "When provided parameters are correct and service exist it should be removed, subsequently created and started" { + Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" + + It "service creation should succeed after deletion of previous service" { + Get-ServiceExistence -serviceName "$correctServiceName" | should Be $true + } + + It "service start should succeed after deletion of previous service" { + Get-ServiceStatus -serviceName "$correctServiceName" -eq "running" | should Be $true + } + } + + Context "When availablePort parameter is passed to this function and it is in LISTENING state and not available" { + Mock Write-ChocolateyFailure + + Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" -availablePort "$availablePort" + + It "should return an error" { + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "$availablePort is in LISTENING state and not available." + Write-Host $failureMessage + } + } + } + Context "When no packageName parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -serviceName "TestWorkingDiectory" -createServiceCommand "TestArguments" + Install-ChocolateyService -serviceName "$unavailableServiceName" -createServiceCommand "$unavailableServiceName" It "should return an error" { Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing PackageName input parameter." } @@ -18,7 +62,7 @@ Describe "Install-ChocolateyService" { Context "When no serviceName parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -createServiceCommand "TestArguments" + Install-ChocolateyService -packageName "$unavailableServiceName" -createServiceCommand "$unavailableServiceName" It "should return an error" { Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing ServiceName input parameter." } @@ -28,7 +72,7 @@ Describe "Install-ChocolateyService" { Context "When no createServiceCommand parameter is passed to this function" { Mock Write-ChocolateyFailure - Install-ChocolateyService -packageName "TestTargetPath" -serviceName "TestWorkingDiectory" + Install-ChocolateyService -packageName "$unavailableServiceName" -serviceName "$unavailableServiceName" It "should return an error" { Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing CreateServiceCommand input parameter." } @@ -37,21 +81,28 @@ Describe "Install-ChocolateyService" { Context "When service does not exist" { Mock Write-ChocolateyFailure - - Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "notepad" - + + Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" -serviceName "$unavailableServiceName" + It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service helloworld does not exist." } + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service unavailableServiceName does not exist." } } } - + Context "When createServiceCommand is incorrect" { Mock Write-ChocolateyFailure - - Install-ChocolateyService -packageName "helloworld" -serviceName "helloworld" -createServiceCommand "c:\helloworld" + + Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" -createServiceCommand "$unavailableServiceName" It "should return an error" { - Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "createServiceCommand c:\helloworld is incorrect." } + Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "The createServiceCommand is incorrect: 'The term 'unavailableServiceName' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'." } } } + + Write-Host "Remove test directory after finishing testing" + Delete-Service -serviceName "$serviceName" + + if (Test-Path $testDirectory) { + Remove-Item -Recurse -Force $testDirectory + } } \ No newline at end of file From 6a603b75e48d32f6fd477b8c7d27b890d6361040 Mon Sep 17 00:00:00 2001 From: 030 Date: Tue, 8 Jul 2014 02:56:07 +0200 Subject: [PATCH 20/25] (GH-489) Delete-Service changed to Remove-Service as Delete was not acceptable by Chocolatey --- src/helpers/chocolateyInstaller.psm1 | 2 +- src/helpers/functions/Install-ChocolateyService.ps1 | 2 +- .../functions/{Delete-Service.ps1 => Remove-Service.ps1} | 2 +- tests/unit/Install-ChocolateyService.Tests.ps1 | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/helpers/functions/{Delete-Service.ps1 => Remove-Service.ps1} (91%) diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index e15ed76..3f0a60c 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -40,4 +40,4 @@ Export-ModuleMember -Function ` Install-ChocolateyService,` Get-ServiceExistence,` Get-ServiceStatus,` - Delete-Service \ No newline at end of file + Remove-Service \ No newline at end of file diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index c5aad8d..20ec8a7 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -23,7 +23,7 @@ param( } try { - Delete-Service -serviceName "$serviceName" + Remove-Service -serviceName "$serviceName" try { Write-Host "$packageName service will be installed" diff --git a/src/helpers/functions/Delete-Service.ps1 b/src/helpers/functions/Remove-Service.ps1 similarity index 91% rename from src/helpers/functions/Delete-Service.ps1 rename to src/helpers/functions/Remove-Service.ps1 index cd92d38..3ad58c4 100644 --- a/src/helpers/functions/Delete-Service.ps1 +++ b/src/helpers/functions/Remove-Service.ps1 @@ -1,4 +1,4 @@ -function Delete-Service { +function Remove-Service { param( [string] $serviceName = '' ) diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index cb9c0ea..65463f4 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -100,7 +100,7 @@ Describe "Install-ChocolateyService" { } Write-Host "Remove test directory after finishing testing" - Delete-Service -serviceName "$serviceName" + Remove-Service -serviceName "$serviceName" if (Test-Path $testDirectory) { Remove-Item -Recurse -Force $testDirectory From 06a498515cc8dac3e255ed8fbf607c9876dd0878 Mon Sep 17 00:00:00 2001 From: 030 Date: Tue, 8 Jul 2014 21:26:42 +0200 Subject: [PATCH 21/25] (GH-489) Incorrect Debug-information fixed --- src/helpers/functions/Install-ChocolateyService.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index 20ec8a7..c76aa3e 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -5,7 +5,7 @@ param( [string] $createServiceCommand, [int] $availablePort ) - Write-Debug "Running 'Install-ChocolateyService' for $packageName with url:`'$url`', unzipLocation: `'$unzipLocation`', url64bit: `'$url64bit`', specificFolder: `'$specificFolder`', checksum: `'$checksum`', checksumType: `'$checksumType`', checksum64: `'$checksum64`', checksumType64: `'$checksumType64`' "; + Write-Debug "Running 'Install-ChocolateyService' for $packageName with serviceName:`'$serviceName`', createServiceCommand: `'$createServiceCommand`', availablePort: `'$availablePort`' "; if(!$packageName) { Write-ChocolateyFailure "Install-ChocolateyService" "Missing PackageName input parameter." From 992daadcfac483c56b20b3079e2cdf503dc09123 Mon Sep 17 00:00:00 2001 From: 030 Date: Tue, 8 Jul 2014 21:56:30 +0200 Subject: [PATCH 22/25] (GH-506) Documentation added --- .../functions/Install-ChocolateyService.ps1 | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index c76aa3e..a1f524b 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -1,4 +1,37 @@ function Install-ChocolateyService { +<# +.SYNOPSIS +Installs a service + +.DESCRIPTION +This will install a service + +.PARAMETER PackageName +The name of the package for whom the service will be installed. + +.PARAMETER ServiceName +The name of service which will be used to install and start the service. + +.PARAMETER CreateServiceCommand +The command which installs the service. + +.PARAMETER AvailablePort (OPTIONAL) +The port which needs to be available in order to start the service. + +.EXAMPLE +Install-ChocolateyService 'PACKAGE_NAME' 'SERVICE_NAME' 'INSTALL_COMMAND' 'PORT' + +.OUTPUTS +None + +.NOTES +This helper reduces the number of lines one would have to write to install a service to 1 line. +This method has error handling built into it. + +.LINK +Get-ChocolateyWebFile +Get-ChocolateyUnzip +#> param( [string] $packageName, [string] $serviceName, From 715aec861d983398e4b20f1d4204295ce08f709c Mon Sep 17 00:00:00 2001 From: 030 Date: Tue, 8 Jul 2014 22:07:59 +0200 Subject: [PATCH 23/25] (GH-506) Remove-Service changed to Uninstall-ChocolateyService. --- src/helpers/chocolateyInstaller.psm1 | 2 +- src/helpers/functions/Install-ChocolateyService.ps1 | 6 +++--- .../{Remove-Service.ps1 => Uninstall-ChocolateyService.ps1} | 2 +- tests/unit/Install-ChocolateyService.Tests.ps1 | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename src/helpers/functions/{Remove-Service.ps1 => Uninstall-ChocolateyService.ps1} (88%) diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index 3f0a60c..5264422 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -40,4 +40,4 @@ Export-ModuleMember -Function ` Install-ChocolateyService,` Get-ServiceExistence,` Get-ServiceStatus,` - Remove-Service \ No newline at end of file + Uninstall-ChocolateyService \ No newline at end of file diff --git a/src/helpers/functions/Install-ChocolateyService.ps1 b/src/helpers/functions/Install-ChocolateyService.ps1 index a1f524b..082cf5a 100644 --- a/src/helpers/functions/Install-ChocolateyService.ps1 +++ b/src/helpers/functions/Install-ChocolateyService.ps1 @@ -29,8 +29,8 @@ This helper reduces the number of lines one would have to write to install a ser This method has error handling built into it. .LINK -Get-ChocolateyWebFile -Get-ChocolateyUnzip +Uninstall-ChocolateyService +Get-ServiceExistence #> param( [string] $packageName, @@ -56,7 +56,7 @@ param( } try { - Remove-Service -serviceName "$serviceName" + Uninstall-ChocolateyService -serviceName "$serviceName" try { Write-Host "$packageName service will be installed" diff --git a/src/helpers/functions/Remove-Service.ps1 b/src/helpers/functions/Uninstall-ChocolateyService.ps1 similarity index 88% rename from src/helpers/functions/Remove-Service.ps1 rename to src/helpers/functions/Uninstall-ChocolateyService.ps1 index 3ad58c4..4b64402 100644 --- a/src/helpers/functions/Remove-Service.ps1 +++ b/src/helpers/functions/Uninstall-ChocolateyService.ps1 @@ -1,4 +1,4 @@ -function Remove-Service { +function Uninstall-ChocolateyService { param( [string] $serviceName = '' ) diff --git a/tests/unit/Install-ChocolateyService.Tests.ps1 b/tests/unit/Install-ChocolateyService.Tests.ps1 index 65463f4..2198bfc 100644 --- a/tests/unit/Install-ChocolateyService.Tests.ps1 +++ b/tests/unit/Install-ChocolateyService.Tests.ps1 @@ -100,7 +100,7 @@ Describe "Install-ChocolateyService" { } Write-Host "Remove test directory after finishing testing" - Remove-Service -serviceName "$serviceName" + Uninstall-ChocolateyService -serviceName "$serviceName" if (Test-Path $testDirectory) { Remove-Item -Recurse -Force $testDirectory From b1be39ccc2fcd78737c257646feba2606487ba2f Mon Sep 17 00:00:00 2001 From: 030 Date: Fri, 11 Jul 2014 20:31:50 +0200 Subject: [PATCH 24/25] (GH-512) Install-ChocolateyArchivePackage wrapper removed by creating new branch. Install-ChocolateyArchivePackage AKA Install-ChocolateyZipPackage created --- src/helpers/functions/Install-ChocolateyZipPackage.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/helpers/functions/Install-ChocolateyZipPackage.ps1 b/src/helpers/functions/Install-ChocolateyZipPackage.ps1 index 15481bb..e3d01b1 100644 --- a/src/helpers/functions/Install-ChocolateyZipPackage.ps1 +++ b/src/helpers/functions/Install-ChocolateyZipPackage.ps1 @@ -63,3 +63,5 @@ param( throw } } + +set-alias Install-ChocolateyArchivePackage Install-ChocolateyZipPackage \ No newline at end of file From fc60f602effb5a033551b8f4eb656199d9a7cf8c Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 13 Jul 2014 05:07:30 +0200 Subject: [PATCH 25/25] (GH-512) Set-alias is not sufficient to create an AKA. Exporting is required as well. --- src/helpers/chocolateyInstaller.psm1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/helpers/chocolateyInstaller.psm1 b/src/helpers/chocolateyInstaller.psm1 index 5264422..3e7bcfe 100644 --- a/src/helpers/chocolateyInstaller.psm1 +++ b/src/helpers/chocolateyInstaller.psm1 @@ -3,7 +3,6 @@ $DebugPreference = "SilentlyContinue" if ($env:ChocolateyEnvironmentDebug -eq 'true') {$DebugPreference = "Continue";} - # grab functions from files Resolve-Path $helpersPath\functions\*.ps1 | ? { -not ($_.ProviderPath.Contains(".Tests.")) } | @@ -40,4 +39,8 @@ Export-ModuleMember -Function ` Install-ChocolateyService,` Get-ServiceExistence,` Get-ServiceStatus,` - Uninstall-ChocolateyService \ No newline at end of file + Uninstall-ChocolateyService + +Export-ModuleMember -Function ` + Install-ChocolateyZipPackage -alias ` + Install-ChocolateyArchivePackage \ No newline at end of file