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