mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
dockershim: add unit tests for sandbox/container status
Also add a fake clock in the FakeDockerClient to allow testing container CreatedAt, StartedAt, FinishedAt timestamps.
This commit is contained in:
@@ -28,6 +28,7 @@ import (
|
||||
|
||||
dockertypes "github.com/docker/engine-api/types"
|
||||
dockercontainer "github.com/docker/engine-api/types/container"
|
||||
"k8s.io/kubernetes/pkg/util/clock"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
@@ -40,6 +41,7 @@ type calledDetail struct {
|
||||
// FakeDockerClient is a simple fake docker client, so that kubelet can be run for testing without requiring a real docker setup.
|
||||
type FakeDockerClient struct {
|
||||
sync.Mutex
|
||||
Clock *clock.FakeClock
|
||||
RunningContainerList []dockertypes.Container
|
||||
ExitedContainerList []dockertypes.Container
|
||||
ContainerMap map[string]*dockertypes.ContainerJSON
|
||||
@@ -75,6 +77,7 @@ func NewFakeDockerClientWithVersion(version, apiVersion string) *FakeDockerClien
|
||||
VersionInfo: dockertypes.Version{Version: version, APIVersion: apiVersion},
|
||||
Errors: make(map[string]error),
|
||||
ContainerMap: make(map[string]*dockertypes.ContainerJSON),
|
||||
Clock: clock.NewFakeClock(time.Time{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,7 +295,11 @@ func (f *FakeDockerClient) InspectContainer(id string) (*dockertypes.ContainerJS
|
||||
if container, ok := f.ContainerMap[id]; ok {
|
||||
return container, err
|
||||
}
|
||||
return nil, err
|
||||
if err != nil {
|
||||
// Use the custom error if it exists.
|
||||
return nil, err
|
||||
}
|
||||
return nil, fmt.Errorf("container %q not found", id)
|
||||
}
|
||||
|
||||
// InspectImage is a test-spy implementation of DockerInterface.InspectImage.
|
||||
@@ -337,7 +344,8 @@ func (f *FakeDockerClient) CreateContainer(c dockertypes.ContainerCreateConfig)
|
||||
f.RunningContainerList = append([]dockertypes.Container{
|
||||
{ID: name, Names: []string{name}, Image: c.Config.Image, Labels: c.Config.Labels},
|
||||
}, f.RunningContainerList...)
|
||||
f.ContainerMap[name] = convertFakeContainer(&FakeContainer{ID: id, Name: name, Config: c.Config, HostConfig: c.HostConfig})
|
||||
f.ContainerMap[name] = convertFakeContainer(&FakeContainer{
|
||||
ID: id, Name: name, Config: c.Config, HostConfig: c.HostConfig, CreatedAt: f.Clock.Now()})
|
||||
f.normalSleep(100, 25, 25)
|
||||
return &dockertypes.ContainerCreateResponse{ID: id}, nil
|
||||
}
|
||||
@@ -358,7 +366,7 @@ func (f *FakeDockerClient) StartContainer(id string) error {
|
||||
}
|
||||
container.State.Running = true
|
||||
container.State.Pid = os.Getpid()
|
||||
container.State.StartedAt = dockerTimestampToString(time.Now())
|
||||
container.State.StartedAt = dockerTimestampToString(f.Clock.Now())
|
||||
container.NetworkSettings.IPAddress = "2.3.4.5"
|
||||
f.ContainerMap[id] = container
|
||||
f.updateContainerStatus(id, statusRunningPrefix)
|
||||
@@ -398,7 +406,7 @@ func (f *FakeDockerClient) StopContainer(id string, timeout int) error {
|
||||
FinishedAt: time.Now(),
|
||||
})
|
||||
} else {
|
||||
container.State.FinishedAt = dockerTimestampToString(time.Now())
|
||||
container.State.FinishedAt = dockerTimestampToString(f.Clock.Now())
|
||||
container.State.Running = false
|
||||
}
|
||||
f.ContainerMap[id] = container
|
||||
|
||||
Reference in New Issue
Block a user