github.com/tilt-dev/tilt@v0.36.0/internal/hud/log_filter_test.go (about) 1 package hud 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/tilt-dev/tilt/pkg/logger" 9 "github.com/tilt-dev/tilt/pkg/model/logstore" 10 ) 11 12 func TestLogFilterMatches(t *testing.T) { 13 type testCase struct { 14 description string 15 logFilter LogFilter 16 input logstore.LogLine 17 expected bool 18 } 19 testCases := []testCase{ 20 { 21 description: "source all matches pod logs", 22 logFilter: LogFilter{source: FilterSourceAll}, 23 input: logstore.LogLine{SpanID: "pod:default:nginx"}, 24 expected: true, 25 }, 26 { 27 description: "source all matches build logs", 28 logFilter: LogFilter{source: FilterSourceAll}, 29 input: logstore.LogLine{SpanID: "build:1"}, 30 expected: true, 31 }, 32 { 33 description: "source all matches cmdimage logs", 34 logFilter: LogFilter{source: FilterSourceBuild}, 35 input: logstore.LogLine{SpanID: "cmdimage:nginx"}, 36 expected: true, 37 }, 38 { 39 description: "source all matches tiltfile logs", 40 logFilter: LogFilter{source: FilterSourceAll}, 41 input: logstore.LogLine{SpanID: "tiltfile:(Tiltfile):1"}, 42 expected: true, 43 }, 44 { 45 description: "source build does not match pod logs", 46 logFilter: LogFilter{source: FilterSourceBuild}, 47 input: logstore.LogLine{SpanID: "pod:default:nginx"}, 48 expected: false, 49 }, 50 { 51 description: "source build matches build logs", 52 logFilter: LogFilter{source: FilterSourceBuild}, 53 input: logstore.LogLine{SpanID: "build:1"}, 54 expected: true, 55 }, 56 { 57 description: "source build matches cmdimage logs", 58 logFilter: LogFilter{source: FilterSourceBuild}, 59 input: logstore.LogLine{SpanID: "cmdimage:nginx"}, 60 expected: true, 61 }, 62 { 63 description: "source build does not match tiltfile logs", 64 logFilter: LogFilter{source: FilterSourceBuild}, 65 input: logstore.LogLine{SpanID: "tiltfile:(Tiltfile):1"}, 66 expected: false, 67 }, 68 { 69 description: "source runtime matches pod logs", 70 logFilter: LogFilter{source: FilterSourceRuntime}, 71 input: logstore.LogLine{SpanID: "pod:default:nginx"}, 72 expected: true, 73 }, 74 { 75 description: "source runtime does not match build logs", 76 logFilter: LogFilter{source: FilterSourceRuntime}, 77 input: logstore.LogLine{SpanID: "build:1"}, 78 expected: false, 79 }, 80 { 81 description: "source runtime does not match cmdimage logs", 82 logFilter: LogFilter{source: FilterSourceRuntime}, 83 input: logstore.LogLine{SpanID: "cmdimage:nginx"}, 84 expected: false, 85 }, 86 { 87 description: "source runtime matches tiltfile logs", 88 logFilter: LogFilter{source: FilterSourceRuntime}, 89 input: logstore.LogLine{SpanID: "tiltfile:(Tiltfile):1"}, 90 expected: true, 91 }, 92 { 93 description: "source all matches logs with buildEvent", 94 logFilter: LogFilter{source: FilterSourceAll}, 95 input: logstore.LogLine{SpanID: "pod:default:nginx", BuildEvent: "init"}, 96 expected: true, 97 }, 98 { 99 description: "source build matches logs with buildEvent", 100 logFilter: LogFilter{source: FilterSourceBuild}, 101 input: logstore.LogLine{SpanID: "pod:default:nginx", BuildEvent: "init"}, 102 expected: true, 103 }, 104 { 105 description: "source runtime matches logs with buildEvent", 106 logFilter: LogFilter{source: FilterSourceRuntime}, 107 input: logstore.LogLine{SpanID: "build:1", BuildEvent: "init"}, 108 expected: true, 109 }, 110 { 111 description: "level lower than warn matches logs with any level", 112 logFilter: LogFilter{source: FilterSourceAll, level: logger.NoneLvl}, 113 input: logstore.LogLine{SpanID: "pod:default:nginx", Level: logger.InfoLvl}, 114 expected: true, 115 }, 116 { 117 description: "level warn matches logs with warn level", 118 logFilter: LogFilter{source: FilterSourceAll, level: logger.WarnLvl}, 119 input: logstore.LogLine{SpanID: "pod:default:nginx", Level: logger.WarnLvl}, 120 expected: true, 121 }, 122 { 123 description: "level warn does not match logs with error level", 124 logFilter: LogFilter{source: FilterSourceAll, level: logger.WarnLvl}, 125 input: logstore.LogLine{SpanID: "pod:default:nginx", Level: logger.ErrorLvl}, 126 expected: false, 127 }, 128 { 129 description: "level error matches logs with error level", 130 logFilter: LogFilter{source: FilterSourceAll, level: logger.ErrorLvl}, 131 input: logstore.LogLine{SpanID: "pod:default:nginx", Level: logger.ErrorLvl}, 132 expected: true, 133 }, 134 { 135 description: "level error does not match logs with warn level", 136 logFilter: LogFilter{source: FilterSourceAll, level: logger.ErrorLvl}, 137 input: logstore.LogLine{SpanID: "pod:default:nginx", Level: logger.WarnLvl}, 138 expected: false, 139 }, 140 { 141 description: "empty manifest name matches any logs", 142 logFilter: LogFilter{source: FilterSourceAll, resources: nil}, 143 input: logstore.LogLine{SpanID: "pod:default:nginx", ManifestName: "nginx"}, 144 expected: true, 145 }, 146 { 147 description: "manifest name matches logs with the same manifest name", 148 logFilter: LogFilter{source: FilterSourceAll, resources: FilterResources{"nginx"}}, 149 input: logstore.LogLine{SpanID: "pod:default:nginx", ManifestName: "nginx"}, 150 expected: true, 151 }, 152 { 153 description: "manifest name does not match logs with a different manifest name", 154 logFilter: LogFilter{source: FilterSourceAll, resources: FilterResources{"test"}}, 155 input: logstore.LogLine{SpanID: "pod:default:nginx", ManifestName: "nginx"}, 156 expected: false, 157 }, 158 } 159 160 for _, tc := range testCases { 161 t.Run(tc.description, func(t *testing.T) { 162 actual := tc.logFilter.Matches(tc.input) 163 assert.Equal(t, tc.expected, actual) 164 }) 165 } 166 } 167 168 func TestLogFilterApply(t *testing.T) { 169 type testCase struct { 170 description string 171 logFilter LogFilter 172 input []logstore.LogLine 173 expected []logstore.LogLine 174 } 175 testCases := []testCase{ 176 { 177 description: "empty filter matches all logs", 178 logFilter: LogFilter{}, 179 input: []logstore.LogLine{ 180 {SpanID: "pod:default:nginx"}, 181 {SpanID: "build:1"}, 182 {SpanID: "cmdimage:nginx"}, 183 {SpanID: "tiltfile:(Tiltfile):1"}, 184 }, 185 expected: []logstore.LogLine{ 186 {SpanID: "pod:default:nginx"}, 187 {SpanID: "build:1"}, 188 {SpanID: "cmdimage:nginx"}, 189 {SpanID: "tiltfile:(Tiltfile):1"}, 190 }, 191 }, 192 { 193 description: "filter with source all matches all logs", 194 logFilter: LogFilter{source: FilterSourceAll}, 195 input: []logstore.LogLine{ 196 {SpanID: "pod:default:nginx"}, 197 {SpanID: "build:1"}, 198 {SpanID: "cmdimage:nginx"}, 199 {SpanID: "tiltfile:(Tiltfile):1"}, 200 }, 201 expected: []logstore.LogLine{ 202 {SpanID: "pod:default:nginx"}, 203 {SpanID: "build:1"}, 204 {SpanID: "cmdimage:nginx"}, 205 {SpanID: "tiltfile:(Tiltfile):1"}, 206 }, 207 }, 208 { 209 description: "filter with source build matches build logs", 210 logFilter: LogFilter{source: FilterSourceBuild}, 211 input: []logstore.LogLine{ 212 {SpanID: "pod:default:nginx"}, 213 {SpanID: "build:1"}, 214 {SpanID: "cmdimage:nginx"}, 215 {SpanID: "tiltfile:(Tiltfile):1"}, 216 }, 217 expected: []logstore.LogLine{ 218 {SpanID: "build:1"}, 219 {SpanID: "cmdimage:nginx"}, 220 }, 221 }, 222 { 223 description: "filter with source runtime matches non-build logs", 224 logFilter: LogFilter{source: FilterSourceRuntime}, 225 input: []logstore.LogLine{ 226 {SpanID: "pod:default:nginx"}, 227 {SpanID: "build:1"}, 228 {SpanID: "cmdimage:nginx"}, 229 {SpanID: "tiltfile:(Tiltfile):1"}, 230 }, 231 expected: []logstore.LogLine{ 232 {SpanID: "pod:default:nginx"}, 233 {SpanID: "tiltfile:(Tiltfile):1"}, 234 }, 235 }, 236 { 237 description: "filter with level warn matches warn logs", 238 logFilter: LogFilter{source: FilterSourceAll, level: logger.WarnLvl}, 239 input: []logstore.LogLine{ 240 {SpanID: "pod:default:nginx", Level: logger.DebugLvl}, 241 {SpanID: "build:1", Level: logger.InfoLvl}, 242 {SpanID: "cmdimage:nginx", Level: logger.WarnLvl}, 243 {SpanID: "tiltfile:(Tiltfile):1", Level: logger.ErrorLvl}, 244 }, 245 expected: []logstore.LogLine{ 246 {SpanID: "cmdimage:nginx", Level: logger.WarnLvl}, 247 }, 248 }, 249 { 250 description: "filter with level error matches error logs", 251 logFilter: LogFilter{source: FilterSourceAll, level: logger.ErrorLvl}, 252 input: []logstore.LogLine{ 253 {SpanID: "pod:default:nginx", Level: logger.DebugLvl}, 254 {SpanID: "build:1", Level: logger.InfoLvl}, 255 {SpanID: "cmdimage:nginx", Level: logger.WarnLvl}, 256 {SpanID: "tiltfile:(Tiltfile):1", Level: logger.ErrorLvl}, 257 }, 258 expected: []logstore.LogLine{ 259 {SpanID: "tiltfile:(Tiltfile):1", Level: logger.ErrorLvl}, 260 }, 261 }, 262 { 263 description: "filter with manifest name matches only logs with the same manifest name", 264 logFilter: LogFilter{source: FilterSourceAll, resources: FilterResources{"nginx"}}, 265 input: []logstore.LogLine{ 266 {SpanID: "pod:default:nginx", ManifestName: "nginx"}, 267 {SpanID: "build:1", ManifestName: "nginx"}, 268 {SpanID: "cmdimage:nginx", ManifestName: "nginx"}, 269 {SpanID: "tiltfile:(Tiltfile):1", ManifestName: "(Tiltfile)"}, 270 }, 271 expected: []logstore.LogLine{ 272 {SpanID: "pod:default:nginx", ManifestName: "nginx"}, 273 {SpanID: "build:1", ManifestName: "nginx"}, 274 {SpanID: "cmdimage:nginx", ManifestName: "nginx"}, 275 }, 276 }, 277 } 278 279 for _, tc := range testCases { 280 t.Run(tc.description, func(t *testing.T) { 281 actual := tc.logFilter.Apply(tc.input) 282 assert.Equal(t, tc.expected, actual) 283 }) 284 } 285 }