Merge pull request #222 from mountkin/master

make use of the "pullImage" parameter of node.Create method
This commit is contained in:
Andrea Luzzardi 2015-01-07 14:06:49 +01:00
commit 322c5476e9
2 changed files with 56 additions and 1 deletions

View File

@ -281,7 +281,7 @@ func (n *Node) Create(config *dockerclient.ContainerConfig, name string, pullIma
if id, err = client.CreateContainer(&newConfig, name); err != nil {
// If the error is other than not found, abort immediately.
if err != dockerclient.ErrNotFound {
if err != dockerclient.ErrNotFound || !pullImage {
return nil, err
}
// Otherwise, try to pull the image...

View File

@ -131,3 +131,58 @@ func TestNodeState(t *testing.T) {
client.Mock.AssertExpectations(t)
}
func TestCreateContainer(t *testing.T) {
var (
config = &dockerclient.ContainerConfig{
Image: "busybox",
CpuShares: 512,
Cmd: []string{"date"},
Tty: false,
}
node = NewNode("test")
client = mockclient.NewMockClient()
)
client.On("Info").Return(mockInfo, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
assert.NoError(t, node.connectClient(client))
assert.True(t, node.IsConnected())
mockConfig := *config
mockConfig.CpuShares = config.CpuShares * mockInfo.NCPU
// Everything is ok
name := "test1"
id := "id1"
client.On("CreateContainer", &mockConfig, name).Return(id, nil).Once()
client.On("ListContainers", true, false, fmt.Sprintf(`{"id":[%q]}`, id)).Return([]dockerclient.Container{{Id: id}}, nil).Once()
client.On("InspectContainer", id).Return(&dockerclient.ContainerInfo{Config: config}, nil).Once()
container, err := node.Create(config, name, false)
assert.Nil(t, err)
assert.Equal(t, container.Id, id)
assert.Len(t, node.Containers(), 1)
// Image not found, pullImage == false
name = "test2"
mockConfig.CpuShares = config.CpuShares * mockInfo.NCPU
client.On("CreateContainer", &mockConfig, name).Return("", dockerclient.ErrNotFound).Once()
container, err = node.Create(config, name, false)
assert.Equal(t, err, dockerclient.ErrNotFound)
assert.Nil(t, container)
// Image not found, pullImage == true, and the image can be pulled successfully
name = "test3"
id = "id3"
mockConfig.CpuShares = config.CpuShares * mockInfo.NCPU
client.On("PullImage", config.Image, mock.Anything).Return(nil).Once()
client.On("CreateContainer", &mockConfig, name).Return("", dockerclient.ErrNotFound).Once()
client.On("CreateContainer", &mockConfig, name).Return(id, nil).Once()
client.On("ListContainers", true, false, fmt.Sprintf(`{"id":[%q]}`, id)).Return([]dockerclient.Container{{Id: id}}, nil).Once()
client.On("InspectContainer", id).Return(&dockerclient.ContainerInfo{Config: config}, nil).Once()
container, err = node.Create(config, name, true)
assert.Nil(t, err)
assert.Equal(t, container.Id, id)
assert.Len(t, node.Containers(), 2)
}