From e4aa2a34ca63a12ec75d9b45336f5647b3402091 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Fri, 24 Mar 2017 12:41:29 -0700 Subject: [PATCH] builder/amazon-chroot: nullable copy_files Allow users to specify that they don't want any files copied into the chroot by setting an empty copy_files list --- builder/amazon/chroot/builder.go | 12 +++++----- builder/amazon/chroot/builder_test.go | 33 ++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 17f725212..e3958ace0 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -87,10 +87,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.ChrootMounts = make([][]string, 0) } - if b.config.CopyFiles == nil { - b.config.CopyFiles = make([]string, 0) - } - if len(b.config.ChrootMounts) == 0 { b.config.ChrootMounts = [][]string{ {"proc", "proc", "/proc"}, @@ -101,8 +97,12 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - if len(b.config.CopyFiles) == 0 && !b.config.FromScratch { - b.config.CopyFiles = []string{"/etc/resolv.conf"} + // set default copy file if we're not giving our own + if b.config.CopyFiles == nil { + b.config.CopyFiles = make([]string, 0) + if !b.config.FromScratch { + b.config.CopyFiles = []string{"/etc/resolv.conf"} + } } if b.config.CommandWrapper == "" { diff --git a/builder/amazon/chroot/builder_test.go b/builder/amazon/chroot/builder_test.go index dccbef39b..525291d8c 100644 --- a/builder/amazon/chroot/builder_test.go +++ b/builder/amazon/chroot/builder_test.go @@ -1,8 +1,9 @@ package chroot import ( - "github.com/mitchellh/packer/packer" "testing" + + "github.com/mitchellh/packer/packer" ) func testConfig() map[string]interface{} { @@ -117,3 +118,33 @@ func TestBuilderPrepare_CommandWrapper(t *testing.T) { t.Errorf("err: %s", err) } } + +func TestBuilderPrepare_CopyFiles(t *testing.T) { + b := &Builder{} + config := testConfig() + + warnings, err := b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err != nil { + t.Errorf("err: %s", err) + } + + if len(b.config.CopyFiles) != 1 && b.config.CopyFiles[0] != "/etc/resolv.conf" { + t.Errorf("Was expecting default value for copy_files.") + } + + config["copy_files"] = []string{} + warnings, err = b.Prepare(config) + if len(warnings) > 0 { + t.Fatalf("bad: %#v", warnings) + } + if err != nil { + t.Errorf("err: %s", err) + } + + if len(b.config.CopyFiles) > 0 { + t.Errorf("Was expecting no default value for copy_files.") + } +}