mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 13:50:01 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			126 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // +build linux
 | |
| 
 | |
| /*
 | |
| Copyright 2016 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 cm
 | |
| 
 | |
| import (
 | |
| 	"strings"
 | |
| 	"testing"
 | |
| 
 | |
| 	"k8s.io/api/core/v1"
 | |
| 	"k8s.io/apimachinery/pkg/types"
 | |
| )
 | |
| 
 | |
| func TestIsCgroupPod(t *testing.T) {
 | |
| 	qosContainersInfo := QOSContainersInfo{
 | |
| 		Guaranteed: RootCgroupName,
 | |
| 		Burstable:  NewCgroupName(RootCgroupName, strings.ToLower(string(v1.PodQOSBurstable))),
 | |
| 		BestEffort: NewCgroupName(RootCgroupName, strings.ToLower(string(v1.PodQOSBestEffort))),
 | |
| 	}
 | |
| 	podUID := types.UID("123")
 | |
| 	testCases := []struct {
 | |
| 		input          CgroupName
 | |
| 		expectedResult bool
 | |
| 		expectedUID    types.UID
 | |
| 	}{
 | |
| 		{
 | |
| 			input:          RootCgroupName,
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.Guaranteed),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.Guaranteed, GetPodCgroupNameSuffix(podUID)),
 | |
| 			expectedResult: true,
 | |
| 			expectedUID:    podUID,
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.Guaranteed, GetPodCgroupNameSuffix(podUID), "container.scope"),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.Burstable),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.Burstable, GetPodCgroupNameSuffix(podUID)),
 | |
| 			expectedResult: true,
 | |
| 			expectedUID:    podUID,
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.Burstable, GetPodCgroupNameSuffix(podUID), "container.scope"),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.BestEffort),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.BestEffort, GetPodCgroupNameSuffix(podUID)),
 | |
| 			expectedResult: true,
 | |
| 			expectedUID:    podUID,
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(qosContainersInfo.BestEffort, GetPodCgroupNameSuffix(podUID), "container.scope"),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(RootCgroupName, "system"),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 		{
 | |
| 			input:          NewCgroupName(RootCgroupName, "system", "kubelet"),
 | |
| 			expectedResult: false,
 | |
| 			expectedUID:    types.UID(""),
 | |
| 		},
 | |
| 	}
 | |
| 	for _, cgroupDriver := range []string{"cgroupfs", "systemd"} {
 | |
| 		pcm := &podContainerManagerImpl{
 | |
| 			cgroupManager:     NewCgroupManager(nil, cgroupDriver),
 | |
| 			enforceCPULimits:  true,
 | |
| 			qosContainersInfo: qosContainersInfo,
 | |
| 		}
 | |
| 		for _, testCase := range testCases {
 | |
| 			// give the right cgroup structure based on driver
 | |
| 			cgroupfs := testCase.input.ToCgroupfs()
 | |
| 			if cgroupDriver == "systemd" {
 | |
| 				cgroupfs = testCase.input.ToSystemd()
 | |
| 			}
 | |
| 			// check if this is a pod or not with the literal cgroupfs input
 | |
| 			result, resultUID := pcm.IsPodCgroup(cgroupfs)
 | |
| 			if result != testCase.expectedResult {
 | |
| 				t.Errorf("Unexpected result for driver: %v, input: %v, expected: %v, actual: %v", cgroupDriver, testCase.input, testCase.expectedResult, result)
 | |
| 			}
 | |
| 			if resultUID != testCase.expectedUID {
 | |
| 				t.Errorf("Unexpected result for driver: %v, input: %v, expected: %v, actual: %v", cgroupDriver, testCase.input, testCase.expectedUID, resultUID)
 | |
| 			}
 | |
| 
 | |
| 		}
 | |
| 	}
 | |
| }
 |