| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package file |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func TestParse(t *testing.T) { |
| 16 | const ( |
| 17 | jobDef = 11 |
| 18 | cfgDef = 22 |
| 19 | modDef = 33 |
| 20 | ) |
| 21 | tests := map[string]struct { |
| 22 | test func(t *testing.T, tmp *tmpDir) |
| 23 | }{ |
| 24 | "static, default: +job +conf +module": { |
| 25 | test: func(t *testing.T, tmp *tmpDir) { |
| 26 | reg := confgroup.Registry{ |
| 27 | "module": { |
| 28 | UpdateEvery: modDef, |
| 29 | AutoDetectionRetry: modDef, |
| 30 | Priority: modDef, |
| 31 | }, |
| 32 | } |
| 33 | cfg := staticConfig{ |
| 34 | Default: confgroup.Default{ |
| 35 | UpdateEvery: cfgDef, |
| 36 | AutoDetectionRetry: cfgDef, |
| 37 | Priority: cfgDef, |
| 38 | }, |
| 39 | Jobs: []confgroup.Config{ |
| 40 | { |
| 41 | "name": "name", |
| 42 | "update_every": jobDef, |
| 43 | "autodetection_retry": jobDef, |
| 44 | "priority": jobDef, |
| 45 | }, |
| 46 | }, |
| 47 | } |
| 48 | filename := tmp.join("module.conf") |
| 49 | tmp.writeYAML(filename, cfg) |
| 50 | |
| 51 | expected := &confgroup.Group{ |
| 52 | Source: filename, |
| 53 | Configs: []confgroup.Config{ |
| 54 | { |
| 55 | "name": "name", |
| 56 | "module": "module", |
| 57 | "update_every": jobDef, |
| 58 | "autodetection_retry": jobDef, |
| 59 | "priority": jobDef, |
| 60 | }, |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | group, err := parse(reg, filename) |
| 65 | |
| 66 | require.NoError(t, err) |
| 67 | assert.Equal(t, expected, group) |
| 68 | }, |
| 69 | }, |
| 70 | "static, default: +job +conf +module (merge all)": { |
| 71 | test: func(t *testing.T, tmp *tmpDir) { |
| 72 | reg := confgroup.Registry{ |
| 73 | "module": { |
| 74 | Priority: modDef, |
| 75 | }, |
| 76 | } |
| 77 | cfg := staticConfig{ |
| 78 | Default: confgroup.Default{ |
| 79 | AutoDetectionRetry: cfgDef, |
| 80 | }, |
| 81 | Jobs: []confgroup.Config{ |
| 82 | { |
| 83 | "name": "name", |
| 84 | "update_every": jobDef, |
| 85 | }, |
| 86 | }, |
| 87 | } |
| 88 | filename := tmp.join("module.conf") |
| 89 | tmp.writeYAML(filename, cfg) |
| 90 | |
| 91 | expected := &confgroup.Group{ |
| 92 | Source: filename, |
| 93 | Configs: []confgroup.Config{ |
| 94 | { |
| 95 | "name": "name", |
| 96 | "module": "module", |
| 97 | "update_every": jobDef, |
| 98 | "autodetection_retry": cfgDef, |
| 99 | "priority": modDef, |
| 100 | }, |
| 101 | }, |
| 102 | } |
| 103 | |
| 104 | group, err := parse(reg, filename) |
| 105 | |
| 106 | require.NoError(t, err) |
| 107 | assert.Equal(t, expected, group) |
| 108 | }, |
| 109 | }, |
| 110 | "static, default: -job +conf +module": { |
| 111 | test: func(t *testing.T, tmp *tmpDir) { |
| 112 | reg := confgroup.Registry{ |
| 113 | "module": { |
| 114 | UpdateEvery: modDef, |
| 115 | AutoDetectionRetry: modDef, |
| 116 | Priority: modDef, |
| 117 | }, |
| 118 | } |
| 119 | cfg := staticConfig{ |
| 120 | Default: confgroup.Default{ |
| 121 | UpdateEvery: cfgDef, |
| 122 | AutoDetectionRetry: cfgDef, |
| 123 | Priority: cfgDef, |
| 124 | }, |
| 125 | Jobs: []confgroup.Config{ |
| 126 | { |
| 127 | "name": "name", |
| 128 | }, |
| 129 | }, |
| 130 | } |
| 131 | filename := tmp.join("module.conf") |
| 132 | tmp.writeYAML(filename, cfg) |
| 133 | |
| 134 | expected := &confgroup.Group{ |
| 135 | Source: filename, |
| 136 | Configs: []confgroup.Config{ |
| 137 | { |
| 138 | "name": "name", |
| 139 | "module": "module", |
| 140 | "update_every": cfgDef, |
| 141 | "autodetection_retry": cfgDef, |
| 142 | "priority": cfgDef, |
| 143 | }, |
| 144 | }, |
| 145 | } |
| 146 | |
| 147 | group, err := parse(reg, filename) |
| 148 | |
| 149 | require.NoError(t, err) |
| 150 | assert.Equal(t, expected, group) |
| 151 | }, |
| 152 | }, |
| 153 | "static, default: -job -conf +module": { |
| 154 | test: func(t *testing.T, tmp *tmpDir) { |
| 155 | reg := confgroup.Registry{ |
| 156 | "module": { |
| 157 | UpdateEvery: modDef, |
| 158 | AutoDetectionRetry: modDef, |
| 159 | Priority: modDef, |
| 160 | }, |
| 161 | } |
| 162 | cfg := staticConfig{ |
| 163 | Jobs: []confgroup.Config{ |
| 164 | { |
| 165 | "name": "name", |
| 166 | }, |
| 167 | }, |
| 168 | } |
| 169 | filename := tmp.join("module.conf") |
| 170 | tmp.writeYAML(filename, cfg) |
| 171 | |
| 172 | expected := &confgroup.Group{ |
| 173 | Source: filename, |
| 174 | Configs: []confgroup.Config{ |
| 175 | { |
| 176 | "name": "name", |
| 177 | "module": "module", |
| 178 | "autodetection_retry": modDef, |
| 179 | "priority": modDef, |
| 180 | "update_every": modDef, |
| 181 | }, |
| 182 | }, |
| 183 | } |
| 184 | |
| 185 | group, err := parse(reg, filename) |
| 186 | |
| 187 | require.NoError(t, err) |
| 188 | assert.Equal(t, expected, group) |
| 189 | }, |
| 190 | }, |
| 191 | "static, default: -job -conf -module (+global)": { |
| 192 | test: func(t *testing.T, tmp *tmpDir) { |
| 193 | reg := confgroup.Registry{ |
| 194 | "module": {}, |
| 195 | } |
| 196 | cfg := staticConfig{ |
| 197 | Jobs: []confgroup.Config{ |
| 198 | { |
| 199 | "name": "name", |
| 200 | }, |
| 201 | }, |
| 202 | } |
| 203 | filename := tmp.join("module.conf") |
| 204 | tmp.writeYAML(filename, cfg) |
| 205 | |
| 206 | expected := &confgroup.Group{ |
| 207 | Source: filename, |
| 208 | Configs: []confgroup.Config{ |
| 209 | { |
| 210 | "name": "name", |
| 211 | "module": "module", |
| 212 | "autodetection_retry": collectorapi.AutoDetectionRetry, |
| 213 | "priority": collectorapi.Priority, |
| 214 | "update_every": collectorapi.UpdateEvery, |
| 215 | }, |
| 216 | }, |
| 217 | } |
| 218 | |
| 219 | group, err := parse(reg, filename) |
| 220 | |
| 221 | require.NoError(t, err) |
| 222 | assert.Equal(t, expected, group) |
| 223 | }, |
| 224 | }, |
| 225 | "sd, default: +job +module": { |
| 226 | test: func(t *testing.T, tmp *tmpDir) { |
| 227 | reg := confgroup.Registry{ |
| 228 | "sd_module": { |
| 229 | UpdateEvery: modDef, |
| 230 | AutoDetectionRetry: modDef, |
| 231 | Priority: modDef, |
| 232 | }, |
| 233 | } |
| 234 | cfg := sdConfig{ |
| 235 | { |
| 236 | "name": "name", |
| 237 | "module": "sd_module", |
| 238 | "update_every": jobDef, |
| 239 | "autodetection_retry": jobDef, |
| 240 | "priority": jobDef, |
| 241 | }, |
| 242 | } |
| 243 | filename := tmp.join("module.conf") |
| 244 | tmp.writeYAML(filename, cfg) |
| 245 | |
| 246 | expected := &confgroup.Group{ |
| 247 | Source: filename, |
| 248 | Configs: []confgroup.Config{ |
| 249 | { |
| 250 | "module": "sd_module", |
| 251 | "name": "name", |
| 252 | "update_every": jobDef, |
| 253 | "autodetection_retry": jobDef, |
| 254 | "priority": jobDef, |
| 255 | }, |
| 256 | }, |
| 257 | } |
| 258 | |
| 259 | group, err := parse(reg, filename) |
| 260 | |
| 261 | require.NoError(t, err) |
| 262 | assert.Equal(t, expected, group) |
| 263 | }, |
| 264 | }, |
| 265 | "sd, default: -job +module": { |
| 266 | test: func(t *testing.T, tmp *tmpDir) { |
| 267 | reg := confgroup.Registry{ |
| 268 | "sd_module": { |
| 269 | UpdateEvery: modDef, |
| 270 | AutoDetectionRetry: modDef, |
| 271 | Priority: modDef, |
| 272 | }, |
| 273 | } |
| 274 | cfg := sdConfig{ |
| 275 | { |
| 276 | "name": "name", |
| 277 | "module": "sd_module", |
| 278 | }, |
| 279 | } |
| 280 | filename := tmp.join("module.conf") |
| 281 | tmp.writeYAML(filename, cfg) |
| 282 | |
| 283 | expected := &confgroup.Group{ |
| 284 | Source: filename, |
| 285 | Configs: []confgroup.Config{ |
| 286 | { |
| 287 | "name": "name", |
| 288 | "module": "sd_module", |
| 289 | "update_every": modDef, |
| 290 | "autodetection_retry": modDef, |
| 291 | "priority": modDef, |
| 292 | }, |
| 293 | }, |
| 294 | } |
| 295 | |
| 296 | group, err := parse(reg, filename) |
| 297 | |
| 298 | require.NoError(t, err) |
| 299 | assert.Equal(t, expected, group) |
| 300 | }, |
| 301 | }, |
| 302 | "sd, default: -job -module (+global)": { |
| 303 | test: func(t *testing.T, tmp *tmpDir) { |
| 304 | reg := confgroup.Registry{ |
| 305 | "sd_module": {}, |
| 306 | } |
| 307 | cfg := sdConfig{ |
| 308 | { |
| 309 | "name": "name", |
| 310 | "module": "sd_module", |
| 311 | }, |
| 312 | } |
| 313 | filename := tmp.join("module.conf") |
| 314 | tmp.writeYAML(filename, cfg) |
| 315 | |
| 316 | expected := &confgroup.Group{ |
| 317 | Source: filename, |
| 318 | Configs: []confgroup.Config{ |
| 319 | { |
| 320 | "name": "name", |
| 321 | "module": "sd_module", |
| 322 | "update_every": collectorapi.UpdateEvery, |
| 323 | "autodetection_retry": collectorapi.AutoDetectionRetry, |
| 324 | "priority": collectorapi.Priority, |
| 325 | }, |
| 326 | }, |
| 327 | } |
| 328 | |
| 329 | group, err := parse(reg, filename) |
| 330 | |
| 331 | require.NoError(t, err) |
| 332 | assert.Equal(t, expected, group) |
| 333 | }, |
| 334 | }, |
| 335 | "sd, job has no 'module' or 'module' is empty": { |
| 336 | test: func(t *testing.T, tmp *tmpDir) { |
| 337 | reg := confgroup.Registry{ |
| 338 | "sd_module": {}, |
| 339 | } |
| 340 | cfg := sdConfig{ |
| 341 | { |
| 342 | "name": "name", |
| 343 | }, |
| 344 | } |
| 345 | filename := tmp.join("module.conf") |
| 346 | tmp.writeYAML(filename, cfg) |
| 347 | |
| 348 | expected := &confgroup.Group{ |
| 349 | Source: filename, |
| 350 | Configs: []confgroup.Config{}, |
| 351 | } |
| 352 | |
| 353 | group, err := parse(reg, filename) |
| 354 | |
| 355 | require.NoError(t, err) |
| 356 | assert.Equal(t, expected, group) |
| 357 | }, |
| 358 | }, |
| 359 | "conf registry has no module": { |
| 360 | test: func(t *testing.T, tmp *tmpDir) { |
| 361 | reg := confgroup.Registry{ |
| 362 | "sd_module": {}, |
| 363 | } |
| 364 | cfg := sdConfig{ |
| 365 | { |
| 366 | "name": "name", |
| 367 | "module": "module", |
| 368 | }, |
| 369 | } |
| 370 | filename := tmp.join("module.conf") |
| 371 | tmp.writeYAML(filename, cfg) |
| 372 | |
| 373 | expected := &confgroup.Group{ |
| 374 | Source: filename, |
| 375 | Configs: []confgroup.Config{}, |
| 376 | } |
| 377 | |
| 378 | group, err := parse(reg, filename) |
| 379 | |
| 380 | require.NoError(t, err) |
| 381 | assert.Equal(t, expected, group) |
| 382 | }, |
| 383 | }, |
| 384 | "empty file": { |
| 385 | test: func(t *testing.T, tmp *tmpDir) { |
| 386 | reg := confgroup.Registry{ |
| 387 | "module": {}, |
| 388 | } |
| 389 | |
| 390 | filename := tmp.createFile("empty-*") |
| 391 | group, err := parse(reg, filename) |
| 392 | |
| 393 | assert.Nil(t, group) |
| 394 | require.NoError(t, err) |
| 395 | }, |
| 396 | }, |
| 397 | "only comments, unknown empty format": { |
| 398 | test: func(t *testing.T, tmp *tmpDir) { |
| 399 | reg := confgroup.Registry{} |
| 400 | |
| 401 | filename := tmp.createFile("unknown-empty-format-*") |
| 402 | tmp.writeString(filename, "# a comment") |
| 403 | group, err := parse(reg, filename) |
| 404 | |
| 405 | assert.Nil(t, group) |
| 406 | assert.NoError(t, err) |
| 407 | }, |
| 408 | }, |
| 409 | "unknown format": { |
| 410 | test: func(t *testing.T, tmp *tmpDir) { |
| 411 | reg := confgroup.Registry{} |
| 412 | |
| 413 | filename := tmp.createFile("unknown-format-*") |
| 414 | tmp.writeYAML(filename, "unknown") |
| 415 | group, err := parse(reg, filename) |
| 416 | |
| 417 | assert.Nil(t, group) |
| 418 | assert.Error(t, err) |
| 419 | }, |
| 420 | }, |
| 421 | } |
| 422 | |
| 423 | for name, scenario := range tests { |
| 424 | t.Run(name, func(t *testing.T) { |
| 425 | tmp := newTmpDir(t, "parse-file-*") |
| 426 | defer tmp.cleanup() |
| 427 | |
| 428 | scenario.test(t, tmp) |
| 429 | }) |
| 430 | } |
| 431 | } |