github.com/google/cadvisor@v0.49.1/resctrl/collector_test.go (about) 1 //go:build linux 2 // +build linux 3 4 // Copyright 2021 Google Inc. All Rights Reserved. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 // Collector tests. 19 package resctrl 20 21 import ( 22 "fmt" 23 "os" 24 "path/filepath" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 29 info "github.com/google/cadvisor/info/v1" 30 ) 31 32 func TestNewCollectorWithSetup(t *testing.T) { 33 rootResctrl = mockResctrl() 34 defer os.RemoveAll(rootResctrl) 35 36 pidsPath = mockContainersPids() 37 defer os.RemoveAll(pidsPath) 38 39 processPath = mockProcFs() 40 defer os.RemoveAll(processPath) 41 42 expectedID := "container" 43 expectedResctrlPath := filepath.Join(rootResctrl, monGroupsDirName, fmt.Sprintf("%s-%s", monGroupPrefix, expectedID)) 44 45 collector := newCollector(expectedID, mockGetContainerPids, 0, 2, "", true) 46 err := collector.setup() 47 48 assert.NoError(t, err) 49 assert.Equal(t, collector.id, expectedID) 50 assert.Equal(t, collector.resctrlPath, expectedResctrlPath) 51 } 52 53 func TestUpdateStats(t *testing.T) { 54 rootResctrl = mockResctrl() 55 defer os.RemoveAll(rootResctrl) 56 57 pidsPath = mockContainersPids() 58 defer os.RemoveAll(pidsPath) 59 60 processPath = mockProcFs() 61 defer os.RemoveAll(processPath) 62 63 collector := newCollector("container", mockGetContainerPids, 0, 2, "", true) 64 err := collector.setup() 65 assert.NoError(t, err) 66 67 mockResctrlMonData(collector.resctrlPath) 68 enabledCMT, enabledMBM = true, true 69 70 stats := info.ContainerStats{} 71 72 // Write some dumb data. 73 err = collector.UpdateStats(&stats) 74 assert.NoError(t, err) 75 assert.Equal(t, stats.Resctrl.Cache, []info.CacheStats{ 76 {LLCOccupancy: 1111}, 77 {LLCOccupancy: 3333}, 78 }) 79 assert.Equal(t, stats.Resctrl.MemoryBandwidth, []info.MemoryBandwidthStats{ 80 { 81 TotalBytes: 3333, 82 LocalBytes: 2222, 83 }, 84 { 85 TotalBytes: 3333, 86 LocalBytes: 1111, 87 }, 88 }) 89 } 90 91 func TestDestroy(t *testing.T) { 92 rootResctrl = mockResctrl() 93 defer os.RemoveAll(rootResctrl) 94 95 pidsPath = mockContainersPids() 96 defer os.RemoveAll(pidsPath) 97 98 processPath = mockProcFs() 99 defer os.RemoveAll(processPath) 100 101 collector := newCollector("container", mockGetContainerPids, 0, 2, "", true) 102 err := collector.setup() 103 if err != nil { 104 t.Fail() 105 } 106 107 path := collector.resctrlPath 108 109 if stat, err := os.Stat(path); stat == nil && err != nil { 110 t.Fail() 111 } 112 113 collector.Destroy() 114 115 if stat, err := os.Stat(path); stat != nil && err == nil { 116 t.Fail() 117 } 118 }