2015-06-27 03:47:50 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/file"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2019-01-10 05:37:41 -05:00
|
|
|
shell_local "github.com/hashicorp/packer/post-processor/shell-local"
|
2015-06-27 03:47:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildOnlyFileCommaFlags(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-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",
|
|
|
|
"apple.txt", "peach.txt", "pear.txt"} {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
if code := c.Run([]string{"-"}); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
|
2019-01-11 08:06:34 -05:00
|
|
|
for _, f := range []string{"vanilla.txt", "cherry.txt", "chocolate.txt"} {
|
|
|
|
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{
|
|
|
|
"-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-01-11 08:06:34 -05:00
|
|
|
for _, f := range []string{"vanilla.txt"} {
|
|
|
|
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",
|
|
|
|
"apple.txt", "peach.txt", "pear.txt"} {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildExceptFileCommaFlags(t *testing.T) {
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2019-01-10 09:27:02 -05:00
|
|
|
"-except=chocolate,apple",
|
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-01-11 08:06:34 -05:00
|
|
|
for _, f := range []string{"chocolate.txt", "apple.txt", "peach.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-01-11 08:06:34 -05:00
|
|
|
for _, f := range []string{"vanilla.txt", "cherry.txt", "pear.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
Builder: func(n string) (packer.Builder, error) {
|
|
|
|
return &file.Builder{}, nil
|
|
|
|
},
|
2019-01-10 05:37:41 -05:00
|
|
|
PostProcessor: func(n string) (packer.PostProcessor, error) {
|
|
|
|
return &shell_local.PostProcessor{}, nil
|
|
|
|
},
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanup() {
|
|
|
|
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")
|
2015-06-27 03:47:50 -04:00
|
|
|
}
|