2015-06-27 03:47:50 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-05-07 05:43:18 -04:00
|
|
|
"fmt"
|
2020-04-09 05:14:37 -04:00
|
|
|
"io/ioutil"
|
2019-05-07 05:43:18 -04:00
|
|
|
"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"
|
2020-04-09 05:14:37 -04:00
|
|
|
"github.com/hashicorp/packer/post-processor/manifest"
|
2019-12-17 05:25:56 -05:00
|
|
|
shell_local_pp "github.com/hashicorp/packer/post-processor/shell-local"
|
2020-04-09 05:14:37 -04:00
|
|
|
filep "github.com/hashicorp/packer/provisioner/file"
|
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
|
|
|
)
|
|
|
|
|
2020-07-06 11:01:55 -04:00
|
|
|
var (
|
|
|
|
spaghettiCarbonara = `spaghetti
|
|
|
|
carbonara
|
|
|
|
`
|
|
|
|
lasagna = `lasagna
|
|
|
|
tomato
|
|
|
|
mozza
|
|
|
|
cooking...
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2020-04-09 05:14:37 -04:00
|
|
|
func TestBuild(t *testing.T) {
|
2020-03-19 08:57:22 -04:00
|
|
|
tc := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedCode int
|
|
|
|
fileCheck
|
|
|
|
}{
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: json - json varfile sets an apple env var",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "apple.json"),
|
|
|
|
filepath.Join(testFixture("var-arg"), "fruit_builder.json"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"apple.txt"}},
|
|
|
|
},
|
2020-03-30 04:31:59 -04:00
|
|
|
{
|
|
|
|
name: "json - json varfile sets an apple env var, " +
|
|
|
|
"override with banana cli var",
|
|
|
|
args: []string{
|
|
|
|
"-var", "fruit=banana",
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "apple.json"),
|
|
|
|
filepath.Join(testFixture("var-arg"), "fruit_builder.json"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"banana.txt"}},
|
|
|
|
},
|
2020-03-19 08:57:22 -04:00
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: json - arg sets a pear env var",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var=fruit=pear",
|
|
|
|
filepath.Join(testFixture("var-arg"), "fruit_builder.json"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"pear.txt"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: json - inexistent var file errs",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "potato.json"),
|
|
|
|
filepath.Join(testFixture("var-arg"), "fruit_builder.json"),
|
|
|
|
},
|
|
|
|
expectedCode: 1,
|
|
|
|
fileCheck: fileCheck{notExpected: []string{"potato.txt"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: hcl - inexistent json var file errs",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "potato.json"),
|
|
|
|
testFixture("var-arg"),
|
|
|
|
},
|
|
|
|
expectedCode: 1,
|
|
|
|
fileCheck: fileCheck{notExpected: []string{"potato.txt"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: hcl - inexistent hcl var file errs",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "potato.hcl"),
|
|
|
|
testFixture("var-arg"),
|
|
|
|
},
|
|
|
|
expectedCode: 1,
|
|
|
|
fileCheck: fileCheck{notExpected: []string{"potato.hcl"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: hcl - auto varfile sets a chocolate env var",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
testFixture("var-arg"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"chocolate.txt"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: hcl - hcl varfile sets a apple env var",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "apple.hcl"),
|
|
|
|
testFixture("var-arg"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"apple.txt"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: hcl - json varfile sets a apple env var",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var-file=" + filepath.Join(testFixture("var-arg"), "apple.json"),
|
|
|
|
testFixture("var-arg"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"apple.txt"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2020-04-09 05:14:37 -04:00
|
|
|
name: "var-args: hcl - arg sets a tomato env var",
|
2020-03-19 08:57:22 -04:00
|
|
|
args: []string{
|
|
|
|
"-var=fruit=tomato",
|
|
|
|
testFixture("var-arg"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{expected: []string{"tomato.txt"}},
|
|
|
|
},
|
2020-04-09 05:14:37 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
name: "build name: HCL",
|
|
|
|
args: []string{
|
|
|
|
"-parallel-builds=1", // to ensure order is kept
|
|
|
|
testFixture("build-name-and-type"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"manifest.json": `{
|
|
|
|
"builds": [
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"builder_type": "null",
|
|
|
|
"files": null,
|
|
|
|
"artifact_id": "Null",
|
|
|
|
"packer_run_uuid": "",
|
|
|
|
"custom_data": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "potato",
|
|
|
|
"builder_type": "null",
|
|
|
|
"files": null,
|
|
|
|
"artifact_id": "Null",
|
|
|
|
"packer_run_uuid": "",
|
|
|
|
"custom_data": null
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"last_run_uuid": ""
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "build name: JSON except potato",
|
|
|
|
args: []string{
|
|
|
|
"-except=potato",
|
|
|
|
"-parallel-builds=1", // to ensure order is kept
|
|
|
|
filepath.Join(testFixture("build-name-and-type"), "all.json"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
expected: []string{
|
|
|
|
"null.test.txt",
|
|
|
|
"null.potato.txt",
|
|
|
|
},
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"manifest.json": `{
|
|
|
|
"builds": [
|
|
|
|
{
|
|
|
|
"name": "test",
|
|
|
|
"builder_type": "null",
|
|
|
|
"files": null,
|
|
|
|
"artifact_id": "Null",
|
|
|
|
"packer_run_uuid": "",
|
|
|
|
"custom_data": null
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"last_run_uuid": ""
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "build name: JSON only potato",
|
|
|
|
args: []string{
|
|
|
|
"-only=potato",
|
|
|
|
"-parallel-builds=1", // to ensure order is kept
|
|
|
|
filepath.Join(testFixture("build-name-and-type"), "all.json"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"manifest.json": `{
|
|
|
|
"builds": [
|
|
|
|
{
|
|
|
|
"name": "potato",
|
|
|
|
"builder_type": "null",
|
|
|
|
"files": null,
|
|
|
|
"artifact_id": "Null",
|
|
|
|
"packer_run_uuid": "",
|
|
|
|
"custom_data": null
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"last_run_uuid": ""
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-23 04:53:16 -04:00
|
|
|
|
|
|
|
// only / except HCL2
|
|
|
|
{
|
|
|
|
name: "hcl - 'except' a build block",
|
|
|
|
args: []string{
|
|
|
|
"-except=my_build.*",
|
|
|
|
testFixture("hcl-only-except"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
expected: []string{"cherry.txt"},
|
|
|
|
notExpected: []string{"chocolate.txt", "vanilla.txt"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "hcl - 'only' a build block",
|
|
|
|
args: []string{
|
|
|
|
"-only=my_build.*",
|
|
|
|
testFixture("hcl-only-except"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
2020-06-29 07:44:57 -04:00
|
|
|
notExpected: []string{"cherry.txt"},
|
2020-07-02 09:07:29 -04:00
|
|
|
expected: []string{"chocolate.txt", "vanilla.txt"},
|
2020-06-23 04:53:16 -04:00
|
|
|
},
|
|
|
|
},
|
2020-07-06 11:01:55 -04:00
|
|
|
|
|
|
|
// complete
|
|
|
|
{
|
|
|
|
name: "hcl - complete",
|
|
|
|
args: []string{
|
|
|
|
testFixture("hcl", "complete"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"NULL.spaghetti_carbonara.txt": spaghettiCarbonara,
|
|
|
|
"NULL.lasagna.txt": lasagna,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "hcl - complete - except carbonara",
|
|
|
|
args: []string{
|
|
|
|
"-except", "recipes.null.spaghetti_carbonara",
|
|
|
|
testFixture("hcl", "complete"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
notExpected: []string{"NULL.spaghetti_carbonara.txt"},
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"NULL.lasagna.txt": lasagna,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "hcl - complete - only lasagna",
|
|
|
|
args: []string{
|
|
|
|
"-only", "*lasagna",
|
|
|
|
testFixture("hcl", "complete"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
notExpected: []string{"NULL.spaghetti_carbonara.txt"},
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"NULL.lasagna.txt": lasagna,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "hcl - complete - only recipes",
|
|
|
|
args: []string{
|
|
|
|
"-only", "recipes.*",
|
|
|
|
testFixture("hcl", "complete"),
|
|
|
|
},
|
|
|
|
fileCheck: fileCheck{
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"NULL.spaghetti_carbonara.txt": spaghettiCarbonara,
|
|
|
|
"NULL.lasagna.txt": lasagna,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-19 08:57:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tc {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-04-09 05:14:37 -04:00
|
|
|
defer tt.cleanup(t)
|
2020-03-19 08:57:22 -04:00
|
|
|
run(t, tt.args, tt.expectedCode)
|
|
|
|
tt.fileCheck.verify(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 03:47:50 -04:00
|
|
|
func TestBuildOnlyFileCommaFlags(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2020-05-08 11:46:33 -04:00
|
|
|
"-parallel-builds=1",
|
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()
|
2020-05-08 11:46:33 -04:00
|
|
|
if code := c.Run([]string{"-parallel-builds=1", "-"}); 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{
|
2020-05-08 11:46:33 -04:00
|
|
|
"-parallel-builds=1",
|
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),
|
|
|
|
}
|
2020-07-02 09:07:29 -04:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
filepath.Join(testFixture("build-variable-sharing"), "template.json"),
|
2020-02-13 11:35:23 -05:00
|
|
|
}
|
|
|
|
|
2020-07-02 09:07:29 -04:00
|
|
|
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)
|
|
|
|
}
|
2020-02-13 11:35:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:25:41 -05:00
|
|
|
func TestBuildEverything(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2020-05-08 11:46:33 -04:00
|
|
|
"-parallel-builds=1",
|
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),
|
|
|
|
}
|
2020-06-09 09:23:29 -04:00
|
|
|
tc := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedFiles []string
|
|
|
|
buildNotExpectedFiles []string
|
|
|
|
postProcNotExpectedFiles []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "JSON: except build and post-processor",
|
|
|
|
args: []string{
|
|
|
|
"-parallel-builds=1",
|
2020-06-09 11:35:53 -04:00
|
|
|
"-except=chocolate,vanilla,tomato",
|
2020-06-09 09:23:29 -04:00
|
|
|
filepath.Join(testFixture("build-only"), "template.json"),
|
|
|
|
},
|
|
|
|
expectedFiles: []string{"apple.txt", "cherry.txt", "peach.txt"},
|
|
|
|
buildNotExpectedFiles: []string{"chocolate.txt", "vanilla.txt", "tomato.txt", "unnamed.txt"},
|
2020-06-09 11:35:53 -04:00
|
|
|
postProcNotExpectedFiles: []string{"pear.txt, banana.txt"},
|
2020-06-09 09:23:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "HCL2: except build and post-processor",
|
|
|
|
args: []string{
|
|
|
|
"-parallel-builds=1",
|
2020-06-09 11:35:53 -04:00
|
|
|
"-except=file.chocolate,file.vanilla,tomato",
|
2020-06-09 09:23:29 -04:00
|
|
|
filepath.Join(testFixture("build-only"), "template.pkr.hcl"),
|
|
|
|
},
|
|
|
|
expectedFiles: []string{"apple.txt", "cherry.txt", "peach.txt"},
|
|
|
|
buildNotExpectedFiles: []string{"chocolate.txt", "vanilla.txt", "tomato.txt", "unnamed.txt"},
|
2020-06-09 11:35:53 -04:00
|
|
|
postProcNotExpectedFiles: []string{"pear.txt, banana.txt"},
|
2020-06-09 09:23:29 -04:00
|
|
|
},
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
|
|
|
|
2020-06-09 09:23:29 -04:00
|
|
|
for _, tt := range tc {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if code := c.Run(tt.args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range tt.buildNotExpectedFiles {
|
|
|
|
if fileExists(f) {
|
|
|
|
t.Errorf("build not skipped: Expected NOT to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range tt.postProcNotExpectedFiles {
|
|
|
|
if fileExists(f) {
|
|
|
|
t.Errorf("post-processor not skipped: Expected NOT to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range tt.expectedFiles {
|
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:03:24 -04:00
|
|
|
func testHCLOnlyExceptFlags(t *testing.T, args, present, notPresent []string) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
2020-05-08 11:46:33 -04:00
|
|
|
finalArgs := []string{"-parallel-builds=1"}
|
2020-04-28 09:03:24 -04:00
|
|
|
finalArgs = append(finalArgs, args...)
|
|
|
|
finalArgs = append(finalArgs, testFixture("hcl-only-except"))
|
|
|
|
|
|
|
|
if code := c.Run(finalArgs); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range notPresent {
|
|
|
|
if fileExists(f) {
|
|
|
|
t.Errorf("Expected NOT to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range present {
|
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildCommand_HCLOnlyExceptOptions(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
args []string
|
|
|
|
present []string
|
|
|
|
notPresent []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"-only=chocolate"},
|
|
|
|
[]string{},
|
|
|
|
[]string{"chocolate.txt", "vanilla.txt", "cherry.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-only=*chocolate*"},
|
|
|
|
[]string{"chocolate.txt"},
|
|
|
|
[]string{"vanilla.txt", "cherry.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-except=*chocolate*"},
|
|
|
|
[]string{"vanilla.txt", "cherry.txt"},
|
|
|
|
[]string{"chocolate.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-except=*ch*"},
|
|
|
|
[]string{"vanilla.txt"},
|
|
|
|
[]string{"chocolate.txt", "cherry.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-only=*chocolate*", "-only=*vanilla*"},
|
|
|
|
[]string{"chocolate.txt", "vanilla.txt"},
|
|
|
|
[]string{"cherry.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-except=*chocolate*", "-except=*vanilla*"},
|
|
|
|
[]string{"cherry.txt"},
|
|
|
|
[]string{"chocolate.txt", "vanilla.txt"},
|
|
|
|
},
|
|
|
|
{
|
2020-06-23 04:53:16 -04:00
|
|
|
[]string{"-only=my_build.file.chocolate"},
|
2020-04-28 09:03:24 -04:00
|
|
|
[]string{"chocolate.txt"},
|
|
|
|
[]string{"vanilla.txt", "cherry.txt"},
|
|
|
|
},
|
|
|
|
{
|
2020-06-23 04:53:16 -04:00
|
|
|
[]string{"-except=my_build.file.chocolate"},
|
2020-04-28 09:03:24 -04:00
|
|
|
[]string{"vanilla.txt", "cherry.txt"},
|
|
|
|
[]string{"chocolate.txt"},
|
|
|
|
},
|
2020-06-23 04:53:16 -04:00
|
|
|
{
|
|
|
|
[]string{"-only=file.cherry"},
|
|
|
|
[]string{"cherry.txt"},
|
|
|
|
[]string{"vanilla.txt", "chocolate.txt"},
|
|
|
|
},
|
2020-04-28 09:03:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s", tt.args), func(t *testing.T) {
|
|
|
|
testHCLOnlyExceptFlags(t, tt.args, tt.present, tt.notPresent)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
2020-05-08 11:46:33 -04:00
|
|
|
"-parallel-builds=1",
|
2019-12-10 12:55:18 -05:00
|
|
|
`-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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 08:57:22 -04:00
|
|
|
func run(t *testing.T, args []string, expectedCode int) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != expectedCode {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileCheck struct {
|
|
|
|
expected, notExpected []string
|
2020-04-09 05:14:37 -04:00
|
|
|
expectedContent map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc fileCheck) cleanup(t *testing.T) {
|
|
|
|
for _, file := range fc.expectedFiles() {
|
|
|
|
t.Logf("removing %v", file)
|
|
|
|
if err := os.Remove(file); err != nil {
|
|
|
|
t.Errorf("failed to remove file %s: %v", file, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc fileCheck) expectedFiles() []string {
|
|
|
|
expected := fc.expected
|
|
|
|
for file := range fc.expectedContent {
|
|
|
|
expected = append(expected, file)
|
|
|
|
}
|
|
|
|
return expected
|
2020-03-19 08:57:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fc fileCheck) verify(t *testing.T) {
|
2020-04-09 05:14:37 -04:00
|
|
|
for _, f := range fc.expectedFiles() {
|
2020-03-19 08:57:22 -04:00
|
|
|
if !fileExists(f) {
|
|
|
|
t.Errorf("Expected to find %s", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range fc.notExpected {
|
|
|
|
if fileExists(f) {
|
|
|
|
t.Errorf("Expected to not find %s", f)
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 05:14:37 -04:00
|
|
|
for file, expectedContent := range fc.expectedContent {
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ioutil.ReadFile: %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(expectedContent, string(content)); diff != "" {
|
|
|
|
t.Errorf("content of %s differs: %s", file, diff)
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 08:57:22 -04:00
|
|
|
}
|
|
|
|
|
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 },
|
2020-04-09 05:14:37 -04:00
|
|
|
"file": func() (packer.Provisioner, error) { return &filep.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 },
|
2020-04-09 05:14:37 -04:00
|
|
|
"manifest": func() (packer.PostProcessor, error) { return &manifest.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")
|
2020-06-09 11:35:53 -04:00
|
|
|
os.RemoveAll("banana.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-03-30 04:31:59 -04:00
|
|
|
os.RemoveAll("banana.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
|
2020-05-08 11:50:48 -04:00
|
|
|
wantCfg *BuildArgs
|
2019-05-07 05:43:18 -04:00
|
|
|
wantExitCode int
|
|
|
|
}{
|
|
|
|
{fields{defaultMeta},
|
|
|
|
args{[]string{"file.json"}},
|
2020-05-08 11:50:48 -04:00
|
|
|
&BuildArgs{
|
2020-05-08 10:41:47 -04:00
|
|
|
MetaArgs: MetaArgs{Path: "file.json"},
|
2019-05-07 05:43:18 -04:00
|
|
|
ParallelBuilds: math.MaxInt64,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
2020-05-08 11:46:33 -04:00
|
|
|
args{[]string{"-parallel-builds=10", "file.json"}},
|
2020-05-08 11:50:48 -04:00
|
|
|
&BuildArgs{
|
2020-05-08 10:41:47 -04:00
|
|
|
MetaArgs: MetaArgs{Path: "file.json"},
|
2020-05-08 11:50:48 -04:00
|
|
|
ParallelBuilds: 10,
|
2019-05-07 05:43:18 -04:00
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
2020-05-08 11:46:33 -04:00
|
|
|
args{[]string{"-parallel-builds=1", "file.json"}},
|
2020-05-08 11:50:48 -04:00
|
|
|
&BuildArgs{
|
2020-05-08 10:41:47 -04:00
|
|
|
MetaArgs: MetaArgs{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"}},
|
2020-05-08 11:50:48 -04:00
|
|
|
&BuildArgs{
|
2020-05-08 10:41:47 -04:00
|
|
|
MetaArgs: MetaArgs{Path: "file.json"},
|
2019-05-07 05:51:21 -04:00
|
|
|
ParallelBuilds: 5,
|
|
|
|
Color: true,
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
{fields{defaultMeta},
|
2020-05-08 11:46:33 -04:00
|
|
|
args{[]string{"-parallel-builds=1", "-parallel-builds=5", "otherfile.json"}},
|
2020-05-08 11:50:48 -04:00
|
|
|
&BuildArgs{
|
2020-05-08 10:41:47 -04:00
|
|
|
MetaArgs: MetaArgs{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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|