From 670dc7f59bb04c0b5133328b8b7c8e5210fcd0bb Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Mon, 17 Nov 2014 16:49:05 -0800 Subject: [PATCH] Node: Fix a bug in containers update. Signed-off-by: Andrea Luzzardi --- node.go | 15 +++++++++------ node_test.go | 5 ++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/node.go b/node.go index 7fa4f5748c..bb42080c63 100644 --- a/node.go +++ b/node.go @@ -113,31 +113,33 @@ func (n *Node) updateContainers() error { return err } - n.Lock() - defer n.Unlock() - - n.containers = make(map[string]*Container) + merged := make(map[string]*Container) for _, c := range containers { if current, exists := n.containers[c.Id]; exists { // The container exists. Update its state. current.Container = c + merged[current.Id] = current } else { // This is a brand new container. container := &Container{} container.Container = c container.node = n - info, err := n.client.InspectContainer(container.Id) + info, err := n.client.InspectContainer(c.Id) if err != nil { log.Errorf("[%s] Unable to update state of %s", n.ID, c.Id) continue } container.Info = *info - n.containers[container.Id] = container + merged[container.Id] = container } } + n.Lock() + n.containers = merged + n.Unlock() + log.Debugf("[%s] Updated state", n.ID) return nil } @@ -203,6 +205,7 @@ func (n *Node) Create(config *dockerclient.ContainerConfig, name string, pullIma // Register the container immediately while waiting for a state refresh. // Force a state refresh to pick up the newly created container. + log.Debug("Updating containers after create") n.updateContainers() return n.containers[id], nil diff --git a/node_test.go b/node_test.go index c015f59635..cd4d87343b 100644 --- a/node_test.go +++ b/node_test.go @@ -67,10 +67,9 @@ func TestNodeState(t *testing.T) { // The client will return one container at first, then a second one will appear. client.On("ListContainers", true, false).Return([]dockerclient.Container{{Id: "one"}}, nil).Once() - client.On("InspectContainer", mock.Anything).Return(&dockerclient.ContainerInfo{}, nil).Once() + client.On("InspectContainer", "one").Return(&dockerclient.ContainerInfo{}, nil).Once() client.On("ListContainers", true, false).Return([]dockerclient.Container{{Id: "one"}, {Id: "two"}}, nil).Once() - client.On("InspectContainer", mock.Anything).Return(&dockerclient.ContainerInfo{}, nil).Once() - client.On("InspectContainer", mock.Anything).Return(&dockerclient.ContainerInfo{}, nil).Once() + client.On("InspectContainer", "two").Return(&dockerclient.ContainerInfo{}, nil).Once() assert.NoError(t, node.connectClient(client)) assert.True(t, node.IsConnected())