Should return an error if the box is a plain gzip file

This commit is contained in:
DanHam 2019-08-21 09:44:13 +01:00
parent 35d326de39
commit 9c6b355088
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
2 changed files with 34 additions and 3 deletions

View File

@ -252,7 +252,7 @@ func providerFromVagrantBox(boxfile string) (providerName string, err error) {
break
}
if err != nil {
return "", fmt.Errorf("%s", err)
return "", fmt.Errorf("Error reading header info from box tar archive: %s", err)
}
if hdr.Name == "metadata.json" {

View File

@ -2,6 +2,7 @@ package vagrantcloud
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io/ioutil"
@ -211,9 +212,9 @@ func TestProviderFromVagrantBox_missing_box(t *testing.T) {
func TestProviderFromVagrantBox_empty_box(t *testing.T) {
// Bad: Empty box file
boxfile, err := ioutil.TempFile(os.TempDir(), "test*.box")
boxfile, err := newBoxFile()
if err != nil {
t.Fatalf("Error creating test box file: %s", err)
t.Fatalf("%s", err)
}
defer os.Remove(boxfile.Name())
@ -223,3 +224,33 @@ func TestProviderFromVagrantBox_empty_box(t *testing.T) {
}
t.Logf("%s", err)
}
func TestProviderFromVagrantBox_gzip_only_box(t *testing.T) {
boxfile, err := newBoxFile()
if err != nil {
t.Fatalf("%s", err)
}
defer os.Remove(boxfile.Name())
// Bad: Box is just a plain gzip file
aw := gzip.NewWriter(boxfile)
_, err = aw.Write([]byte("foo content"))
if err != nil {
t.Fatal("Error zipping test box file")
}
aw.Close() // Flush the gzipped contents to file
_, err = providerFromVagrantBox(boxfile.Name())
if err == nil {
t.Fatalf("Should have error as box file is a plain gzip file: %s", err)
}
t.Logf("%s", err)
}
func newBoxFile() (boxfile *os.File, err error) {
boxfile, err = ioutil.TempFile(os.TempDir(), "test*.box")
if err != nil {
return boxfile, fmt.Errorf("Error creating test box file: %s", err)
}
return boxfile, nil
}