kubelet: Move TestPortForwardNoSuchContainer() to dockertools package.

Also refactor TestPortForward() to be neutral to container runtime.
This commit is contained in:
Yifan Gu 2015-06-05 14:10:45 -07:00
parent f83d5356d7
commit 6ddffdd736
5 changed files with 47 additions and 55 deletions

View File

@ -19,6 +19,7 @@ package container
import ( import (
"fmt" "fmt"
"io" "io"
"reflect"
"strings" "strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -282,6 +283,11 @@ func (p *Pod) FindContainerByName(containerName string) *Container {
return nil return nil
} }
// IsEmpty returns true if the pod is empty.
func (p *Pod) IsEmpty() bool {
return reflect.DeepEqual(p, &Pod{})
}
// GetPodFullName returns a name that uniquely identifies a pod. // GetPodFullName returns a name that uniquely identifies a pod.
func GetPodFullName(pod *api.Pod) string { func GetPodFullName(pod *api.Pod) string {
// Use underscore as the delimiter because it is not allowed in pod name // Use underscore as the delimiter because it is not allowed in pod name

View File

@ -1006,6 +1006,10 @@ func (dm *DockerManager) ExecInContainer(containerId string, cmd []string, stdin
return dm.execHandler.ExecInContainer(dm.client, container, cmd, stdin, stdout, stderr, tty) return dm.execHandler.ExecInContainer(dm.client, container, cmd, stdin, stdout, stderr, tty)
} }
func noPodInfraContainerError(podName, podNamespace string) error {
return fmt.Errorf("cannot find pod infra container in pod %q", kubecontainer.BuildPodFullName(podName, podNamespace))
}
// PortForward executes socat in the pod's network namespace and copies // PortForward executes socat in the pod's network namespace and copies
// data between stream (representing the user's local connection on their // data between stream (representing the user's local connection on their
// computer) and the specified port in the container. // computer) and the specified port in the container.
@ -1017,7 +1021,7 @@ func (dm *DockerManager) ExecInContainer(containerId string, cmd []string, stdin
func (dm *DockerManager) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error { func (dm *DockerManager) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error {
podInfraContainer := pod.FindContainerByName(PodInfraContainerName) podInfraContainer := pod.FindContainerByName(PodInfraContainerName)
if podInfraContainer == nil { if podInfraContainer == nil {
return fmt.Errorf("cannot find pod infra container in pod %q", kubecontainer.BuildPodFullName(pod.Name, pod.Namespace)) return noPodInfraContainerError(pod.Name, pod.Namespace)
} }
container, err := dm.client.InspectContainer(string(podInfraContainer.ID)) container, err := dm.client.InspectContainer(string(podInfraContainer.ID))
if err != nil { if err != nil {

View File

@ -1914,3 +1914,26 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
t.Errorf("Wrong stopped container, expected: bar, get: %q", dockerName.ContainerName) t.Errorf("Wrong stopped container, expected: bar, get: %q", dockerName.ContainerName)
} }
} }
func TestPortForwardNoSuchContainer(t *testing.T) {
dm, _ := newTestDockerManager()
podName, podNamespace := "podName", "podNamespace"
err := dm.PortForward(
&kubecontainer.Pod{
ID: "podID",
Name: podName,
Namespace: podNamespace,
Containers: nil,
},
5000,
nil,
)
if err == nil {
t.Fatal("unexpected non-error")
}
expectedErr := noPodInfraContainerError(podName, podNamespace)
if !reflect.DeepEqual(err, expectedErr) {
t.Fatalf("expected %v, but saw %v", expectedErr, err)
}
}

View File

@ -2235,6 +2235,9 @@ func (kl *Kubelet) PortForward(podFullName string, podUID types.UID, port uint16
return err return err
} }
pod := kubecontainer.Pods(pods).FindPod(podFullName, podUID) pod := kubecontainer.Pods(pods).FindPod(podFullName, podUID)
if pod.IsEmpty() {
return fmt.Errorf("pod not found (%q)", podFullName)
}
return kl.runner.PortForward(&pod, port, stream) return kl.runner.PortForward(&pod, port, stream)
} }

