github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/agent/metric/provisioner/malachite/provisioner_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 malachite
    18  
    19  import (
    20  	"testing"
    21  
    22  	"bou.ke/monkey"
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    26  	"github.com/kubewharf/katalyst-core/pkg/config/agent/metaserver"
    27  	malachitetypes "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/provisioner/malachite/types"
    28  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod"
    29  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    30  	"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
    31  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    32  	utilmetric "github.com/kubewharf/katalyst-core/pkg/util/metric"
    33  )
    34  
    35  func Test_noneExistMetricsProvisioner(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	store := utilmetric.NewMetricStore()
    39  
    40  	var err error
    41  	implement := NewMalachiteMetricsProvisioner(&global.BaseConfiguration{
    42  		ReclaimRelativeRootCgroupPath: "test",
    43  		MalachiteConfiguration: &global.MalachiteConfiguration{
    44  			GeneralRelativeCgroupPaths:  []string{"d1", "d2"},
    45  			OptionalRelativeCgroupPaths: []string{"d3", "d4"},
    46  		},
    47  	}, &metaserver.MetricConfiguration{}, metrics.DummyMetrics{}, &pod.PodFetcherStub{}, store)
    48  
    49  	fakeSystemCompute := &malachitetypes.SystemComputeData{
    50  		CPU: []malachitetypes.CPU{
    51  			{
    52  				Name: "CPU1111",
    53  			},
    54  		},
    55  	}
    56  	fakeSystemMemory := &malachitetypes.SystemMemoryData{
    57  		Numa: []malachitetypes.Numa{
    58  			{},
    59  		},
    60  	}
    61  	fakeSystemIO := &malachitetypes.SystemDiskIoData{
    62  		DiskIo: []malachitetypes.DiskIo{
    63  			{
    64  				PrimaryDeviceID:   8,
    65  				SecondaryDeviceID: 16,
    66  				DeviceName:        "sdb",
    67  				DiskType:          "HDD",
    68  				WBTValue:          1234,
    69  			},
    70  			{
    71  				PrimaryDeviceID:   8,
    72  				SecondaryDeviceID: 24,
    73  				DeviceName:        "sdc",
    74  				DiskType:          "SSD",
    75  				WBTValue:          2234,
    76  			},
    77  			{
    78  				PrimaryDeviceID:   8,
    79  				SecondaryDeviceID: 32,
    80  				DeviceName:        "nvme01",
    81  				DiskType:          "NVME",
    82  				WBTValue:          3234,
    83  			},
    84  		},
    85  	}
    86  	fakeSystemNet := &malachitetypes.SystemNetworkData{
    87  		TCP: malachitetypes.TCP{},
    88  		NetworkCard: []malachitetypes.NetworkCard{
    89  			{
    90  				Name: "eth0",
    91  			},
    92  		},
    93  	}
    94  	fakeCgroupInfoV1 := &malachitetypes.MalachiteCgroupInfo{
    95  		CgroupType: "V1",
    96  		V1: &malachitetypes.MalachiteCgroupV1Info{
    97  			Memory: &malachitetypes.MemoryCgDataV1{},
    98  			Blkio:  &malachitetypes.BlkIOCgDataV1{},
    99  			NetCls: &malachitetypes.NetClsCgData{},
   100  			CpuSet: &malachitetypes.CPUSetCgDataV1{},
   101  			Cpu:    &malachitetypes.CPUCgDataV1{},
   102  		},
   103  	}
   104  	fakeCgroupInfoV2 := &malachitetypes.MalachiteCgroupInfo{
   105  		CgroupType: "V2",
   106  		V2: &malachitetypes.MalachiteCgroupV2Info{
   107  			Memory: &malachitetypes.MemoryCgDataV2{},
   108  			Blkio:  &malachitetypes.BlkIOCgDataV2{},
   109  			NetCls: &malachitetypes.NetClsCgData{},
   110  			CpuSet: &malachitetypes.CPUSetCgDataV2{},
   111  			Cpu:    &malachitetypes.CPUCgDataV2{},
   112  		},
   113  	}
   114  
   115  	implement.(*MalachiteMetricsProvisioner).processSystemComputeData(fakeSystemCompute)
   116  	implement.(*MalachiteMetricsProvisioner).processSystemMemoryData(fakeSystemMemory)
   117  	implement.(*MalachiteMetricsProvisioner).processSystemIOData(fakeSystemIO)
   118  	implement.(*MalachiteMetricsProvisioner).processSystemNumaData(fakeSystemMemory)
   119  	implement.(*MalachiteMetricsProvisioner).processSystemCPUComputeData(fakeSystemCompute)
   120  	implement.(*MalachiteMetricsProvisioner).processSystemNetData(fakeSystemNet)
   121  
   122  	implement.(*MalachiteMetricsProvisioner).processContainerCPUData("pod-not-exist", "container-not-exist", fakeCgroupInfoV1)
   123  	implement.(*MalachiteMetricsProvisioner).processContainerMemoryData("pod-not-exist", "container-not-exist", fakeCgroupInfoV1)
   124  	implement.(*MalachiteMetricsProvisioner).processContainerBlkIOData("pod-not-exist", "container-not-exist", fakeCgroupInfoV1)
   125  	implement.(*MalachiteMetricsProvisioner).processContainerNetData("pod-not-exist", "container-not-exist", fakeCgroupInfoV1)
   126  	implement.(*MalachiteMetricsProvisioner).processContainerPerfData("pod-not-exist", "container-not-exist", fakeCgroupInfoV1)
   127  	implement.(*MalachiteMetricsProvisioner).processContainerPerNumaMemoryData("pod-not-exist", "container-not-exist", fakeCgroupInfoV1)
   128  
   129  	implement.(*MalachiteMetricsProvisioner).processContainerCPUData("pod-not-exist", "container-not-exist", fakeCgroupInfoV2)
   130  	implement.(*MalachiteMetricsProvisioner).processContainerCPUData("pod-not-exist", "container-not-exist", nil)
   131  	implement.(*MalachiteMetricsProvisioner).processContainerMemoryData("pod-not-exist", "container-not-exist", fakeCgroupInfoV2)
   132  	implement.(*MalachiteMetricsProvisioner).processContainerMemoryData("pod-not-exist", "container-not-exist", nil)
   133  	implement.(*MalachiteMetricsProvisioner).processContainerBlkIOData("pod-not-exist", "container-not-exist", fakeCgroupInfoV2)
   134  	implement.(*MalachiteMetricsProvisioner).processContainerBlkIOData("pod-not-exist", "container-not-exist", nil)
   135  	implement.(*MalachiteMetricsProvisioner).processContainerNetData("pod-not-exist", "container-not-exist", fakeCgroupInfoV2)
   136  	implement.(*MalachiteMetricsProvisioner).processContainerNetData("pod-not-exist", "container-not-exist", nil)
   137  	implement.(*MalachiteMetricsProvisioner).processContainerPerfData("pod-not-exist", "container-not-exist", fakeCgroupInfoV2)
   138  	implement.(*MalachiteMetricsProvisioner).processContainerPerfData("pod-not-exist", "container-not-exist", nil)
   139  	implement.(*MalachiteMetricsProvisioner).processContainerPerNumaMemoryData("pod-not-exist", "container-not-exist", fakeCgroupInfoV2)
   140  	implement.(*MalachiteMetricsProvisioner).processContainerPerNumaMemoryData("pod-not-exist", "container-not-exist", nil)
   141  	implement.(*MalachiteMetricsProvisioner).processContainerPerNumaMemoryData("pod-not-exist", "container-not-exist",
   142  		&malachitetypes.MalachiteCgroupInfo{CgroupType: "V2"})
   143  
   144  	_, err = store.GetNodeMetric("test-not-exist")
   145  	if err == nil {
   146  		t.Errorf("GetNode() error = %v, wantErr not nil", err)
   147  		return
   148  	}
   149  
   150  	_, err = store.GetNumaMetric(1, "test-not-exist")
   151  	if err == nil {
   152  		t.Errorf("GetNode() error = %v, wantErr not nil", err)
   153  		return
   154  	}
   155  
   156  	_, err = store.GetDeviceMetric("device-not-exist", "test-not-exist")
   157  	if err == nil {
   158  		t.Errorf("GetNode() error = %v, wantErr not nil", err)
   159  		return
   160  	}
   161  
   162  	_, err = store.GetCPUMetric(1, "test-not-exist")
   163  	if err == nil {
   164  		t.Errorf("GetNode() error = %v, wantErr not nil", err)
   165  		return
   166  	}
   167  
   168  	_, err = store.GetContainerMetric("pod-not-exist", "container-not-exist", "test-not-exist")
   169  	if err == nil {
   170  		t.Errorf("GetNode() error = %v, wantErr not nil", err)
   171  		return
   172  	}
   173  
   174  	_, err = store.GetContainerNumaMetric("pod-not-exist", "container-not-exist", "", "test-not-exist")
   175  	if err == nil {
   176  		t.Errorf("GetContainerNuma() error = %v, wantErr not nil", err)
   177  		return
   178  	}
   179  
   180  	monkey.Patch(general.IsPathExists, func(path string) bool {
   181  		if path == "/sys/fs/cgroup/d3" {
   182  			return true
   183  		}
   184  		return false
   185  	})
   186  	monkey.Patch(common.CheckCgroup2UnifiedMode, func() bool { return true })
   187  	defer monkey.UnpatchAll()
   188  
   189  	paths := implement.(*MalachiteMetricsProvisioner).getCgroupPaths()
   190  	assert.ElementsMatch(t, paths, []string{"d1", "d2", "d3", "/kubepods/burstable", "/kubepods/besteffort", "test"})
   191  }