github.com/google/cadvisor@v0.49.1/container/docker/handler_test.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Handler for Docker containers. 16 package docker 17 18 import ( 19 "os" 20 "path" 21 "strings" 22 "testing" 23 24 "github.com/docker/docker/api/types/container" 25 "github.com/stretchr/testify/assert" 26 27 "github.com/google/cadvisor/fs" 28 info "github.com/google/cadvisor/info/v1" 29 ) 30 31 func TestStorageDirDetectionWithOldVersions(t *testing.T) { 32 as := assert.New(t) 33 rwLayer, err := getRwLayerID("abcd", "/", AufsStorageDriver, []int{1, 9, 0}) 34 as.Nil(err) 35 as.Equal(rwLayer, "abcd") 36 } 37 38 func TestStorageDirDetectionWithNewVersions(t *testing.T) { 39 as := assert.New(t) 40 testDir := t.TempDir() 41 containerID := "abcd" 42 randomizedID := "xyz" 43 randomIDPath := path.Join(testDir, "image/aufs/layerdb/mounts/", containerID) 44 as.Nil(os.MkdirAll(randomIDPath, os.ModePerm)) 45 as.Nil(os.WriteFile(path.Join(randomIDPath, "mount-id"), []byte(randomizedID), os.ModePerm)) 46 rwLayer, err := getRwLayerID(containerID, testDir, "aufs", []int{1, 10, 0}) 47 as.Nil(err) 48 as.Equal(rwLayer, randomizedID) 49 rwLayer, err = getRwLayerID(containerID, testDir, "aufs", []int{1, 10, 0}) 50 as.Nil(err) 51 as.Equal(rwLayer, randomizedID) 52 53 } 54 55 func rawMetadataEnvMatch(dockerEnvWhiteList string, cntConfig container.Config) map[string]string { 56 metadataEnvAllowList := strings.Split(dockerEnvWhiteList, ",") 57 handlerEnvs := make(map[string]string) 58 59 // split env vars to get metadata map. 60 for _, exposedEnv := range metadataEnvAllowList { 61 for _, envVar := range cntConfig.Env { 62 if envVar != "" { 63 splits := strings.SplitN(envVar, "=", 2) 64 if len(splits) == 2 && splits[0] == exposedEnv { 65 handlerEnvs[strings.ToLower(exposedEnv)] = splits[1] 66 } 67 } 68 } 69 } 70 71 return handlerEnvs 72 } 73 74 func newMetadataEnvMatch(dockerEnvWhiteList string, cntConfig container.Config) map[string]string { 75 metadataEnvAllowList := strings.Split(dockerEnvWhiteList, ",") 76 handlerEnvs := make(map[string]string) 77 78 // split env vars to get metadata map. 79 for _, exposedEnv := range metadataEnvAllowList { 80 if exposedEnv == "" { 81 // if no dockerEnvWhitelist provided, len(metadataEnvAllowList) == 1, metadataEnvAllowList[0] == "" 82 continue 83 } 84 85 for _, envVar := range cntConfig.Env { 86 if envVar != "" { 87 splits := strings.SplitN(envVar, "=", 2) 88 if len(splits) == 2 && strings.HasPrefix(splits[0], exposedEnv) { 89 handlerEnvs[strings.ToLower(splits[0])] = splits[1] 90 } 91 } 92 } 93 } 94 95 return handlerEnvs 96 } 97 98 func TestDockerEnvWhitelist(t *testing.T) { 99 as := assert.New(t) 100 101 envTotalMatch := "TEST_REGION,TEST_ZONE" 102 envMatchWithPrefix := "TEST_" 103 envMatchWithPrefixEmpty := "" 104 105 rawCntConfig := container.Config{Env: []string{"TEST_REGION=FRA", "TEST_ZONE=A", "HELLO=WORLD"}} 106 newCntConfig := container.Config{Env: []string{"TEST_REGION=FRA", "TEST_ZONE=A", "TEST_POOL=TOOLING", "HELLO=WORLD"}} 107 108 rawExpected := map[string]string{ 109 "test_region": "FRA", 110 "test_zone": "A", 111 } 112 newExpected := map[string]string{ 113 "test_region": "FRA", 114 "test_zone": "A", 115 "test_pool": "TOOLING", 116 } 117 emptyExpected := map[string]string{} 118 119 rawEnvsTotalMatch := rawMetadataEnvMatch(envTotalMatch, rawCntConfig) 120 newEnvsTotalMatch := newMetadataEnvMatch(envTotalMatch, rawCntConfig) 121 122 // make sure total match does not change 123 as.Equal(rawEnvsTotalMatch, newEnvsTotalMatch) 124 as.Equal(rawEnvsTotalMatch, rawExpected) 125 126 rawEnvsTotalMatch2 := rawMetadataEnvMatch(envTotalMatch, newCntConfig) 127 newEnvsTotalMatch2 := newMetadataEnvMatch(envTotalMatch, newCntConfig) 128 129 // make sure total match does not change with more envs exposed 130 as.Equal(rawEnvsTotalMatch2, newEnvsTotalMatch2) 131 as.Equal(rawEnvsTotalMatch2, rawExpected) 132 133 newEnvsMatchWithPrefix := newMetadataEnvMatch(envMatchWithPrefix, rawCntConfig) 134 newEnvsMatchWithPrefix2 := newMetadataEnvMatch(envMatchWithPrefix, newCntConfig) 135 136 // make sure new method can return envs with prefix specified 137 as.Equal(newEnvsMatchWithPrefix, rawExpected) 138 as.Equal(newEnvsMatchWithPrefix2, newExpected) 139 140 newEnvsMatchWithEmptyPrefix := newMetadataEnvMatch(envMatchWithPrefixEmpty, newCntConfig) 141 rawEnvsMatchWithEmptyWhitelist := rawMetadataEnvMatch(envMatchWithPrefixEmpty, newCntConfig) 142 143 // make sure empty whitelist returns nothing 144 as.Equal(newEnvsMatchWithEmptyPrefix, emptyExpected) 145 as.Equal(rawEnvsMatchWithEmptyWhitelist, emptyExpected) 146 147 } 148 149 func TestAddDiskStatsCheck(t *testing.T) { 150 var readsCompleted, readsMerged, sectorsRead, readTime, writesCompleted, writesMerged, sectorsWritten, 151 writeTime, ioInProgress, ioTime, weightedIoTime uint64 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 152 153 fileSystem := fs.Fs{ 154 DiskStats: fs.DiskStats{ 155 ReadsCompleted: readsCompleted, 156 ReadsMerged: readsMerged, 157 SectorsRead: sectorsRead, 158 ReadTime: readTime, 159 WritesCompleted: writesCompleted, 160 WritesMerged: writesMerged, 161 SectorsWritten: sectorsWritten, 162 WriteTime: writeTime, 163 IoInProgress: ioInProgress, 164 IoTime: ioTime, 165 WeightedIoTime: weightedIoTime, 166 }, 167 } 168 169 fileSystems := []fs.Fs{fileSystem} 170 171 var fsStats info.FsStats 172 addDiskStats(fileSystems, nil, &fsStats) 173 } 174 175 func TestAddDiskStats(t *testing.T) { 176 // Arrange 177 as := assert.New(t) 178 var readsCompleted, readsMerged, sectorsRead, readTime, writesCompleted, writesMerged, sectorsWritten, 179 writeTime, ioInProgress, ioTime, weightedIoTime uint64 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 180 var fsStats info.FsStats 181 182 fsInfo := info.FsInfo{ 183 DeviceMajor: 4, 184 DeviceMinor: 64, 185 } 186 187 fileSystem := fs.Fs{ 188 DiskStats: fs.DiskStats{ 189 ReadsCompleted: readsCompleted, 190 ReadsMerged: readsMerged, 191 SectorsRead: sectorsRead, 192 ReadTime: readTime, 193 WritesCompleted: writesCompleted, 194 WritesMerged: writesMerged, 195 SectorsWritten: sectorsWritten, 196 WriteTime: writeTime, 197 IoInProgress: ioInProgress, 198 IoTime: ioTime, 199 WeightedIoTime: weightedIoTime, 200 }, 201 } 202 203 fileSystems := []fs.Fs{fileSystem} 204 205 // Act 206 addDiskStats(fileSystems, &fsInfo, &fsStats) 207 208 // Assert 209 as.Equal(readsCompleted, fileSystem.DiskStats.ReadsCompleted, "ReadsCompleted metric should be %d but was %d", readsCompleted, fileSystem.DiskStats.ReadsCompleted) 210 as.Equal(readsMerged, fileSystem.DiskStats.ReadsMerged, "ReadsMerged metric should be %d but was %d", readsMerged, fileSystem.DiskStats.ReadsMerged) 211 as.Equal(sectorsRead, fileSystem.DiskStats.SectorsRead, "SectorsRead metric should be %d but was %d", sectorsRead, fileSystem.DiskStats.SectorsRead) 212 as.Equal(readTime, fileSystem.DiskStats.ReadTime, "ReadTime metric should be %d but was %d", readTime, fileSystem.DiskStats.ReadTime) 213 as.Equal(writesCompleted, fileSystem.DiskStats.WritesCompleted, "WritesCompleted metric should be %d but was %d", writesCompleted, fileSystem.DiskStats.WritesCompleted) 214 as.Equal(writesMerged, fileSystem.DiskStats.WritesMerged, "WritesMerged metric should be %d but was %d", writesMerged, fileSystem.DiskStats.WritesMerged) 215 as.Equal(sectorsWritten, fileSystem.DiskStats.SectorsWritten, "SectorsWritten metric should be %d but was %d", sectorsWritten, fileSystem.DiskStats.SectorsWritten) 216 as.Equal(writeTime, fileSystem.DiskStats.WriteTime, "WriteTime metric should be %d but was %d", writeTime, fileSystem.DiskStats.WriteTime) 217 as.Equal(ioInProgress, fileSystem.DiskStats.IoInProgress, "IoInProgress metric should be %d but was %d", ioInProgress, fileSystem.DiskStats.IoInProgress) 218 as.Equal(ioTime, fileSystem.DiskStats.IoTime, "IoTime metric should be %d but was %d", ioTime, fileSystem.DiskStats.IoTime) 219 as.Equal(weightedIoTime, fileSystem.DiskStats.WeightedIoTime, "WeightedIoTime metric should be %d but was %d", weightedIoTime, fileSystem.DiskStats.WeightedIoTime) 220 }