github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_get_certificate_test.go (about)

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