k8s.io/kubernetes@v1.29.3/pkg/kubelet/winstats/winstats_test.go (about) 1 //go:build windows 2 // +build windows 3 4 /* 5 Copyright 2017 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 winstats 21 22 import ( 23 "os" 24 "testing" 25 "time" 26 27 cadvisorapi "github.com/google/cadvisor/info/v1" 28 cadvisorapiv2 "github.com/google/cadvisor/info/v2" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 var timeStamp = time.Now() 33 34 type fakeWinNodeStatsClient struct{} 35 36 func (f fakeWinNodeStatsClient) startMonitoring() error { 37 return nil 38 } 39 40 func (f fakeWinNodeStatsClient) getNodeMetrics() (nodeMetrics, error) { 41 return nodeMetrics{ 42 cpuUsageCoreNanoSeconds: 123, 43 cpuUsageNanoCores: 23, 44 memoryPrivWorkingSetBytes: 1234, 45 memoryCommittedBytes: 12345, 46 timeStamp: timeStamp, 47 }, nil 48 } 49 50 func (f fakeWinNodeStatsClient) getNodeInfo() nodeInfo { 51 return nodeInfo{ 52 kernelVersion: "v42", 53 memoryPhysicalCapacityBytes: 1.6e+10, 54 } 55 } 56 func (f fakeWinNodeStatsClient) getMachineInfo() (*cadvisorapi.MachineInfo, error) { 57 return &cadvisorapi.MachineInfo{ 58 NumCores: 4, 59 MemoryCapacity: 1.6e+10, 60 MachineID: "somehostname", 61 SystemUUID: "E6C8AC43-582B-3575-4E1F-6DA170888906", 62 }, nil 63 } 64 65 func (f fakeWinNodeStatsClient) getVersionInfo() (*cadvisorapi.VersionInfo, error) { 66 return &cadvisorapi.VersionInfo{ 67 KernelVersion: "v42", 68 }, nil 69 } 70 71 func TestWinContainerInfos(t *testing.T) { 72 c := getClient(t) 73 74 actualRootInfos, err := c.WinContainerInfos() 75 assert.NoError(t, err) 76 77 var stats []*cadvisorapiv2.ContainerStats 78 stats = append(stats, &cadvisorapiv2.ContainerStats{ 79 Timestamp: timeStamp, 80 Cpu: &cadvisorapi.CpuStats{ 81 Usage: cadvisorapi.CpuUsage{ 82 Total: 123, 83 }, 84 }, 85 CpuInst: &cadvisorapiv2.CpuInstStats{ 86 Usage: cadvisorapiv2.CpuInstUsage{ 87 Total: 23, 88 }, 89 }, 90 Memory: &cadvisorapi.MemoryStats{ 91 WorkingSet: 1234, 92 Usage: 12345, 93 }, 94 }) 95 infos := make(map[string]cadvisorapiv2.ContainerInfo) 96 infos["/"] = cadvisorapiv2.ContainerInfo{ 97 Spec: cadvisorapiv2.ContainerSpec{ 98 HasCpu: true, 99 HasMemory: true, 100 HasNetwork: true, 101 Memory: cadvisorapiv2.MemorySpec{ 102 Limit: 1.6e+10, 103 }, 104 }, 105 Stats: stats, 106 } 107 108 assert.Equal(t, len(actualRootInfos), len(infos)) 109 assert.Equal(t, actualRootInfos["/"].Spec, infos["/"].Spec) 110 assert.Equal(t, len(actualRootInfos["/"].Stats), len(infos["/"].Stats)) 111 assert.Equal(t, actualRootInfos["/"].Stats[0].Cpu, infos["/"].Stats[0].Cpu) 112 assert.Equal(t, actualRootInfos["/"].Stats[0].CpuInst, infos["/"].Stats[0].CpuInst) 113 assert.Equal(t, actualRootInfos["/"].Stats[0].Memory, infos["/"].Stats[0].Memory) 114 } 115 116 func TestWinMachineInfo(t *testing.T) { 117 c := getClient(t) 118 119 machineInfo, err := c.WinMachineInfo() 120 assert.NoError(t, err) 121 assert.Equal(t, machineInfo, &cadvisorapi.MachineInfo{ 122 NumCores: 4, 123 MemoryCapacity: 1.6e+10, 124 MachineID: "somehostname", 125 SystemUUID: "E6C8AC43-582B-3575-4E1F-6DA170888906"}) 126 } 127 128 func TestWinVersionInfo(t *testing.T) { 129 c := getClient(t) 130 131 versionInfo, err := c.WinVersionInfo() 132 assert.NoError(t, err) 133 assert.Equal(t, versionInfo, &cadvisorapi.VersionInfo{ 134 KernelVersion: "v42"}) 135 } 136 137 func TestGetDirFsInfo(t *testing.T) { 138 c := getClient(t) 139 140 // Try with a non-existent path. 141 _, err := c.GetDirFsInfo("foo/lish") 142 expectedErrMsg := "The system cannot find the path specified." 143 if err == nil || err.Error() != expectedErrMsg { 144 t.Fatalf("expected error message `%s` but got `%v`", expectedErrMsg, err) 145 } 146 147 dir, err := os.MkdirTemp("", "fsinfo") 148 assert.NoError(t, err) 149 defer os.RemoveAll(dir) 150 151 fsInfo, err := c.GetDirFsInfo(dir) 152 assert.NoError(t, err) 153 assert.NotZero(t, fsInfo.Capacity) 154 assert.NotZero(t, fsInfo.Available) 155 } 156 157 func getClient(t *testing.T) Client { 158 f := fakeWinNodeStatsClient{} 159 c, err := newClient(f) 160 assert.NoError(t, err) 161 assert.NotNil(t, c) 162 return c 163 }