2015-06-27 03:47:50 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-05-07 05:43:18 -04:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2015-06-27 03:47:50 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-07 05:43:18 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/file"
|
2019-09-25 16:38:12 -04:00
|
|
|
"github.com/hashicorp/packer/builder/null"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2019-12-17 05:25:56 -05:00
|
|
|
shell_local_pp "github.com/hashicorp/packer/post-processor/shell-local"
|
2019-09-25 16:38:12 -04:00
|
|
|
"github.com/hashicorp/packer/provisioner/shell"
|
2019-12-17 05:25:56 -05:00
|
|
|
shell_local "github.com/hashicorp/packer/provisioner/shell-local"
|
2015-06-27 03:47:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildOnlyFileCommaFlags(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2019-05-02 10:01:35 -04:00
|
|
|
"-parallel=false",
|
2015-06-27 03:47:50 -04:00
|
|
|
"-only=chocolate,vanilla",
|
|
|
|
filepath.Join(testFixture("build-only"), "template.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
2019-01-11 08:06:34 -05:00
|
|
|
for _, f := range []string{"chocolate.txt", "vanilla.txt",
|
2019-02-20 05:03:17 -05:00
|
|
|
"apple.txt", "peach.txt", "pear.txt", "unnamed.txt"} {
|
2019-01-11 08:06:34 -05:00
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
2019-01-11 08:06:34 -05:00
|
|
|
|
2015-06-27 03:47:50 -04:00
|
|
|
if fileExists("cherry.txt") {
|
|
|
|
t.Error("Expected NOT to find cherry.txt")
|
2015-06-30 13:56:14 -04:00
|
|
|
}
|
2019-02-01 09:17:09 -05:00
|
|
|
|
|
|
|
if !fileExists("tomato.txt") {
|
|
|
|
t.Error("Expected to find tomato.txt")
|
|
|
|
}
|
2015-06-30 13:56:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildStdin(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
f, err := os.Open(filepath.Join(testFixture("build-only"), "template.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
stdin := os.Stdin
|
|
|
|
os.Stdin = f
|
|
|
|
defer func() { os.Stdin = stdin }()
|
|
|
|
|
|
|
|
defer cleanup()
|
2019-05-02 10:01:35 -04:00
|
|
|
if code := c.Run([]string{"-parallel=false", "-"}); code != 0 {
|
2015-06-30 13:56:14 -04:00
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
2019-02-20 05:03:17 -05:00
|
|
|
for _, f := range []string{"vanilla.txt", "cherry.txt", "chocolate.txt",
|
|
|
|
"unnamed.txt"} {
|
2019-01-11 08:06:34 -05:00
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildOnlyFileMultipleFlags(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2019-05-02 10:01:35 -04:00
|
|
|
"-parallel=false",
|
2015-06-27 03:47:50 -04:00
|
|
|
"-only=chocolate",
|
|
|
|
"-only=cherry",
|
2019-01-11 08:06:34 -05:00
|
|
|
"-only=apple", // ignored
|
|
|
|
"-only=peach", // ignored
|
|
|
|
"-only=pear", // ignored
|
2015-06-27 03:47:50 -04:00
|
|
|
filepath.Join(testFixture("build-only"), "template.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
2019-02-19 10:40:42 -05:00
|
|
|
for _, f := range []string{"vanilla.txt", "tomato.txt"} {
|
2019-01-11 08:06:34 -05:00
|
|
|
if fileExists(f) {
|
|
|
|
t.Errorf("Expected NOT to find %s", f)
|
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
2019-01-11 08:06:34 -05:00
|
|
|
for _, f := range []string{"chocolate.txt", "cherry.txt",
|
2019-02-20 05:03:17 -05:00
|
|
|
"apple.txt", "peach.txt", "pear.txt", "unnamed.txt"} {
|
2019-01-11 08:06:34 -05:00
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
2019-01-10 09:32:54 -05:00
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:35:23 -05:00
|
|
|
func TestBuildProvisionAndPosProcessWithBuildVariablesSharing(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
filepath.Join(testFixture("build-variable-sharing"), "template.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
files := []string{
|
|
|
|
"provisioner.Null.txt",
|
|
|
|
"post-processor.Null.txt",
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup(files...)
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:25:41 -05:00
|
|
|
func TestBuildEverything(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2019-05-02 10:01:35 -04:00
|
|
|
"-parallel=false",
|
2019-02-20 05:03:17 -05:00
|
|
|
`-except=`,
|
2019-02-19 09:25:41 -05:00
|
|
|
filepath.Join(testFixture("build-only"), "template.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt",
|
2019-02-20 05:03:17 -05:00
|
|
|
"apple.txt", "cherry.txt", "pear.txt", "peach.txt", "unnamed.txt"} {
|
2019-02-19 09:25:41 -05:00
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:47:50 -04:00
|
|
|
func TestBuildExceptFileCommaFlags(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2019-05-02 10:01:35 -04:00
|
|
|
"-parallel=false",
|
2019-02-01 09:50:06 -05:00
|
|
|
"-except=chocolate,vanilla",
|
2015-06-27 03:47:50 -04:00
|
|
|
filepath.Join(testFixture("build-only"), "template.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
2019-02-20 05:03:17 -05:00
|
|
|
for _, f := range []string{"chocolate.txt", "vanilla.txt", "tomato.txt",
|
|
|
|
"unnamed.txt"} {
|
2019-01-10 05:37:41 -05:00
|
|
|
if fileExists(f) {
|
|
|
|
t.Errorf("Expected NOT to find %s", f)
|
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
2019-02-01 09:50:06 -05:00
|
|
|
for _, f := range []string{"apple.txt", "cherry.txt", "pear.txt", "peach.txt"} {
|
2019-01-10 05:37:41 -05:00
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func TestBuildWithNonExistingBuilder(t *testing.T) {
|
2019-12-10 12:55:18 -05:00
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-parallel=false",
|
|
|
|
`-except=`,
|
|
|
|
filepath.Join(testFixture("build-only"), "not-found.json"),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Errorf("Expected to find exit code 1, found %d", code)
|
|
|
|
}
|
|
|
|
if !fileExists("chocolate.txt") {
|
|
|
|
t.Errorf("Expected to find chocolate.txt")
|
|
|
|
}
|
|
|
|
if fileExists("vanilla.txt") {
|
|
|
|
t.Errorf("NOT expected to find vanilla.tx")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:47:50 -04:00
|
|
|
// fileExists returns true if the filename is found
|
|
|
|
func fileExists(filename string) bool {
|
|
|
|
if _, err := os.Stat(filename); err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// testCoreConfigBuilder creates a packer CoreConfig that has a file builder
|
|
|
|
// available. This allows us to test a builder that writes files to disk.
|
|
|
|
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
|
|
|
|
components := packer.ComponentFinder{
|
2019-12-17 05:25:56 -05:00
|
|
|
BuilderStore: packer.MapOfBuilder{
|
|
|
|
"file": func() (packer.Builder, error) { return &file.Builder{}, nil },
|
|
|
|
"null": func() (packer.Builder, error) { return &null.Builder{}, nil },
|
2019-09-25 16:38:12 -04:00
|
|
|
},
|
2019-12-17 05:25:56 -05:00
|
|
|
ProvisionerStore: packer.MapOfProvisioner{
|
|
|
|
"shell-local": func() (packer.Provisioner, error) { return &shell_local.Provisioner{}, nil },
|
|
|
|
"shell": func() (packer.Provisioner, error) { return &shell.Provisioner{}, nil },
|
2015-06-27 03:47:50 -04:00
|
|
|
},
|
2019-12-17 05:25:56 -05:00
|
|
|
PostProcessorStore: packer.MapOfPostProcessor{
|
|
|
|
"shell-local": func() (packer.PostProcessor, error) { return &shell_local_pp.PostProcessor{}, nil },
|
2019-01-10 05:37:41 -05:00
|
|
|
},
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
|
|
|
return &packer.CoreConfig{
|
|
|
|
Components: components,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMetaFile creates a Meta object that includes a file builder
|
|
|
|
func testMetaFile(t *testing.T) Meta {
|
|
|
|
var out, err bytes.Buffer
|
|
|
|
return Meta{
|
|
|
|
CoreConfig: testCoreConfigBuilder(t),
|
|
|
|
Ui: &packer.BasicUi{
|
|
|
|
Writer: &out,
|
|
|
|
ErrorWriter: &err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:35:23 -05:00
|
|
|
func cleanup(moreFiles ...string) {
|
2015-06-27 03:47:50 -04:00
|
|
|
os.RemoveAll("chocolate.txt")
|
|
|
|
os.RemoveAll("vanilla.txt")
|
|
|
|
os.RemoveAll("cherry.txt")
|
2019-01-10 05:37:41 -05:00
|
|
|
os.RemoveAll("apple.txt")
|
|
|
|
os.RemoveAll("peach.txt")
|
2019-01-11 08:06:34 -05:00
|
|
|
os.RemoveAll("pear.txt")
|
2019-02-01 09:17:09 -05:00
|
|
|
os.RemoveAll("tomato.txt")
|
2019-02-20 05:03:17 -05:00
|
|
|
os.RemoveAll("unnamed.txt")
|
2019-04-08 07:41:06 -04:00
|
|
|
os.RemoveAll("roses.txt")
|
|
|
|
os.RemoveAll("fuchsias.txt")
|
|
|
|
os.RemoveAll("lilas.txt")
|
|
|
|
os.RemoveAll("campanules.txt")
|
2019-09-25 16:38:12 -04:00
|
|
|
os.RemoveAll("ducky.txt")
|
2020-02-13 11:35:23 -05:00
|
|
|
for _, file := range moreFiles {
|
|
|
|
os.RemoveAll(file)
|
|
|
|
}
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
2019-05-07 05:43:18 -04:00
|
|
|
|
|
|
|
func TestBuildCommand_ParseArgs(t *testing.T) {
|
|
|
|
defaultMeta := testMetaFile(t)
|
|
|
|
type fields struct {
|
|
|
|
Meta Meta
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantCfg Config
|
|
|
|
wantExitCode int
|
|
|
|
}{
|
|
|
|
{fields{defaultMeta},
|
|
|
|
args{[]string{"file.json"}},
|
|
|
|
Config{
|
2019-05-07 05:51:21 -04:00
|
|
|
Path: "file.json",
|
2019-05-07 05:43:18 -04:00
|
|
|
ParallelBuilds: math.MaxInt64,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
|
|
|
args{[]string{"-parallel=true", "file.json"}},
|
|
|
|
Config{
|
2019-05-07 05:51:21 -04:00
|
|
|
Path: "file.json",
|
2019-05-07 05:43:18 -04:00
|
|
|
ParallelBuilds: math.MaxInt64,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
|
|
|
args{[]string{"-parallel=false", "file.json"}},
|
|
|
|
Config{
|
2019-05-07 05:51:21 -04:00
|
|
|
Path: "file.json",
|
2019-05-07 05:43:18 -04:00
|
|
|
ParallelBuilds: 1,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
|
|
|
args{[]string{"-parallel-builds=5", "file.json"}},
|
|
|
|
Config{
|
2019-05-07 05:51:21 -04:00
|
|
|
Path: "file.json",
|
|
|
|
ParallelBuilds: 5,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
|
|
|
args{[]string{"-parallel=false", "-parallel-builds=5", "otherfile.json"}},
|
|
|
|
Config{
|
|
|
|
Path: "otherfile.json",
|
2019-05-07 05:43:18 -04:00
|
|
|
ParallelBuilds: 5,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s", tt.args.args), func(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: tt.fields.Meta,
|
|
|
|
}
|
|
|
|
gotCfg, gotExitCode := c.ParseArgs(tt.args.args)
|
|
|
|
if diff := cmp.Diff(gotCfg, tt.wantCfg); diff != "" {
|
|
|
|
t.Fatalf("BuildCommand.ParseArgs() unexpected cfg %s", diff)
|
|
|
|
}
|
|
|
|
if gotExitCode != tt.wantExitCode {
|
|
|
|
t.Fatalf("BuildCommand.ParseArgs() gotExitCode = %v, want %v", gotExitCode, tt.wantExitCode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|