packer/plugin: Only allow client start once, lock
This commit is contained in:
parent
fb2ffde29e
commit
250cb0106b
|
@ -20,6 +20,8 @@ var managedClients = make([]*client, 0, 5)
|
||||||
type client struct {
|
type client struct {
|
||||||
config *ClientConfig
|
config *ClientConfig
|
||||||
exited bool
|
exited bool
|
||||||
|
started bool
|
||||||
|
startL sync.Mutex
|
||||||
doneLogging bool
|
doneLogging bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,12 +88,7 @@ func NewClient(config *ClientConfig) (c *client) {
|
||||||
config.StartTimeout = 1 * time.Minute
|
config.StartTimeout = 1 * time.Minute
|
||||||
}
|
}
|
||||||
|
|
||||||
c = &client{
|
c = &client{config: config}
|
||||||
config,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Managed {
|
if config.Managed {
|
||||||
managedClients = append(managedClients, c)
|
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
|
// Once a client has been started once, it cannot be started again, even if
|
||||||
// it was killed.
|
// it was killed.
|
||||||
func (c *client) Start() (address string, err error) {
|
func (c *client) Start() (address string, err error) {
|
||||||
// TODO: Make only run once
|
c.startL.Lock()
|
||||||
// TODO: Mutex
|
defer c.startL.Unlock()
|
||||||
|
|
||||||
|
if c.started {
|
||||||
|
panic("plugin client already started once")
|
||||||
|
}
|
||||||
|
c.started = true
|
||||||
|
|
||||||
env := []string{
|
env := []string{
|
||||||
fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort),
|
fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort),
|
||||||
|
|
|
@ -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) {
|
func TestClient_Start_Timeout(t *testing.T) {
|
||||||
config := &ClientConfig{
|
config := &ClientConfig{
|
||||||
Cmd: helperProcess("start-timeout"),
|
Cmd: helperProcess("start-timeout"),
|
||||||
|
|
Loading…
Reference in New Issue