mirror of
https://github.com/docker/docs.git
synced 2026-11-14 04:50:52 +03:00
Merge pull request #993 from ealang/master
606- Prevent panic if a container is removed during a refresh
This commit is contained in:
@@ -209,9 +209,11 @@ func (e *Engine) RefreshContainers(full bool) error {
|
||||
|
||||
merged := make(map[string]*Container)
|
||||
for _, c := range containers {
|
||||
merged, err = e.updateContainer(c, merged, full)
|
||||
mergedUpdate, err := e.updateContainer(c, merged, full)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Errorf("Unable to update state of container %q", c.Id)
|
||||
log.WithFields(log.Fields{"name": e.Name, "id": e.ID}).Errorf("Unable to update state of container %q: %v", c.Id, err)
|
||||
} else {
|
||||
merged = mergedUpdate
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -251,3 +251,36 @@ func TestUsedCpus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerRemovedDuringRefresh(t *testing.T) {
|
||||
var (
|
||||
container1 = dockerclient.Container{Id: "c1"}
|
||||
container2 = dockerclient.Container{Id: "c2"}
|
||||
info1 *dockerclient.ContainerInfo
|
||||
info2 = &dockerclient.ContainerInfo{Id: "c2", Config: &dockerclient.ContainerConfig{}}
|
||||
)
|
||||
|
||||
engine := NewEngine("test", 0)
|
||||
assert.False(t, engine.isConnected())
|
||||
|
||||
// A container is removed before it can be inspected.
|
||||
client := mockclient.NewMockClient()
|
||||
|
||||
client.On("Info").Return(mockInfo, nil)
|
||||
client.On("Version").Return(mockVersion, nil)
|
||||
client.On("ListImages").Return([]*dockerclient.Image{}, nil)
|
||||
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
|
||||
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{container1, container2}, nil)
|
||||
client.On("InspectContainer", "c1").Return(info1, errors.New("Not found"))
|
||||
client.On("InspectContainer", "c2").Return(info2, nil)
|
||||
|
||||
assert.NoError(t, engine.ConnectWithClient(client))
|
||||
assert.Nil(t, engine.RefreshContainers(true))
|
||||
|
||||
// List of containers is still valid
|
||||
containers := engine.Containers()
|
||||
assert.Len(t, containers, 1)
|
||||
assert.Equal(t, containers[0].Id, "c2")
|
||||
|
||||
client.Mock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user