github.com/grafana/synthetic-monitoring-agent@v0.64.2/pkg/accounting/accounting_test.go (about) 1 package accounting 2 3 import ( 4 "path" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func getTestCases() map[string]struct { 14 input synthetic_monitoring.Check 15 class string 16 } { 17 return map[string]struct { 18 input synthetic_monitoring.Check 19 class string 20 }{ 21 "dns": { 22 input: synthetic_monitoring.Check{ 23 Target: "127.0.0.1", 24 Settings: synthetic_monitoring.CheckSettings{ 25 Dns: &synthetic_monitoring.DnsSettings{}, 26 }, 27 }, 28 class: "dns", 29 }, 30 "dns_basic": { 31 input: synthetic_monitoring.Check{ 32 Target: "127.0.0.1", 33 BasicMetricsOnly: true, 34 Settings: synthetic_monitoring.CheckSettings{ 35 Dns: &synthetic_monitoring.DnsSettings{}, 36 }, 37 }, 38 class: "dns_basic", 39 }, 40 41 "http": { 42 input: synthetic_monitoring.Check{ 43 Target: "http://127.0.0.1/", 44 Settings: synthetic_monitoring.CheckSettings{ 45 Http: &synthetic_monitoring.HttpSettings{}, 46 }, 47 }, 48 class: "http", 49 }, 50 "http_ssl": { 51 input: synthetic_monitoring.Check{ 52 Target: "https://127.0.0.1/", 53 Settings: synthetic_monitoring.CheckSettings{ 54 Http: &synthetic_monitoring.HttpSettings{}, 55 }, 56 }, 57 class: "http_ssl", 58 }, 59 "http_basic": { 60 input: synthetic_monitoring.Check{ 61 Target: "http://127.0.0.1/", 62 BasicMetricsOnly: true, 63 Settings: synthetic_monitoring.CheckSettings{ 64 Http: &synthetic_monitoring.HttpSettings{}, 65 }, 66 }, 67 class: "http_basic", 68 }, 69 "http_ssl_basic": { 70 input: synthetic_monitoring.Check{ 71 Target: "https://127.0.0.1/", 72 BasicMetricsOnly: true, 73 Settings: synthetic_monitoring.CheckSettings{ 74 Http: &synthetic_monitoring.HttpSettings{}, 75 }, 76 }, 77 class: "http_ssl_basic", 78 }, 79 80 "ping": { 81 input: synthetic_monitoring.Check{ 82 Target: "127.0.0.1", 83 Settings: synthetic_monitoring.CheckSettings{ 84 Ping: &synthetic_monitoring.PingSettings{}, 85 }, 86 }, 87 class: "ping", 88 }, 89 "ping_basic": { 90 input: synthetic_monitoring.Check{ 91 Target: "127.0.0.1", 92 BasicMetricsOnly: true, 93 Settings: synthetic_monitoring.CheckSettings{ 94 Ping: &synthetic_monitoring.PingSettings{}, 95 }, 96 }, 97 class: "ping_basic", 98 }, 99 100 "tcp": { 101 input: synthetic_monitoring.Check{ 102 Target: "127.0.0.1:8080", 103 Settings: synthetic_monitoring.CheckSettings{ 104 Tcp: &synthetic_monitoring.TcpSettings{}, 105 }, 106 }, 107 class: "tcp", 108 }, 109 "tcp_ssl": { 110 input: synthetic_monitoring.Check{ 111 Target: "127.0.0.1:8080", 112 Settings: synthetic_monitoring.CheckSettings{ 113 Tcp: &synthetic_monitoring.TcpSettings{ 114 Tls: true, 115 }, 116 }, 117 }, 118 class: "tcp_ssl", 119 }, 120 "tcp_basic": { 121 input: synthetic_monitoring.Check{ 122 Target: "127.0.0.1:8080", 123 BasicMetricsOnly: true, 124 Settings: synthetic_monitoring.CheckSettings{ 125 Tcp: &synthetic_monitoring.TcpSettings{}, 126 }, 127 }, 128 class: "tcp_basic", 129 }, 130 "tcp_ssl_basic": { 131 input: synthetic_monitoring.Check{ 132 Target: "127.0.0.1:8080", 133 BasicMetricsOnly: true, 134 Settings: synthetic_monitoring.CheckSettings{ 135 Tcp: &synthetic_monitoring.TcpSettings{ 136 Tls: true, 137 }, 138 }, 139 }, 140 class: "tcp_ssl_basic", 141 }, 142 "traceroute": { 143 input: synthetic_monitoring.Check{ 144 Target: "127.0.0.1", 145 BasicMetricsOnly: true, 146 Settings: synthetic_monitoring.CheckSettings{ 147 Traceroute: &synthetic_monitoring.TracerouteSettings{ 148 MaxHops: 64, 149 HopTimeout: 100, 150 }, 151 }, 152 }, 153 class: "traceroute_basic", 154 }, 155 "traceroute_basic": { 156 input: synthetic_monitoring.Check{ 157 Target: "127.0.0.1", 158 BasicMetricsOnly: true, 159 Settings: synthetic_monitoring.CheckSettings{ 160 Traceroute: &synthetic_monitoring.TracerouteSettings{ 161 MaxHops: 64, 162 HopTimeout: 100, 163 }, 164 }, 165 }, 166 class: "traceroute_basic", 167 }, 168 "scripted": { 169 input: synthetic_monitoring.Check{ 170 Target: "http://127.0.0.1/", 171 Timeout: 2000, 172 Settings: synthetic_monitoring.CheckSettings{ 173 Scripted: &synthetic_monitoring.ScriptedSettings{ 174 Script: []byte(`export default function() {}`), 175 }, 176 }, 177 }, 178 class: "scripted", 179 }, 180 "scripted_basic": { 181 input: synthetic_monitoring.Check{ 182 Target: "http://127.0.0.1/", 183 Timeout: 2000, 184 BasicMetricsOnly: true, 185 Settings: synthetic_monitoring.CheckSettings{ 186 Scripted: &synthetic_monitoring.ScriptedSettings{ 187 Script: []byte(`export default function() {}`), 188 }, 189 }, 190 }, 191 class: "scripted_basic", 192 }, 193 "multihttp": { 194 input: synthetic_monitoring.Check{ 195 Target: "http://127.0.0.1/", 196 Timeout: 2000, 197 Settings: synthetic_monitoring.CheckSettings{ 198 Multihttp: &synthetic_monitoring.MultiHttpSettings{ 199 Entries: []*synthetic_monitoring.MultiHttpEntry{ 200 { 201 Request: &synthetic_monitoring.MultiHttpEntryRequest{ 202 Method: synthetic_monitoring.HttpMethod_GET, 203 Url: "http://127.0.0.1/", 204 }, 205 }, 206 }, 207 }, 208 }, 209 }, 210 class: "multihttp", 211 }, 212 "multihttp_basic": { 213 input: synthetic_monitoring.Check{ 214 Target: "http://127.0.0.1/", 215 Timeout: 2000, 216 BasicMetricsOnly: true, 217 Settings: synthetic_monitoring.CheckSettings{ 218 Multihttp: &synthetic_monitoring.MultiHttpSettings{ 219 Entries: []*synthetic_monitoring.MultiHttpEntry{ 220 { 221 Request: &synthetic_monitoring.MultiHttpEntryRequest{ 222 Method: synthetic_monitoring.HttpMethod_GET, 223 Url: "http://127.0.0.1/", 224 }, 225 }, 226 }, 227 }, 228 }, 229 }, 230 class: "multihttp_basic", 231 }, 232 "grpc": { 233 input: synthetic_monitoring.Check{ 234 Target: "127.0.0.1:9000", 235 Settings: synthetic_monitoring.CheckSettings{ 236 Grpc: &synthetic_monitoring.GrpcSettings{}, 237 }, 238 }, 239 class: "grpc", 240 }, 241 "grpc_ssl": { 242 input: synthetic_monitoring.Check{ 243 Target: "127.0.0.1:9000", 244 Settings: synthetic_monitoring.CheckSettings{ 245 Grpc: &synthetic_monitoring.GrpcSettings{ 246 Tls: true, 247 }, 248 }, 249 }, 250 class: "grpc_ssl", 251 }, 252 "grpc_basic": { 253 input: synthetic_monitoring.Check{ 254 Target: "127.0.0.1:9000", 255 BasicMetricsOnly: true, 256 Settings: synthetic_monitoring.CheckSettings{ 257 Grpc: &synthetic_monitoring.GrpcSettings{}, 258 }, 259 }, 260 class: "grpc_basic", 261 }, 262 "grpc_ssl_basic": { 263 input: synthetic_monitoring.Check{ 264 Target: "127.0.0.1:9000", 265 BasicMetricsOnly: true, 266 Settings: synthetic_monitoring.CheckSettings{ 267 Grpc: &synthetic_monitoring.GrpcSettings{ 268 Tls: true, 269 }, 270 }, 271 }, 272 class: "grpc_ssl_basic", 273 }, 274 "browser": { 275 input: synthetic_monitoring.Check{ 276 Target: "http://127.0.0.1/", 277 Timeout: 2000, 278 Settings: synthetic_monitoring.CheckSettings{ 279 Browser: &synthetic_monitoring.BrowserSettings{ 280 Script: []byte(`export default function() {}`), 281 }, 282 }, 283 }, 284 class: "browser", 285 }, 286 "browser_basic": { 287 input: synthetic_monitoring.Check{ 288 Target: "http://127.0.0.1/", 289 Timeout: 2000, 290 BasicMetricsOnly: true, 291 Settings: synthetic_monitoring.CheckSettings{ 292 Browser: &synthetic_monitoring.BrowserSettings{ 293 Script: []byte(`export default function() {}`), 294 }, 295 }, 296 }, 297 class: "browser_basic", 298 }, 299 } 300 } 301 302 // TestGetActiveSeriesForCheck verifies that GetActiveSeriesForCheck 303 // returns the data in activeSeriesByCheckType. This makes sure that the 304 // function is applying the correct criteria to select the entry from 305 // the map. It also verifies that all the entries in that map are 306 // covered. 307 func TestGetActiveSeriesForCheck(t *testing.T) { 308 testcases := getTestCases() 309 310 // For know simply expect that every element in 311 // activeSeriesByCheckType has a corresponding test case of the 312 // same name. This ensures that if additional checks are added, 313 // or new variants are introduced, they don't go unnoticed here. 314 // 315 // If more test cases are added, they can use names other than 316 // the keys in activeSeriesByCheckType. 317 for checkType := range activeSeriesByCheckType { 318 _, found := testcases[checkType] 319 require.Truef(t, found, "every element in activeSeriesByCheckType must be tested, missing %s", checkType) 320 } 321 322 for name, tc := range testcases { 323 t.Run(name, func(t *testing.T) { 324 actual, err := GetActiveSeriesForCheck(tc.input) 325 require.NoError(t, err) 326 require.Equal(t, activeSeriesByCheckType[tc.class], actual) 327 }) 328 } 329 } 330 331 func TestGetCheckAccountingClass(t *testing.T) { 332 testcases := getTestCases() 333 334 // See comment in TestGetActiveSeriesForCheck 335 for checkType := range activeSeriesByCheckType { 336 _, found := testcases[checkType] 337 require.Truef(t, found, "every element in activeSeriesByCheckType must be tested, missing %s", checkType) 338 } 339 340 for name, tc := range testcases { 341 t.Run(name, func(t *testing.T) { 342 actual, err := GetCheckAccountingClass(tc.input) 343 require.NoError(t, err) 344 require.Equal(t, tc.class, actual) 345 }) 346 } 347 } 348 349 func TestGetAccountingClassInfo(t *testing.T) { 350 info := GetAccountingClassInfo() 351 352 for accountingClass, expectedSeries := range activeSeriesByCheckType { 353 actual, found := info[accountingClass] 354 require.True(t, found, "every element in activeSeriesByCheckType must be tested") 355 require.Equal(t, expectedSeries, actual.Series) 356 require.Equal(t, getTypeFromClass(accountingClass), actual.CheckType) 357 } 358 } 359 360 // TestGetTypeFromClass verifies that the helper returns the correct 361 // type for the corresponding check. 362 func TestGetTypeFromClass(t *testing.T) { 363 for name, tc := range getTestCases() { 364 t.Run(name, func(t *testing.T) { 365 expected := tc.input.Type() 366 actual := getTypeFromClass(tc.class) 367 require.Equal(t, expected, actual) 368 }) 369 } 370 } 371 372 // TestActiveSeriesByCheckTypeInSyncWithData checks that all the entries 373 // in the file system have a corresponding entry in 374 // activeSeriesByCheckType, to make sure that code generation runs 375 // whenever new check types or check variants are added. 376 // 377 // FIXME(mem): the coupling between this function and the package 378 // layout is a little annoying. 379 func TestActiveSeriesByCheckTypeInSyncWithData(t *testing.T) { 380 entries, err := filepath.Glob("../../internal/scraper/testdata/*.txt") 381 require.NoError(t, err) 382 require.NotEmpty(t, entries) 383 384 for _, fn := range entries { 385 casename := strings.TrimSuffix(path.Base(fn), ".txt") 386 _, found := activeSeriesByCheckType[casename] 387 require.Truef(t, found, "case %s (%s) not found in activeSeriesByCheckType", casename, fn) 388 } 389 }