mirror of https://github.com/hashicorp/packer
parent
84894ca459
commit
408eba88ad
@ -1,36 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunConfigPrepare(t *testing.T) {
|
||||
var c *RunConfig
|
||||
|
||||
// Test a default boot_wait
|
||||
c = new(RunConfig)
|
||||
c.RawBootWait = ""
|
||||
errs := c.Prepare(testConfigTemplate(t))
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
if c.RawBootWait != "10s" {
|
||||
t.Fatalf("bad value: %s", c.RawBootWait)
|
||||
}
|
||||
|
||||
// Test with a bad boot_wait
|
||||
c = new(RunConfig)
|
||||
c.RawBootWait = "this is not good"
|
||||
errs = c.Prepare(testConfigTemplate(t))
|
||||
if len(errs) == 0 {
|
||||
t.Fatal("should error")
|
||||
}
|
||||
|
||||
// Test with a good one
|
||||
c = new(RunConfig)
|
||||
c.RawBootWait = "5s"
|
||||
errs = c.Prepare(testConfigTemplate(t))
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package bootcommand
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/packer/template/interpolate"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
RawBootWait string `mapstructure:"boot_wait"`
|
||||
BootCommand []string `mapstructure:"boot_command"`
|
||||
|
||||
BootWait time.Duration ``
|
||||
}
|
||||
|
||||
type VNCConfig struct {
|
||||
Config
|
||||
DisableVNC bool `mapstructure:"disable_vnc"`
|
||||
}
|
||||
|
||||
func (c *Config) Prepare(ctx *interpolate.Context) (errs []error) {
|
||||
if c.RawBootWait == "" {
|
||||
c.RawBootWait = "10s"
|
||||
}
|
||||
if c.RawBootWait != "" {
|
||||
bw, err := time.ParseDuration(c.RawBootWait)
|
||||
if err != nil {
|
||||
errs = append(
|
||||
errs, fmt.Errorf("Failed parsing boot_wait: %s", err))
|
||||
} else {
|
||||
c.BootWait = bw
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) FlatBootCommand() string {
|
||||
return strings.Join(c.BootCommand, "")
|
||||
}
|
||||
|
||||
func (c *VNCConfig) Prepare(ctx *interpolate.Context) (errs []error) {
|
||||
if len(c.BootCommand) > 0 && c.DisableVNC {
|
||||
errs = append(errs,
|
||||
fmt.Errorf("A boot command cannot be used when vnc is disabled."))
|
||||
}
|
||||
errs = append(errs, c.Config.Prepare(ctx)...)
|
||||
return
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package bootcommand
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer/template/interpolate"
|
||||
)
|
||||
|
||||
func TestConfigPrepare(t *testing.T) {
|
||||
var c *Config
|
||||
|
||||
// Test a default boot_wait
|
||||
c = new(Config)
|
||||
c.RawBootWait = ""
|
||||
errs := c.Prepare(&interpolate.Context{})
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
if c.RawBootWait != "10s" {
|
||||
t.Fatalf("bad value: %s", c.RawBootWait)
|
||||
}
|
||||
|
||||
// Test with a bad boot_wait
|
||||
c = new(Config)
|
||||
c.RawBootWait = "this is not good"
|
||||
errs = c.Prepare(&interpolate.Context{})
|
||||
if len(errs) == 0 {
|
||||
t.Fatal("should error")
|
||||
}
|
||||
|
||||
// Test with a good one
|
||||
c = new(Config)
|
||||
c.RawBootWait = "5s"
|
||||
errs = c.Prepare(&interpolate.Context{})
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVNCConfigPrepare(t *testing.T) {
|
||||
var c *VNCConfig
|
||||
|
||||
// Test with a boot command
|
||||
c = new(VNCConfig)
|
||||
c.BootCommand = []string{"a", "b"}
|
||||
errs := c.Prepare(&interpolate.Context{})
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
|
||||
// Test with disabled vnc
|
||||
c.DisableVNC = true
|
||||
errs = c.Prepare(&interpolate.Context{})
|
||||
if len(errs) == 0 {
|
||||
t.Fatal("should error")
|
||||
}
|
||||
|
||||
// Test no boot command with no vnc
|
||||
c = new(VNCConfig)
|
||||
c.DisableVNC = true
|
||||
errs = c.Prepare(&interpolate.Context{})
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue