gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/metricserver/metricserver_test.go (about) 1 // Copyright 2023 The gVisor Authors. 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 package metricserver 16 17 import ( 18 "io/fs" 19 "os" 20 "syscall" 21 "testing" 22 "time" 23 ) 24 25 type fakeFileInfo struct { 26 fs.FileInfo 27 28 modTime time.Time 29 size int64 30 sys any 31 } 32 33 func (f *fakeFileInfo) ModTime() time.Time { return f.modTime } 34 35 func (f *fakeFileInfo) Size() int64 { return f.size } 36 37 func (f *fakeFileInfo) Sys() any { return f.sys } 38 39 // TestSufficientlyEqualStats tests sufficientlyEqualStats. 40 func TestSufficientlyEqualStats(t *testing.T) { 41 now := time.Now() 42 twoHoursAgo := now.Add(-2 * time.Hour) 43 for _, test := range []struct { 44 name string 45 a, b os.FileInfo 46 want bool 47 }{ 48 { 49 name: "empty", 50 a: &fakeFileInfo{}, 51 b: &fakeFileInfo{}, 52 want: true, 53 }, 54 { 55 name: "different modification time", 56 a: &fakeFileInfo{ 57 modTime: twoHoursAgo, 58 }, 59 b: &fakeFileInfo{ 60 modTime: now, 61 }, 62 want: false, 63 }, 64 { 65 name: "different size", 66 a: &fakeFileInfo{ 67 size: 1337, 68 }, 69 b: &fakeFileInfo{ 70 size: 42, 71 }, 72 want: false, 73 }, 74 { 75 name: "same mod time and size, not a syscall.Stat_t", 76 a: &fakeFileInfo{ 77 modTime: twoHoursAgo, 78 size: 1337, 79 sys: struct{ foo string }{"bla"}, 80 }, 81 b: &fakeFileInfo{ 82 modTime: twoHoursAgo, 83 size: 1337, 84 sys: struct{ foo int }{42}, 85 }, 86 want: true, 87 }, 88 { 89 name: "same mod time and size, only one is *syscall.Stat_t", 90 a: &fakeFileInfo{ 91 modTime: twoHoursAgo, 92 size: 1337, 93 sys: &syscall.Stat_t{}, 94 }, 95 b: &fakeFileInfo{ 96 modTime: twoHoursAgo, 97 size: 1337, 98 sys: struct{ foo string }{"blablabla"}, 99 }, 100 want: false, 101 }, 102 { 103 name: "same mod time and size, one is *syscall.Stat_t, other is syscall.Stat_t", 104 a: &fakeFileInfo{ 105 modTime: twoHoursAgo, 106 size: 1337, 107 sys: &syscall.Stat_t{}, 108 }, 109 b: &fakeFileInfo{ 110 modTime: twoHoursAgo, 111 size: 1337, 112 sys: syscall.Stat_t{}, 113 }, 114 want: false, 115 }, 116 { 117 name: "same mod time and size, two empty *syscall.Stat_t", 118 a: &fakeFileInfo{ 119 modTime: twoHoursAgo, 120 size: 1337, 121 sys: &syscall.Stat_t{}, 122 }, 123 b: &fakeFileInfo{ 124 modTime: twoHoursAgo, 125 size: 1337, 126 sys: &syscall.Stat_t{}, 127 }, 128 want: true, 129 }, 130 { 131 name: "same mod time and size, different *syscall.Stat_t.Dev", 132 a: &fakeFileInfo{ 133 modTime: twoHoursAgo, 134 size: 1337, 135 sys: &syscall.Stat_t{ 136 Dev: 42, 137 }, 138 }, 139 b: &fakeFileInfo{ 140 modTime: twoHoursAgo, 141 size: 1337, 142 sys: &syscall.Stat_t{ 143 Dev: 43, 144 }, 145 }, 146 want: false, 147 }, 148 { 149 name: "same mod time and size, different *syscall.Stat_t.Ino", 150 a: &fakeFileInfo{ 151 modTime: twoHoursAgo, 152 size: 1337, 153 sys: &syscall.Stat_t{ 154 Ino: 42, 155 }, 156 }, 157 b: &fakeFileInfo{ 158 modTime: twoHoursAgo, 159 size: 1337, 160 sys: &syscall.Stat_t{ 161 Ino: 43, 162 }, 163 }, 164 want: false, 165 }, 166 { 167 name: "same mod time and size, same *syscall.Stat_t.{Dev,Ino}", 168 a: &fakeFileInfo{ 169 modTime: twoHoursAgo, 170 size: 1337, 171 sys: &syscall.Stat_t{ 172 Ino: 42, 173 Dev: 44, 174 }, 175 }, 176 b: &fakeFileInfo{ 177 modTime: twoHoursAgo, 178 size: 1337, 179 sys: &syscall.Stat_t{ 180 Ino: 42, 181 Dev: 44, 182 }, 183 }, 184 want: true, 185 }, 186 } { 187 t.Run(test.name, func(t *testing.T) { 188 got := sufficientlyEqualStats(test.a, test.b) 189 if got != test.want { 190 t.Errorf("got equal=%t want %t", got, test.want) 191 } 192 gotReverse := sufficientlyEqualStats(test.b, test.a) 193 if gotReverse != got { 194 t.Errorf("sufficientlyEqualStats(a, b) = %v yet sufficientlyEqualStats(b, a) = %v", got, gotReverse) 195 } 196 }) 197 } 198 }