2014-06-16 15:53:37 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2019-08-21 05:04:32 -04:00
|
|
|
"archive/tar"
|
2014-06-16 15:53:37 -04:00
|
|
|
"bytes"
|
2019-08-21 04:44:13 -04:00
|
|
|
"compress/gzip"
|
2019-08-16 10:42:56 -04:00
|
|
|
"context"
|
2018-10-24 09:39:09 -04:00
|
|
|
"fmt"
|
2019-08-20 12:56:24 -04:00
|
|
|
"io/ioutil"
|
2018-10-24 09:39:09 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-06-07 13:40:31 -04:00
|
|
|
"os"
|
2019-08-16 10:42:56 -04:00
|
|
|
"strings"
|
2014-06-16 15:53:37 -04:00
|
|
|
"testing"
|
2016-07-30 20:10:16 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2019-08-21 07:32:42 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-06-16 15:53:37 -04:00
|
|
|
)
|
|
|
|
|
2019-08-21 05:47:08 -04:00
|
|
|
type tarFiles []struct {
|
|
|
|
Name, Body string
|
|
|
|
}
|
|
|
|
|
2014-06-25 10:56:09 -04:00
|
|
|
func testGoodConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"access_token": "foo",
|
|
|
|
"version_description": "bar",
|
|
|
|
"box_tag": "hashicorp/precise64",
|
|
|
|
"version": "0.5",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBadConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"access_token": "foo",
|
|
|
|
"box_tag": "baz",
|
|
|
|
"version_description": "bar",
|
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2019-09-12 06:37:46 -04:00
|
|
|
func testNoAccessTokenProvidedConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"box_tag": "baz",
|
|
|
|
"version_description": "bar",
|
|
|
|
"version": "0.5",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:39:09 -04:00
|
|
|
func newSecureServer(token string, handler http.HandlerFunc) *httptest.Server {
|
|
|
|
token = fmt.Sprintf("Bearer %s", token)
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get("authorization") != token {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if handler != nil {
|
|
|
|
handler(rw, req)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:37:37 -05:00
|
|
|
func newSelfSignedSslServer(token string, handler http.HandlerFunc) *httptest.Server {
|
|
|
|
token = fmt.Sprintf("Bearer %s", token)
|
|
|
|
return httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get("authorization") != token {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if handler != nil {
|
|
|
|
handler(rw, req)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-09-12 06:37:46 -04:00
|
|
|
func newNoAuthServer(handler http.HandlerFunc) *httptest.Server {
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Header.Get("authorization") != "" {
|
|
|
|
http.Error(rw, "Authorization header was provider", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if handler != nil {
|
|
|
|
handler(rw, req)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:37:37 -05:00
|
|
|
func TestPostProcessor_Insecure_Ssl(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
server := newSelfSignedSslServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
config := testGoodConfig()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
|
|
|
config["insecure_skip_tls_verify"] = true
|
|
|
|
if err := p.Configure(config); err != nil {
|
2019-03-05 08:39:50 -05:00
|
|
|
t.Fatalf("Expected TLS to skip certificate validation: %s", err)
|
2019-03-05 08:37:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:40:31 -04:00
|
|
|
func TestPostProcessor_Configure_fromVagrantEnv(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
config := testGoodConfig()
|
2018-10-24 09:39:09 -04:00
|
|
|
server := newSecureServer("bar", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2017-06-07 13:40:31 -04:00
|
|
|
config["access_token"] = ""
|
|
|
|
os.Setenv("VAGRANT_CLOUD_TOKEN", "bar")
|
|
|
|
defer func() {
|
|
|
|
os.Setenv("VAGRANT_CLOUD_TOKEN", "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.AccessToken != "bar" {
|
|
|
|
t.Fatalf("Expected to get token from VAGRANT_CLOUD_TOKEN env var. Got '%s' instead",
|
|
|
|
p.config.AccessToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_Configure_fromAtlasEnv(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
config := testGoodConfig()
|
|
|
|
config["access_token"] = ""
|
2018-10-24 09:39:09 -04:00
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2017-06-07 13:40:31 -04:00
|
|
|
os.Setenv("ATLAS_TOKEN", "foo")
|
|
|
|
defer func() {
|
|
|
|
os.Setenv("ATLAS_TOKEN", "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.AccessToken != "foo" {
|
|
|
|
t.Fatalf("Expected to get token from ATLAS_TOKEN env var. Got '%s' instead",
|
|
|
|
p.config.AccessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.warnAtlasToken {
|
|
|
|
t.Fatal("Expected warn flag to be set when getting token from atlas env var.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 10:56:09 -04:00
|
|
|
func TestPostProcessor_Configure_Good(t *testing.T) {
|
2018-10-24 09:39:09 -04:00
|
|
|
config := testGoodConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2014-06-16 15:53:37 -04:00
|
|
|
var p PostProcessor
|
2018-10-24 09:39:09 -04:00
|
|
|
if err := p.Configure(config); err != nil {
|
2014-06-16 15:53:37 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-06-25 10:56:09 -04:00
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
|
2014-06-25 10:56:09 -04:00
|
|
|
func TestPostProcessor_Configure_Bad(t *testing.T) {
|
2018-10-24 09:39:09 -04:00
|
|
|
config := testBadConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
2014-06-25 10:56:09 -04:00
|
|
|
var p PostProcessor
|
2018-10-24 09:39:09 -04:00
|
|
|
if err := p.Configure(config); err == nil {
|
2014-06-25 10:56:09 -04:00
|
|
|
t.Fatalf("should have err")
|
|
|
|
}
|
2014-06-16 15:53:37 -04:00
|
|
|
}
|
|
|
|
|
2019-09-12 06:37:46 -04:00
|
|
|
func TestPostProcessor_Configure_checkAccessTokenIsRequiredByDefault(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
config := testNoAccessTokenProvidedConfig()
|
|
|
|
if err := p.Configure(config); err == nil {
|
|
|
|
t.Fatalf("Expected access token to be required.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_Configure_checkAccessTokenIsNotRequiredForOverridenVagrantCloud(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
server := newNoAuthServer(nil)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
config := testNoAccessTokenProvidedConfig()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
|
|
|
if err := p.Configure(config); err != nil {
|
|
|
|
t.Fatalf("Expected blank access token to be allowed and authenticate to pass: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 10:42:56 -04:00
|
|
|
func TestPostProcessor_PostProcess_checkArtifactType(t *testing.T) {
|
|
|
|
artifact := &packer.MockArtifact{
|
|
|
|
BuilderIdValue: "invalid.builder",
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testGoodConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
p.Configure(config)
|
|
|
|
_, _, _, err := p.PostProcess(context.Background(), testUi(), artifact)
|
|
|
|
if !strings.Contains(err.Error(), "Unknown artifact type") {
|
|
|
|
t.Fatalf("Should error with message 'Unknown artifact type...' with BuilderId: %s", artifact.BuilderIdValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 10:48:28 -04:00
|
|
|
func TestPostProcessor_PostProcess_checkArtifactFileIsBox(t *testing.T) {
|
|
|
|
artifact := &packer.MockArtifact{
|
|
|
|
BuilderIdValue: "mitchellh.post-processor.vagrant", // good
|
|
|
|
FilesValue: []string{"invalid.boxfile"}, // should have .box extension
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testGoodConfig()
|
|
|
|
server := newSecureServer("foo", nil)
|
|
|
|
defer server.Close()
|
|
|
|
config["vagrant_cloud_url"] = server.URL
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
p.Configure(config)
|
|
|
|
_, _, _, err := p.PostProcess(context.Background(), testUi(), artifact)
|
|
|
|
if !strings.Contains(err.Error(), "Unknown files in artifact") {
|
|
|
|
t.Fatalf("Should error with message 'Unknown files in artifact...' with artifact file: %s",
|
|
|
|
artifact.FilesValue[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 15:53:37 -04:00
|
|
|
func testUi() *packer.BasicUi {
|
|
|
|
return &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
|
|
|
|
var _ packer.PostProcessor = new(PostProcessor)
|
|
|
|
}
|
|
|
|
|
2016-07-30 20:10:16 -04:00
|
|
|
func TestProviderFromBuilderName(t *testing.T) {
|
2014-06-16 15:53:37 -04:00
|
|
|
if providerFromBuilderName("foobar") != "foobar" {
|
|
|
|
t.Fatal("should copy unknown provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
if providerFromBuilderName("vmware") != "vmware_desktop" {
|
|
|
|
t.Fatal("should convert provider")
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 11:36:01 -04:00
|
|
|
|
|
|
|
func TestProviderFromVagrantBox_missing_box(t *testing.T) {
|
2019-08-20 12:56:24 -04:00
|
|
|
// Bad: Box does not exist
|
2019-08-20 11:36:01 -04:00
|
|
|
boxfile := "i_dont_exist.box"
|
|
|
|
_, err := providerFromVagrantBox(boxfile)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should have error as box file does not exist")
|
|
|
|
}
|
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
2019-08-20 12:56:24 -04:00
|
|
|
|
|
|
|
func TestProviderFromVagrantBox_empty_box(t *testing.T) {
|
|
|
|
// Bad: Empty box file
|
2019-08-21 04:44:13 -04:00
|
|
|
boxfile, err := newBoxFile()
|
2019-08-20 12:56:24 -04:00
|
|
|
if err != nil {
|
2019-08-21 04:44:13 -04:00
|
|
|
t.Fatalf("%s", err)
|
2019-08-20 12:56:24 -04:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2019-08-21 04:44:13 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-21 05:04:32 -04:00
|
|
|
func TestProviderFromVagrantBox_no_files_in_archive(t *testing.T) {
|
2019-08-21 05:47:08 -04:00
|
|
|
// Bad: Box contains no files
|
|
|
|
boxfile, err := createBox(tarFiles{})
|
2019-08-21 05:04:32 -04:00
|
|
|
if err != nil {
|
2019-08-21 05:47:08 -04:00
|
|
|
t.Fatalf("Error creating test box: %s", err)
|
2019-08-21 05:04:32 -04:00
|
|
|
}
|
|
|
|
defer os.Remove(boxfile.Name())
|
|
|
|
|
2019-08-21 05:47:08 -04:00
|
|
|
_, err = providerFromVagrantBox(boxfile.Name())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Should have error as box file has no contents")
|
2019-08-21 05:04:32 -04:00
|
|
|
}
|
2019-08-21 05:47:08 -04:00
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProviderFromVagrantBox_no_metadata(t *testing.T) {
|
|
|
|
// Bad: Box contains no metadata/metadata.json file
|
|
|
|
files := tarFiles{
|
|
|
|
{"foo.txt", "This is a foo file"},
|
|
|
|
{"bar.txt", "This is a bar file"},
|
|
|
|
}
|
|
|
|
boxfile, err := createBox(files)
|
2019-08-21 05:04:32 -04:00
|
|
|
if err != nil {
|
2019-08-21 05:47:08 -04:00
|
|
|
t.Fatalf("Error creating test box: %s", err)
|
2019-08-21 05:04:32 -04:00
|
|
|
}
|
2019-08-21 05:47:08 -04:00
|
|
|
defer os.Remove(boxfile.Name())
|
2019-08-21 05:04:32 -04:00
|
|
|
|
|
|
|
_, err = providerFromVagrantBox(boxfile.Name())
|
|
|
|
if err == nil {
|
2019-08-21 05:47:08 -04:00
|
|
|
t.Fatalf("Should have error as box file does not include metadata.json file")
|
2019-08-21 05:04:32 -04:00
|
|
|
}
|
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 07:55:28 -04:00
|
|
|
func TestProviderFromVagrantBox_metadata_empty(t *testing.T) {
|
|
|
|
// Bad: Create a box with an empty metadata.json file
|
|
|
|
files := tarFiles{
|
|
|
|
{"foo.txt", "This is a foo file"},
|
|
|
|
{"bar.txt", "This is a bar file"},
|
|
|
|
{"metadata.json", ""},
|
|
|
|
}
|
|
|
|
boxfile, err := createBox(files)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating test box: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(boxfile.Name())
|
|
|
|
|
|
|
|
_, err = providerFromVagrantBox(boxfile.Name())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Should have error as box files metadata.json file is empty")
|
|
|
|
}
|
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:10:43 -04:00
|
|
|
func TestProviderFromVagrantBox_metadata_bad_json(t *testing.T) {
|
|
|
|
// Bad: Create a box with bad JSON in the metadata.json file
|
|
|
|
files := tarFiles{
|
|
|
|
{"foo.txt", "This is a foo file"},
|
|
|
|
{"bar.txt", "This is a bar file"},
|
|
|
|
{"metadata.json", "{provider: badjson}"},
|
|
|
|
}
|
|
|
|
boxfile, err := createBox(files)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating test box: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(boxfile.Name())
|
|
|
|
|
|
|
|
_, err = providerFromVagrantBox(boxfile.Name())
|
|
|
|
if err == nil {
|
2019-08-22 04:48:32 -04:00
|
|
|
t.Fatalf("Should have error as box files metadata.json file contains badly formatted JSON")
|
2019-08-21 08:10:43 -04:00
|
|
|
}
|
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:18:56 -04:00
|
|
|
func TestProviderFromVagrantBox_metadata_no_provider_key(t *testing.T) {
|
|
|
|
// Bad: Create a box with no 'provider' key in the metadata.json file
|
|
|
|
files := tarFiles{
|
|
|
|
{"foo.txt", "This is a foo file"},
|
|
|
|
{"bar.txt", "This is a bar file"},
|
|
|
|
{"metadata.json", `{"cows":"moo"}`},
|
|
|
|
}
|
|
|
|
boxfile, err := createBox(files)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating test box: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(boxfile.Name())
|
|
|
|
|
|
|
|
_, err = providerFromVagrantBox(boxfile.Name())
|
|
|
|
if err == nil {
|
2019-08-22 04:48:32 -04:00
|
|
|
t.Fatalf("Should have error as provider key/value pair is missing from boxes metadata.json file")
|
2019-08-21 08:18:56 -04:00
|
|
|
}
|
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:02:42 -04:00
|
|
|
func TestProviderFromVagrantBox_metadata_provider_value_empty(t *testing.T) {
|
|
|
|
// Bad: The boxes metadata.json file 'provider' key has an empty value
|
|
|
|
files := tarFiles{
|
|
|
|
{"foo.txt", "This is a foo file"},
|
|
|
|
{"bar.txt", "This is a bar file"},
|
|
|
|
{"metadata.json", `{"provider":""}`},
|
|
|
|
}
|
|
|
|
boxfile, err := createBox(files)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating test box: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(boxfile.Name())
|
|
|
|
|
|
|
|
_, err = providerFromVagrantBox(boxfile.Name())
|
|
|
|
if err == nil {
|
2019-08-22 04:48:32 -04:00
|
|
|
t.Fatalf("Should have error as value associated with 'provider' key in boxes metadata.json file is empty")
|
2019-08-21 08:02:42 -04:00
|
|
|
}
|
|
|
|
t.Logf("%s", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 07:32:42 -04:00
|
|
|
func TestProviderFromVagrantBox_metadata_ok(t *testing.T) {
|
2019-08-21 07:55:28 -04:00
|
|
|
// Good: The boxes metadata.json file has the 'provider' key/value pair
|
2019-08-21 07:32:42 -04:00
|
|
|
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())
|
2019-11-14 21:26:40 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting provider from vagrant box %s:%v", boxfile.Name(), err)
|
|
|
|
}
|
2019-08-21 07:32:42 -04:00
|
|
|
assert.Equal(t, expectedProvider, provider, "Error: Expected provider: '%s'. Got '%s'", expectedProvider, provider)
|
|
|
|
t.Logf("Expected provider '%s'. Got provider '%s'", expectedProvider, provider)
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:31:29 -04:00
|
|
|
func TestGetProvider_artifice(t *testing.T) {
|
|
|
|
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 := getProvider("", boxfile.Name(), "artifice")
|
2019-11-14 21:26:40 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting provider %s:%v", boxfile.Name(), err)
|
|
|
|
}
|
2019-08-21 08:31:29 -04:00
|
|
|
assert.Equal(t, expectedProvider, provider, "Error: Expected provider: '%s'. Got '%s'", expectedProvider, provider)
|
|
|
|
t.Logf("Expected provider '%s'. Got provider '%s'", expectedProvider, provider)
|
|
|
|
}
|
|
|
|
|
2019-08-21 08:36:50 -04:00
|
|
|
func TestGetProvider_other(t *testing.T) {
|
|
|
|
expectedProvider := "virtualbox"
|
|
|
|
|
|
|
|
provider, _ := getProvider(expectedProvider, "foo.box", "other")
|
|
|
|
assert.Equal(t, expectedProvider, provider, "Error: Expected provider: '%s'. Got '%s'", expectedProvider, provider)
|
|
|
|
t.Logf("Expected provider '%s'. Got provider '%s'", expectedProvider, provider)
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:44:13 -04:00
|
|
|
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
|
|
|
|
}
|
2019-08-21 05:47:08 -04:00
|
|
|
|
|
|
|
func createBox(files tarFiles) (boxfile *os.File, err error) {
|
|
|
|
boxfile, err = newBoxFile()
|
|
|
|
if err != nil {
|
|
|
|
return boxfile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Box files are gzipped tar archives
|
|
|
|
aw := gzip.NewWriter(boxfile)
|
|
|
|
tw := tar.NewWriter(aw)
|
|
|
|
|
|
|
|
// Add each file to the box
|
|
|
|
for _, file := range files {
|
|
|
|
// Create and write the tar file header
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: file.Name,
|
|
|
|
Mode: 0644,
|
|
|
|
Size: int64(len(file.Body)),
|
|
|
|
}
|
|
|
|
err = tw.WriteHeader(hdr)
|
|
|
|
if err != nil {
|
|
|
|
return boxfile, fmt.Errorf("Error writing box tar file header: %s", err)
|
|
|
|
}
|
|
|
|
// Write the file contents
|
|
|
|
_, err = tw.Write([]byte(file.Body))
|
|
|
|
if err != nil {
|
|
|
|
return boxfile, fmt.Errorf("Error writing box tar file contents: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Flush and close each writer
|
|
|
|
err = tw.Close()
|
|
|
|
if err != nil {
|
|
|
|
return boxfile, fmt.Errorf("Error flushing tar file contents: %s", err)
|
|
|
|
}
|
|
|
|
err = aw.Close()
|
|
|
|
if err != nil {
|
|
|
|
return boxfile, fmt.Errorf("Error flushing gzip file contents: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return boxfile, nil
|
|
|
|
}
|