From 4b4fe2280d291a19c356bbc793ecf6760983748b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 13 Jun 2015 17:50:45 -0400 Subject: [PATCH] helper/communicator: can be disabled --- helper/communicator/config.go | 5 +++ helper/communicator/config_test.go | 28 +++++++++++++++++ helper/communicator/step_connect.go | 28 +++++++++++++---- helper/communicator/step_connect_test.go | 39 ++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 helper/communicator/config_test.go create mode 100644 helper/communicator/step_connect_test.go diff --git a/helper/communicator/config.go b/helper/communicator/config.go index 2500a7d2d..28d1d3a43 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -10,6 +10,7 @@ import ( // Config is the common configuration that communicators allow within // a builder. type Config struct { + Type string `mapstructure:"communicator"` SSHHost string `mapstructure:"ssh_host"` SSHPort int `mapstructure:"ssh_port"` SSHUsername string `mapstructure:"ssh_username"` @@ -20,6 +21,10 @@ type Config struct { } func (c *Config) Prepare(ctx *interpolate.Context) []error { + if c.Type == "" { + c.Type = "ssh" + } + if c.SSHPort == 0 { c.SSHPort = 22 } diff --git a/helper/communicator/config_test.go b/helper/communicator/config_test.go new file mode 100644 index 000000000..f57fb68ca --- /dev/null +++ b/helper/communicator/config_test.go @@ -0,0 +1,28 @@ +package communicator + +import ( + "testing" + + "github.com/mitchellh/packer/template/interpolate" +) + +func testConfig() *Config { + return &Config{ + SSHUsername: "root", + } +} + +func TestConfigType(t *testing.T) { + c := testConfig() + if err := c.Prepare(testContext(t)); len(err) > 0 { + t.Fatalf("bad: %#v", err) + } + + if c.Type != "ssh" { + t.Fatal("bad: %#v", c) + } +} + +func testContext(t *testing.T) *interpolate.Context { + return nil +} diff --git a/helper/communicator/step_connect.go b/helper/communicator/step_connect.go index 77feebfb9..e6338027e 100644 --- a/helper/communicator/step_connect.go +++ b/helper/communicator/step_connect.go @@ -1,6 +1,9 @@ package communicator import ( + "fmt" + "log" + "github.com/mitchellh/multistep" gossh "golang.org/x/crypto/ssh" ) @@ -26,14 +29,27 @@ type StepConnect struct { } func (s *StepConnect) Run(state multistep.StateBag) multistep.StepAction { - // Eventually we might switch between multiple of these depending - // on the communicator type. - s.substep = &StepConnectSSH{ - Config: s.Config, - SSHAddress: s.SSHAddress, - SSHConfig: s.SSHConfig, + typeMap := map[string]multistep.Step{ + "none": nil, + "ssh": &StepConnectSSH{ + Config: s.Config, + SSHAddress: s.SSHAddress, + SSHConfig: s.SSHConfig, + }, } + step, ok := typeMap[s.Config.Type] + if !ok { + state.Put("error", fmt.Errorf("unknown communicator type: %s", s.Config.Type)) + return multistep.ActionHalt + } + + if step == nil { + log.Printf("[INFO] communicator disabled, will not connect") + return multistep.ActionContinue + } + + s.substep = step return s.substep.Run(state) } diff --git a/helper/communicator/step_connect_test.go b/helper/communicator/step_connect_test.go new file mode 100644 index 000000000..bf908f8fb --- /dev/null +++ b/helper/communicator/step_connect_test.go @@ -0,0 +1,39 @@ +package communicator + +import ( + "bytes" + "testing" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" +) + +func TestStepConnect_impl(t *testing.T) { + var _ multistep.Step = new(StepConnect) +} + +func TestStepConnect_none(t *testing.T) { + state := testState(t) + + step := &StepConnect{ + Config: &Config{ + Type: "none", + }, + } + defer step.Cleanup(state) + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } +} + +func testState(t *testing.T) multistep.StateBag { + state := new(multistep.BasicStateBag) + state.Put("hook", &packer.MockHook{}) + state.Put("ui", &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + }) + return state +}