From a9e22a6bb28529334c5e58f3a78d87f194ef02ea Mon Sep 17 00:00:00 2001 From: DanHam Date: Wed, 21 Aug 2019 12:32:42 +0100 Subject: [PATCH] Should return the provider by parsing the json in the box metadata file --- .../vagrant-cloud/post-processor.go | 19 +++++++++++------ .../vagrant-cloud/post-processor_test.go | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/post-processor/vagrant-cloud/post-processor.go b/post-processor/vagrant-cloud/post-processor.go index bc04373f6..90e885ffc 100644 --- a/post-processor/vagrant-cloud/post-processor.go +++ b/post-processor/vagrant-cloud/post-processor.go @@ -8,10 +8,10 @@ import ( "archive/tar" "compress/gzip" "context" + "encoding/json" "fmt" "io" "io/ioutil" - "log" "os" "strings" @@ -244,12 +244,18 @@ func providerFromVagrantBox(boxfile string) (providerName string, err error) { } tr := tar.NewReader(ar) + // The metadata.json file in the tar archive contains a 'provider' key + type metadata struct { + ProviderName string `json:"provider"` + } + md := metadata{} + // Loop through the files in the archive and read the provider // information from the boxes metadata.json file for { hdr, err := tr.Next() if err == io.EOF { - if providerName == "" { + if md.ProviderName == "" { return "", fmt.Errorf("Error: Provider info was not found in box: %s", boxfile) } break @@ -263,11 +269,12 @@ func providerFromVagrantBox(boxfile string) (providerName string, err error) { if err != nil { return "", fmt.Errorf("Error reading contents of metadata.json file from box file: %s", err) } - // TODO: Parse the json for the provider - log.Printf("Contents: %s", contents) + err = json.Unmarshal(contents, &md) + if err != nil { + return "", fmt.Errorf("Error parsing metadata.json file: %s", err) + } break } } - - return "", nil + return md.ProviderName, nil } diff --git a/post-processor/vagrant-cloud/post-processor_test.go b/post-processor/vagrant-cloud/post-processor_test.go index 837b07de6..73a8a47e7 100644 --- a/post-processor/vagrant-cloud/post-processor_test.go +++ b/post-processor/vagrant-cloud/post-processor_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "github.com/stretchr/testify/assert" ) type tarFiles []struct { @@ -286,6 +287,26 @@ func TestProviderFromVagrantBox_no_metadata(t *testing.T) { t.Logf("%s", err) } +func TestProviderFromVagrantBox_metadata_ok(t *testing.T) { + // Good: The box contains the metadata.json file with the required + // 'provider' key/value + expectedProvider := "virtualbox" + files := tarFiles{ + {"foo.txt", "This is a foo file"}, + {"bar.txt", "This is a bar file"}, + {"metadata.json", `{"provider":"` + expectedProvider + `"}`}, + } + boxfile, err := createBox(files) + if err != nil { + t.Fatalf("Error creating test box: %s", err) + } + defer os.Remove(boxfile.Name()) + + provider, err := providerFromVagrantBox(boxfile.Name()) + assert.Equal(t, expectedProvider, provider, "Error: Expected provider: '%s'. Got '%s'", expectedProvider, provider) + t.Logf("Expected provider '%s'. Got provider '%s'", expectedProvider, provider) +} + func newBoxFile() (boxfile *os.File, err error) { boxfile, err = ioutil.TempFile(os.TempDir(), "test*.box") if err != nil {