github.com/mitchellh/packer@v1.3.2/builder/azure/arm/config_retriever_test.go (about)

     1  package arm
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/Azure/go-autorest/autorest/azure"
     8  )
     9  
    10  func TestConfigRetrieverFillsTenantIDWhenEmpty(t *testing.T) {
    11  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
    12  	if expected := ""; c.TenantID != expected {
    13  		t.Errorf("Expected TenantID to be %q but got %q", expected, c.TenantID)
    14  	}
    15  
    16  	sut := newTestConfigRetriever()
    17  	retrievedTid := "my-tenant-id"
    18  	sut.findTenantID = func(azure.Environment, string) (string, error) { return retrievedTid, nil }
    19  	if err := sut.FillParameters(c); err != nil {
    20  		t.Errorf("Unexpected error when calling sut.FillParameters: %v", err)
    21  	}
    22  
    23  	if expected := retrievedTid; c.TenantID != expected {
    24  		t.Errorf("Expected TenantID to be %q but got %q", expected, c.TenantID)
    25  	}
    26  }
    27  
    28  func TestConfigRetrieverLeavesTenantIDWhenNotEmpty(t *testing.T) {
    29  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
    30  	userSpecifiedTid := "not-empty"
    31  	c.TenantID = userSpecifiedTid
    32  
    33  	sut := newTestConfigRetriever()
    34  	sut.findTenantID = nil // assert that this not even called
    35  	if err := sut.FillParameters(c); err != nil {
    36  		t.Errorf("Unexpected error when calling sut.FillParameters: %v", err)
    37  	}
    38  
    39  	if expected := userSpecifiedTid; c.TenantID != expected {
    40  		t.Errorf("Expected TenantID to be %q but got %q", expected, c.TenantID)
    41  	}
    42  }
    43  
    44  func TestConfigRetrieverReturnsErrorWhenTenantIDEmptyAndRetrievalFails(t *testing.T) {
    45  	c, _, _ := newConfig(getArmBuilderConfiguration(), getPackerConfiguration())
    46  	if expected := ""; c.TenantID != expected {
    47  		t.Errorf("Expected TenantID to be %q but got %q", expected, c.TenantID)
    48  	}
    49  
    50  	sut := newTestConfigRetriever()
    51  	errorString := "sorry, I failed"
    52  	sut.findTenantID = func(azure.Environment, string) (string, error) { return "", errors.New(errorString) }
    53  	if err := sut.FillParameters(c); err != nil && err.Error() != errorString {
    54  		t.Errorf("Unexpected error when calling sut.FillParameters: %v", err)
    55  	}
    56  }
    57  
    58  func newTestConfigRetriever() configRetriever {
    59  	return configRetriever{
    60  		findTenantID: func(azure.Environment, string) (string, error) { return "findTenantID is mocked", nil },
    61  	}
    62  }