Skip to content

Instantly share code, notes, and snippets.

@john-nicholson
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-nicholson/acf283f293d0cc90ad58 to your computer and use it in GitHub Desktop.
Save john-nicholson/acf283f293d0cc90ad58 to your computer and use it in GitHub Desktop.
Example of Testing DSC with Pester ( AWS Read S3 Object)
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Path,
[parameter(Mandatory = $true)]
[System.String]
$BucketName,
[parameter(Mandatory = $true)]
[System.String]
$Key,
[parameter(Mandatory = $false)]
[System.String]
$ProfileName
)
# We are not checking in this whether the file is present (always return absent)
Write-Verbose -Message "Creating hashtable."
$returnValue = @{
Path = $Path
Key = $Key
Bucket = $BucketName
Ensure = "Absent"
}
$returnValue
}
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Path,
[parameter(Mandatory = $true)]
[System.String]
$BucketName,
[parameter(Mandatory = $true)]
[System.String]
$Key,
[parameter(Mandatory = $false)]
[System.String]
$ProfileName,
[ValidateSet("Folder","File")]
[System.String]
$ItemType,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
if($Ensure -eq "Present")
{
# Read the file from S3
Write-Verbose -Message "Reading the File from S3"
if($ItemType -eq "File")
{
if($ProfileName){
Read-S3Object -BucketName $BucketName -Key $Key -ProfileName $ProfileName -File $Path
}
else{
Read-S3Object -BucketName $BucketName -Key $Key -File $Path
}
}
else{
if($ProfileName){
Read-S3Object -BucketName $BucketName -KeyPrefix $Key -Folder $Path -ProfileName $ProfileName
}
else{
Read-S3Object -BucketName $BucketName -KeyPrefix $Key -Folder $Path
}
}
}
else
{
Write-Verbose -Message "Removing the file"
#Remove the file
Remove-Item -Path $Path -Force
}
}
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Path,
[parameter(Mandatory = $true)]
[System.String]
$BucketName,
[parameter(Mandatory = $true)]
[System.String]
$Key,
[parameter(Mandatory = $false)]
[System.String]
$ProfileName,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
$exists = Test-Path $Path
if($Ensure = "Present")
{
$localhash = Get-FileHash $Path -Algorithm MD5
$transformedHash = """$($localhash.Hash)"""
$metaData = { Etag = ""}
if($ProfileName){
$metaData = Get-S3Object -ProfileName $ProfileName -Key $Key -BucketName $BucketName
}
else{
Get-S3Object -Key $Key -BucketName $BucketName
}
return $transformedHash -eq $metaData.ETag
}
else{
# Ensure file is absent
return !$exists
}
}
Export-ModuleMember -Function *-TargetResource
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$moduleName = ($MyInvocation.MyCommand.Name -replace '^(.+?)\..*', '$1')
$sut = "..\DSCResources\$moduleName\$moduleName"
Import-Module "$here\$sut"
# Unit tests for IJYI_cAWSCredentials.schema.mof
Describe "S3 Bucket Test-TargetResource" {
$hash = Get-FileHash $MyInvocation.MyCommand.Name -Algorithm MD5
Context "File is Present"{
Mock Test-Path { return true }
Mock Get-S3Object { return { ETag: $hash } }
Context "when Ensure Present"{
It "Test returns Present" {
(Get-TargetResource -ProfileName $ProfileName).Ensure | Should Be "Absent"
}
It "Test returns True" {
Test-TargetResource -ProfileName $ProfileName -Ensure Absent | Should Be $true
}
}
It "Test returns False" {
Test-TargetResource -ProfileName $ProfileName -Ensure Present | Should Be $false
}
It "Set Stores credentials" {
($ProfileName -in (Get-AWSCredentials -ListProfiles)) | Should Be $false
Set-TargetResource -AccessKey $AccessKey -SecretKey $SecretKey -ProfileName $ProfileName -Ensure Present
($ProfileName -in (Get-AWSCredentials -ListProfiles)) | Should Be $true
}
AfterAll{
Write-Verbose "Cleaning up Profile"
Clear-AWSCredentials -ProfileName $ProfileName
}
}
}
Describe "S3 Bucket Get-Target Resource"{
BeforeAll{
Clear-AWSCredentials -ProfileName $ProfileName
Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs $ProfileName
}
It "Get returns Present" {
(Get-TargetResource -ProfileName $ProfileName).Ensure | Should Be "Present"
}
Context "when Ensure Present"{
It "Test returns True, if ensure Present" {
Test-TargetResource -ProfileName $ProfileName -Ensure Present | Should Be $true
}
}
Context "when Ensure Absent"{
It "Test returns False, if ensure Absent" {
Test-TargetResource -ProfileName $ProfileName -Ensure Absent | Should Be $false
}
It "Set Clears credentials, if ensure Absent " {
($ProfileName -in (Get-AWSCredentials -ListProfiles)) | Should Be $true
Set-TargetResource -AccessKey $AccessKey -SecretKey $SecretKey -ProfileName $ProfileName -Ensure Absent
($ProfileName -in (Get-AWSCredentials -ListProfiles)) | Should Be $false
}
AfterAll{
# Reset Credentials
Clear-AWSCredentials -ProfileName $ProfileName
Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs $ProfileName
}
}
AfterAll{
Clear-AWSCredentials -ProfileName $ProfileName
}
}
Describe "S3 Bucket Test-TargetResource" {
Mock Test-Path { return true }
Mock Get-S3Object { return { ETag: $hash } }
Context "when Ensure Present"{
It "Test returns Absent" {
(Get-TargetResource -ProfileName $ProfileName).Ensure | Should Be "Absent"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment