Add basic workings to function. Return an error if box file is empty

pull/8018/head
DanHam 7 years ago
parent 6b5cf6dcb2
commit 35d326de39
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E

@ -5,8 +5,13 @@
package vagrantcloud
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
@ -232,5 +237,34 @@ func providerFromVagrantBox(boxfile string) (providerName string, err error) {
}
defer f.Close()
// Vagrant boxes are gzipped tar archives
ar, err := gzip.NewReader(f)
if err != nil {
return "", fmt.Errorf("Error unzipping box archive: %s", err)
}
tr := tar.NewReader(ar)
// 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 {
break
}
if err != nil {
return "", fmt.Errorf("%s", err)
}
if hdr.Name == "metadata.json" {
contents, err := ioutil.ReadAll(tr)
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)
break
}
}
return "", nil
}

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@ -199,6 +200,7 @@ func TestProviderFromBuilderName(t *testing.T) {
}
func TestProviderFromVagrantBox_missing_box(t *testing.T) {
// Bad: Box does not exist
boxfile := "i_dont_exist.box"
_, err := providerFromVagrantBox(boxfile)
if err == nil {
@ -206,3 +208,18 @@ func TestProviderFromVagrantBox_missing_box(t *testing.T) {
}
t.Logf("%s", err)
}
func TestProviderFromVagrantBox_empty_box(t *testing.T) {
// Bad: Empty box file
boxfile, err := ioutil.TempFile(os.TempDir(), "test*.box")
if err != nil {
t.Fatalf("Error creating test box file: %s", err)
}
defer os.Remove(boxfile.Name())
_, err = providerFromVagrantBox(boxfile.Name())
if err == nil {
t.Fatal("Should have error as box file is empty")
}
t.Logf("%s", err)
}

Loading…
Cancel
Save