github.com/gocrane/crane@v0.11.0/pkg/utils/cgroup_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	v1 "k8s.io/api/core/v1"
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/types"
    11  )
    12  
    13  func TestCgroupName_ToCgroupfs(t *testing.T) {
    14  	want := "/memory/memory.failcnt"
    15  	got := CgroupName{"memory", "memory.failcnt"}.ToCgroupfs()
    16  	if got != want {
    17  		t.Errorf("CgroupName.ToCgroupfs() = %v, want %v", got, want)
    18  	}
    19  }
    20  
    21  func TestGetCgroupPath(t *testing.T) {
    22  	tests := []struct {
    23  		name         string
    24  		p            *v1.Pod
    25  		cgroupDriver string
    26  		want         string
    27  	}{
    28  		{
    29  			name:         "base",
    30  			p:            &v1.Pod{},
    31  			cgroupDriver: "systemd",
    32  			want:         "/",
    33  		},
    34  		{
    35  			name:         "cgroupDriver is cgroupfs",
    36  			p:            &v1.Pod{},
    37  			cgroupDriver: "cgroupfs",
    38  			want:         "/",
    39  		},
    40  		{
    41  			name:         "cgroupDriver is default",
    42  			p:            &v1.Pod{},
    43  			cgroupDriver: "default",
    44  			want:         "",
    45  		},
    46  	}
    47  	for _, tt := range tests {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			if got := GetCgroupPath(tt.p, tt.cgroupDriver); got != tt.want {
    50  				t.Errorf("GetCgroupPath() = %v, want %v", got, tt.want)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestGetCgroupName(t *testing.T) {
    57  	tests := []struct {
    58  		name string
    59  		p    *v1.Pod
    60  		want CgroupName
    61  	}{
    62  		{
    63  			name: "base",
    64  			p:    &v1.Pod{},
    65  			want: CgroupName{},
    66  		},
    67  		{
    68  			name: "status is equal to PodQOSGuaranteed",
    69  			p: &v1.Pod{
    70  				ObjectMeta: metav1.ObjectMeta{
    71  					UID: types.UID("fake-uid"),
    72  				},
    73  				Status: v1.PodStatus{QOSClass: v1.PodQOSGuaranteed},
    74  			},
    75  			want: NewCgroupName(RootCgroupName, CgroupKubePods, GetPodCgroupNameSuffix(types.UID("fake-uid"))),
    76  		},
    77  		{
    78  			name: "status is equal to PodQOSBurstable",
    79  			p: &v1.Pod{
    80  				ObjectMeta: metav1.ObjectMeta{
    81  					UID: types.UID("fake-uid"),
    82  				},
    83  				Status: v1.PodStatus{QOSClass: v1.PodQOSBurstable},
    84  			},
    85  			want: NewCgroupName(RootCgroupName, CgroupKubePods, strings.ToLower(string(v1.PodQOSBurstable)), GetPodCgroupNameSuffix(types.UID("fake-uid"))),
    86  		},
    87  		{
    88  			name: "status is equal to PodQOSBestEffort",
    89  			p: &v1.Pod{
    90  				ObjectMeta: metav1.ObjectMeta{
    91  					UID: types.UID("fake-uid"),
    92  				},
    93  				Status: v1.PodStatus{QOSClass: v1.PodQOSBestEffort},
    94  			},
    95  			want: NewCgroupName(RootCgroupName, CgroupKubePods, strings.ToLower(string(v1.PodQOSBestEffort)), GetPodCgroupNameSuffix(types.UID("fake-uid"))),
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			t.Log(tt.want)
   101  			if got := GetCgroupName(tt.p); !reflect.DeepEqual(got, tt.want) {
   102  				t.Errorf("GetCgroupName() = %v, want %v", got, tt.want)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestGetPodCgroupNameSuffix(t *testing.T) {
   109  	want := "podfake-uid"
   110  	got := GetPodCgroupNameSuffix(types.UID("fake-uid"))
   111  	if got != want {
   112  		t.Errorf("GetPodCgroupNameSuffix() = %v, want %v", got, want)
   113  	}
   114  }
   115  
   116  func TestNewCgroupName(t *testing.T) {
   117  	want := CgroupName{"cpu", "memory"}
   118  	got := NewCgroupName(CgroupName{"cpu"}, []string{"memory"}...)
   119  	if !reflect.DeepEqual(got, want) {
   120  		t.Errorf("NewCgroupName() = %v, want %v", got, want)
   121  	}
   122  }
   123  
   124  func TestCgroupName_ToSystemd(t *testing.T) {
   125  	tests := []struct {
   126  		name       string
   127  		cgroupName CgroupName
   128  		want       string
   129  	}{
   130  		{
   131  			name:       "base",
   132  			cgroupName: GetCgroupName(&v1.Pod{}),
   133  			want:       "/",
   134  		},
   135  		{
   136  			name: "len(slice) < len(suffix)",
   137  			cgroupName: GetCgroupName(&v1.Pod{
   138  				ObjectMeta: metav1.ObjectMeta{
   139  					UID: types.UID("fake-uid"),
   140  				},
   141  				Status: v1.PodStatus{QOSClass: v1.PodQOSGuaranteed},
   142  			}), //[kubepods podfake-uid]
   143  			want: "/kubepods.slice/kubepods-podfake_uid.slice",
   144  		},
   145  	}
   146  	for _, tt := range tests {
   147  		t.Run(tt.name, func(t *testing.T) {
   148  			if got := tt.cgroupName.ToSystemd(); got != tt.want {
   149  				t.Errorf("CgroupName.ToSystemd() = %v, want %v", got, tt.want)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func Test_escapeSystemdCgroupName(t *testing.T) {
   156  	want := "cpu_memory"
   157  	got := escapeSystemdCgroupName("cpu-memory")
   158  	if got != want {
   159  		t.Errorf("escapeSystemdCgroupName() = %v, want %v", got, want)
   160  	}
   161  }
   162  
   163  func TestExpandSlice(t *testing.T) {
   164  	tests := []struct {
   165  		name    string
   166  		slice   string
   167  		want    string
   168  		wantErr bool
   169  	}{
   170  		{
   171  			name:    "base",
   172  			slice:   "",
   173  			want:    "",
   174  			wantErr: true,
   175  		},
   176  		{
   177  			name:    "slice contains '/'",
   178  			slice:   "/.slice",
   179  			want:    "",
   180  			wantErr: true,
   181  		},
   182  		{
   183  			name:    "slice contains '-'",
   184  			slice:   "-.slice",
   185  			want:    "/",
   186  			wantErr: false,
   187  		},
   188  		{
   189  			name:    "slice contains many '-'",
   190  			slice:   "--.slice",
   191  			want:    "",
   192  			wantErr: true,
   193  		},
   194  		{
   195  			name:    "slice contains some contents and many '-'",
   196  			slice:   "cpu-memory.slice",
   197  			want:    "/cpu.slice/cpu-memory.slice",
   198  			wantErr: false,
   199  		},
   200  	}
   201  	for _, tt := range tests {
   202  		t.Run(tt.name, func(t *testing.T) {
   203  			got, err := ExpandSlice(tt.slice)
   204  			if (err != nil) != tt.wantErr {
   205  				t.Errorf("ExpandSlice() error = %v, wantErr %v", err, tt.wantErr)
   206  				return
   207  			}
   208  			if got != tt.want {
   209  				t.Errorf("ExpandSlice() = %v, want %v", got, tt.want)
   210  			}
   211  		})
   212  	}
   213  }