Merge pull request #14 from xetorthio/jonas_master
Instances in a session should be created sequentially
This commit is contained in:
@@ -192,6 +192,9 @@ func (p *pwd) checkHostnameExists(session *Session, hostname string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *pwd) InstanceNew(session *Session, conf InstanceConfig) (*Instance, error) {
|
func (p *pwd) InstanceNew(session *Session, conf InstanceConfig) (*Instance, error) {
|
||||||
|
session.rw.Lock()
|
||||||
|
defer session.rw.Unlock()
|
||||||
|
|
||||||
if conf.ImageName == "" {
|
if conf.ImageName == "" {
|
||||||
conf.ImageName = config.GetDindImageName()
|
conf.ImageName = config.GetDindImageName()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package pwd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -86,6 +87,48 @@ func TestInstanceNew(t *testing.T) {
|
|||||||
assert.Equal(t, expectedContainerOpts, containerOpts)
|
assert.Equal(t, expectedContainerOpts, containerOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInstanceNew_Concurrency(t *testing.T) {
|
||||||
|
i := 0
|
||||||
|
dock := &mockDocker{}
|
||||||
|
dock.createContainer = func(opts docker.CreateContainerOpts) (string, error) {
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
i++
|
||||||
|
return fmt.Sprintf("10.0.0.%d", i), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks := &mockTasks{}
|
||||||
|
broadcast := &mockBroadcast{}
|
||||||
|
storage := &mockStorage{}
|
||||||
|
|
||||||
|
p := NewPWD(dock, tasks, broadcast, storage)
|
||||||
|
|
||||||
|
session, err := p.SessionNew(time.Hour, "", "")
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
var instance1 *Instance
|
||||||
|
var instance2 *Instance
|
||||||
|
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
instance, err := p.InstanceNew(session, InstanceConfig{})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
instance1 = instance
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
instance, err := p.InstanceNew(session, InstanceConfig{})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
instance2 = instance
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
assert.Subset(t, []string{"node1", "node2"}, []string{instance1.Hostname, instance2.Hostname})
|
||||||
|
}
|
||||||
|
|
||||||
func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
|
func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
|
||||||
containerOpts := docker.CreateContainerOpts{}
|
containerOpts := docker.CreateContainerOpts{}
|
||||||
dock := &mockDocker{}
|
dock := &mockDocker{}
|
||||||
|
|||||||
Reference in New Issue
Block a user