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
This commit is contained in:
Matthew Hooker 2017-03-24 12:41:29 -07:00
parent 0c4b67ff14
commit e4aa2a34ca
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 38 additions and 7 deletions

View File

@ -87,10 +87,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
b.config.ChrootMounts = make([][]string, 0) b.config.ChrootMounts = make([][]string, 0)
} }
if b.config.CopyFiles == nil {
b.config.CopyFiles = make([]string, 0)
}
if len(b.config.ChrootMounts) == 0 { if len(b.config.ChrootMounts) == 0 {
b.config.ChrootMounts = [][]string{ b.config.ChrootMounts = [][]string{
{"proc", "proc", "/proc"}, {"proc", "proc", "/proc"},
@ -101,8 +97,12 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
} }
if len(b.config.CopyFiles) == 0 && !b.config.FromScratch { // set default copy file if we're not giving our own
b.config.CopyFiles = []string{"/etc/resolv.conf"} 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 == "" { if b.config.CommandWrapper == "" {

View File

@ -1,8 +1,9 @@
package chroot package chroot
import ( import (
"github.com/mitchellh/packer/packer"
"testing" "testing"
"github.com/mitchellh/packer/packer"
) )
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
@ -117,3 +118,33 @@ func TestBuilderPrepare_CommandWrapper(t *testing.T) {
t.Errorf("err: %s", err) 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.")
}
}