From 517301df21d9c30a34cd2e4f8e281cdfbc184b71 Mon Sep 17 00:00:00 2001 From: Shyam Jeedigunta Date: Tue, 13 Feb 2018 21:49:42 +0100 Subject: [PATCH] Fake docker-client assigns random IPs to containers --- pkg/kubelet/dockershim/docker_sandbox_test.go | 17 +++++++---------- pkg/kubelet/dockershim/docker_service_test.go | 3 ++- pkg/kubelet/dockershim/libdocker/fake_client.go | 12 +++++++++++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pkg/kubelet/dockershim/docker_sandbox_test.go b/pkg/kubelet/dockershim/docker_sandbox_test.go index a9c55a1104b..e1398d9a9fb 100644 --- a/pkg/kubelet/dockershim/docker_sandbox_test.go +++ b/pkg/kubelet/dockershim/docker_sandbox_test.go @@ -19,6 +19,7 @@ package dockershim import ( "errors" "fmt" + "math/rand" "net" "testing" "time" @@ -96,10 +97,8 @@ func TestSandboxStatus(t *testing.T) { labels := map[string]string{"label": "foobar1"} annotations := map[string]string{"annotation": "abc"} config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations) - - // TODO: The following variables depend on the internal - // implementation of FakeDockerClient, and should be fixed. - fakeIP := "2.3.4.5" + r := rand.New(rand.NewSource(0)).Uint32() + podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r)) state := runtimeapi.PodSandboxState_SANDBOX_READY ct := int64(0) @@ -107,7 +106,7 @@ func TestSandboxStatus(t *testing.T) { State: state, CreatedAt: ct, Metadata: config.Metadata, - Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP}, + Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP}, Linux: &runtimeapi.LinuxPodSandboxStatus{ Namespaces: &runtimeapi.Namespace{ Options: &runtimeapi.NamespaceOption{ @@ -160,10 +159,8 @@ func TestSandboxStatus(t *testing.T) { func TestSandboxStatusAfterRestart(t *testing.T) { ds, _, fClock := newTestDockerService() config := makeSandboxConfig("foo", "bar", "1", 0) - - // TODO: The following variables depend on the internal - // implementation of FakeDockerClient, and should be fixed. - fakeIP := "2.3.4.5" + r := rand.New(rand.NewSource(0)).Uint32() + podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r)) state := runtimeapi.PodSandboxState_SANDBOX_READY ct := int64(0) @@ -171,7 +168,7 @@ func TestSandboxStatusAfterRestart(t *testing.T) { State: state, CreatedAt: ct, Metadata: config.Metadata, - Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP}, + Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP}, Linux: &runtimeapi.LinuxPodSandboxStatus{ Namespaces: &runtimeapi.Namespace{ Options: &runtimeapi.NamespaceOption{ diff --git a/pkg/kubelet/dockershim/docker_service_test.go b/pkg/kubelet/dockershim/docker_service_test.go index 750d3f42591..9bbf4cb8f65 100644 --- a/pkg/kubelet/dockershim/docker_service_test.go +++ b/pkg/kubelet/dockershim/docker_service_test.go @@ -18,6 +18,7 @@ package dockershim import ( "errors" + "math/rand" "testing" "time" @@ -44,7 +45,7 @@ func newTestNetworkPlugin(t *testing.T) *nettest.MockNetworkPlugin { func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) { fakeClock := clock.NewFakeClock(time.Time{}) - c := libdocker.NewFakeDockerClient().WithClock(fakeClock).WithVersion("1.11.2", "1.23") + c := libdocker.NewFakeDockerClient().WithClock(fakeClock).WithVersion("1.11.2", "1.23").WithRandSource(rand.NewSource(0)) pm := network.NewPluginManager(&network.NoopNetworkPlugin{}) return &dockerService{ client: c, diff --git a/pkg/kubelet/dockershim/libdocker/fake_client.go b/pkg/kubelet/dockershim/libdocker/fake_client.go index 21e27eba8fc..54dafcaf767 100644 --- a/pkg/kubelet/dockershim/libdocker/fake_client.go +++ b/pkg/kubelet/dockershim/libdocker/fake_client.go @@ -61,6 +61,7 @@ type FakeDockerClient struct { called []calledDetail pulled []string EnableTrace bool + RandGenerator *rand.Rand // Created, Started, Stopped and Removed all contain container docker ID Created []string @@ -99,6 +100,7 @@ func NewFakeDockerClient() *FakeDockerClient { EnableTrace: true, ImageInspects: make(map[string]*dockertypes.ImageInspect), ImageIDsNeedingAuth: make(map[string]dockertypes.AuthConfig), + RandGenerator: rand.New(rand.NewSource(time.Now().UnixNano())), } } @@ -123,6 +125,13 @@ func (f *FakeDockerClient) WithTraceDisabled() *FakeDockerClient { return f } +func (f *FakeDockerClient) WithRandSource(source rand.Source) *FakeDockerClient { + f.Lock() + defer f.Unlock() + f.RandGenerator = rand.New(source) + return f +} + func (f *FakeDockerClient) appendCalled(callDetail calledDetail) { if f.EnableTrace { f.called = append(f.called, callDetail) @@ -597,7 +606,8 @@ func (f *FakeDockerClient) StartContainer(id string) error { container.State.Running = true container.State.Pid = os.Getpid() container.State.StartedAt = dockerTimestampToString(timestamp) - container.NetworkSettings.IPAddress = "2.3.4.5" + r := f.RandGenerator.Uint32() + container.NetworkSettings.IPAddress = fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r)) f.ContainerMap[id] = container f.updateContainerStatus(id, StatusRunningPrefix) f.normalSleep(200, 50, 50)