diff --git a/packer/plugin/command.go b/packer/plugin/command.go index 6ffbbf655..da1d4ca56 100644 --- a/packer/plugin/command.go +++ b/packer/plugin/command.go @@ -10,7 +10,7 @@ import ( "time" ) -func Command(cmd *exec.Cmd) packer.Command { +func Command(cmd *exec.Cmd) (result packer.Command, err error) { env := []string{ "PACKER_PLUGIN_MIN_PORT=10000", "PACKER_PLUGIN_MAX_PORT=25000", @@ -19,7 +19,10 @@ func Command(cmd *exec.Cmd) packer.Command { out := new(bytes.Buffer) cmd.Env = append(cmd.Env, env...) cmd.Stdout = out - cmd.Start() + err = cmd.Start() + if err != nil { + return + } // TODO: timeout // TODO: check that command is even running @@ -39,5 +42,6 @@ func Command(cmd *exec.Cmd) packer.Command { panic(err) } - return packrpc.Command(client) + result = packrpc.Command(client) + return } diff --git a/packer/plugin/command_test.go b/packer/plugin/command_test.go new file mode 100644 index 000000000..2088b6cd5 --- /dev/null +++ b/packer/plugin/command_test.go @@ -0,0 +1,24 @@ +package plugin + +import ( + "cgl.tideland.biz/asserts" + "os/exec" + "testing" +) + +func TestCommand_NoExist(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + _, err := Command(exec.Command("i-should-never-ever-ever-exist")) + assert.NotNil(err, "should have an error") +} + +func TestCommand_Good(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + command, err := Command(helperProcess("command")) + assert.Nil(err, "should start command properly") + + result := command.Synopsis() + assert.Equal(result, "1", "should return result") +} diff --git a/packer/plugin/plugin_test.go b/packer/plugin/plugin_test.go index 3b7b79978..630bf3eda 100644 --- a/packer/plugin/plugin_test.go +++ b/packer/plugin/plugin_test.go @@ -1,7 +1,6 @@ package plugin import ( - "cgl.tideland.biz/asserts" "github.com/mitchellh/packer/packer" "os" "os/exec" @@ -32,15 +31,6 @@ func helperProcess(s... string) *exec.Cmd { return cmd } -func TestClient(t *testing.T) { - assert := asserts.NewTestingAsserts(t, true) - - command := Command(helperProcess("command")) - result := command.Synopsis() - - assert.Equal(result, "1", "should return result") -} - // This is not a real test. This is just a helper process kicked off by // tests. func TestHelperProcess(*testing.T) {