diff --git a/builder/virtualbox/common/guest_addition_modes.go b/builder/virtualbox/common/guest_addition_modes.go deleted file mode 100644 index b713df7c8..000000000 --- a/builder/virtualbox/common/guest_addition_modes.go +++ /dev/null @@ -1,9 +0,0 @@ -package common - -// These are the different valid mode values for "guest_additions_mode" which -// determine how guest additions are delivered to the guest. -const ( - GuestAdditionsModeDisable string = "disable" - GuestAdditionsModeAttach = "attach" - GuestAdditionsModeUpload = "upload" -) diff --git a/builder/virtualbox/common/guest_additions_config.go b/builder/virtualbox/common/guest_additions_config.go new file mode 100644 index 000000000..22d16b716 --- /dev/null +++ b/builder/virtualbox/common/guest_additions_config.go @@ -0,0 +1,31 @@ +package common + +import ( + "fmt" + + "github.com/hashicorp/packer/template/interpolate" +) + +// These are the different valid mode values for "guest_additions_mode" which +// determine how guest additions are delivered to the guest. +const ( + GuestAdditionsModeDisable string = "disable" + GuestAdditionsModeAttach = "attach" + GuestAdditionsModeUpload = "upload" +) + +type GuestAdditionsConfig struct { + Communicator string `mapstructure:"communicator"` + GuestAdditionsMode string `mapstructure:"guest_additions_mode"` +} + +func (c *GuestAdditionsConfig) Prepare(ctx *interpolate.Context) []error { + var errs []error + + if c.Communicator == "none" && c.GuestAdditionsMode != "disable" { + errs = append(errs, fmt.Errorf("guest_additions_mode has to be "+ + "'disable' when communicator = 'none'.")) + } + + return errs +} diff --git a/builder/virtualbox/common/guest_additions_config_test.go b/builder/virtualbox/common/guest_additions_config_test.go new file mode 100644 index 000000000..f35f75e14 --- /dev/null +++ b/builder/virtualbox/common/guest_additions_config_test.go @@ -0,0 +1,17 @@ +package common + +import ( + "testing" +) + +func TestGuestAdditionsConfigPrepare(t *testing.T) { + c := new(GuestAdditionsConfig) + var errs []error + + c.GuestAdditionsMode = "disable" + c.Communicator = "none" + errs = c.Prepare(testConfigTemplate(t)) + if len(errs) > 0 { + t.Fatalf("should not have error: %s", errs) + } +} diff --git a/builder/virtualbox/common/vbox_version_config.go b/builder/virtualbox/common/vbox_version_config.go index 2b2b87886..96a62c8a2 100644 --- a/builder/virtualbox/common/vbox_version_config.go +++ b/builder/virtualbox/common/vbox_version_config.go @@ -1,18 +1,28 @@ package common import ( + "fmt" + "github.com/hashicorp/packer/template/interpolate" ) type VBoxVersionConfig struct { + Communicator string `mapstructure:"communicator"` VBoxVersionFile *string `mapstructure:"virtualbox_version_file"` } func (c *VBoxVersionConfig) Prepare(ctx *interpolate.Context) []error { + var errs []error + if c.VBoxVersionFile == nil { default_file := ".vbox_version" c.VBoxVersionFile = &default_file } - return nil + if c.Communicator == "none" && *c.VBoxVersionFile != "" { + errs = append(errs, fmt.Errorf("virtualbox_version_file has to be an "+ + "empty string when communicator = 'none'.")) + } + + return errs } diff --git a/builder/virtualbox/common/vbox_version_config_test.go b/builder/virtualbox/common/vbox_version_config_test.go index 811ba60ff..076018289 100644 --- a/builder/virtualbox/common/vbox_version_config_test.go +++ b/builder/virtualbox/common/vbox_version_config_test.go @@ -62,3 +62,18 @@ func TestVBoxVersionConfigPrepare_empty(t *testing.T) { t.Fatalf("bad value: %s", *c.VBoxVersionFile) } } + +func TestVBoxVersionConfigPrepare_communicator(t *testing.T) { + var c *VBoxVersionConfig + var errs []error + + // Test with 'none' communicator and non-empty virtualbox_version_file + c = new(VBoxVersionConfig) + filename := "test" + c.VBoxVersionFile = &filename + c.Communicator = "none" + errs = c.Prepare(testConfigTemplate(t)) + if len(errs) == 0 { + t.Fatalf("should have an error") + } +} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 3e35787e7..0a32fbd45 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -40,6 +40,7 @@ type Config struct { vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"` vboxcommon.VBoxBundleConfig `mapstructure:",squash"` + vboxcommon.GuestAdditionsConfig `mapstructure:",squash"` DiskSize uint `mapstructure:"disk_size"` GuestAdditionsMode string `mapstructure:"guest_additions_mode"` @@ -101,6 +102,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.VBoxManagePostConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VBoxVersionConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.GuestAdditionsConfig.Prepare(&b.config.ctx)...) if b.config.DiskSize == 0 { b.config.DiskSize = 40000 diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 0136b7509..dd22766aa 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -28,6 +28,7 @@ type Config struct { vboxcommon.VBoxManageConfig `mapstructure:",squash"` vboxcommon.VBoxManagePostConfig `mapstructure:",squash"` vboxcommon.VBoxVersionConfig `mapstructure:",squash"` + vboxcommon.GuestAdditionsConfig `mapstructure:",squash"` Checksum string `mapstructure:"checksum"` ChecksumType string `mapstructure:"checksum_type"` @@ -97,6 +98,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, c.VBoxManagePostConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.GuestAdditionsConfig.Prepare(&c.ctx)...) c.ChecksumType = strings.ToLower(c.ChecksumType) c.Checksum = strings.ToLower(c.Checksum)