From a3b49cdc08a763eb95b0ab5c1f8090db7d7b446f Mon Sep 17 00:00:00 2001 From: Paul Meyer Date: Wed, 25 Mar 2020 18:12:34 +0000 Subject: [PATCH] Stub metadata service for builder tests --- builder/azure/chroot/builder_test.go | 20 +++++++++++--------- builder/azure/chroot/metadatastub_test.go | 18 ++++++++++++++++++ builder/azure/common/client/metadata.go | 13 ++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 builder/azure/chroot/metadatastub_test.go diff --git a/builder/azure/chroot/builder_test.go b/builder/azure/chroot/builder_test.go index f84dd9ccc..71cde7ed3 100644 --- a/builder/azure/chroot/builder_test.go +++ b/builder/azure/chroot/builder_test.go @@ -53,18 +53,20 @@ func TestBuilder_Prepare(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - b := &Builder{} + withMetadataStub(func() { + b := &Builder{} - _, _, err := b.Prepare(tt.config) + _, _, err := b.Prepare(tt.config) - if (err != nil) != tt.wantErr { - t.Errorf("Builder.Prepare() error = %v, wantErr %v", err, tt.wantErr) - return - } + if (err != nil) != tt.wantErr { + t.Errorf("Builder.Prepare() error = %v, wantErr %v", err, tt.wantErr) + return + } - if tt.validate != nil { - tt.validate(b.config) - } + if tt.validate != nil { + tt.validate(b.config) + } + }) }) } } diff --git a/builder/azure/chroot/metadatastub_test.go b/builder/azure/chroot/metadatastub_test.go new file mode 100644 index 000000000..575fcdafc --- /dev/null +++ b/builder/azure/chroot/metadatastub_test.go @@ -0,0 +1,18 @@ +package chroot + +import "github.com/hashicorp/packer/builder/azure/common/client" + +func withMetadataStub(f func()) { + mdc := client.DefaultMetadataClient + defer func() { client.DefaultMetadataClient = mdc }() + client.DefaultMetadataClient = client.MetadataClientStub{ + ComputeInfo: client.ComputeInfo{ + SubscriptionID: "testSubscriptionID", + ResourceGroupName: "testResourceGroup", + Name: "testVM", + Location: "testLocation", + }, + } + + f() +} diff --git a/builder/azure/common/client/metadata.go b/builder/azure/common/client/metadata.go index 99f6535f1..da8bbef45 100644 --- a/builder/azure/common/client/metadata.go +++ b/builder/azure/common/client/metadata.go @@ -12,11 +12,22 @@ import ( // DefaultMetadataClient is the default instance metadata client for Azure. Replace this variable for testing purposes only var DefaultMetadataClient = NewMetadataClient() -// MetadataClient holds methods that Packer uses to get information about the current VM +// MetadataClientAPI holds methods that Packer uses to get information about the current VM type MetadataClientAPI interface { GetComputeInfo() (*ComputeInfo, error) } +// MetadataClientStub is an easy way to put a test hook in DefaultMetadataClient +type MetadataClientStub struct { + ComputeInfo +} + +//GetComputeInfo implements MetadataClientAPI +func (s MetadataClientStub) GetComputeInfo() (*ComputeInfo, error) { + return &s.ComputeInfo, nil +} + +// ComputeInfo defines the Azure VM metadata that is used in Packer type ComputeInfo struct { Name string ResourceGroupName string