@ -1,6 +1,7 @@
package arm
import (
"errors"
"fmt"
"testing"
@ -8,6 +9,33 @@ import (
"github.com/mitchellh/multistep"
)
func TestStepCreateResourceGroupShouldFailIfBothGroupNames ( t * testing . T ) {
stateBag := new ( multistep . BasicStateBag )
stateBag . Put ( constants . ArmDoubleResourceGroupNameSet , true )
value := "Unit Test: Tags"
tags := map [ string ] * string {
"tag01" : & value ,
}
stateBag . Put ( constants . ArmTags , & tags )
var testSubject = & StepCreateResourceGroup {
create : func ( string , string , * map [ string ] * string ) error { return nil } ,
say : func ( message string ) { } ,
error : func ( e error ) { } ,
exists : func ( string ) ( bool , error ) { return false , nil } ,
}
var result = testSubject . Run ( stateBag )
if result != multistep . ActionHalt {
t . Fatalf ( "Expected the step to return 'ActionHalt', but got '%d'." , result )
}
if _ , ok := stateBag . GetOk ( constants . Error ) ; ok == false {
t . Fatalf ( "Expected the step to set stateBag['%s'], but it was not." , constants . Error )
}
}
func TestStepCreateResourceGroupShouldFailIfCreateFails ( t * testing . T ) {
var testSubject = & StepCreateResourceGroup {
create : func ( string , string , * map [ string ] * string ) error { return fmt . Errorf ( "!! Unit Test FAIL !!" ) } ,
@ -28,6 +56,26 @@ func TestStepCreateResourceGroupShouldFailIfCreateFails(t *testing.T) {
}
}
func TestStepCreateResourceGroupShouldFailIfExistsFails ( t * testing . T ) {
var testSubject = & StepCreateResourceGroup {
create : func ( string , string , * map [ string ] * string ) error { return nil } ,
say : func ( message string ) { } ,
error : func ( e error ) { } ,
exists : func ( string ) ( bool , error ) { return false , errors . New ( "FAIL" ) } ,
}
stateBag := createTestStateBagStepCreateResourceGroup ( )
var result = testSubject . Run ( stateBag )
if result != multistep . ActionHalt {
t . Fatalf ( "Expected the step to return 'ActionHalt', but got '%d'." , result )
}
if _ , ok := stateBag . GetOk ( constants . Error ) ; ok == false {
t . Fatalf ( "Expected the step to set stateBag['%s'], but it was not." , constants . Error )
}
}
func TestStepCreateResourceGroupShouldPassIfCreatePasses ( t * testing . T ) {
var testSubject = & StepCreateResourceGroup {
create : func ( string , string , * map [ string ] * string ) error { return nil } ,
@ -94,7 +142,7 @@ func TestStepCreateResourceGroupShouldTakeStepArgumentsFromStateBag(t *testing.T
}
}
func TestStepCreateResourceGroupMark AsNonExistingIf DoesntExist( t * testing . T ) {
func TestStepCreateResourceGroupMark ShouldFailIfTryingExistingBut DoesntExist( t * testing . T ) {
var testSubject = & StepCreateResourceGroup {
create : func ( string , string , * map [ string ] * string ) error { return fmt . Errorf ( "!! Unit Test FAIL !!" ) } ,
say : func ( message string ) { } ,
@ -102,18 +150,19 @@ func TestStepCreateResourceGroupMarkAsNonExistingIfDoesntExist(t *testing.T) {
exists : func ( string ) ( bool , error ) { return false , nil } ,
}
stateBag := createTest StateBagStepCreateResourceGroup( )
stateBag := createTest Existing StateBagStepCreateResourceGroup( )
if _ , ok := stateBag . GetOk ( constants . ArmIsExistingResourceGroup ) ; ok == true {
t . Fatalf ( "Expected 'constants.ArmIsExistingResourceGroup' not to be set, but it was" )
var result = testSubject . Run ( stateBag )
if result != multistep . ActionHalt {
t . Fatalf ( "Expected the step to return 'ActionHalt', but got '%d'." , result )
}
testSubject . Run ( stateBag )
if stateBag. Get ( constants . ArmIsExistingResourceGroup ) . ( bool ) == tru e {
t . Fatalf ( "Expected 'constants.ArmIsExistingResourceGroup' to be set to false, but it wasn't." )
if _, ok := stateBag . GetOk ( constants . Error ) ; ok == fals e {
t . Fatalf ( "Expected the step to set stateBag['%s'], but it was not.", constants . Error )
}
}
func TestStepCreateResourceGroupMark AsExistingIfExists ( t * testing . T ) {
func TestStepCreateResourceGroupMark ShouldFailIfTryingTempButExist ( t * testing . T ) {
var testSubject = & StepCreateResourceGroup {
create : func ( string , string , * map [ string ] * string ) error { return fmt . Errorf ( "!! Unit Test FAIL !!" ) } ,
say : func ( message string ) { } ,
@ -123,12 +172,13 @@ func TestStepCreateResourceGroupMarkAsExistingIfExists(t *testing.T) {
stateBag := createTestStateBagStepCreateResourceGroup ( )
if _ , ok := stateBag . GetOk ( constants . ArmIsExistingResourceGroup ) ; ok == true {
t . Fatalf ( "Expected 'constants.ArmIsExistingResourceGroup' not to be set, but it was" )
var result = testSubject . Run ( stateBag )
if result != multistep . ActionHalt {
t . Fatalf ( "Expected the step to return 'ActionHalt', but got '%d'." , result )
}
testSubject . Run ( stateBag )
if stateBag. Get ( constants . ArmIsExistingResourceGroup ) . ( bool ) == false {
t . Fatalf ( "Expected 'constants.ArmIsExistingResourceGroup' to be set to true, but it wasn't." )
if _, ok := stateBag . GetOk ( constants . Error ) ; ok == false {
t . Fatalf ( "Expected the step to set stateBag['%s'], but it was not.", constants . Error )
}
}
@ -137,6 +187,23 @@ func createTestStateBagStepCreateResourceGroup() multistep.StateBag {
stateBag . Put ( constants . ArmLocation , "Unit Test: Location" )
stateBag . Put ( constants . ArmResourceGroupName , "Unit Test: ResourceGroupName" )
stateBag . Put ( constants . ArmIsExistingResourceGroup , false )
value := "Unit Test: Tags"
tags := map [ string ] * string {
"tag01" : & value ,
}
stateBag . Put ( constants . ArmTags , & tags )
return stateBag
}
func createTestExistingStateBagStepCreateResourceGroup ( ) multistep . StateBag {
stateBag := new ( multistep . BasicStateBag )
stateBag . Put ( constants . ArmLocation , "Unit Test: Location" )
stateBag . Put ( constants . ArmResourceGroupName , "Unit Test: ResourceGroupName" )
stateBag . Put ( constants . ArmIsExistingResourceGroup , true )
value := "Unit Test: Tags"
tags := map [ string ] * string {