View File

@ -934,6 +934,7 @@ func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
type fakeContainerCommandRunner struct { type fakeContainerCommandRunner struct {
Cmd []string Cmd []string
ID string ID string
PodID types.UID
E error E error
Stdin io.Reader Stdin io.Reader
Stdout io.WriteCloser Stdout io.WriteCloser
@ -960,11 +961,7 @@ func (f *fakeContainerCommandRunner) ExecInContainer(id string, cmd []string, in
} }
func (f *fakeContainerCommandRunner) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error { func (f *fakeContainerCommandRunner) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error {
podInfraContainer := pod.FindContainerByName(dockertools.PodInfraContainerName) f.PodID = pod.ID
if podInfraContainer == nil {
return fmt.Errorf("cannot find pod infra container in pod %q", kubecontainer.BuildPodFullName(pod.Name, pod.Namespace))
}
f.ID = string(podInfraContainer.ID)
f.Port = port f.Port = port
f.Stream = stream f.Stream = stream
return nil return nil
@ -2123,73 +2120,32 @@ func TestPortForwardNoSuchPod(t *testing.T) {
} }
} }
func TestPortForwardNoSuchContainer(t *testing.T) { func TestPortForward(t *testing.T) {
testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet := newTestKubeletWithFakeRuntime(t)
kubelet := testKubelet.kubelet kubelet := testKubelet.kubelet
fakeRuntime := testKubelet.fakeRuntime fakeRuntime := testKubelet.fakeRuntime
fakeCommandRunner := fakeContainerCommandRunner{}
kubelet.runner = &fakeCommandRunner
podName := "podFoo" podName := "podFoo"
podNamespace := "nsFoo" podNamespace := "nsFoo"
var port uint16 = 5000 podID := types.UID("12345678")
fakeRuntime.PodList = []*kubecontainer.Pod{ fakeRuntime.PodList = []*kubecontainer.Pod{
{ {
ID: "12345678", ID: podID,
Name: podName, Name: podName,
Namespace: podNamespace, Namespace: podNamespace,
Containers: []*kubecontainer.Container{ Containers: []*kubecontainer.Container{
{Name: "bar", {
ID: "barID"}, Name: "foo",
ID: "containerFoo",
},
}, },
}, },
} }
err := kubelet.PortForward(
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
UID: "12345678",
Name: podName,
Namespace: podNamespace,
}}),
"",
port,
nil,
)
if err == nil {
t.Fatal("unexpected non-error")
}
if fakeCommandRunner.ID != "" {
t.Fatal("unexpected invocation of runner.PortForward")
}
}
func TestPortForward(t *testing.T) {
fakeCommandRunner := fakeContainerCommandRunner{} fakeCommandRunner := fakeContainerCommandRunner{}
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
fakeDocker := testKubelet.fakeDocker
kubelet.runner = &fakeCommandRunner kubelet.runner = &fakeCommandRunner
podName := "podFoo"
podNamespace := "nsFoo"
containerID := "containerFoo"
var port uint16 = 5000 var port uint16 = 5000
stream := &fakeReadWriteCloser{} stream := &fakeReadWriteCloser{}
infraContainerID := "infra"
fakeDocker.ContainerList = []docker.APIContainers{
{
ID: infraContainerID,
Names: []string{"/k8s_POD" + "_" + podName + "_" + podNamespace + "_12345678_42"},
},
{
ID: containerID,
Names: []string{"/k8s_" + containerID + "_" + podName + "_" + podNamespace + "_12345678_42"},
},
}
err := kubelet.PortForward( err := kubelet.PortForward(
kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{ kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{
UID: "12345678", UID: "12345678",
@ -2203,7 +2159,7 @@ func TestPortForward(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
if e, a := infraContainerID, fakeCommandRunner.ID; e != a { if e, a := podID, fakeCommandRunner.PodID; e != a {
t.Fatalf("container id: expected %q, got %q", e, a) t.Fatalf("container id: expected %q, got %q", e, a)
} }
if e, a := port, fakeCommandRunner.Port; e != a { if e, a := port, fakeCommandRunner.Port; e != a {