packer/plugin: Only allow client start once, lock

This commit is contained in:
Mitchell Hashimoto 2013-06-11 10:43:55 -07:00
parent fb2ffde29e
commit 250cb0106b
2 changed files with 26 additions and 8 deletions

View File

@ -20,6 +20,8 @@ var managedClients = make([]*client, 0, 5)
type client struct {
config *ClientConfig
exited bool
started bool
startL sync.Mutex
doneLogging bool
}
@ -86,12 +88,7 @@ func NewClient(config *ClientConfig) (c *client) {
config.StartTimeout = 1 * time.Minute
}
c = &client{
config,
false,
false,
}
c = &client{config: config}
if config.Managed {
managedClients = append(managedClients, c)
}
@ -139,8 +136,13 @@ func (c *client) Kill() {
// Once a client has been started once, it cannot be started again, even if
// it was killed.
func (c *client) Start() (address string, err error) {
// TODO: Make only run once
// TODO: Mutex
c.startL.Lock()
defer c.startL.Unlock()
if c.started {
panic("plugin client already started once")
}
c.started = true
env := []string{
fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort),

View File

@ -33,6 +33,22 @@ func TestClient(t *testing.T) {
}
}
func TestClient_Start_Once(t *testing.T) {
process := helperProcess("mock")
c := NewClient(&ClientConfig{Cmd: process})
defer c.Kill()
defer func() {
p := recover()
if p == nil {
t.Fatal("should've paniced")
}
}()
c.Start()
c.Start()
}
func TestClient_Start_Timeout(t *testing.T) {
config := &ClientConfig{
Cmd: helperProcess("start-timeout"),