Skip to content

Commit

Permalink
Develop (#43)
Browse files Browse the repository at this point in the history
* Use buildhelpers in the unit tests

* Add unit test for ASTFunctions

* Add some of the bootstrap function tests

* Add more unit tests for the bootstrap functions

* Fix ScriptAnazlyer warnings

* Fix passing named parameters to Remove-PSSession

* Add ConfigurationData tests

* Add few unit tests for PSRemotely

* Add unit test template for Node.ps1

* Move PSSession creation above InModuleScope block

* Feature unittest (#36)

consolidated merge of the unit test branch

* Add PSDeafult param for New-PSSession

- set the EnableNetworkAccess switch on by default for the cmdlet

* Add -enabletnetworkaccess switch explicitly

- Unit tests failing on Appveyor, adding switch

* Move PSSession inside InModuleScope and Describe block

* Remove the PSSession creation

* Add RDP to worker in appveyor.yml

* Fixing the integration tests breaking

- build.ps1 - remove ps default parameter setting, no effect
- PSRemotely.json - remove required artifacts
- PSRemotely.psm1 - set ps default parameter
- Basic and ConfigData tests - Typo in the assertion

* Replace psake with InvokeBuild

- few more minor fixes

* Update the .psd1

- minor fix to the InvokeBuild.ps1

* Remove RDP to worker from appveyor.yml

* Add few fixes to the quick start doc

* Add fix docs badge link

* Remove exit with psake build success

- where alias replaced

* Add Contributing guideline

- copied from PSDeploy's
- !Deploy to Gallery

* Add check to the psdeploy file for the NugetAPIKey

- Revert the scope for AllNodes to be global
- Fix README

* replace write-error with throw in the InvokeBuild

- restart winrm service in once in one of the integration test

* Add clean up after unit tests complete
  • Loading branch information
DexterPOSH authored Jan 18, 2017
1 parent 8645114 commit 31a487c
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 59 deletions.
50 changes: 50 additions & 0 deletions Contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# How to Contribute

Contributions to PSRemotely would be quite welcome and helpful. This page describes some ideas and guidelines around contributing.

# What Should I Contribute?

There are many ways to contribute, including:

* Open an issue for...
* A bug you find
* A feature you think would be valuable
* A question on using PSRemotely (you might not be the only one with the question)
* Edit the docs to...
* Improve or expand on [existing docs](https://PSRemotely.readthedocs.io)
* Document a deployment type
* Write a 'How do I...' scenario
* Submit pull requests to...
* Improve or expand on the [Readme.md](https://github.com/DexterPOSH/PSRemotely/blob/master/README.md) or this Contributing.md
* Provide bug fixes, new features, cleaner code, or other improvements

# How Do I Contribute?

All of these require that you [have a GitHub account](https://github.com/signup/free).

* [Submit an issue](https://github.com/DexterPOSH/PSRemotely/issues)
* Use the search box and flip through open/closed issues to avoid duplication
* If the issue is for a bug fix, provide reproducible code. If you can't, and you think an issue is still warranted, provide your code, and related details on your environment
* Contribute to [the docs](https://PSRemotely.readthedocs.io)
* Fork the repo
* Checkout and work in the master branch
* Organization is described in the [mkdocs.yml](https://github.com/DexterPOSH/PSRemotely/blob/master/mkdocs.yml) file. If you add a file or section, mkdocs.yml needs to know
* mkdocs.yml points to markdown files in [the docs folder](https://github.com/DexterPOSH/PSRemotely/tree/master/docs)
* Images are stored and accessible from docs/images
* Commit changes
* Submit a pull request to the master branch
* Submit a pull request
* Fork the repo
* Checkout and work in the master branch
* Where possible, add Pester tests for your change
* Submit your pull request to the master branch

# Additional Resources
* [General GitHub documentation](https://help.github.com/)
* [GitHub forking documentation](https://guides.github.com/activities/forking/)
* [GitHub pull request documentation](https://help.github.com/send-pull-requests/)
* [GitHub Flow guide](https://guides.github.com/introduction/flow/)
* [GitHub's guide to contributing to open source projects](https://guides.github.com/activities/contributing-to-open-source/)

More to come, just getting this out there. Thanks to RamblingCookieMonster's PSDeploy module for the module organization.

46 changes: 29 additions & 17 deletions psake.ps1 → InvokeBuild.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
<#
.Synopsis
Build script invoked by Invoke-Build.
.Description
TODO: Declare build parameters as standard script parameters. Parameters
are specified directly for Invoke-Build if their names do not conflict.
Otherwise or alternatively they are passed in as "-Parameters @{...}".
#>

# TODO: [CmdletBinding()] is optional but recommended for strict name checks.
[CmdletBinding()]
param(
)
# PSake makes variables declared here available in other scriptblocks
# Init some things
Properties {
# TODO: Move some properties to script param() in order to use as parameters.

# Find the build folder based on build system
$ProjectRoot = $ENV:BHProjectPath
if(-not $ProjectRoot)
{
$ProjectRoot = $PSScriptRoot
}
$ProjectRoot = $ENV:BHProjectPath
if(-not $ProjectRoot)
{
$ProjectRoot = $PSScriptRoot
}

$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$PSVersion = $PSVersionTable.PSVersion.Major
Expand All @@ -18,19 +33,19 @@ Properties {
{
$Verbose = @{Verbose = $True}
}
}

Task Default -Depends Deploy
# TODO: Default task. If it is the first then any name can be used instead.
task . Deploy

Task Init {
task Init {
$lines
Set-Location $ProjectRoot
"Build System Details:"
Get-Item ENV:BH*
"`n"
}

Task Test -Depends Init {
task Test Init, {
$lines

foreach ($TestType in @('Unit','Integration')) {
Expand All @@ -40,9 +55,6 @@ Task Test -Depends Init {
# Gather test results. Store them in a variable and file
$TestResults = Invoke-Pester -Path "$ProjectRoot\Tests\$TestType" -PassThru -OutputFormat NUnitXml -OutputFile "$ProjectRoot\$TestFile"


"`n`tSTATUS: Integration testing with PowerShell $PSVersion"

# In Appveyor? Upload our tests! #Abstract this into a function?
If($ENV:BHBuildSystem -eq 'AppVeyor')
{
Expand All @@ -57,15 +69,15 @@ Task Test -Depends Init {
# Need to tell psake or it will proceed to the deployment. Danger!
if($TestResults.FailedCount -gt 0)
{
Write-Error "Failed '$($TestResults.FailedCount)' $TestType tests, build failed"
throw "Failed '$($TestResults.FailedCount)' $TestType tests, build failed"
break # break out if any of the test fails
}
"`n"
}

}

Task Build -Depends Test {
task Build Test, {
$lines

# Load the module, read the exported functions, update the psd1 FunctionsToExport
Expand All @@ -83,7 +95,7 @@ Task Build -Depends Test {
}
}

Task Deploy -Depends Build {
task Deploy Build, {
$lines

$Params = @{
Expand All @@ -92,4 +104,4 @@ Task Deploy -Depends Build {
Recurse = $false # We keep psdeploy artifacts, avoid deploying those : )
}
Invoke-PSDeploy @Verbose @Params
}
}
2 changes: 1 addition & 1 deletion PSRemotely/PSRemotely.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
}
],
"ArtifactsRequired":[
"DeploymentManifest.xml"
""
]
}
Binary file modified PSRemotely/PSRemotely.psd1
Binary file not shown.
3 changes: 3 additions & 0 deletions PSRemotely/PSRemotely.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ $PSRemotely = ConvertPSObjectToHashTable -InputObject $jsonObject
# module variables
$PSRemotely.Add('NodeMap', @())
$PSRemotely.Add('sessionHashTable', @{})
# Setting the default value for New-PSSession to use the EnabletNetworkAccess switch
# Found an issue with the Appveyor VM, which won't let the tests in place create the loopback sessions.
$PSDefaultParameterValues=@{"New-PSSession:EnableNetworkAccess"=$True}

# create an alias for PSRemotely
New-Alias -Name Remotely -Value PSRemotely -Description "Remote Ops validation, using PSRemotely."
Expand Down
6 changes: 3 additions & 3 deletions PSRemotely/private/ConfigurationData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Function Test-ConfigData {
# Check for the duplicate nodeName Values
if ($NodeNames.Contains($node['NodeName'])) {
# Duplicate node in the list
throw ("There is a duplicate NodeName '{0}' in the configurationData passed in." -f $node['NodeName'])
throw $("There is a duplicate NodeName '{0}' in the configurationData passed in." -f $node['NodeName'])
}

$null = $NodeNames.Add($node['NodeName'])
Expand All @@ -41,8 +41,8 @@ Function Update-ConfigData {
[Hashtable]$ConfigurationData
)

if ($configurationData.AllNodes | Where -Property NodeName -eq '*') {
$AllNodeSettings = $configurationData.AllNodes | Where -Property NodeName -eq '*'
if ($configurationData.AllNodes | Where-Object -Property NodeName -eq '*') {
$AllNodeSettings = $configurationData.AllNodes | Where-Object -Property NodeName -eq '*'
}

# Copy all the settings for the nodes
Expand Down
4 changes: 2 additions & 2 deletions PSRemotely/public/PSRemotely.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function PSRemotely

# Define the AllNodes variable in current scope
Write-VerboseLog -Message 'Creating the AllNodes global scope variable'
New-Variable -Name AllNodes -Value $configurationData.AllNodes -Scope Script -Force
New-Variable -Name AllNodes -Value $configurationData.AllNodes -Scope Global -Force

if ($AllNodes.NodeName) {
Write-VerboseLog -Message 'Creating sessions'
Expand Down Expand Up @@ -145,7 +145,7 @@ function PSRemotely

& $Body # invoke the body
Write-VerboseLog -Message 'Clearing the AllNodes global variable'
Remove-Variable -Name AllNodes -Scope Script -Force -ErrorAction SilentlyContinue
Remove-Variable -Name AllNodes -Scope Global -Force -ErrorAction SilentlyContinue

}

Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build status](https://ci.appveyor.com/api/projects/status/w22ytbuyjr7a10ia/branch/master?svg=true)](https://ci.appveyor.com/project/DexterPOSH/psremotely/branch/master) [![Documentation Status](http://psremotely.readthedocs.io/en/latest/?badge=latest)](http://psremotely.readthedocs.io/en/latest/?badge=latestt)
[![Build status](https://ci.appveyor.com/api/projects/status/w22ytbuyjr7a10ia/branch/master?svg=true)](https://ci.appveyor.com/project/DexterPOSH/psremotely/branch/master) [![Documentation Status](https://readthedocs.org/projects/psremotely/badge/?version=latest)](https://readthedocs.org/projects/psremotely/badge/?version=latest)

PSRemotely
============
Expand Down Expand Up @@ -31,7 +31,7 @@ PSRemotely workflow is as under :
Well this is a term coined by us for some of the infrastucture validation being done for Engineered solutions.
So taking liberty to use this here.

Suppose, you already have below existing Pester test for some nodes in our environment:
Suppose, you already have below Pester test for some nodes in our environment:

```powershell
Describe 'Bits Service test' {
Expand All @@ -47,7 +47,8 @@ Describe 'Bits Service test' {
}
}
```
If you want to run the very same tests on the remote node named say AD, then below is how you use PSRemotely.
If you want to target the very same tests on the remote node named say Node1,Node2 & Node3 from your current workstation, you can use PSRemotely.


Usage with PSRemotely:

Expand All @@ -71,10 +72,10 @@ PSRemotely {
}
}
```
Once you have the tests file ready, below is how you invoke the PSRemotely framework to start the remote ops validation :
Once you have the tests file ready , save it with a <FileName>.PSRemotely.ps1 extension, below is how you invoke the PSRemotely framework to start the remote ops validation :

```powershell
Invoke-PSRemotely -Script <Path_to_Tests.ps1>
Invoke-PSRemotely -Script <FileName>.PSRemotely.ps1
```

Output of the above is a JSON object, if the tests pass then an empty JSON object array of *TestResult* is returned
Expand All @@ -90,7 +91,7 @@ otherwise the Error record thrown by Pester is returned :

],
"Result": true,
"Name": "Service test"
"Name": "Bits Service test"
}
]
}
Expand Down Expand Up @@ -120,7 +121,7 @@ otherwise the Error record thrown by Pester is returned :
```
## More Information

The [PSRemotely docs](psremotely.readthedocs.io) will include more information, including :
The [PSRemotely docs](http://psremotely.readthedocs.io/) will include more information, including :

* PSRemotely Basics
* PSRemotely Examples
Expand All @@ -139,11 +140,11 @@ Thanks goes to :
## TO DO

PSRemotely in its current form works for our needs to validate some of the Engineered solutions.
But it has the potential to grow to be much bigger, below are some of the items in the wishlist :
But it has the potential to grow into something bigger, below are some of the items in the wishlist :

- Use PowerShell classes, and add support for providers for nodes running either On-prem or on Cloud (AWS or Azure).
- Integrate with JEA, since PSRemotely uses PSSession endpoints these can be locked down using JEA on the nodes.
- More unit tests, lacking far behind in this aspect.
- More unit tests, lacking far behind in this aspect. More focus on it once we start working with the classes implementation.
- Faster background processing of the remote node jobs, using runspaces.

Feel free to submit any ideas, bugs or pull requests.
8 changes: 5 additions & 3 deletions Tests/Integration/Basic.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ $ArtifactsPath = "$Env:BHPSModulePath\lib\Artifacts"
# Import the TestHelpers
Get-ChildItem -Path "$env:BHProjectPath\Tests\TestHelpers\*.psm1" |
Foreach-Object {
Import-Module -Name $PSItem -verbose -Force
Remove-Module -Name $PSitem.BaseName -Force -ErrorAction SilentlyContinue # reload the module, the script module might have changes
Import-Module -Name $PSItem.FullName -Force
}


# use a dummy artifact with PSRemotely-
Set-PSRemotelyToUseDummyArtifact -Path $RemotelyJSONFile
Copy-DummyArtifact -Path "$ArtifactsPath\DeploymentManifest.xml"

Get-PSSession | Remove-PSSession # clean up older PSSessions created during the Unit tests
Remove-Variable -Name AllNodes,PSRemotely -Scope Global -Force -ErrorAction SilentlyContinue # Just a fail safe
Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $ENV:BHProjectPath $ENV:BHProjectName) -Force

Expand Down Expand Up @@ -155,7 +157,7 @@ try {
}

It 'Should return more details about the test failed in the TestResult' {
$Object.Tests[0].TestResult.Describe | Should BeExactly 'Bits Service Test'
$Object.Tests[0].TestResult.Describe | Should Be 'Bits Service Test'
$Object.Tests[0].TestResult.Name | Should BeExactly 'Should be running'
$Object.Tests[0].TestResult.Result | Should Be 'Failed'
$Object.Tests[0].TestResult.ErrorRecord | Should NOT BeNullOrEmpty
Expand Down
2 changes: 1 addition & 1 deletion Tests/Integration/ConfigData.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Foreach ($RemotelyTestFile in $RemotelyTestFiles) {
}

It 'Should return more details about the test failed in the TestResult' {
$Object.Tests[0].TestResult.Describe | Should BeExactly 'Bits Service Test'
$Object.Tests[0].TestResult.Describe | Should Be 'Bits Service Test'
$Object.Tests[0].TestResult.Name | Should BeExactly 'Should be running'
$Object.Tests[0].TestResult.Result | Should Be 'Failed'
$Object.Tests[0].TestResult.ErrorRecord | Should NOT BeNullOrEmpty
Expand Down
3 changes: 2 additions & 1 deletion Tests/Integration/Generic.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ $ArtifactsPath = "$Env:BHPSModulePath\lib\Artifacts"
# Import the TestHelpers
Get-ChildItem -Path "$env:BHProjectPath\Tests\TestHelpers\*.psm1" |
Foreach-Object {
Import-Module -Name $PSItem -verbose -Force
Remove-Module -Name $PSitem.BaseName -Force -ErrorAction SilentlyContinue # reload the module, the script module might have changes
Import-Module -Name $PSItem.FullName -Force
}

Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue
Expand Down
7 changes: 4 additions & 3 deletions Tests/Integration/ReRunTest.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Import-Module (Join-Path $ENV:BHProjectPath $ENV:BHProjectName) -Force
# Import the TestHelpers
Get-ChildItem -Path "$env:BHProjectPath\Tests\TestHelpers\*.psm1" |
Foreach-Object {
Import-Module -Name $PSItem -verbose -Force
Remove-Module -Name $PSitem.BaseName -Force -ErrorAction SilentlyContinue # reload the module, the script module might have changes
Import-Module -Name $PSItem.FullName -Force
}

try {
Expand Down Expand Up @@ -117,7 +118,7 @@ try {
}

It 'Should return more details about the test failed in the TestResult' {
$Object.Tests[0].TestResult.Describe | Should BeExactly 'Bits Service Test'
$Object.Tests[0].TestResult.Describe | Should Be 'Bits Service Test'
$Object.Tests[0].TestResult.Name | Should BeExactly 'Should be running'
$Object.Tests[0].TestResult.Result | Should Be 'Failed'
$Object.Tests[0].TestResult.ErrorRecord | Should NOT BeNullOrEmpty
Expand Down Expand Up @@ -187,7 +188,7 @@ try {
}

It 'Should return more details about the test failed in the TestResult' {
$Object.Tests[0].TestResult.Describe | Should BeExactly 'Bits Service Test'
$Object.Tests[0].TestResult.Describe | Should Be 'Bits Service Test'
$Object.Tests[0].TestResult.Name | Should BeExactly 'Should be running'
$Object.Tests[0].TestResult.Result | Should Be 'Failed'
$Object.Tests[0].TestResult.ErrorRecord | Should NOT BeNullOrEmpty
Expand Down
5 changes: 4 additions & 1 deletion Tests/Unit/Node.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ $PSVersion = $PSVersionTable.PSVersion.Major
Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $ENV:BHProjectPath $ENV:BHProjectName) -Force




InModuleScope -ModuleName $ENV:BHProjectName {

Get-Service -Name WinRM | Restart-Service
$Session = New-PSSession -ComputerName Localhost
$Session = New-PSSession -ComputerName Localhost -EnableNetworkAccess

Describe 'Node' {

Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/bootstrap.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ $PSVersion = $PSVersionTable.PSVersion.Major
Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $ENV:BHProjectPath $ENV:BHProjectName) -Force


InModuleScope -ModuleName $ENV:BHProjectName {
# Module Preamble - put initialization code here
$ModuleRequired = @( # Array of the hashtables for FullyQualifedNames for modules
Expand All @@ -21,7 +20,7 @@ InModuleScope -ModuleName $ENV:BHProjectName {
ModuleVersion='1.1.10';
})
Get-Service -Name WinRM | Restart-Service
$Session = New-PSSession -ComputerName Localhost
$Session = New-PSSession -ComputerName Localhost -EnableNetworkAccess

Describe "BootStrapRemotelyNode $PSVersion" -Tags UnitTest {

Expand Down Expand Up @@ -76,6 +75,7 @@ Describe "BootStrapRemotelyNode $PSVersion" -Tags UnitTest {

It "Should archive the existing tests files on the PSRemotely path on the node" {
Assert-MockCalled -CommandName CleanupPSRemotelyNodePath -Times 1 -Exactly -Scope Context

}

It 'Should update the PSRemotely variable at the beginning and end of the bootstrap function' {
Expand Down
Loading

0 comments on commit 31a487c

Please sign in to comment.