...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/fake_topology_manager_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/topologymanager

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package topologymanager
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/api/core/v1"
    24  	"k8s.io/kubernetes/pkg/kubelet/lifecycle"
    25  )
    26  
    27  func TestNewFakeManager(t *testing.T) {
    28  	fm := NewFakeManager()
    29  
    30  	if _, ok := fm.(Manager); !ok {
    31  		t.Errorf("Result is not Manager type")
    32  
    33  	}
    34  }
    35  
    36  func TestFakeGetAffinity(t *testing.T) {
    37  	tcases := []struct {
    38  		name          string
    39  		containerName string
    40  		podUID        string
    41  		expected      TopologyHint
    42  	}{
    43  		{
    44  			name:          "Case1",
    45  			containerName: "nginx",
    46  			podUID:        "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
    47  			expected:      TopologyHint{},
    48  		},
    49  	}
    50  	for _, tc := range tcases {
    51  		fm := fakeManager{}
    52  		actual := fm.GetAffinity(tc.podUID, tc.containerName)
    53  		if !reflect.DeepEqual(actual, tc.expected) {
    54  			t.Errorf("Expected Affinity in result to be %v, got %v", tc.expected, actual)
    55  		}
    56  	}
    57  }
    58  
    59  func TestFakeRemoveContainer(t *testing.T) {
    60  	testCases := []struct {
    61  		name        string
    62  		containerID string
    63  		podUID      string
    64  	}{
    65  		{
    66  			name:        "Case1",
    67  			containerID: "nginx",
    68  			podUID:      "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
    69  		},
    70  		{
    71  			name:        "Case2",
    72  			containerID: "Busy_Box",
    73  			podUID:      "b3ee37fc-39a5-11e9-bcb1-a4bf01040474",
    74  		},
    75  	}
    76  	fm := fakeManager{}
    77  	for _, tc := range testCases {
    78  		err := fm.RemoveContainer(tc.containerID)
    79  		if err != nil {
    80  			t.Errorf("Expected error to be nil but got: %v", err)
    81  		}
    82  
    83  	}
    84  
    85  }
    86  
    87  func TestFakeAdmit(t *testing.T) {
    88  	tcases := []struct {
    89  		name     string
    90  		result   lifecycle.PodAdmitResult
    91  		qosClass v1.PodQOSClass
    92  		expected bool
    93  	}{
    94  		{
    95  			name:     "QOSClass set as Guaranteed",
    96  			result:   lifecycle.PodAdmitResult{},
    97  			qosClass: v1.PodQOSGuaranteed,
    98  			expected: true,
    99  		},
   100  		{
   101  			name:     "QOSClass set as Burstable",
   102  			result:   lifecycle.PodAdmitResult{},
   103  			qosClass: v1.PodQOSBurstable,
   104  			expected: true,
   105  		},
   106  		{
   107  			name:     "QOSClass set as BestEffort",
   108  			result:   lifecycle.PodAdmitResult{},
   109  			qosClass: v1.PodQOSBestEffort,
   110  			expected: true,
   111  		},
   112  	}
   113  	fm := fakeManager{}
   114  	for _, tc := range tcases {
   115  		podAttr := lifecycle.PodAdmitAttributes{}
   116  		pod := v1.Pod{}
   117  		pod.Status.QOSClass = tc.qosClass
   118  		podAttr.Pod = &pod
   119  		actual := fm.Admit(&podAttr)
   120  		if reflect.DeepEqual(actual, tc.result) {
   121  			t.Errorf("Error occurred, expected Admit in result to be %v got %v", tc.result, actual.Admit)
   122  		}
   123  	}
   124  }
   125  

View as plain text