Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Aka install chocolatey zip package #549

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
476c823
Work on #489. Install-ChocolateyService created. Entry function added…
Jul 4, 2014
61581b1
Work on #489. Creating Pester test for Install-ChocolateyService
Jul 4, 2014
e6e507d
Work on #489. Creating Pester test for Install-ChocolateyService
Jul 4, 2014
0a73276
Work on #489. Created tests to validate all input parameters
Jul 5, 2014
100209e
Work on #489. Multiple functions created
Jul 5, 2014
51906e2
(GH-489) Multiple functions created
Jul 5, 2014
0b99c70
(GH-489) Port changed from required to optional
Jul 5, 2014
3df0334
(GH-489) Functions inside function issue solved
Jul 6, 2014
f5d1e88
(GH-489) Details about how to install services removed
Jul 6, 2014
2f24256
(GH-489) Created tests
Jul 6, 2014
6c93cac
(GH-489) Moved Install-ChocolateyService parameter to the end of file…
Jul 6, 2014
e8a7bb5
(GH-489) Function created to test whether service exists
Jul 6, 2014
8a5cccb
(GH-489) Function created to test whether status is running or stopped
Jul 6, 2014
f5a2873
(GH-489) ServiceExistence and Status functions moved from functions t…
Jul 6, 2014
0685746
(GH-489) Service create to delete services
Jul 6, 2014
26011d9
(GH-489) Get-ServiceExistence, Service-Status and Delete-Service func…
Jul 6, 2014
1658dd5
(GH-489) Function created to test Install-ChocolateyService if valid …
Jul 6, 2014
fb2236f
(GH-489) For loop created to start service 12 times with an interval …
Jul 6, 2014
ee0bfa7
(GH-489) Test created to test validity of installCommand and serviceS…
Jul 6, 2014
6a603b7
(GH-489) Delete-Service changed to Remove-Service as Delete was not a…
Jul 8, 2014
06a4985
(GH-489) Incorrect Debug-information fixed
Jul 8, 2014
992daad
(GH-506) Documentation added
Jul 8, 2014
715aec8
(GH-506) Remove-Service changed to Uninstall-ChocolateyService.
Jul 8, 2014
b1be39c
(GH-512) Install-ChocolateyArchivePackage wrapper removed by creating…
Jul 11, 2014
fc60f60
(GH-512) Set-alias is not sufficient to create an AKA. Exporting is r…
Jul 13, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/helpers/chocolateyInstaller.psm1
Expand Up @@ -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.")) } |
Expand Down Expand Up @@ -36,4 +35,12 @@ Export-ModuleMember -Function `
Update-SessionEnvironment,`
Get-EnvironmentVariableNames,`
Get-EnvironmentVariable,`
Set-EnvironmentVariable
Set-EnvironmentVariable,`
Install-ChocolateyService,`
Get-ServiceExistence,`
Get-ServiceStatus,`
Uninstall-ChocolateyService

Export-ModuleMember -Function `
Install-ChocolateyZipPackage -alias `
Install-ChocolateyArchivePackage
6 changes: 6 additions & 0 deletions src/helpers/functions/Get-ServiceExistence.ps1
@@ -0,0 +1,6 @@
function Get-ServiceExistence {
param(
[string] $serviceName = ''
)
Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
}
7 changes: 7 additions & 0 deletions src/helpers/functions/Get-ServiceStatus.ps1
@@ -0,0 +1,7 @@
function Get-ServiceStatus {
param(
[string] $serviceName = ''
)
$serviceStatus = Get-Service -Name $serviceName
$serviceStatus.Status
}
101 changes: 101 additions & 0 deletions src/helpers/functions/Install-ChocolateyService.ps1
@@ -0,0 +1,101 @@
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
Uninstall-ChocolateyService
Get-ServiceExistence
#>
param(
[string] $packageName,
[string] $serviceName,
[string] $createServiceCommand,
[int] $availablePort
)
Write-Debug "Running 'Install-ChocolateyService' for $packageName with serviceName:`'$serviceName`', createServiceCommand: `'$createServiceCommand`', availablePort: `'$availablePort`' ";

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
}

try {
Uninstall-ChocolateyService -serviceName "$serviceName"

try {
Write-Host "$packageName service will be installed"
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"}
if (!$listeningStatePort) {
return $TRUE
} else {
Write-ChocolateyFailure "Install-ChocolateyService" "$availablePort is in LISTENING state and not available."
return
}
}

if (Get-ServiceExistence -serviceName "$serviceName") {
Write-Host "$packageName service will be started"

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
}
} catch {
Write-ChocolateyFailure "Install-ChocolateyService" "There were errors attempting to create the $packageName service. The error message was '$_'."
}
}
2 changes: 2 additions & 0 deletions src/helpers/functions/Install-ChocolateyZipPackage.ps1
Expand Up @@ -63,3 +63,5 @@ param(
throw
}
}

set-alias Install-ChocolateyArchivePackage Install-ChocolateyZipPackage
11 changes: 11 additions & 0 deletions src/helpers/functions/Uninstall-ChocolateyService.ps1
@@ -0,0 +1,11 @@
function Uninstall-ChocolateyService {
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()
}
}
108 changes: 108 additions & 0 deletions tests/unit/Install-ChocolateyService.Tests.ps1
@@ -0,0 +1,108 @@
$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"
. "$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 "$unavailableServiceName" -createServiceCommand "$unavailableServiceName"

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 "$unavailableServiceName" -createServiceCommand "$unavailableServiceName"

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

Install-ChocolateyService -packageName "$unavailableServiceName" -serviceName "$unavailableServiceName"

It "should return an error" {
Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "Missing CreateServiceCommand input parameter." }
}
}

Context "When service does not exist" {
Mock Write-ChocolateyFailure

Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" -serviceName "$unavailableServiceName"

It "should return an error" {
Assert-MockCalled Write-ChocolateyFailure -parameterFilter { $failureMessage -eq "service unavailableServiceName does not exist." }
}
}

Context "When createServiceCommand is incorrect" {
Mock Write-ChocolateyFailure

Install-ChocolateyServiceCorrectParameters.Tests -testDirectory "$testDirectory" -createServiceCommand "$unavailableServiceName"

It "should return an error" {
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"
Uninstall-ChocolateyService -serviceName "$serviceName"

if (Test-Path $testDirectory) {
Remove-Item -Recurse -Force $testDirectory
}
}
21 changes: 21 additions & 0 deletions 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"
}