Skip to content

Commit

Permalink
Merge pull request #3 from joeypiccola/pester
Browse files Browse the repository at this point in the history
adding pester tests and updating docs
  • Loading branch information
joeypiccola committed Oct 6, 2019
2 parents e64f16a + 7377193 commit 50c76b5
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.5]
### Added
- added pester tests
- token nodes in readme
- be consistent with how we write out object with > item

## [0.1.4]
### Added
- bump build reqs
Expand Down
2 changes: 1 addition & 1 deletion PSPuppetOrchestrator/PSPuppetOrchestrator.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSPuppetOrchestrator.psm1'

# Version number of this module.
ModuleVersion = '0.1.4'
ModuleVersion = '0.1.5'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
5 changes: 2 additions & 3 deletions PSPuppetOrchestrator/Public/Get-PuppetJobReport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ Function Get-PuppetJobReport {
$hoststr = "https://$master`:8143/orchestrator/v1/jobs/$id/report"
$headers = @{'X-Authentication' = $Token}
$result = Invoke-RestMethod -Uri $hoststr -Method Get -Headers $headers
$result.count
foreach ($server in $result.report) {
Write-Output $server
foreach ($report in $result.report) {
Write-Output $report
}
}
4 changes: 3 additions & 1 deletion PSPuppetOrchestrator/Public/Get-PuppetJobResults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ Function Get-PuppetJobResults {
$hoststr = "https://$master`:8143/orchestrator/v1/jobs/$id/nodes"
$headers = @{'X-Authentication' = $Token}
$result = Invoke-RestMethod -Uri $hoststr -Method Get -Headers $headers
Write-Output $result.items
foreach ($item in $result.items) {
Write-Output $item
}
}
2 changes: 1 addition & 1 deletion PSPuppetOrchestrator/Public/Get-PuppetTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Function Get-PuppetTask {

# try and get the task in it's standard form $moduleName/$taskName
try {
$result = Invoke-RestMethod -Uri $hoststr -Method Get -Headers $headers -ErrorAction SilentlyContinue
$result = Invoke-RestMethod -Uri $hoststr -Method Get -Headers $headers -ErrorAction SilentlyContinue
} catch {
# try and get the task again assuming it's built in with a default task name of 'init' (e.g. reboot/init)
try {
Expand Down
4 changes: 3 additions & 1 deletion PSPuppetOrchestrator/Public/Get-PuppetTasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ Function Get-PuppetTasks {
$headers = @{'X-Authentication' = $Token}
$body = @{'environment' = $environment}
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -Body $body
Write-Output $result.items
foreach ($item in $result.items) {
Write-Output $item
}
}
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,47 @@ PowerShell module for calling the Puppet Orchestrator API.
PS> Install-Module -Name PSPuppetOrchestrator
```

## Included Functions
`Invoke-PuppetTask`

`Get-PuppetTask`

`Get-PuppetTasks`

`Get-PuppetJob`

`Get-PuppetJobReport`

`Get-PuppetJobResults`

`Get-PuppetPCPNodeBrokerDetails`

## Token
In order to call the `orchestrator` API endpoint you'll need a user token. There are a couple methods to acquire a Puppet Enterprise user token. In the spirit of using PowerShell, below is a script to get a token via the `rbac-api` endpoint. Specify a numeric value followed by “y” (years), “d” (days), “h” (hours), “m” (minutes), or “s”(seconds) for `lifeimte`. If you do not want the token to expire, set the lifetime to “0”. Setting it to zero gives the token a lifetime of approximately 10 years. Do consider correctly filling out 'description', 'client', and 'label' with accurate information (they're all optional and can be removed if needed). 'description', 'client', and 'label' help with token audits.

```powershell
$puppetMaster = 'puppet.contoso.com'
$cred = Get-Credential -Message 'Puppet Credentials (e.g. myuser-p)'
$req = [PSCustomObject]@{
login = $cred.UserName
password = $cred.GetNetworkCredential().Password
lifetime = '1d'
description = "Token used for testing puppet tasks"
client = ""
label = "personal workstation token"
} | ConvertTo-Json
$hoststr = "https://$puppet`:4433/rbac-api/v1/auth/token"
$result = Invoke-RestMethod -Uri $hoststr -Method Post -Body $req -ContentType 'application/json'
$result
```
OUTPUT
```
token
-----
***********************************wCB8
```

## Examples
### Get tasks
Get a list of tasks.
Expand Down
12 changes: 12 additions & 0 deletions Tests/Get-PuppetJob.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
InModuleScope PSPuppetOrchestrator {
Describe 'Get-PuppetJob' -Tag unit {
function Invoke-RestMethod {}
Mock Invoke-RestMethod { return @{name = 100} }
context 'returns jobs as expected' {
it "should return job 100" {
(Get-PuppetJob -Token 1 -Master 'pm' -ID 100).name | should -be 100
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
}
}
}
36 changes: 36 additions & 0 deletions Tests/Get-PuppetJobReport.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
InModuleScope PSPuppetOrchestrator {
Describe 'Get-PuppetJobReport' -Tag unit {
function Invoke-RestMethod {}
context 'returns reports as expected' {
Mock Invoke-RestMethod {
[PSCustomObject]@{
report = @('a')
}
}
it "returns one report" {
(Get-PuppetJobReport -Token 1 -Master 'pm' -ID 100 | Measure-Object).count | should -be 1
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
Mock Invoke-RestMethod {
[PSCustomObject]@{
report = @('a','b')
}
}
it "returns two reports" {
(Get-PuppetJobReport -Token 1 -Master 'pm' -ID 100 | Measure-Object).count | should -be 2
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
Mock Invoke-RestMethod {
[PSCustomObject]@{
report = @{
node = 'node1'
}
}
}
it "returns single report for node1" {
(Get-PuppetJobReport -Token 1 -Master 'pm' -ID 100).node | should -be 'node1'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
}
}
}
36 changes: 36 additions & 0 deletions Tests/Get-PuppetJobResults.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
InModuleScope PSPuppetOrchestrator {
Describe 'Get-PuppetJobResults' -Tag unit {
function Invoke-RestMethod {}
context 'returns reports as expected' {
Mock Invoke-RestMethod {
[PSCustomObject]@{
items = @('a')
}
}
it "returns one result" {
(Get-PuppetJobResults -Token 1 -Master 'pm' -ID 100 | Measure-Object).count | should -be 1
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
Mock Invoke-RestMethod {
[PSCustomObject]@{
items = @('a','b')
}
}
it "returns two result" {
(Get-PuppetJobResults -Token 1 -Master 'pm' -ID 100 | Measure-Object).count | should -be 2
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
Mock Invoke-RestMethod {
[PSCustomObject]@{
items = @{
name = 'node1'
}
}
}
it "returns single result for node1" {
(Get-PuppetJobResults -Token 1 -Master 'pm' -ID 100).name | should -be 'node1'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
}
}
}
12 changes: 12 additions & 0 deletions Tests/Get-PuppetNodePCPBrokerDetails.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
InModuleScope PSPuppetOrchestrator {
Describe 'Get-PuppetPCPNodeBrokerDetails' -Tag unit {
function Invoke-RestMethod {}
Mock Invoke-RestMethod { return @{name = 'node1'} }
context 'returns PCP Broker Details as expected' {
it "should return pcp broker details for node1" {
(Get-PuppetPCPNodeBrokerDetails -Token 1 -Master 'pm' -Node 'node1').name | should -be 'node1'
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
}
}
}
25 changes: 25 additions & 0 deletions Tests/Get-PuppetTasks.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
InModuleScope PSPuppetOrchestrator {
Describe 'Get-PuppetTasks' -Tag unit {
function Invoke-RestMethod {}
context 'returns tasks as expected' {
Mock Invoke-RestMethod {
[PSCustomObject]@{
items = @('a')
}
}
it "returns one task" {
(Get-PuppetTasks -Token 1 -Master 'pm' | Measure-Object).count | should -be 1
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
Mock Invoke-RestMethod {
[PSCustomObject]@{
items = @('a','b')
}
}
it "returns two tasks" {
(Get-PuppetTasks -Token 1 -Master 'pm' | Measure-Object).count | should -be 2
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly -Scope It
}
}
}
}

0 comments on commit 50c76b5

Please sign in to comment.