diff --git a/packer/plugin/client.go b/packer/plugin/client.go index 7aa63859c..c504cbe41 100644 --- a/packer/plugin/client.go +++ b/packer/plugin/client.go @@ -16,6 +16,8 @@ import ( var managedClients = make([]*client, 0, 5) type client struct { + StartTimeout time.Duration + cmd *exec.Cmd exited bool doneLogging bool @@ -52,6 +54,7 @@ func CleanupClients() { // be properly cleaned. func NewClient(cmd *exec.Cmd) *client { return &client{ + 1 * time.Minute, cmd, false, false, @@ -122,7 +125,7 @@ func (c *client) Start() (address string, err error) { go c.logStderr(stderr) // Some channels for the next step - timeout := time.After(1 * time.Minute) + timeout := time.After(c.StartTimeout) // Start looking for the address for done := false; !done; { diff --git a/packer/plugin/client_test.go b/packer/plugin/client_test.go new file mode 100644 index 000000000..d76b42486 --- /dev/null +++ b/packer/plugin/client_test.go @@ -0,0 +1,47 @@ +package plugin + +import ( + "testing" + "time" +) + +func TestClient(t *testing.T) { + process := helperProcess("mock") + c := NewClient(process) + defer c.Kill() + + // Test that it parses the proper address + addr, err := c.Start() + if err != nil { + t.Fatalf("err should be nil, got %s", err) + } + + if addr != ":1234" { + t.Fatalf("incorrect addr %s", addr) + } + + // Test that it exits properly if killed + c.Kill() + + if process.ProcessState == nil { + t.Fatal("should have process state") + } + + // Test that it knows it is exited + if !c.Exited() { + t.Fatal("should say client has exited") + } +} + +func TestClient_Start_Timeout(t *testing.T) { + c := NewClient(helperProcess("start-timeout")) + defer c.Kill() + + // Set a shorter timeout + c.StartTimeout = 50 * time.Millisecond + + _, err := c.Start() + if err == nil { + t.Fatal("err should not be nil") + } +} diff --git a/packer/plugin/plugin_test.go b/packer/plugin/plugin_test.go index 939e2e8fd..786503491 100644 --- a/packer/plugin/plugin_test.go +++ b/packer/plugin/plugin_test.go @@ -56,6 +56,9 @@ func TestHelperProcess(*testing.T) { ServeHook(new(helperHook)) case "invalid-rpc-address": fmt.Println("lolinvalid") + case "mock": + fmt.Println(":1234") + <-make(chan int) case "provisioner": ServeProvisioner(new(helperProvisioner)) case "start-timeout":