Revert "Generate valid container id in fake docker client."

This commit is contained in:
Jordan Liggitt 2017-02-18 10:06:24 -05:00 committed by GitHub
parent ff12e5688c
commit 66dec96735
8 changed files with 185 additions and 159 deletions

View File

@ -51,7 +51,7 @@ func imageToRuntimeAPIImage(image *dockertypes.Image) (*runtimeapi.Image, error)
} }
func imageInspectToRuntimeAPIImage(image *dockertypes.ImageInspect) (*runtimeapi.Image, error) { func imageInspectToRuntimeAPIImage(image *dockertypes.ImageInspect) (*runtimeapi.Image, error) {
if image == nil || image.Config == nil { if image == nil {
return nil, fmt.Errorf("unable to convert a nil pointer to a runtime API image") return nil, fmt.Errorf("unable to convert a nil pointer to a runtime API image")
} }

View File

@ -217,7 +217,7 @@ func (ds *dockerService) createContainerLogSymlink(containerID string) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to get container %q log path: %v", containerID, err) return fmt.Errorf("failed to get container %q log path: %v", containerID, err)
} }
if path != "" && realPath != "" { if path != "" {
// Only create the symlink when container log path is specified. // Only create the symlink when container log path is specified.
if err = ds.os.Symlink(realPath, path); err != nil { if err = ds.os.Symlink(realPath, path); err != nil {
return fmt.Errorf("failed to create symbolic link %q to the container log file %q for container %q: %v", return fmt.Errorf("failed to create symbolic link %q to the container log file %q for container %q: %v",

View File

@ -24,7 +24,6 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing" containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
@ -243,20 +242,23 @@ func TestContainerCreationConflict(t *testing.T) {
expectFields: 6, expectFields: 6,
}, },
"random create error": { "random create error": {
createError: randomError, createError: randomError,
expectError: randomError, expectError: randomError,
expectCalls: []string{"create"}, expectCalls: []string{"create"},
expectFields: 1,
}, },
"conflict create error with successful remove": { "conflict create error with successful remove": {
createError: conflictError, createError: conflictError,
expectError: conflictError, expectError: conflictError,
expectCalls: []string{"create", "remove"}, expectCalls: []string{"create", "remove"},
expectFields: 1,
}, },
"conflict create error with random remove error": { "conflict create error with random remove error": {
createError: conflictError, createError: conflictError,
removeError: randomError, removeError: randomError,
expectError: conflictError, expectError: conflictError,
expectCalls: []string{"create", "remove"}, expectCalls: []string{"create", "remove"},
expectFields: 1,
}, },
"conflict create error with no such container remove error": { "conflict create error with no such container remove error": {
createError: conflictError, createError: conflictError,
@ -274,13 +276,9 @@ func TestContainerCreationConflict(t *testing.T) {
if test.removeError != nil { if test.removeError != nil {
fDocker.InjectError("remove", test.removeError) fDocker.InjectError("remove", test.removeError)
} }
id, err := ds.CreateContainer(sandboxId, config, sConfig) name, err := ds.CreateContainer(sandboxId, config, sConfig)
require.Equal(t, test.expectError, err) assert.Equal(t, test.expectError, err)
assert.NoError(t, fDocker.AssertCalls(test.expectCalls)) assert.NoError(t, fDocker.AssertCalls(test.expectCalls))
if err == nil { assert.Len(t, strings.Split(name, nameDelimiter), test.expectFields)
c, err := fDocker.InspectContainer(id)
assert.NoError(t, err)
assert.Len(t, strings.Split(c.Name, nameDelimiter), test.expectFields)
}
} }
} }

View File

@ -26,7 +26,6 @@ import (
runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockertools"
"k8s.io/kubernetes/pkg/kubelet/network" "k8s.io/kubernetes/pkg/kubelet/network"
"k8s.io/kubernetes/pkg/kubelet/types" "k8s.io/kubernetes/pkg/kubelet/types"
) )
@ -158,7 +157,7 @@ func TestNetworkPluginInvocation(t *testing.T) {
map[string]string{"label": name}, map[string]string{"label": name},
map[string]string{"annotation": ns}, map[string]string{"annotation": ns},
) )
cID := kubecontainer.ContainerID{Type: runtimeName, ID: dockertools.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))} cID := kubecontainer.ContainerID{Type: runtimeName, ID: fmt.Sprintf("/%v", makeSandboxName(c))}
mockPlugin.EXPECT().Name().Return("mockNetworkPlugin").AnyTimes() mockPlugin.EXPECT().Name().Return("mockNetworkPlugin").AnyTimes()
setup := mockPlugin.EXPECT().SetUpPod(ns, name, cID) setup := mockPlugin.EXPECT().SetUpPod(ns, name, cID)
@ -196,7 +195,7 @@ func TestHostNetworkPluginInvocation(t *testing.T) {
}, },
}, },
} }
cID := kubecontainer.ContainerID{Type: runtimeName, ID: dockertools.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))} cID := kubecontainer.ContainerID{Type: runtimeName, ID: fmt.Sprintf("/%v", makeSandboxName(c))}
// No calls to network plugin are expected // No calls to network plugin are expected
_, err := ds.RunPodSandbox(c) _, err := ds.RunPodSandbox(c)

