2013-06-26 22:09:24 -04:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
2013-12-19 17:04:45 -05:00
|
|
|
"bytes"
|
|
|
|
"compress/flate"
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2014-11-24 17:45:49 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-12-19 17:04:45 -05:00
|
|
|
"strings"
|
2013-06-26 22:09:24 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-26 22:09:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:04:45 -05:00
|
|
|
func testPP(t *testing.T) *PostProcessor {
|
|
|
|
var p PostProcessor
|
|
|
|
if err := p.Configure(testConfig()); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUi() *packer.BasicUi {
|
|
|
|
return &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 22:09:24 -04:00
|
|
|
func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
|
2013-12-19 16:22:46 -05:00
|
|
|
var _ packer.PostProcessor = new(PostProcessor)
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 17:04:45 -05:00
|
|
|
func TestPostProcessorPrepare_compressionLevel(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
// Default
|
|
|
|
c := testConfig()
|
|
|
|
delete(c, "compression_level")
|
|
|
|
if err := p.Configure(c); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
config := p.config
|
2013-12-19 17:44:15 -05:00
|
|
|
if config.CompressionLevel != flate.DefaultCompression {
|
|
|
|
t.Fatalf("bad: %#v", config.CompressionLevel)
|
2013-12-19 17:04:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set
|
|
|
|
c = testConfig()
|
|
|
|
c["compression_level"] = 7
|
|
|
|
if err := p.Configure(c); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
config = p.config
|
2013-12-19 17:44:15 -05:00
|
|
|
if config.CompressionLevel != 7 {
|
|
|
|
t.Fatalf("bad: %#v", config.CompressionLevel)
|
2013-12-19 17:04:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_outputPath(t *testing.T) {
|
2013-06-26 22:09:24 -04:00
|
|
|
var p PostProcessor
|
|
|
|
|
2013-06-27 13:53:43 -04:00
|
|
|
// Default
|
2013-06-26 22:09:24 -04:00
|
|
|
c := testConfig()
|
|
|
|
delete(c, "output")
|
|
|
|
err := p.Configure(c)
|
2013-06-27 13:53:43 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad template
|
|
|
|
c["output"] = "bad {{{{.Template}}}}"
|
|
|
|
err = p.Configure(c)
|
2013-06-26 22:09:24 -04:00
|
|
|
if err == nil {
|
2013-06-27 13:53:43 -04:00
|
|
|
t.Fatal("should have error")
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 10:16:38 -04:00
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
func TestSpecificConfig(t *testing.T) {
|
2013-12-19 17:44:15 -05:00
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
// Default
|
|
|
|
c := testConfig()
|
2020-01-14 05:13:16 -05:00
|
|
|
c["compression_level"] = 1
|
|
|
|
c["output"] = "folder"
|
2013-12-19 17:44:15 -05:00
|
|
|
c["override"] = map[string]interface{}{
|
|
|
|
"aws": map[string]interface{}{
|
|
|
|
"compression_level": 7,
|
|
|
|
},
|
|
|
|
}
|
2020-01-14 05:13:16 -05:00
|
|
|
if err := p.Configure(c); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// overrides config
|
|
|
|
config, err := p.specificConfig("aws")
|
2013-12-19 17:44:15 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
if config.CompressionLevel != 7 {
|
|
|
|
t.Fatalf("bad: %#v", config.CompressionLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.OutputPath != "folder" {
|
|
|
|
t.Fatalf("bad: %#v", config.OutputPath)
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
// does NOT overrides config
|
|
|
|
config, err = p.specificConfig("virtualbox")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
if config.CompressionLevel != 1 {
|
|
|
|
t.Fatalf("bad: %#v", config.CompressionLevel)
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
if config.OutputPath != "folder" {
|
|
|
|
t.Fatalf("bad: %#v", config.OutputPath)
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 06:41:24 -05:00
|
|
|
func TestPostProcessorPrepare_vagrantfileTemplateExists(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-01-30 14:43:15 -05:00
|
|
|
name := f.Name()
|
2014-11-25 06:41:24 -05:00
|
|
|
c := testConfig()
|
2015-01-30 14:43:15 -05:00
|
|
|
c["vagrantfile_template"] = name
|
2014-11-25 06:41:24 -05:00
|
|
|
|
2015-01-30 14:43:15 -05:00
|
|
|
if err := f.Close(); err != nil {
|
2015-02-24 23:37:36 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
2015-01-30 14:43:15 -05:00
|
|
|
}
|
2014-11-25 06:41:24 -05:00
|
|
|
|
2019-08-05 13:26:59 -04:00
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
if err := p.Configure(c); err != nil {
|
|
|
|
t.Fatal("no error expected as vagrantfile_template exists")
|
|
|
|
}
|
|
|
|
|
2015-01-30 14:43:15 -05:00
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.Configure(c); err == nil {
|
2019-08-05 13:30:39 -04:00
|
|
|
t.Fatal("expected error since vagrantfile_template does not exist and vagrantfile_template_generated is unset")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The vagrantfile_template will be generated during the build process
|
|
|
|
c["vagrantfile_template_generated"] = true
|
|
|
|
|
|
|
|
if err := p.Configure(c); err != nil {
|
|
|
|
t.Fatal("no error expected due to missing vagrantfile_template as vagrantfile_template_generated is set")
|
2014-11-25 06:41:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:04:45 -05:00
|
|
|
func TestPostProcessorPostProcess_badId(t *testing.T) {
|
|
|
|
artifact := &packer.MockArtifact{
|
|
|
|
BuilderIdValue: "invalid.packer",
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
_, _, _, err := testPP(t).PostProcess(context.Background(), testUi(), artifact)
|
2013-12-19 17:04:45 -05:00
|
|
|
if !strings.Contains(err.Error(), "artifact type") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:45:49 -05:00
|
|
|
func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) {
|
|
|
|
var p PostProcessor
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
c := map[string]interface{}{
|
|
|
|
"packer_user_variables": map[string]string{
|
|
|
|
"foo": f.Name(),
|
|
|
|
},
|
|
|
|
|
|
|
|
"vagrantfile_template": "{{user `foo`}}",
|
|
|
|
}
|
|
|
|
err = p.Configure(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &packer.MockArtifact{
|
|
|
|
BuilderIdValue: "packer.parallels",
|
|
|
|
}
|
2019-04-08 13:59:42 -04:00
|
|
|
a2, _, _, err := p.PostProcess(context.Background(), testUi(), a)
|
2014-11-24 17:45:49 -05:00
|
|
|
if a2 != nil {
|
|
|
|
for _, fn := range a2.Files() {
|
|
|
|
defer os.Remove(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
func TestProviderForName(t *testing.T) {
|
|
|
|
if v, ok := providerForName("virtualbox").(*VBoxProvider); !ok {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2013-06-28 10:16:38 -04:00
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
if providerForName("nope") != nil {
|
|
|
|
t.Fatal("should be nil if bad provider")
|
2013-06-28 10:16:38 -04:00
|
|
|
}
|
|
|
|
}
|