From 6a7db380dee4a3ca70e551e447aa380bf9cad6f9 Mon Sep 17 00:00:00 2001 From: Kevin Klues Date: Mon, 10 Jun 2019 18:20:50 -0700 Subject: [PATCH] Add tests for new containertMap type in the CPUManager --- pkg/kubelet/cm/cpumanager/BUILD | 1 + .../cm/cpumanager/container_map_test.go | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 pkg/kubelet/cm/cpumanager/container_map_test.go diff --git a/pkg/kubelet/cm/cpumanager/BUILD b/pkg/kubelet/cm/cpumanager/BUILD index 6236164c799..9982e73cbb9 100644 --- a/pkg/kubelet/cm/cpumanager/BUILD +++ b/pkg/kubelet/cm/cpumanager/BUILD @@ -31,6 +31,7 @@ go_library( go_test( name = "go_default_test", srcs = [ + "container_map_test.go", "cpu_assignment_test.go", "cpu_manager_test.go", "policy_none_test.go", diff --git a/pkg/kubelet/cm/cpumanager/container_map_test.go b/pkg/kubelet/cm/cpumanager/container_map_test.go new file mode 100644 index 00000000000..6e0f43fb652 --- /dev/null +++ b/pkg/kubelet/cm/cpumanager/container_map_test.go @@ -0,0 +1,76 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cpumanager + +import ( + "testing" + + "k8s.io/api/core/v1" + apimachinery "k8s.io/apimachinery/pkg/types" +) + +func TestContainerMap(t *testing.T) { + testCases := []struct { + podUID string + containerNames []string + containerIDs []string + }{ + { + "fakePodUID", + []string{"fakeContainerName-1", "fakeContainerName-2"}, + []string{"fakeContainerID-1", "fakeContainerName-2"}, + }, + } + + for _, tc := range testCases { + pod := v1.Pod{} + pod.UID = apimachinery.UID(tc.podUID) + + // Build a new containerMap from the testCases, checking proper + // addition, retrieval along the way. + cm := newContainerMap() + for i := range tc.containerNames { + container := v1.Container{Name: tc.containerNames[i]} + + cm.Add(&pod, &container, tc.containerIDs[i]) + containerID, err := cm.Get(&pod, &container) + if err != nil { + t.Errorf("error adding and retrieving container: %v", err) + } + if containerID != tc.containerIDs[i] { + t.Errorf("mismatched containerIDs %v, %v", containerID, tc.containerIDs[i]) + } + } + + // Remove all entries from the containerMap, checking proper removal of + // each along the way. + for i := range tc.containerNames { + container := v1.Container{Name: tc.containerNames[i]} + cm.Remove(tc.containerIDs[i]) + containerID, err := cm.Get(&pod, &container) + if err == nil { + t.Errorf("unexpected retrieval of containerID after removal: %v", containerID) + } + } + + // Verify containerMap now empty. + if len(cm) != 0 { + t.Errorf("unexpected entries still in containerMap: %v", cm) + } + + } +}