Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Functions/Get-MyAWSRegion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"
#requires -Version 3 -Modules AWSPowerShell

$currentDirectory = Split-Path -Parent $PSCommandPath
$sourceFile = (Split-Path -Leaf $PSCommandPath).Replace(".Tests.", ".")
. "$currentDirectory\$sourceFile"

Describe "Get-MyAWSRegion" {
Mock Invoke-RestMethod { return 'regionA' }

$result = Get-MyAWSRegion
It "An internal error should throw an exception" {

Mock -CommandName Invoke-RestMethod -MockWith {throw}

{ Get-MyAWSRegion } | Should throw
}

It "With mock checking for null" {

Mock -CommandName Invoke-RestMethod -MockWith {$null}

{Get-MyAWSRegion} | Should throw
}

It "With mock checking return value" {

$mockObject = New-Object PSCustomObject
$mockObject | Add-Member -MemberType NoteProperty �Name "region" �Value "regionA"
$actualOutput = "regionA"

Mock -CommandName Invoke-RestMethod -MockWith {$mockObject}

It "returns region" {
$result | Should be 'region'
Get-MyAWSRegion | Should Be $actualOutput
}
}
23 changes: 17 additions & 6 deletions Functions/Get-MyAWSRegion.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
<#
.Synopsis
When run on an EC2 instance it will return the region that the instance is in
.Synopsis
When run on an EC2 instance it will return the region that the instance is in
.OUTPUTS
Region name in string
.EXAMPLE
Get-MyAWSRegion
#>

function Get-MyAWSRegion {
$availability_zone = Invoke-RestMethod 'http://169.254.169.254/latest/meta-data/placement/availability-zone'
return $availability_zone.Substring(0, $availability_zone.Length - 1)
function Get-MyAWSRegion
{
[OutputType([String])]

$metadata = Invoke-RestMethod 'http://169.254.169.254/latest/dynamic/instance-identity/document'
if (!$metadata)
{
throw "Metadata information could not be retrieved"
}
return $metadata.region
}