packer/plugin: Start testing the client

This commit is contained in:
Mitchell Hashimoto 2013-06-01 19:35:19 -07:00
parent 45c590f413
commit e629eef97a
3 changed files with 54 additions and 1 deletions

View File

@ -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; {

View File

@ -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")
}
}

View File

@ -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":