View File

@ -118,7 +118,13 @@ func TestSeccompIsUnconfinedByDefaultWithDockerV110(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {
@ -151,7 +157,13 @@ func TestUnconfinedSeccompProfileWithDockerV110(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar4"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo4_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar4\\.[a-f0-9]+_foo4_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {
@ -180,7 +192,13 @@ func TestDefaultSeccompProfileWithDockerV110(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar1"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo1_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar1\\.[a-f0-9]+_foo1_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {
@ -210,7 +228,13 @@ func TestSeccompContainerAnnotationTrumpsPod(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar2"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo2_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar2\\.[a-f0-9]+_foo2_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {
@ -236,7 +260,13 @@ func TestSecurityOptsAreNilWithDockerV19(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {
@ -276,7 +306,13 @@ func TestCreateAppArmorContanier(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "test"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_test\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
// Verify security opts. // Verify security opts.
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
@ -348,7 +384,13 @@ func TestSeccompLocalhostProfileIsLoaded(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar2"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo2_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar2\\.[a-f0-9]+_foo2_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {

View File

@ -639,9 +639,13 @@ func TestSyncPodCreateNetAndContainer(t *testing.T) {
if !found { if !found {
t.Errorf("Custom pod infra container not found: %v", fakeDocker.RunningContainerList) t.Errorf("Custom pod infra container not found: %v", fakeDocker.RunningContainerList)
} }
fakeDocker.Unlock()
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar"})) if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
} }
func TestSyncPodCreatesNetAndContainerPullsImage(t *testing.T) { func TestSyncPodCreatesNetAndContainerPullsImage(t *testing.T) {
@ -666,12 +670,17 @@ func TestSyncPodCreatesNetAndContainerPullsImage(t *testing.T) {
}) })
fakeDocker.Lock() fakeDocker.Lock()
if !reflect.DeepEqual(puller.ImagesPulled, []string{"foo/infra_image:v1", "foo/something:v0"}) { if !reflect.DeepEqual(puller.ImagesPulled, []string{"foo/infra_image:v1", "foo/something:v0"}) {
t.Errorf("unexpected pulled containers: %v", puller.ImagesPulled) t.Errorf("unexpected pulled containers: %v", puller.ImagesPulled)
} }
fakeDocker.Unlock()
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar"})) if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
} }
func TestSyncPodWithPodInfraCreatesContainer(t *testing.T) { func TestSyncPodWithPodInfraCreatesContainer(t *testing.T) {
@ -694,7 +703,12 @@ func TestSyncPodWithPodInfraCreatesContainer(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByName([]string{"bar"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 1 ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
} }
func TestSyncPodDeletesWithNoPodInfraContainer(t *testing.T) { func TestSyncPodDeletesWithNoPodInfraContainer(t *testing.T) {
@ -720,7 +734,16 @@ func TestSyncPodDeletesWithNoPodInfraContainer(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertStopped([]string{"1234"})) // A map iteration is used to delete containers, so must not depend on
// order here.
expectedToStop := map[string]bool{
"1234": true,
}
fakeDocker.Lock()
if len(fakeDocker.Stopped) != 1 || !expectedToStop[fakeDocker.Stopped[0]] {
t.Errorf("Wrong containers were stopped: %v", fakeDocker.Stopped)
}
fakeDocker.Unlock()
} }
func TestSyncPodDeletesDuplicate(t *testing.T) { func TestSyncPodDeletesDuplicate(t *testing.T) {
@ -919,7 +942,7 @@ func TestSyncPodWithRestartPolicy(t *testing.T) {
// 'stop' is because the pod infra container is killed when no container is running. // 'stop' is because the pod infra container is killed when no container is running.
verifyCalls(t, fakeDocker, tt.calls) verifyCalls(t, fakeDocker, tt.calls)
if err := fakeDocker.AssertCreatedByName(tt.created); err != nil { if err := fakeDocker.AssertCreated(tt.created); err != nil {
t.Errorf("case [%d]: %v", i, err) t.Errorf("case [%d]: %v", i, err)
} }
if err := fakeDocker.AssertStopped(tt.stopped); err != nil { if err := fakeDocker.AssertStopped(tt.stopped); err != nil {
@ -1157,8 +1180,12 @@ func TestSyncPodWithPodInfraCreatesContainerCallsHandler(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByName([]string{"bar"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 1 ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
if fakeHTTPClient.url != "http://foo:8080/bar" { if fakeHTTPClient.url != "http://foo:8080/bar" {
t.Errorf("unexpected handler: %q", fakeHTTPClient.url) t.Errorf("unexpected handler: %q", fakeHTTPClient.url)
} }
@ -1198,7 +1225,16 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
"stop", "stop",
}) })
assert.NoError(t, fakeDocker.AssertStoppedByName([]string{"bar"})) if len(fakeDocker.Stopped) != 1 {
t.Fatalf("Wrong containers were stopped: %v", fakeDocker.Stopped)
}
dockerName, _, err := ParseDockerName(fakeDocker.Stopped[0])
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if dockerName.ContainerName != "bar" {
t.Errorf("Wrong stopped container, expected: bar, get: %q", dockerName.ContainerName)
}
} }
type fakeReadWriteCloser struct{} type fakeReadWriteCloser struct{}
@ -1253,8 +1289,13 @@ func TestSyncPodWithTerminationLog(t *testing.T) {
defer os.Remove(testPodContainerDir) defer os.Remove(testPodContainerDir)
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {
t.Fatalf("unexpected error %v", err) t.Fatalf("unexpected error %v", err)
@ -1286,7 +1327,13 @@ func TestSyncPodWithHostNetwork(t *testing.T) {
"create", "start", "inspect_container", "create", "start", "inspect_container",
}) })
assert.NoError(t, fakeDocker.AssertCreatedByNameWithOrder([]string{"POD", "bar"})) fakeDocker.Lock()
if len(fakeDocker.Created) != 2 ||
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
t.Errorf("unexpected containers created %v", fakeDocker.Created)
}
fakeDocker.Unlock()
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1]) newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
if err != nil { if err != nil {

View File

@ -19,20 +19,18 @@ package dockertools
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"hash/fnv"
"math/rand" "math/rand"
"os" "os"
"reflect" "reflect"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
dockertypes "github.com/docker/engine-api/types" dockertypes "github.com/docker/engine-api/types"
dockercontainer "github.com/docker/engine-api/types/container" dockercontainer "github.com/docker/engine-api/types/container"
"k8s.io/client-go/util/clock" "k8s.io/client-go/util/clock"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
) )
@ -73,13 +71,9 @@ type FakeDockerClient struct {
ImageHistoryMap map[string][]dockertypes.ImageHistory ImageHistoryMap map[string][]dockertypes.ImageHistory
} }
const ( // We don't check docker version now, just set the docker version of fake docker client to 1.8.1.
// We don't check docker version now, just set the docker version of fake docker client to 1.8.1. // Notice that if someday we also have minimum docker version requirement, this should also be updated.
// Notice that if someday we also have minimum docker version requirement, this should also be updated. const fakeDockerVersion = "1.8.1"
fakeDockerVersion = "1.8.1"
fakeImageSize = 1024
)
func NewFakeDockerClient() *FakeDockerClient { func NewFakeDockerClient() *FakeDockerClient {
return &FakeDockerClient{ return &FakeDockerClient{
@ -279,39 +273,17 @@ func (f *FakeDockerClient) AssertCallDetails(calls ...calledDetail) (err error)
return return
} }
// idsToNames converts container ids into names. The caller must hold the lock. func (f *FakeDockerClient) AssertCreated(created []string) error {
func (f *FakeDockerClient) idsToNames(ids []string) ([]string, error) { f.Lock()
names := []string{} defer f.Unlock()
for _, id := range ids {
dockerName, _, err := ParseDockerName(f.ContainerMap[id].Name) actualCreated := []string{}
for _, c := range f.Created {
dockerName, _, err := ParseDockerName(c)
if err != nil { if err != nil {
return nil, fmt.Errorf("unexpected error: %v", err) return fmt.Errorf("unexpected error: %v", err)
} }
names = append(names, dockerName.ContainerName) actualCreated = append(actualCreated, dockerName.ContainerName)
}
return names, nil
}
func (f *FakeDockerClient) AssertCreatedByNameWithOrder(created []string) error {
f.Lock()
defer f.Unlock()
actualCreated, err := f.idsToNames(f.Created)
if err != nil {
return err
}
if !reflect.DeepEqual(created, actualCreated) {
return fmt.Errorf("expected %#v, got %#v", created, actualCreated)
}
return nil
}
func (f *FakeDockerClient) AssertCreatedByName(created []string) error {
f.Lock()
defer f.Unlock()
actualCreated, err := f.idsToNames(f.Created)
if err != nil {
return err
} }
sort.StringSlice(created).Sort() sort.StringSlice(created).Sort()
sort.StringSlice(actualCreated).Sort() sort.StringSlice(actualCreated).Sort()
@ -321,18 +293,13 @@ func (f *FakeDockerClient) AssertCreatedByName(created []string) error {
return nil return nil
} }
func (f *FakeDockerClient) AssertStoppedByName(stopped []string) error { func (f *FakeDockerClient) AssertStarted(started []string) error {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
sort.StringSlice(started).Sort()
actualStopped, err := f.idsToNames(f.Stopped) sort.StringSlice(f.Started).Sort()
if err != nil { if !reflect.DeepEqual(started, f.Started) {
return err return fmt.Errorf("expected %#v, got %#v", started, f.Started)
}
sort.StringSlice(stopped).Sort()
sort.StringSlice(actualStopped).Sort()
if !reflect.DeepEqual(stopped, actualStopped) {
return fmt.Errorf("expected %#v, got %#v", stopped, actualStopped)
} }
return nil return nil
} }
@ -340,12 +307,21 @@ func (f *FakeDockerClient) AssertStoppedByName(stopped []string) error {
func (f *FakeDockerClient) AssertStopped(stopped []string) error { func (f *FakeDockerClient) AssertStopped(stopped []string) error {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
// Copy stopped to avoid modifying it.
actualStopped := append([]string{}, f.Stopped...)
sort.StringSlice(stopped).Sort() sort.StringSlice(stopped).Sort()
sort.StringSlice(actualStopped).Sort() sort.StringSlice(f.Stopped).Sort()
if !reflect.DeepEqual(stopped, actualStopped) { if !reflect.DeepEqual(stopped, f.Stopped) {
return fmt.Errorf("expected %#v, got %#v", stopped, actualStopped) return fmt.Errorf("expected %#v, got %#v", stopped, f.Stopped)
}
return nil
}
func (f *FakeDockerClient) AssertRemoved(removed []string) error {
f.Lock()
defer f.Unlock()
sort.StringSlice(removed).Sort()
sort.StringSlice(f.Removed).Sort()
if !reflect.DeepEqual(removed, f.Removed) {
return fmt.Errorf("expected %#v, got %#v", removed, f.Removed)
} }
return nil return nil
} }
@ -377,57 +353,30 @@ func (f *FakeDockerClient) ListContainers(options dockertypes.ContainerListOptio
// TODO(random-liu): Is a fully sorted array needed? // TODO(random-liu): Is a fully sorted array needed?
containerList = append(containerList, f.ExitedContainerList...) containerList = append(containerList, f.ExitedContainerList...)
} }
// Filter containers with id, only support 1 id. // TODO: Support other filters.
idFilters := options.Filter.Get("id")
if len(idFilters) != 0 {
var filtered []dockertypes.Container
for _, container := range containerList {
for _, idFilter := range idFilters {
if container.ID == idFilter {
filtered = append(filtered, container)
break
}
}
}
containerList = filtered
}
// Filter containers with status, only support 1 status.
statusFilters := options.Filter.Get("status")
if len(statusFilters) == 1 {
var filtered []dockertypes.Container
for _, container := range containerList {
for _, statusFilter := range statusFilters {
if container.Status == statusFilter {
filtered = append(filtered, container)
break
}
}
}
containerList = filtered
}
// Filter containers with label filter. // Filter containers with label filter.
labelFilters := options.Filter.Get("label") labelFilters := options.Filter.Get("label")
if len(labelFilters) != 0 { if len(labelFilters) == 0 {
var filtered []dockertypes.Container return containerList, err
for _, container := range containerList { }
match := true var filtered []dockertypes.Container
for _, labelFilter := range labelFilters { for _, container := range containerList {
kv := strings.Split(labelFilter, "=") match := true
if len(kv) != 2 { for _, labelFilter := range labelFilters {
return nil, fmt.Errorf("invalid label filter %q", labelFilter) kv := strings.Split(labelFilter, "=")
} if len(kv) != 2 {
if container.Labels[kv[0]] != kv[1] { return nil, fmt.Errorf("invalid label filter %q", labelFilter)
match = false
break
}
} }
if match { if container.Labels[kv[0]] != kv[1] {
filtered = append(filtered, container) match = false
break
} }
} }
containerList = filtered if match {
filtered = append(filtered, container)
}
} }
return containerList, err return filtered, err
} }
// InspectContainer is a test-spy implementation of DockerInterface.InspectContainer. // InspectContainer is a test-spy implementation of DockerInterface.InspectContainer.
@ -481,13 +430,6 @@ func (f *FakeDockerClient) normalSleep(mean, stdDev, cutOffMillis int) {
time.Sleep(delay) time.Sleep(delay)
} }
// GetFakeContainerID generates a fake container id from container name with a hash.
func GetFakeContainerID(name string) string {
hash := fnv.New64a()
hash.Write([]byte(name))
return strconv.FormatUint(hash.Sum64(), 16)
}
// CreateContainer is a test-spy implementation of DockerInterface.CreateContainer. // CreateContainer is a test-spy implementation of DockerInterface.CreateContainer.
// It adds an entry "create" to the internal method call record. // It adds an entry "create" to the internal method call record.
func (f *FakeDockerClient) CreateContainer(c dockertypes.ContainerCreateConfig) (*dockertypes.ContainerCreateResponse, error) { func (f *FakeDockerClient) CreateContainer(c dockertypes.ContainerCreateConfig) (*dockertypes.ContainerCreateResponse, error) {
@ -500,13 +442,13 @@ func (f *FakeDockerClient) CreateContainer(c dockertypes.ContainerCreateConfig)
// This is not a very good fake. We'll just add this container's name to the list. // This is not a very good fake. We'll just add this container's name to the list.
// Docker likes to add a '/', so copy that behavior. // Docker likes to add a '/', so copy that behavior.
name := "/" + c.Name name := "/" + c.Name
id := GetFakeContainerID(name) id := name
f.appendContainerTrace("Created", id) f.appendContainerTrace("Created", name)
// The newest container should be in front, because we assume so in GetPodStatus() // The newest container should be in front, because we assume so in GetPodStatus()
f.RunningContainerList = append([]dockertypes.Container{ f.RunningContainerList = append([]dockertypes.Container{
{ID: id, Names: []string{name}, Image: c.Config.Image, Labels: c.Config.Labels}, {ID: name, Names: []string{name}, Image: c.Config.Image, Labels: c.Config.Labels},
}, f.RunningContainerList...) }, f.RunningContainerList...)
f.ContainerMap[id] = convertFakeContainer(&FakeContainer{ f.ContainerMap[name] = convertFakeContainer(&FakeContainer{
ID: id, Name: name, Config: c.Config, HostConfig: c.HostConfig, CreatedAt: f.Clock.Now()}) ID: id, Name: name, Config: c.Config, HostConfig: c.HostConfig, CreatedAt: f.Clock.Now()})
f.normalSleep(100, 25, 25) f.normalSleep(100, 25, 25)
return &dockertypes.ContainerCreateResponse{ID: id}, nil return &dockertypes.ContainerCreateResponse{ID: id}, nil
@ -618,10 +560,6 @@ func (f *FakeDockerClient) PullImage(image string, auth dockertypes.AuthConfig,
f.Image = &dockertypes.ImageInspect{ f.Image = &dockertypes.ImageInspect{
ID: image, ID: image,
RepoTags: []string{image}, RepoTags: []string{image},
// Image size is required to be non-zero for CRI integration.
VirtualSize: fakeImageSize,
Size: fakeImageSize,
Config: &dockercontainer.Config{},
} }
f.appendPulled(fmt.Sprintf("%s using %s", image, string(authJson))) f.appendPulled(fmt.Sprintf("%s using %s", image, string(authJson)))
} }

View File

@ -160,6 +160,8 @@ func GetHollowKubeletConfig(
c.SerializeImagePulls = true c.SerializeImagePulls = true
c.SystemCgroups = "" c.SystemCgroups = ""
c.ProtectKernelDefaults = false c.ProtectKernelDefaults = false
// TODO: This is a temporary workaround until we fix CRI+kubemark properly.
c.EnableCRI = false
// TODO(mtaufen): Note that PodInfraContainerImage was being set to the empty value before, // TODO(mtaufen): Note that PodInfraContainerImage was being set to the empty value before,
// but this may not have been intentional. (previous code (SimpleKubelet) // but this may not have been intentional. (previous code (SimpleKubelet)