github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_get_certificate_test.go (about) 1 package arm 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/packer/builder/azure/common/constants" 8 "github.com/mitchellh/multistep" 9 ) 10 11 func TestStepGetCertificateShouldFailIfGetFails(t *testing.T) { 12 var testSubject = &StepGetCertificate{ 13 get: func(string, string) (string, error) { return "", fmt.Errorf("!! Unit Test FAIL !!") }, 14 say: func(message string) {}, 15 error: func(e error) {}, 16 pause: func() {}, 17 } 18 19 stateBag := createTestStateBagStepGetCertificate() 20 21 var result = testSubject.Run(stateBag) 22 if result != multistep.ActionHalt { 23 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 24 } 25 26 if _, ok := stateBag.GetOk(constants.Error); ok == false { 27 t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) 28 } 29 } 30 31 func TestStepGetCertificateShouldPassIfGetPasses(t *testing.T) { 32 var testSubject = &StepGetCertificate{ 33 get: func(string, string) (string, error) { return "", nil }, 34 say: func(message string) {}, 35 error: func(e error) {}, 36 pause: func() {}, 37 } 38 39 stateBag := createTestStateBagStepGetCertificate() 40 41 var result = testSubject.Run(stateBag) 42 if result != multistep.ActionContinue { 43 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 44 } 45 46 if _, ok := stateBag.GetOk(constants.Error); ok == true { 47 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 48 } 49 } 50 51 func TestStepGetCertificateShouldTakeStepArgumentsFromStateBag(t *testing.T) { 52 var actualKeyVaultName string 53 var actualSecretName string 54 55 var testSubject = &StepGetCertificate{ 56 get: func(keyVaultName string, secretName string) (string, error) { 57 actualKeyVaultName = keyVaultName 58 actualSecretName = secretName 59 60 return "http://key.vault/1", nil 61 }, 62 say: func(message string) {}, 63 error: func(e error) {}, 64 pause: func() {}, 65 } 66 67 stateBag := createTestStateBagStepGetCertificate() 68 var result = testSubject.Run(stateBag) 69 70 if result != multistep.ActionContinue { 71 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 72 } 73 74 var expectedKeyVaultName = stateBag.Get(constants.ArmKeyVaultName).(string) 75 76 if actualKeyVaultName != expectedKeyVaultName { 77 t.Fatal("Expected StepGetCertificate to source 'constants.ArmKeyVaultName' from the state bag, but it did not.") 78 } 79 if actualSecretName != DefaultSecretName { 80 t.Fatal("Expected StepGetCertificate to use default value for secret, but it did not.") 81 } 82 83 expectedCertificateUrl, ok := stateBag.GetOk(constants.ArmCertificateUrl) 84 if !ok { 85 t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.ArmCertificateUrl) 86 } 87 88 if expectedCertificateUrl != "http://key.vault/1" { 89 t.Fatalf("Expected the value of stateBag[%s] to be 'http://key.vault/1', but got '%s'.", constants.ArmCertificateUrl, expectedCertificateUrl) 90 } 91 } 92 93 func createTestStateBagStepGetCertificate() multistep.StateBag { 94 stateBag := new(multistep.BasicStateBag) 95 stateBag.Put(constants.ArmKeyVaultName, "Unit Test: KeyVaultName") 96 97 return stateBag 98 }