Compare containers by name, not by pointer equality.

pull/6/head
Daniel Smith 2014-12-09 17:06:39 -08:00
parent 4e7808089e
commit ba62943b8e
2 changed files with 37 additions and 1 deletions

View File

@ -425,7 +425,7 @@ func (kl *Kubelet) runHandler(podFullName, uuid string, container *api.Container
func fieldPath(pod *api.BoundPod, container *api.Container) (string, error) {
for i := range pod.Spec.Containers {
here := &pod.Spec.Containers[i]
if here == container {
if here.Name == container.Name {
return fmt.Sprintf("spec.containers[%d]", i), nil
}
}

View File

@ -901,6 +901,42 @@ func TestCheckHostPortConflicts(t *testing.T) {
}
}
func TestFieldPath(t *testing.T) {
pod := &api.BoundPod{Spec: api.PodSpec{Containers: []api.Container{
{Name: "foo"},
{Name: "bar"},
{Name: "baz"},
}}}
table := map[string]struct {
pod *api.BoundPod
container *api.Container
path string
success bool
}{
"basic": {pod, &api.Container{Name: "foo"}, "spec.containers[0]", true},
"basic2": {pod, &api.Container{Name: "baz"}, "spec.containers[2]", true},
"basicSamePointer": {pod, &pod.Spec.Containers[0], "spec.containers[0]", true},
"missing": {pod, &api.Container{Name: "qux"}, "", false},
}
for name, item := range table {
res, err := fieldPath(item.pod, item.container)
if item.success == false {
if err == nil {
t.Errorf("%v: unexpected non-error", name)
}
continue
}
if err != nil {
t.Errorf("%v: unexpected error: %v", name, err)
continue
}
if e, a := item.path, res; e != a {
t.Errorf("%v: wanted %v, got %v", name, e, a)
}
}
}
type mockCadvisorClient struct {
mock.Mock
}