diff --git a/pkg/kubelet/cm/topologymanager/scope_test.go b/pkg/kubelet/cm/topologymanager/scope_test.go new file mode 100644 index 00000000000..4d6315adb72 --- /dev/null +++ b/pkg/kubelet/cm/topologymanager/scope_test.go @@ -0,0 +1,118 @@ +/* +Copyright 2020 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 topologymanager + +import ( + "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + "reflect" + "testing" +) + +func TestGetAffinity(t *testing.T) { + tcases := []struct { + name string + containerName string + podUID string + expected TopologyHint + }{ + { + name: "case1", + containerName: "nginx", + podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474", + expected: TopologyHint{}, + }, + } + for _, tc := range tcases { + scope := scope{} + actual := scope.GetAffinity(tc.podUID, tc.containerName) + if !reflect.DeepEqual(actual, tc.expected) { + t.Errorf("Expected Affinity in result to be %v, got %v", tc.expected, actual) + } + } +} + +func TestAddContainer(t *testing.T) { + testCases := []struct { + name string + containerID string + podUID types.UID + }{ + { + name: "Case1", + containerID: "nginx", + podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474", + }, + { + name: "Case2", + containerID: "Busy_Box", + podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474", + }, + } + scope := scope{} + scope.podMap = make(map[string]string) + for _, tc := range testCases { + pod := v1.Pod{} + pod.UID = tc.podUID + err := scope.AddContainer(&pod, tc.containerID) + if err != nil { + t.Errorf("Expected error to be nil but got: %v", err) + } + if val, ok := scope.podMap[tc.containerID]; ok { + if reflect.DeepEqual(val, pod.UID) { + t.Errorf("Error occurred") + } + } else { + t.Errorf("Error occurred, Pod not added to podMap") + } + } +} + +func TestRemoveContainer(t *testing.T) { + testCases := []struct { + name string + containerID string + podUID types.UID + }{ + { + name: "Case1", + containerID: "nginx", + podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474", + }, + { + name: "Case2", + containerID: "Busy_Box", + podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474", + }, + } + var len1, len2 int + scope := scope{} + scope.podMap = make(map[string]string) + for _, tc := range testCases { + scope.podMap[tc.containerID] = string(tc.podUID) + len1 = len(scope.podMap) + err := scope.RemoveContainer(tc.containerID) + len2 = len(scope.podMap) + if err != nil { + t.Errorf("Expected error to be nil but got: %v", err) + } + if len1-len2 != 1 { + t.Errorf("Remove Pod resulted in error") + } + } + +} diff --git a/pkg/kubelet/cm/topologymanager/topology_manager_test.go b/pkg/kubelet/cm/topologymanager/topology_manager_test.go index 30c6fb63848..947151baab3 100644 --- a/pkg/kubelet/cm/topologymanager/topology_manager_test.go +++ b/pkg/kubelet/cm/topologymanager/topology_manager_test.go @@ -23,7 +23,6 @@ import ( "testing" "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/types" "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask" "k8s.io/kubernetes/pkg/kubelet/lifecycle" ) @@ -144,29 +143,6 @@ func (m *mockHintProvider) Allocate(pod *v1.Pod, container *v1.Container) error return nil } -func TestGetAffinity(t *testing.T) { - tcases := []struct { - name string - containerName string - podUID string - expected TopologyHint - }{ - { - name: "case1", - containerName: "nginx", - podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474", - expected: TopologyHint{}, - }, - } - for _, tc := range tcases { - mngr := manager{} - actual := mngr.GetAffinity(tc.podUID, tc.containerName) - if !reflect.DeepEqual(actual, tc.expected) { - t.Errorf("Expected Affinity in result to be %v, got %v", tc.expected, actual) - } - } -} - func TestAccumulateProvidersHints(t *testing.T) { tcases := []struct { name string @@ -414,76 +390,6 @@ func TestCalculateAffinity(t *testing.T) { } } -func TestAddContainer(t *testing.T) { - testCases := []struct { - name string - containerID string - podUID types.UID - }{ - { - name: "Case1", - containerID: "nginx", - podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474", - }, - { - name: "Case2", - containerID: "Busy_Box", - podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474", - }, - } - mngr := manager{} - mngr.podMap = make(map[string]string) - for _, tc := range testCases { - pod := v1.Pod{} - pod.UID = tc.podUID - err := mngr.AddContainer(&pod, tc.containerID) - if err != nil { - t.Errorf("Expected error to be nil but got: %v", err) - } - if val, ok := mngr.podMap[tc.containerID]; ok { - if reflect.DeepEqual(val, pod.UID) { - t.Errorf("Error occurred") - } - } else { - t.Errorf("Error occurred, Pod not added to podMap") - } - } -} - -func TestRemoveContainer(t *testing.T) { - testCases := []struct { - name string - containerID string - podUID types.UID - }{ - { - name: "Case1", - containerID: "nginx", - podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474", - }, - { - name: "Case2", - containerID: "Busy_Box", - podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474", - }, - } - var len1, len2 int - mngr := manager{} - mngr.podMap = make(map[string]string) - for _, tc := range testCases { - mngr.podMap[tc.containerID] = string(tc.podUID) - len1 = len(mngr.podMap) - err := mngr.RemoveContainer(tc.containerID) - len2 = len(mngr.podMap) - if err != nil { - t.Errorf("Expected error to be nil but got: %v", err) - } - if len1-len2 != 1 { - t.Errorf("Remove Pod resulted in error") - } - } - -} func TestAddHintProvider(t *testing.T) { tcases := []struct { name string