...

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

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

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2016 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package cm
    21  
    22  import (
    23  	"path"
    24  	"reflect"
    25  	"testing"
    26  )
    27  
    28  // TestNewCgroupName tests confirms that #68416 is fixed
    29  func TestNewCgroupName(t *testing.T) {
    30  	a := ParseCgroupfsToCgroupName("/a/")
    31  	ab := NewCgroupName(a, "b")
    32  
    33  	expectedAB := CgroupName([]string{"a", "", "b"})
    34  	if !reflect.DeepEqual(ab, expectedAB) {
    35  		t.Errorf("Expected %d%+v; got %d%+v", len(expectedAB), expectedAB, len(ab), ab)
    36  	}
    37  
    38  	abc := NewCgroupName(ab, "c")
    39  
    40  	expectedABC := CgroupName([]string{"a", "", "b", "c"})
    41  	if !reflect.DeepEqual(abc, expectedABC) {
    42  		t.Errorf("Expected %d%+v; got %d%+v", len(expectedABC), expectedABC, len(abc), abc)
    43  	}
    44  
    45  	_ = NewCgroupName(ab, "d")
    46  
    47  	if !reflect.DeepEqual(abc, expectedABC) {
    48  		t.Errorf("Expected %d%+v; got %d%+v", len(expectedABC), expectedABC, len(abc), abc)
    49  	}
    50  }
    51  
    52  func TestCgroupNameToSystemdBasename(t *testing.T) {
    53  	testCases := []struct {
    54  		input    CgroupName
    55  		expected string
    56  	}{
    57  		{
    58  			input:    RootCgroupName,
    59  			expected: "/",
    60  		},
    61  		{
    62  			input:    NewCgroupName(RootCgroupName, "system"),
    63  			expected: "system.slice",
    64  		},
    65  		{
    66  			input:    NewCgroupName(RootCgroupName, "system", "Burstable"),
    67  			expected: "system-Burstable.slice",
    68  		},
    69  		{
    70  			input:    NewCgroupName(RootCgroupName, "Burstable", "pod-123"),
    71  			expected: "Burstable-pod_123.slice",
    72  		},
    73  		{
    74  			input:    NewCgroupName(RootCgroupName, "test", "a", "b"),
    75  			expected: "test-a-b.slice",
    76  		},
    77  		{
    78  			input:    NewCgroupName(RootCgroupName, "test", "a", "b", "Burstable"),
    79  			expected: "test-a-b-Burstable.slice",
    80  		},
    81  		{
    82  			input:    NewCgroupName(RootCgroupName, "Burstable"),
    83  			expected: "Burstable.slice",
    84  		},
    85  		{
    86  			input:    NewCgroupName(RootCgroupName, "BestEffort", "pod-6c1a4e95-6bb6-11e6-bc26-28d2444e470d"),
    87  			expected: "BestEffort-pod_6c1a4e95_6bb6_11e6_bc26_28d2444e470d.slice",
    88  		},
    89  	}
    90  	for _, testCase := range testCases {
    91  		if actual := path.Base(testCase.input.ToSystemd()); actual != testCase.expected {
    92  			t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
    93  		}
    94  	}
    95  }
    96  
    97  func TestCgroupNameToSystemd(t *testing.T) {
    98  	testCases := []struct {
    99  		input    CgroupName
   100  		expected string
   101  	}{
   102  		{
   103  			input:    RootCgroupName,
   104  			expected: "/",
   105  		},
   106  		{
   107  			input:    NewCgroupName(RootCgroupName, "Burstable"),
   108  			expected: "/Burstable.slice",
   109  		},
   110  		{
   111  			input:    NewCgroupName(RootCgroupName, "Burstable", "pod-123"),
   112  			expected: "/Burstable.slice/Burstable-pod_123.slice",
   113  		},
   114  		{
   115  			input:    NewCgroupName(RootCgroupName, "BestEffort", "pod-6c1a4e95-6bb6-11e6-bc26-28d2444e470d"),
   116  			expected: "/BestEffort.slice/BestEffort-pod_6c1a4e95_6bb6_11e6_bc26_28d2444e470d.slice",
   117  		},
   118  		{
   119  			input:    NewCgroupName(RootCgroupName, "kubepods"),
   120  			expected: "/kubepods.slice",
   121  		},
   122  	}
   123  	for _, testCase := range testCases {
   124  		if actual := testCase.input.ToSystemd(); actual != testCase.expected {
   125  			t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
   126  		}
   127  	}
   128  }
   129  
   130  func TestCgroupNameToCgroupfs(t *testing.T) {
   131  	testCases := []struct {
   132  		input    CgroupName
   133  		expected string
   134  	}{
   135  		{
   136  			input:    RootCgroupName,
   137  			expected: "/",
   138  		},
   139  		{
   140  			input:    NewCgroupName(RootCgroupName, "Burstable"),
   141  			expected: "/Burstable",
   142  		},
   143  	}
   144  	for _, testCase := range testCases {
   145  		if actual := testCase.input.ToCgroupfs(); actual != testCase.expected {
   146  			t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
   147  		}
   148  	}
   149  }
   150  
   151  func TestParseSystemdToCgroupName(t *testing.T) {
   152  	testCases := []struct {
   153  		input    string
   154  		expected CgroupName
   155  	}{
   156  		{
   157  			input:    "/test",
   158  			expected: []string{"test"},
   159  		},
   160  		{
   161  			input:    "/test.slice",
   162  			expected: []string{"test"},
   163  		},
   164  	}
   165  
   166  	for _, testCase := range testCases {
   167  		if actual := ParseSystemdToCgroupName(testCase.input); !reflect.DeepEqual(actual, testCase.expected) {
   168  			t.Errorf("Unexpected result, input: %v, expected: %v, actual: %v", testCase.input, testCase.expected, actual)
   169  		}
   170  	}
   171  }
   172  
   173  func TestCpuSharesToCpuWeight(t *testing.T) {
   174  	testCases := []struct {
   175  		cpuShares         uint64
   176  		expectedCpuWeight uint64
   177  	}{
   178  		{
   179  			cpuShares:         2,
   180  			expectedCpuWeight: 1,
   181  		},
   182  		{
   183  			cpuShares:         3,
   184  			expectedCpuWeight: 1,
   185  		},
   186  		{
   187  			cpuShares:         4,
   188  			expectedCpuWeight: 1,
   189  		},
   190  		{
   191  			cpuShares:         28,
   192  			expectedCpuWeight: 1,
   193  		},
   194  		{
   195  			cpuShares:         29,
   196  			expectedCpuWeight: 2,
   197  		},
   198  		{
   199  			cpuShares:         245,
   200  			expectedCpuWeight: 10,
   201  		},
   202  		{
   203  			cpuShares:         262144,
   204  			expectedCpuWeight: 10000,
   205  		},
   206  	}
   207  
   208  	for _, testCase := range testCases {
   209  		if actual := CpuSharesToCpuWeight(testCase.cpuShares); actual != testCase.expectedCpuWeight {
   210  			t.Errorf("cpuShares: %v, expectedCpuWeight: %v, actualCpuWeight: %v",
   211  				testCase.cpuShares, testCase.expectedCpuWeight, actual)
   212  		}
   213  	}
   214  }
   215  
   216  func TestCpuWeightToCpuShares(t *testing.T) {
   217  	testCases := []struct {
   218  		cpuWeight         uint64
   219  		expectedCpuShares uint64
   220  	}{
   221  		{
   222  			cpuWeight:         1,
   223  			expectedCpuShares: 2,
   224  		},
   225  		{
   226  			cpuWeight:         2,
   227  			expectedCpuShares: 28,
   228  		},
   229  		{
   230  			cpuWeight:         3,
   231  			expectedCpuShares: 54,
   232  		},
   233  		{
   234  			cpuWeight:         4,
   235  			expectedCpuShares: 80,
   236  		},
   237  		{
   238  			cpuWeight:         245,
   239  			expectedCpuShares: 6398,
   240  		},
   241  		{
   242  			cpuWeight:         10000,
   243  			expectedCpuShares: 262144,
   244  		},
   245  	}
   246  
   247  	for _, testCase := range testCases {
   248  		if actual := CpuWeightToCpuShares(testCase.cpuWeight); actual != testCase.expectedCpuShares {
   249  			t.Errorf("cpuWeight: %v, expectedCpuShares: %v, actualCpuShares: %v",
   250  				testCase.cpuWeight, testCase.expectedCpuShares, actual)
   251  		}
   252  	}
   253  }
   254  

View as plain text