| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package jobmgr |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/dyncfg" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/framework/functions" |
| 13 | ) |
| 14 | |
| 15 | func TestManager_Run(t *testing.T) { |
| 16 | tests := map[string]struct { |
| 17 | createSim func() *runSim |
| 18 | }{ |
| 19 | "stock => ok: add and remove": { |
| 20 | createSim: func() *runSim { |
| 21 | cfg := prepareStockCfg("success", "name") |
| 22 | |
| 23 | return &runSim{ |
| 24 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 25 | sendConfGroup(in, cfg.Source(), cfg) |
| 26 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 27 | UID: "1-enable", |
| 28 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 29 | })) |
| 30 | |
| 31 | sendConfGroup(in, cfg.Source()) |
| 32 | }, |
| 33 | wantDiscovered: nil, |
| 34 | wantSeen: nil, |
| 35 | wantExposed: nil, |
| 36 | wantRunning: nil, |
| 37 | wantDyncfg: ` |
| 38 | CONFIG test:collector:success:name create accepted job /collectors/test/Jobs stock 'type=stock,module=success,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 39 | |
| 40 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 41 | {"status":200,"message":""} |
| 42 | FUNCTION_RESULT_END |
| 43 | |
| 44 | CONFIG test:collector:success:name status running |
| 45 | |
| 46 | CONFIG test:collector:success:name delete |
| 47 | `, |
| 48 | } |
| 49 | }, |
| 50 | }, |
| 51 | "stock => nok: add": { |
| 52 | createSim: func() *runSim { |
| 53 | cfg := prepareStockCfg("fail", "name") |
| 54 | |
| 55 | return &runSim{ |
| 56 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 57 | sendConfGroup(in, cfg.Source(), cfg) |
| 58 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 59 | UID: "1-enable", |
| 60 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 61 | })) |
| 62 | }, |
| 63 | wantDiscovered: []confgroup.Config{cfg}, |
| 64 | wantSeen: []confgroup.Config{ |
| 65 | cfg, |
| 66 | }, |
| 67 | wantExposed: nil, |
| 68 | wantRunning: nil, |
| 69 | wantDyncfg: ` |
| 70 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs stock 'type=stock,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 71 | |
| 72 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 73 | {"status":200,"message":"job enable failed: mock failed init"} |
| 74 | FUNCTION_RESULT_END |
| 75 | |
| 76 | CONFIG test:collector:fail:name delete |
| 77 | `, |
| 78 | } |
| 79 | }, |
| 80 | }, |
| 81 | "stock => nok: add and remove": { |
| 82 | createSim: func() *runSim { |
| 83 | cfg := prepareStockCfg("fail", "name") |
| 84 | |
| 85 | return &runSim{ |
| 86 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 87 | sendConfGroup(in, cfg.Source(), cfg) |
| 88 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 89 | UID: "1-enable", |
| 90 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 91 | })) |
| 92 | |
| 93 | sendConfGroup(in, cfg.Source()) |
| 94 | }, |
| 95 | wantDiscovered: nil, |
| 96 | wantSeen: nil, |
| 97 | wantExposed: nil, |
| 98 | wantRunning: nil, |
| 99 | wantDyncfg: ` |
| 100 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs stock 'type=stock,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 101 | |
| 102 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 103 | {"status":200,"message":"job enable failed: mock failed init"} |
| 104 | FUNCTION_RESULT_END |
| 105 | |
| 106 | CONFIG test:collector:fail:name delete |
| 107 | `, |
| 108 | } |
| 109 | }, |
| 110 | }, |
| 111 | "user => ok: add and remove": { |
| 112 | createSim: func() *runSim { |
| 113 | cfg := prepareUserCfg("success", "name") |
| 114 | |
| 115 | return &runSim{ |
| 116 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 117 | sendConfGroup(in, cfg.Source(), cfg) |
| 118 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 119 | UID: "1-enable", |
| 120 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 121 | })) |
| 122 | |
| 123 | sendConfGroup(in, cfg.Source()) |
| 124 | }, |
| 125 | wantDiscovered: nil, |
| 126 | wantSeen: nil, |
| 127 | wantExposed: nil, |
| 128 | wantRunning: nil, |
| 129 | wantDyncfg: ` |
| 130 | CONFIG test:collector:success:name create accepted job /collectors/test/Jobs user 'type=user,module=success,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 131 | |
| 132 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 133 | {"status":200,"message":""} |
| 134 | FUNCTION_RESULT_END |
| 135 | |
| 136 | CONFIG test:collector:success:name status running |
| 137 | |
| 138 | CONFIG test:collector:success:name delete |
| 139 | `, |
| 140 | } |
| 141 | }, |
| 142 | }, |
| 143 | "user => nok: add and remove": { |
| 144 | createSim: func() *runSim { |
| 145 | cfg := prepareUserCfg("fail", "name") |
| 146 | |
| 147 | return &runSim{ |
| 148 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 149 | sendConfGroup(in, cfg.Source(), cfg) |
| 150 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 151 | UID: "1-enable", |
| 152 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 153 | })) |
| 154 | |
| 155 | sendConfGroup(in, cfg.Source()) |
| 156 | }, |
| 157 | wantDiscovered: nil, |
| 158 | wantSeen: nil, |
| 159 | wantExposed: nil, |
| 160 | wantRunning: nil, |
| 161 | wantDyncfg: ` |
| 162 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs user 'type=user,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 163 | |
| 164 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 165 | {"status":200,"message":"job enable failed: mock failed init"} |
| 166 | FUNCTION_RESULT_END |
| 167 | |
| 168 | CONFIG test:collector:fail:name status failed |
| 169 | |
| 170 | CONFIG test:collector:fail:name delete |
| 171 | `, |
| 172 | } |
| 173 | }, |
| 174 | }, |
| 175 | "disc => ok: add and remove": { |
| 176 | createSim: func() *runSim { |
| 177 | cfg := prepareDiscoveredCfg("success", "name") |
| 178 | |
| 179 | return &runSim{ |
| 180 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 181 | sendConfGroup(in, cfg.Source(), cfg) |
| 182 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 183 | UID: "1-enable", |
| 184 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 185 | })) |
| 186 | |
| 187 | sendConfGroup(in, cfg.Source()) |
| 188 | }, |
| 189 | wantDiscovered: nil, |
| 190 | wantSeen: nil, |
| 191 | wantExposed: nil, |
| 192 | wantRunning: nil, |
| 193 | wantDyncfg: ` |
| 194 | CONFIG test:collector:success:name create accepted job /collectors/test/Jobs discovered 'type=discovered,module=success,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 195 | |
| 196 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 197 | {"status":200,"message":""} |
| 198 | FUNCTION_RESULT_END |
| 199 | |
| 200 | CONFIG test:collector:success:name status running |
| 201 | |
| 202 | CONFIG test:collector:success:name delete |
| 203 | `, |
| 204 | } |
| 205 | }, |
| 206 | }, |
| 207 | "disc => nok: add and remove": { |
| 208 | createSim: func() *runSim { |
| 209 | cfg := prepareDiscoveredCfg("fail", "name") |
| 210 | |
| 211 | return &runSim{ |
| 212 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 213 | sendConfGroup(in, cfg.Source(), cfg) |
| 214 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 215 | UID: "1-enable", |
| 216 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 217 | })) |
| 218 | |
| 219 | sendConfGroup(in, cfg.Source()) |
| 220 | }, |
| 221 | wantDiscovered: nil, |
| 222 | wantSeen: nil, |
| 223 | wantExposed: nil, |
| 224 | wantRunning: nil, |
| 225 | wantDyncfg: ` |
| 226 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs discovered 'type=discovered,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 227 | |
| 228 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 229 | {"status":200,"message":"job enable failed: mock failed init"} |
| 230 | FUNCTION_RESULT_END |
| 231 | |
| 232 | CONFIG test:collector:fail:name status failed |
| 233 | |
| 234 | CONFIG test:collector:fail:name delete |
| 235 | `, |
| 236 | } |
| 237 | }, |
| 238 | }, |
| 239 | "non-dyncfg => nok: diff src, diff name: add": { |
| 240 | createSim: func() *runSim { |
| 241 | stockCfg := prepareStockCfg("fail", "stock") |
| 242 | discCfg := prepareDiscoveredCfg("fail", "discovered") |
| 243 | userCfg := prepareUserCfg("fail", "user") |
| 244 | |
| 245 | return &runSim{ |
| 246 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 247 | sendConfGroup(in, stockCfg.Source(), stockCfg) |
| 248 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 249 | UID: "1-enable", |
| 250 | Args: []string{mgr.dyncfgJobID(stockCfg), "enable"}, |
| 251 | })) |
| 252 | |
| 253 | sendConfGroup(in, discCfg.Source(), discCfg) |
| 254 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 255 | UID: "2-enable", |
| 256 | Args: []string{mgr.dyncfgJobID(discCfg), "enable"}, |
| 257 | })) |
| 258 | |
| 259 | sendConfGroup(in, userCfg.Source(), userCfg) |
| 260 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 261 | UID: "3-enable", |
| 262 | Args: []string{mgr.dyncfgJobID(userCfg), "enable"}, |
| 263 | })) |
| 264 | }, |
| 265 | wantDiscovered: []confgroup.Config{ |
| 266 | stockCfg, |
| 267 | userCfg, |
| 268 | discCfg, |
| 269 | }, |
| 270 | wantSeen: []confgroup.Config{ |
| 271 | stockCfg, |
| 272 | discCfg, |
| 273 | userCfg, |
| 274 | }, |
| 275 | wantExposed: []wantExposedEntry{ |
| 276 | {cfg: discCfg, status: dyncfg.StatusFailed}, |
| 277 | {cfg: userCfg, status: dyncfg.StatusFailed}, |
| 278 | }, |
| 279 | wantRunning: nil, |
| 280 | wantDyncfg: ` |
| 281 | CONFIG test:collector:fail:stock create accepted job /collectors/test/Jobs stock 'type=stock,module=fail,job=stock' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 282 | |
| 283 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 284 | {"status":200,"message":"job enable failed: mock failed init"} |
| 285 | FUNCTION_RESULT_END |
| 286 | |
| 287 | CONFIG test:collector:fail:stock delete |
| 288 | |
| 289 | CONFIG test:collector:fail:discovered create accepted job /collectors/test/Jobs discovered 'type=discovered,module=fail,job=discovered' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 290 | |
| 291 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 292 | {"status":200,"message":"job enable failed: mock failed init"} |
| 293 | FUNCTION_RESULT_END |
| 294 | |
| 295 | CONFIG test:collector:fail:discovered status failed |
| 296 | |
| 297 | CONFIG test:collector:fail:user create accepted job /collectors/test/Jobs user 'type=user,module=fail,job=user' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 298 | |
| 299 | FUNCTION_RESULT_BEGIN 3-enable 200 application/json |
| 300 | {"status":200,"message":"job enable failed: mock failed init"} |
| 301 | FUNCTION_RESULT_END |
| 302 | |
| 303 | CONFIG test:collector:fail:user status failed |
| 304 | `, |
| 305 | } |
| 306 | }, |
| 307 | }, |
| 308 | "non-dyncfg => nok: diff src,src prio asc,same name: add": { |
| 309 | createSim: func() *runSim { |
| 310 | stockCfg := prepareStockCfg("fail", "name") |
| 311 | discCfg := prepareDiscoveredCfg("fail", "name") |
| 312 | userCfg := prepareUserCfg("fail", "name") |
| 313 | |
| 314 | return &runSim{ |
| 315 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 316 | sendConfGroup(in, stockCfg.Source(), stockCfg) |
| 317 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 318 | UID: "1-enable", |
| 319 | Args: []string{mgr.dyncfgJobID(stockCfg), "enable"}, |
| 320 | })) |
| 321 | |
| 322 | sendConfGroup(in, discCfg.Source(), discCfg) |
| 323 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 324 | UID: "2-enable", |
| 325 | Args: []string{mgr.dyncfgJobID(discCfg), "enable"}, |
| 326 | })) |
| 327 | |
| 328 | sendConfGroup(in, userCfg.Source(), userCfg) |
| 329 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 330 | UID: "3-enable", |
| 331 | Args: []string{mgr.dyncfgJobID(userCfg), "enable"}, |
| 332 | })) |
| 333 | }, |
| 334 | wantDiscovered: []confgroup.Config{ |
| 335 | stockCfg, |
| 336 | userCfg, |
| 337 | discCfg, |
| 338 | }, |
| 339 | wantSeen: []confgroup.Config{ |
| 340 | stockCfg, |
| 341 | discCfg, |
| 342 | userCfg, |
| 343 | }, |
| 344 | wantExposed: []wantExposedEntry{ |
| 345 | {cfg: userCfg, status: dyncfg.StatusFailed}, |
| 346 | }, |
| 347 | wantRunning: nil, |
| 348 | wantDyncfg: ` |
| 349 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs stock 'type=stock,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 350 | |
| 351 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 352 | {"status":200,"message":"job enable failed: mock failed init"} |
| 353 | FUNCTION_RESULT_END |
| 354 | |
| 355 | CONFIG test:collector:fail:name delete |
| 356 | |
| 357 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs discovered 'type=discovered,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 358 | |
| 359 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 360 | {"status":200,"message":"job enable failed: mock failed init"} |
| 361 | FUNCTION_RESULT_END |
| 362 | |
| 363 | CONFIG test:collector:fail:name status failed |
| 364 | |
| 365 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs user 'type=user,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 366 | |
| 367 | FUNCTION_RESULT_BEGIN 3-enable 200 application/json |
| 368 | {"status":200,"message":"job enable failed: mock failed init"} |
| 369 | FUNCTION_RESULT_END |
| 370 | |
| 371 | CONFIG test:collector:fail:name status failed |
| 372 | `, |
| 373 | } |
| 374 | }, |
| 375 | }, |
| 376 | "non-dyncfg => nok: diff src,src prio asc,same name: add and remove": { |
| 377 | createSim: func() *runSim { |
| 378 | stockCfg := prepareStockCfg("fail", "name") |
| 379 | discCfg := prepareDiscoveredCfg("fail", "name") |
| 380 | userCfg := prepareUserCfg("fail", "name") |
| 381 | |
| 382 | return &runSim{ |
| 383 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 384 | sendConfGroup(in, stockCfg.Source(), stockCfg) |
| 385 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 386 | UID: "1-enable", |
| 387 | Args: []string{mgr.dyncfgJobID(stockCfg), "enable"}, |
| 388 | })) |
| 389 | |
| 390 | sendConfGroup(in, discCfg.Source(), discCfg) |
| 391 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 392 | UID: "2-enable", |
| 393 | Args: []string{mgr.dyncfgJobID(discCfg), "enable"}, |
| 394 | })) |
| 395 | |
| 396 | sendConfGroup(in, userCfg.Source(), userCfg) |
| 397 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 398 | UID: "3-enable", |
| 399 | Args: []string{mgr.dyncfgJobID(userCfg), "enable"}, |
| 400 | })) |
| 401 | |
| 402 | sendConfGroup(in, stockCfg.Source()) |
| 403 | sendConfGroup(in, discCfg.Source()) |
| 404 | sendConfGroup(in, userCfg.Source()) |
| 405 | }, |
| 406 | wantDiscovered: nil, |
| 407 | wantSeen: nil, |
| 408 | wantExposed: nil, |
| 409 | wantRunning: nil, |
| 410 | wantDyncfg: ` |
| 411 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs stock 'type=stock,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 412 | |
| 413 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 414 | {"status":200,"message":"job enable failed: mock failed init"} |
| 415 | FUNCTION_RESULT_END |
| 416 | |
| 417 | CONFIG test:collector:fail:name delete |
| 418 | |
| 419 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs discovered 'type=discovered,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 420 | |
| 421 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 422 | {"status":200,"message":"job enable failed: mock failed init"} |
| 423 | FUNCTION_RESULT_END |
| 424 | |
| 425 | CONFIG test:collector:fail:name status failed |
| 426 | |
| 427 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs user 'type=user,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 428 | |
| 429 | FUNCTION_RESULT_BEGIN 3-enable 200 application/json |
| 430 | {"status":200,"message":"job enable failed: mock failed init"} |
| 431 | FUNCTION_RESULT_END |
| 432 | |
| 433 | CONFIG test:collector:fail:name status failed |
| 434 | |
| 435 | CONFIG test:collector:fail:name delete |
| 436 | `, |
| 437 | } |
| 438 | }, |
| 439 | }, |
| 440 | "non-dyncfg => nok: diff src,src prio desc,same name: add": { |
| 441 | createSim: func() *runSim { |
| 442 | userCfg := prepareUserCfg("fail", "name") |
| 443 | discCfg := prepareDiscoveredCfg("fail", "name") |
| 444 | stockCfg := prepareStockCfg("fail", "name") |
| 445 | |
| 446 | return &runSim{ |
| 447 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 448 | sendConfGroup(in, userCfg.Source(), userCfg) |
| 449 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 450 | UID: "1-enable", |
| 451 | Args: []string{mgr.dyncfgJobID(userCfg), "enable"}, |
| 452 | })) |
| 453 | |
| 454 | sendConfGroup(in, discCfg.Source(), discCfg) |
| 455 | sendConfGroup(in, stockCfg.Source(), stockCfg) |
| 456 | }, |
| 457 | wantDiscovered: []confgroup.Config{ |
| 458 | stockCfg, |
| 459 | userCfg, |
| 460 | discCfg, |
| 461 | }, |
| 462 | wantSeen: []confgroup.Config{ |
| 463 | userCfg, |
| 464 | discCfg, |
| 465 | stockCfg, |
| 466 | }, |
| 467 | wantExposed: []wantExposedEntry{ |
| 468 | {cfg: userCfg, status: dyncfg.StatusFailed}, |
| 469 | }, |
| 470 | wantRunning: nil, |
| 471 | wantDyncfg: ` |
| 472 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs user 'type=user,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 473 | |
| 474 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 475 | {"status":200,"message":"job enable failed: mock failed init"} |
| 476 | FUNCTION_RESULT_END |
| 477 | |
| 478 | CONFIG test:collector:fail:name status failed |
| 479 | `, |
| 480 | } |
| 481 | }, |
| 482 | }, |
| 483 | "non-dyncfg => nok: diff src,src prio desc,same name: add and remove": { |
| 484 | createSim: func() *runSim { |
| 485 | userCfg := prepareUserCfg("fail", "name") |
| 486 | discCfg := prepareDiscoveredCfg("fail", "name") |
| 487 | stockCfg := prepareStockCfg("fail", "name") |
| 488 | |
| 489 | return &runSim{ |
| 490 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 491 | sendConfGroup(in, userCfg.Source(), userCfg) |
| 492 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 493 | UID: "1-enable", |
| 494 | Args: []string{mgr.dyncfgJobID(userCfg), "enable"}, |
| 495 | })) |
| 496 | |
| 497 | sendConfGroup(in, discCfg.Source(), discCfg) |
| 498 | sendConfGroup(in, stockCfg.Source(), stockCfg) |
| 499 | |
| 500 | sendConfGroup(in, userCfg.Source()) |
| 501 | sendConfGroup(in, discCfg.Source()) |
| 502 | sendConfGroup(in, stockCfg.Source()) |
| 503 | }, |
| 504 | wantDiscovered: nil, |
| 505 | wantSeen: nil, |
| 506 | wantExposed: nil, |
| 507 | wantRunning: nil, |
| 508 | wantDyncfg: ` |
| 509 | CONFIG test:collector:fail:name create accepted job /collectors/test/Jobs user 'type=user,module=fail,job=name' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 510 | |
| 511 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 512 | {"status":200,"message":"job enable failed: mock failed init"} |
| 513 | FUNCTION_RESULT_END |
| 514 | |
| 515 | CONFIG test:collector:fail:name status failed |
| 516 | |
| 517 | CONFIG test:collector:fail:name delete |
| 518 | `, |
| 519 | } |
| 520 | }, |
| 521 | }, |
| 522 | } |
| 523 | |
| 524 | for name, test := range tests { |
| 525 | t.Run(name, func(t *testing.T) { |
| 526 | sim := test.createSim() |
| 527 | sim.run(t) |
| 528 | }) |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | func TestManager_Run_Dyncfg_Get(t *testing.T) { |
| 533 | t.Setenv("DYNCFG_GET_SECRET", "resolved-secret-value") |
| 534 | |
| 535 | tests := map[string]struct { |
| 536 | createSim func() *runSim |
| 537 | }{ |
| 538 | "[get] non-existing": { |
| 539 | createSim: func() *runSim { |
| 540 | cfg := prepareDyncfgCfg("success", "test") |
| 541 | |
| 542 | return &runSim{ |
| 543 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 544 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 545 | UID: "1-get", |
| 546 | Args: []string{mgr.dyncfgJobID(cfg), "get"}, |
| 547 | })) |
| 548 | }, |
| 549 | wantDiscovered: nil, |
| 550 | wantSeen: nil, |
| 551 | wantExposed: nil, |
| 552 | wantRunning: nil, |
| 553 | wantDyncfg: ` |
| 554 | |
| 555 | FUNCTION_RESULT_BEGIN 1-get 404 application/json |
| 556 | {"status":404,"errorMessage":"The specified module 'success' job 'test' is not registered."} |
| 557 | FUNCTION_RESULT_END |
| 558 | `, |
| 559 | } |
| 560 | }, |
| 561 | }, |
| 562 | "[get] existing": { |
| 563 | createSim: func() *runSim { |
| 564 | cfg := prepareDyncfgCfg("success", "test"). |
| 565 | Set("option_str", "1"). |
| 566 | Set("option_int", 1) |
| 567 | bs, _ := json.Marshal(cfg) |
| 568 | |
| 569 | return &runSim{ |
| 570 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 571 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 572 | UID: "1-add", |
| 573 | Source: "type=dyncfg", |
| 574 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 575 | Payload: bs, |
| 576 | })) |
| 577 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 578 | UID: "2-get", |
| 579 | Args: []string{mgr.dyncfgJobID(cfg), "get"}, |
| 580 | })) |
| 581 | }, |
| 582 | wantDiscovered: nil, |
| 583 | wantSeen: []confgroup.Config{ |
| 584 | cfg, |
| 585 | }, |
| 586 | wantExposed: []wantExposedEntry{ |
| 587 | {cfg: cfg, status: dyncfg.StatusAccepted}, |
| 588 | }, |
| 589 | wantRunning: nil, |
| 590 | wantDyncfg: ` |
| 591 | |
| 592 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 593 | {"status":202,"message":""} |
| 594 | FUNCTION_RESULT_END |
| 595 | |
| 596 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 597 | |
| 598 | FUNCTION_RESULT_BEGIN 2-get 200 application/json |
| 599 | {"option_str":"1","option_int":1} |
| 600 | FUNCTION_RESULT_END |
| 601 | `, |
| 602 | } |
| 603 | }, |
| 604 | }, |
| 605 | "[get] existing with secret ref remains unresolved": { |
| 606 | createSim: func() *runSim { |
| 607 | cfg := prepareDyncfgCfg("success", "test"). |
| 608 | Set("option_str", "${env:DYNCFG_GET_SECRET}") |
| 609 | bs, _ := json.Marshal(cfg) |
| 610 | |
| 611 | return &runSim{ |
| 612 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 613 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 614 | UID: "1-add", |
| 615 | Source: "type=dyncfg", |
| 616 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 617 | Payload: bs, |
| 618 | })) |
| 619 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 620 | UID: "2-get", |
| 621 | Args: []string{mgr.dyncfgJobID(cfg), "get"}, |
| 622 | })) |
| 623 | }, |
| 624 | wantDiscovered: nil, |
| 625 | wantSeen: []confgroup.Config{ |
| 626 | cfg, |
| 627 | }, |
| 628 | wantExposed: []wantExposedEntry{ |
| 629 | {cfg: cfg, status: dyncfg.StatusAccepted}, |
| 630 | }, |
| 631 | wantRunning: nil, |
| 632 | wantDyncfg: ` |
| 633 | |
| 634 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 635 | {"status":202,"message":""} |
| 636 | FUNCTION_RESULT_END |
| 637 | |
| 638 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 639 | |
| 640 | FUNCTION_RESULT_BEGIN 2-get 200 application/json |
| 641 | {"option_str":"${env:DYNCFG_GET_SECRET}","option_int":0} |
| 642 | FUNCTION_RESULT_END |
| 643 | `, |
| 644 | } |
| 645 | }, |
| 646 | }, |
| 647 | } |
| 648 | |
| 649 | for name, test := range tests { |
| 650 | t.Run(name, func(t *testing.T) { |
| 651 | sim := test.createSim() |
| 652 | sim.run(t) |
| 653 | }) |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | func TestManager_Run_Dyncfg_Userconfig(t *testing.T) { |
| 658 | tests := map[string]struct { |
| 659 | createSim func() *runSim |
| 660 | }{ |
| 661 | "[userconfig] existing": { |
| 662 | createSim: func() *runSim { |
| 663 | cfg := prepareDyncfgCfg("success", "test") |
| 664 | |
| 665 | return &runSim{ |
| 666 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 667 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 668 | UID: "1-userconfig", |
| 669 | Args: []string{mgr.dyncfgJobID(cfg), "userconfig"}, |
| 670 | })) |
| 671 | }, |
| 672 | wantDiscovered: nil, |
| 673 | wantSeen: nil, |
| 674 | wantExposed: nil, |
| 675 | wantRunning: nil, |
| 676 | wantDyncfg: ` |
| 677 | FUNCTION_RESULT_BEGIN 1-userconfig 200 application/yaml |
| 678 | jobs: |
| 679 | - name: test |
| 680 | option_one: one |
| 681 | option_two: 2 |
| 682 | |
| 683 | FUNCTION_RESULT_END |
| 684 | `, |
| 685 | } |
| 686 | }, |
| 687 | }, |
| 688 | "[userconfig] non-existing": { |
| 689 | createSim: func() *runSim { |
| 690 | cfg := prepareDyncfgCfg("success!", "test") |
| 691 | |
| 692 | return &runSim{ |
| 693 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 694 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 695 | UID: "1-userconfig", |
| 696 | Args: []string{mgr.dyncfgJobID(cfg), "userconfig"}, |
| 697 | })) |
| 698 | }, |
| 699 | wantDiscovered: nil, |
| 700 | wantSeen: nil, |
| 701 | wantExposed: nil, |
| 702 | wantRunning: nil, |
| 703 | wantDyncfg: ` |
| 704 | FUNCTION_RESULT_BEGIN 1-userconfig 404 application/json |
| 705 | {"status":404,"errorMessage":"The specified module 'success!' is not registered."} |
| 706 | FUNCTION_RESULT_END |
| 707 | `, |
| 708 | } |
| 709 | }, |
| 710 | }, |
| 711 | } |
| 712 | |
| 713 | for name, test := range tests { |
| 714 | t.Run(name, func(t *testing.T) { |
| 715 | sim := test.createSim() |
| 716 | sim.run(t) |
| 717 | }) |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | func TestManager_Run_Dyncfg_Add(t *testing.T) { |
| 722 | tests := map[string]struct { |
| 723 | createSim func() *runSim |
| 724 | }{ |
| 725 | "[add] dyncfg:ok": { |
| 726 | createSim: func() *runSim { |
| 727 | cfg := prepareDyncfgCfg("success", "test") |
| 728 | |
| 729 | return &runSim{ |
| 730 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 731 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 732 | UID: "1-add", |
| 733 | Source: "type=dyncfg", |
| 734 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 735 | Payload: []byte("{}"), |
| 736 | })) |
| 737 | }, |
| 738 | wantDiscovered: nil, |
| 739 | wantSeen: []confgroup.Config{ |
| 740 | cfg, |
| 741 | }, |
| 742 | wantExposed: []wantExposedEntry{ |
| 743 | {cfg: cfg, status: dyncfg.StatusAccepted}, |
| 744 | }, |
| 745 | wantRunning: nil, |
| 746 | wantDyncfg: ` |
| 747 | |
| 748 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 749 | {"status":202,"message":""} |
| 750 | FUNCTION_RESULT_END |
| 751 | |
| 752 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 753 | `, |
| 754 | } |
| 755 | }, |
| 756 | }, |
| 757 | "[add] dyncfg:nok": { |
| 758 | createSim: func() *runSim { |
| 759 | cfg := prepareDyncfgCfg("fail", "test") |
| 760 | |
| 761 | return &runSim{ |
| 762 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 763 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 764 | UID: "1-add", |
| 765 | Source: "type=dyncfg", |
| 766 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 767 | Payload: []byte("{}"), |
| 768 | })) |
| 769 | }, |
| 770 | wantDiscovered: nil, |
| 771 | wantSeen: []confgroup.Config{ |
| 772 | cfg, |
| 773 | }, |
| 774 | wantExposed: []wantExposedEntry{ |
| 775 | {cfg: cfg, status: dyncfg.StatusAccepted}, |
| 776 | }, |
| 777 | wantRunning: nil, |
| 778 | wantDyncfg: ` |
| 779 | |
| 780 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 781 | {"status":202,"message":""} |
| 782 | FUNCTION_RESULT_END |
| 783 | |
| 784 | CONFIG test:collector:fail:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 785 | `, |
| 786 | } |
| 787 | }, |
| 788 | }, |
| 789 | "[add] dyncfg:ok twice": { |
| 790 | createSim: func() *runSim { |
| 791 | cfg := prepareDyncfgCfg("success", "test") |
| 792 | |
| 793 | return &runSim{ |
| 794 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 795 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 796 | UID: "1-add", |
| 797 | Source: "type=dyncfg", |
| 798 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 799 | Payload: []byte("{}"), |
| 800 | })) |
| 801 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 802 | UID: "2-add", |
| 803 | Source: "type=dyncfg", |
| 804 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 805 | Payload: []byte("{}"), |
| 806 | })) |
| 807 | }, |
| 808 | wantDiscovered: nil, |
| 809 | wantSeen: []confgroup.Config{ |
| 810 | cfg, |
| 811 | }, |
| 812 | wantExposed: []wantExposedEntry{ |
| 813 | {cfg: cfg, status: dyncfg.StatusAccepted}, |
| 814 | }, |
| 815 | wantRunning: nil, |
| 816 | wantDyncfg: ` |
| 817 | |
| 818 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 819 | {"status":202,"message":""} |
| 820 | FUNCTION_RESULT_END |
| 821 | |
| 822 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 823 | |
| 824 | FUNCTION_RESULT_BEGIN 2-add 202 application/json |
| 825 | {"status":202,"message":""} |
| 826 | FUNCTION_RESULT_END |
| 827 | |
| 828 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 829 | `, |
| 830 | } |
| 831 | }, |
| 832 | }, |
| 833 | } |
| 834 | |
| 835 | for name, test := range tests { |
| 836 | t.Run(name, func(t *testing.T) { |
| 837 | sim := test.createSim() |
| 838 | sim.run(t) |
| 839 | }) |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | func TestManager_Run_Dyncfg_Enable(t *testing.T) { |
| 844 | tests := map[string]struct { |
| 845 | createSim func() *runSim |
| 846 | }{ |
| 847 | "[enable] non-existing": { |
| 848 | createSim: func() *runSim { |
| 849 | cfg := prepareDyncfgCfg("success", "test") |
| 850 | |
| 851 | return &runSim{ |
| 852 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 853 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 854 | UID: "1-enable", |
| 855 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 856 | })) |
| 857 | }, |
| 858 | wantDiscovered: nil, |
| 859 | wantSeen: nil, |
| 860 | wantExposed: nil, |
| 861 | wantRunning: nil, |
| 862 | wantDyncfg: ` |
| 863 | |
| 864 | FUNCTION_RESULT_BEGIN 1-enable 404 application/json |
| 865 | {"status":404,"errorMessage":"job not found."} |
| 866 | FUNCTION_RESULT_END |
| 867 | `, |
| 868 | } |
| 869 | }, |
| 870 | }, |
| 871 | "[enable] dyncfg:ok": { |
| 872 | createSim: func() *runSim { |
| 873 | cfg := prepareDyncfgCfg("success", "test") |
| 874 | |
| 875 | return &runSim{ |
| 876 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 877 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 878 | UID: "1-add", |
| 879 | Source: "type=dyncfg", |
| 880 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 881 | Payload: []byte("{}"), |
| 882 | })) |
| 883 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 884 | UID: "2-enable", |
| 885 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 886 | })) |
| 887 | }, |
| 888 | wantDiscovered: nil, |
| 889 | wantSeen: []confgroup.Config{ |
| 890 | cfg, |
| 891 | }, |
| 892 | wantExposed: []wantExposedEntry{ |
| 893 | {cfg: cfg, status: dyncfg.StatusRunning}, |
| 894 | }, |
| 895 | wantRunning: []string{cfg.FullName()}, |
| 896 | wantDyncfg: ` |
| 897 | |
| 898 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 899 | {"status":202,"message":""} |
| 900 | FUNCTION_RESULT_END |
| 901 | |
| 902 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 903 | |
| 904 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 905 | {"status":200,"message":""} |
| 906 | FUNCTION_RESULT_END |
| 907 | |
| 908 | CONFIG test:collector:success:test status running |
| 909 | `, |
| 910 | } |
| 911 | }, |
| 912 | }, |
| 913 | "[enable] dyncfg:ok twice": { |
| 914 | createSim: func() *runSim { |
| 915 | cfg := prepareDyncfgCfg("success", "test") |
| 916 | |
| 917 | return &runSim{ |
| 918 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 919 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 920 | UID: "1-add", |
| 921 | Source: "type=dyncfg", |
| 922 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 923 | Payload: []byte("{}"), |
| 924 | })) |
| 925 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 926 | UID: "2-enable", |
| 927 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 928 | })) |
| 929 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 930 | UID: "3-enable", |
| 931 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 932 | })) |
| 933 | }, |
| 934 | wantDiscovered: nil, |
| 935 | wantSeen: []confgroup.Config{ |
| 936 | cfg, |
| 937 | }, |
| 938 | wantExposed: []wantExposedEntry{ |
| 939 | {cfg: cfg, status: dyncfg.StatusRunning}, |
| 940 | }, |
| 941 | wantRunning: []string{cfg.FullName()}, |
| 942 | wantDyncfg: ` |
| 943 | |
| 944 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 945 | {"status":202,"message":""} |
| 946 | FUNCTION_RESULT_END |
| 947 | |
| 948 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 949 | |
| 950 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 951 | {"status":200,"message":""} |
| 952 | FUNCTION_RESULT_END |
| 953 | |
| 954 | CONFIG test:collector:success:test status running |
| 955 | |
| 956 | FUNCTION_RESULT_BEGIN 3-enable 200 application/json |
| 957 | {"status":200,"message":""} |
| 958 | FUNCTION_RESULT_END |
| 959 | |
| 960 | CONFIG test:collector:success:test status running |
| 961 | `, |
| 962 | } |
| 963 | }, |
| 964 | }, |
| 965 | "[enable] dyncfg:nok": { |
| 966 | createSim: func() *runSim { |
| 967 | cfg := prepareDyncfgCfg("fail", "test") |
| 968 | |
| 969 | return &runSim{ |
| 970 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 971 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 972 | UID: "1-add", |
| 973 | Source: "type=dyncfg", |
| 974 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 975 | Payload: []byte("{}"), |
| 976 | })) |
| 977 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 978 | UID: "2-enable", |
| 979 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 980 | })) |
| 981 | }, |
| 982 | wantDiscovered: nil, |
| 983 | wantSeen: []confgroup.Config{ |
| 984 | cfg, |
| 985 | }, |
| 986 | wantExposed: []wantExposedEntry{ |
| 987 | {cfg: cfg, status: dyncfg.StatusFailed}, |
| 988 | }, |
| 989 | wantRunning: nil, |
| 990 | wantDyncfg: ` |
| 991 | |
| 992 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 993 | {"status":202,"message":""} |
| 994 | FUNCTION_RESULT_END |
| 995 | |
| 996 | CONFIG test:collector:fail:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 997 | |
| 998 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 999 | {"status":200,"message":"job enable failed: mock failed init"} |
| 1000 | FUNCTION_RESULT_END |
| 1001 | |
| 1002 | CONFIG test:collector:fail:test status failed |
| 1003 | `, |
| 1004 | } |
| 1005 | }, |
| 1006 | }, |
| 1007 | "[enable] dyncfg:nok twice": { |
| 1008 | createSim: func() *runSim { |
| 1009 | cfg := prepareDyncfgCfg("fail", "test") |
| 1010 | |
| 1011 | return &runSim{ |
| 1012 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1013 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1014 | UID: "1-add", |
| 1015 | Source: "type=dyncfg", |
| 1016 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1017 | Payload: []byte("{}"), |
| 1018 | })) |
| 1019 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1020 | UID: "2-enable", |
| 1021 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 1022 | })) |
| 1023 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1024 | UID: "3-enable", |
| 1025 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 1026 | })) |
| 1027 | }, |
| 1028 | wantDiscovered: nil, |
| 1029 | wantSeen: []confgroup.Config{ |
| 1030 | cfg, |
| 1031 | }, |
| 1032 | wantExposed: []wantExposedEntry{ |
| 1033 | {cfg: cfg, status: dyncfg.StatusFailed}, |
| 1034 | }, |
| 1035 | wantRunning: nil, |
| 1036 | wantDyncfg: ` |
| 1037 | |
| 1038 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1039 | {"status":202,"message":""} |
| 1040 | FUNCTION_RESULT_END |
| 1041 | |
| 1042 | CONFIG test:collector:fail:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1043 | |
| 1044 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 1045 | {"status":200,"message":"job enable failed: mock failed init"} |
| 1046 | FUNCTION_RESULT_END |
| 1047 | |
| 1048 | CONFIG test:collector:fail:test status failed |
| 1049 | |
| 1050 | FUNCTION_RESULT_BEGIN 3-enable 200 application/json |
| 1051 | {"status":200,"message":"job enable failed: mock failed init"} |
| 1052 | FUNCTION_RESULT_END |
| 1053 | |
| 1054 | CONFIG test:collector:fail:test status failed |
| 1055 | `, |
| 1056 | } |
| 1057 | }, |
| 1058 | }, |
| 1059 | } |
| 1060 | |
| 1061 | for name, test := range tests { |
| 1062 | t.Run(name, func(t *testing.T) { |
| 1063 | sim := test.createSim() |
| 1064 | sim.run(t) |
| 1065 | }) |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | func TestManager_Run_Dyncfg_Disable(t *testing.T) { |
| 1070 | tests := map[string]struct { |
| 1071 | createSim func() *runSim |
| 1072 | }{ |
| 1073 | "[disable] non-existing": { |
| 1074 | createSim: func() *runSim { |
| 1075 | cfg := prepareDyncfgCfg("success", "test") |
| 1076 | |
| 1077 | return &runSim{ |
| 1078 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1079 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1080 | UID: "1-disable", |
| 1081 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1082 | })) |
| 1083 | }, |
| 1084 | wantDiscovered: nil, |
| 1085 | wantSeen: nil, |
| 1086 | wantExposed: nil, |
| 1087 | wantRunning: nil, |
| 1088 | wantDyncfg: ` |
| 1089 | |
| 1090 | FUNCTION_RESULT_BEGIN 1-disable 404 application/json |
| 1091 | {"status":404,"errorMessage":"job not found."} |
| 1092 | FUNCTION_RESULT_END |
| 1093 | `, |
| 1094 | } |
| 1095 | }, |
| 1096 | }, |
| 1097 | "[disable] dyncfg:ok": { |
| 1098 | createSim: func() *runSim { |
| 1099 | cfg := prepareDyncfgCfg("success", "test") |
| 1100 | |
| 1101 | return &runSim{ |
| 1102 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1103 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1104 | UID: "1-add", |
| 1105 | Source: "type=dyncfg", |
| 1106 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1107 | Payload: []byte("{}"), |
| 1108 | })) |
| 1109 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1110 | UID: "2-disable", |
| 1111 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1112 | })) |
| 1113 | }, |
| 1114 | wantDiscovered: nil, |
| 1115 | wantSeen: []confgroup.Config{ |
| 1116 | cfg, |
| 1117 | }, |
| 1118 | wantExposed: []wantExposedEntry{ |
| 1119 | {cfg: cfg, status: dyncfg.StatusDisabled}, |
| 1120 | }, |
| 1121 | wantRunning: nil, |
| 1122 | wantDyncfg: ` |
| 1123 | |
| 1124 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1125 | {"status":202,"message":""} |
| 1126 | FUNCTION_RESULT_END |
| 1127 | |
| 1128 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1129 | |
| 1130 | FUNCTION_RESULT_BEGIN 2-disable 200 application/json |
| 1131 | {"status":200,"message":""} |
| 1132 | FUNCTION_RESULT_END |
| 1133 | |
| 1134 | CONFIG test:collector:success:test status disabled |
| 1135 | `, |
| 1136 | } |
| 1137 | }, |
| 1138 | }, |
| 1139 | "[disable] dyncfg:ok twice": { |
| 1140 | createSim: func() *runSim { |
| 1141 | cfg := prepareDyncfgCfg("success", "test") |
| 1142 | |
| 1143 | return &runSim{ |
| 1144 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1145 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1146 | UID: "1-add", |
| 1147 | Source: "type=dyncfg", |
| 1148 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1149 | Payload: []byte("{}"), |
| 1150 | })) |
| 1151 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1152 | UID: "2-disable", |
| 1153 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1154 | })) |
| 1155 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1156 | UID: "3-disable", |
| 1157 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1158 | })) |
| 1159 | }, |
| 1160 | wantDiscovered: nil, |
| 1161 | wantSeen: []confgroup.Config{ |
| 1162 | cfg, |
| 1163 | }, |
| 1164 | wantExposed: []wantExposedEntry{ |
| 1165 | {cfg: cfg, status: dyncfg.StatusDisabled}, |
| 1166 | }, |
| 1167 | wantRunning: nil, |
| 1168 | wantDyncfg: ` |
| 1169 | |
| 1170 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1171 | {"status":202,"message":""} |
| 1172 | FUNCTION_RESULT_END |
| 1173 | |
| 1174 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1175 | |
| 1176 | FUNCTION_RESULT_BEGIN 2-disable 200 application/json |
| 1177 | {"status":200,"message":""} |
| 1178 | FUNCTION_RESULT_END |
| 1179 | |
| 1180 | CONFIG test:collector:success:test status disabled |
| 1181 | |
| 1182 | FUNCTION_RESULT_BEGIN 3-disable 200 application/json |
| 1183 | {"status":200,"message":""} |
| 1184 | FUNCTION_RESULT_END |
| 1185 | |
| 1186 | CONFIG test:collector:success:test status disabled |
| 1187 | `, |
| 1188 | } |
| 1189 | }, |
| 1190 | }, |
| 1191 | "[disable] dyncfg:nok": { |
| 1192 | createSim: func() *runSim { |
| 1193 | cfg := prepareDyncfgCfg("fail", "test") |
| 1194 | |
| 1195 | return &runSim{ |
| 1196 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1197 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1198 | UID: "1-add", |
| 1199 | Source: "type=dyncfg", |
| 1200 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1201 | Payload: []byte("{}"), |
| 1202 | })) |
| 1203 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1204 | UID: "2-disable", |
| 1205 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1206 | })) |
| 1207 | }, |
| 1208 | wantDiscovered: nil, |
| 1209 | wantSeen: []confgroup.Config{ |
| 1210 | cfg, |
| 1211 | }, |
| 1212 | wantExposed: []wantExposedEntry{ |
| 1213 | {cfg: cfg, status: dyncfg.StatusDisabled}, |
| 1214 | }, |
| 1215 | wantRunning: nil, |
| 1216 | wantDyncfg: ` |
| 1217 | |
| 1218 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1219 | {"status":202,"message":""} |
| 1220 | FUNCTION_RESULT_END |
| 1221 | |
| 1222 | CONFIG test:collector:fail:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1223 | |
| 1224 | FUNCTION_RESULT_BEGIN 2-disable 200 application/json |
| 1225 | {"status":200,"message":""} |
| 1226 | FUNCTION_RESULT_END |
| 1227 | |
| 1228 | CONFIG test:collector:fail:test status disabled |
| 1229 | `, |
| 1230 | } |
| 1231 | }, |
| 1232 | }, |
| 1233 | "[disable] dyncfg:nok twice": { |
| 1234 | createSim: func() *runSim { |
| 1235 | cfg := prepareDyncfgCfg("fail", "test") |
| 1236 | |
| 1237 | return &runSim{ |
| 1238 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1239 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1240 | UID: "1-add", |
| 1241 | Source: "type=dyncfg", |
| 1242 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1243 | Payload: []byte("{}"), |
| 1244 | })) |
| 1245 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1246 | UID: "2-disable", |
| 1247 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1248 | })) |
| 1249 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1250 | UID: "3-disable", |
| 1251 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1252 | })) |
| 1253 | }, |
| 1254 | wantDiscovered: nil, |
| 1255 | wantSeen: []confgroup.Config{ |
| 1256 | cfg, |
| 1257 | }, |
| 1258 | wantExposed: []wantExposedEntry{ |
| 1259 | {cfg: cfg, status: dyncfg.StatusDisabled}, |
| 1260 | }, |
| 1261 | wantRunning: nil, |
| 1262 | wantDyncfg: ` |
| 1263 | |
| 1264 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1265 | {"status":202,"message":""} |
| 1266 | FUNCTION_RESULT_END |
| 1267 | |
| 1268 | CONFIG test:collector:fail:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1269 | |
| 1270 | FUNCTION_RESULT_BEGIN 2-disable 200 application/json |
| 1271 | {"status":200,"message":""} |
| 1272 | FUNCTION_RESULT_END |
| 1273 | |
| 1274 | CONFIG test:collector:fail:test status disabled |
| 1275 | |
| 1276 | FUNCTION_RESULT_BEGIN 3-disable 200 application/json |
| 1277 | {"status":200,"message":""} |
| 1278 | FUNCTION_RESULT_END |
| 1279 | |
| 1280 | CONFIG test:collector:fail:test status disabled |
| 1281 | `, |
| 1282 | } |
| 1283 | }, |
| 1284 | }, |
| 1285 | } |
| 1286 | |
| 1287 | for name, test := range tests { |
| 1288 | t.Run(name, func(t *testing.T) { |
| 1289 | sim := test.createSim() |
| 1290 | sim.run(t) |
| 1291 | }) |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | func TestManager_Run_Dyncfg_Restart(t *testing.T) { |
| 1296 | tests := map[string]struct { |
| 1297 | createSim func() *runSim |
| 1298 | }{ |
| 1299 | "[restart] non-existing": { |
| 1300 | createSim: func() *runSim { |
| 1301 | cfg := prepareDyncfgCfg("success", "test") |
| 1302 | |
| 1303 | return &runSim{ |
| 1304 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1305 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1306 | UID: "1-restart", |
| 1307 | Args: []string{mgr.dyncfgJobID(cfg), "restart"}, |
| 1308 | })) |
| 1309 | }, |
| 1310 | wantDiscovered: nil, |
| 1311 | wantSeen: nil, |
| 1312 | wantExposed: nil, |
| 1313 | wantRunning: nil, |
| 1314 | wantDyncfg: ` |
| 1315 | |
| 1316 | FUNCTION_RESULT_BEGIN 1-restart 404 application/json |
| 1317 | {"status":404,"errorMessage":"job not found."} |
| 1318 | FUNCTION_RESULT_END |
| 1319 | `, |
| 1320 | } |
| 1321 | }, |
| 1322 | }, |
| 1323 | "[restart] not enabled dyncfg:ok": { |
| 1324 | createSim: func() *runSim { |
| 1325 | cfg := prepareDyncfgCfg("success", "test") |
| 1326 | |
| 1327 | return &runSim{ |
| 1328 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1329 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1330 | UID: "1-add", |
| 1331 | Source: "type=dyncfg", |
| 1332 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1333 | Payload: []byte("{}"), |
| 1334 | })) |
| 1335 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1336 | UID: "2-restart", |
| 1337 | Args: []string{mgr.dyncfgJobID(cfg), "restart"}, |
| 1338 | })) |
| 1339 | }, |
| 1340 | wantDiscovered: nil, |
| 1341 | wantSeen: []confgroup.Config{ |
| 1342 | cfg, |
| 1343 | }, |
| 1344 | wantExposed: []wantExposedEntry{ |
| 1345 | {cfg: cfg, status: dyncfg.StatusAccepted}, |
| 1346 | }, |
| 1347 | wantRunning: nil, |
| 1348 | wantDyncfg: ` |
| 1349 | |
| 1350 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1351 | {"status":202,"message":""} |
| 1352 | FUNCTION_RESULT_END |
| 1353 | |
| 1354 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1355 | |
| 1356 | FUNCTION_RESULT_BEGIN 2-restart 405 application/json |
| 1357 | {"status":405,"errorMessage":"restarting is not allowed in 'accepted' state."} |
| 1358 | FUNCTION_RESULT_END |
| 1359 | |
| 1360 | CONFIG test:collector:success:test status accepted |
| 1361 | `, |
| 1362 | } |
| 1363 | }, |
| 1364 | }, |
| 1365 | "[restart] enabled dyncfg:ok": { |
| 1366 | createSim: func() *runSim { |
| 1367 | cfg := prepareDyncfgCfg("success", "test") |
| 1368 | |
| 1369 | return &runSim{ |
| 1370 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1371 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1372 | UID: "1-add", |
| 1373 | Source: "type=dyncfg", |
| 1374 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1375 | Payload: []byte("{}"), |
| 1376 | })) |
| 1377 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1378 | UID: "2-enable", |
| 1379 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 1380 | })) |
| 1381 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1382 | UID: "3-restart", |
| 1383 | Args: []string{mgr.dyncfgJobID(cfg), "restart"}, |
| 1384 | })) |
| 1385 | }, |
| 1386 | wantDiscovered: nil, |
| 1387 | wantSeen: []confgroup.Config{ |
| 1388 | cfg, |
| 1389 | }, |
| 1390 | wantExposed: []wantExposedEntry{ |
| 1391 | {cfg: cfg, status: dyncfg.StatusRunning}, |
| 1392 | }, |
| 1393 | wantRunning: []string{cfg.FullName()}, |
| 1394 | wantDyncfg: ` |
| 1395 | |
| 1396 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1397 | {"status":202,"message":""} |
| 1398 | FUNCTION_RESULT_END |
| 1399 | |
| 1400 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1401 | |
| 1402 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 1403 | {"status":200,"message":""} |
| 1404 | FUNCTION_RESULT_END |
| 1405 | |
| 1406 | CONFIG test:collector:success:test status running |
| 1407 | |
| 1408 | FUNCTION_RESULT_BEGIN 3-restart 200 application/json |
| 1409 | {"status":200,"message":""} |
| 1410 | FUNCTION_RESULT_END |
| 1411 | |
| 1412 | CONFIG test:collector:success:test status running |
| 1413 | `, |
| 1414 | } |
| 1415 | }, |
| 1416 | }, |
| 1417 | "[restart] disabled dyncfg:ok": { |
| 1418 | createSim: func() *runSim { |
| 1419 | cfg := prepareDyncfgCfg("success", "test") |
| 1420 | |
| 1421 | return &runSim{ |
| 1422 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1423 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1424 | UID: "1-add", |
| 1425 | Source: "type=dyncfg", |
| 1426 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1427 | Payload: []byte("{}"), |
| 1428 | })) |
| 1429 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1430 | UID: "2-disable", |
| 1431 | Args: []string{mgr.dyncfgJobID(cfg), "disable"}, |
| 1432 | })) |
| 1433 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1434 | UID: "3-restart", |
| 1435 | Args: []string{mgr.dyncfgJobID(cfg), "restart"}, |
| 1436 | })) |
| 1437 | }, |
| 1438 | wantDiscovered: nil, |
| 1439 | wantSeen: []confgroup.Config{ |
| 1440 | cfg, |
| 1441 | }, |
| 1442 | wantExposed: []wantExposedEntry{ |
| 1443 | {cfg: cfg, status: dyncfg.StatusDisabled}, |
| 1444 | }, |
| 1445 | wantRunning: nil, |
| 1446 | wantDyncfg: ` |
| 1447 | |
| 1448 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1449 | {"status":202,"message":""} |
| 1450 | FUNCTION_RESULT_END |
| 1451 | |
| 1452 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1453 | |
| 1454 | FUNCTION_RESULT_BEGIN 2-disable 200 application/json |
| 1455 | {"status":200,"message":""} |
| 1456 | FUNCTION_RESULT_END |
| 1457 | |
| 1458 | CONFIG test:collector:success:test status disabled |
| 1459 | |
| 1460 | FUNCTION_RESULT_BEGIN 3-restart 405 application/json |
| 1461 | {"status":405,"errorMessage":"restarting is not allowed in 'disabled' state."} |
| 1462 | FUNCTION_RESULT_END |
| 1463 | |
| 1464 | CONFIG test:collector:success:test status disabled |
| 1465 | `, |
| 1466 | } |
| 1467 | }, |
| 1468 | }, |
| 1469 | "[restart] enabled dyncfg:ok multiple times": { |
| 1470 | createSim: func() *runSim { |
| 1471 | cfg := prepareDyncfgCfg("success", "test") |
| 1472 | |
| 1473 | return &runSim{ |
| 1474 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1475 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1476 | UID: "1-add", |
| 1477 | Source: "type=dyncfg", |
| 1478 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1479 | Payload: []byte("{}"), |
| 1480 | })) |
| 1481 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1482 | UID: "2-enable", |
| 1483 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 1484 | })) |
| 1485 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1486 | UID: "3-restart", |
| 1487 | Args: []string{mgr.dyncfgJobID(cfg), "restart"}, |
| 1488 | })) |
| 1489 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1490 | UID: "4-restart", |
| 1491 | Args: []string{mgr.dyncfgJobID(cfg), "restart"}, |
| 1492 | })) |
| 1493 | }, |
| 1494 | wantDiscovered: nil, |
| 1495 | wantSeen: []confgroup.Config{ |
| 1496 | cfg, |
| 1497 | }, |
| 1498 | wantExposed: []wantExposedEntry{ |
| 1499 | {cfg: cfg, status: dyncfg.StatusRunning}, |
| 1500 | }, |
| 1501 | wantRunning: []string{cfg.FullName()}, |
| 1502 | wantDyncfg: ` |
| 1503 | |
| 1504 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1505 | {"status":202,"message":""} |
| 1506 | FUNCTION_RESULT_END |
| 1507 | |
| 1508 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1509 | |
| 1510 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 1511 | {"status":200,"message":""} |
| 1512 | FUNCTION_RESULT_END |
| 1513 | |
| 1514 | CONFIG test:collector:success:test status running |
| 1515 | |
| 1516 | FUNCTION_RESULT_BEGIN 3-restart 200 application/json |
| 1517 | {"status":200,"message":""} |
| 1518 | FUNCTION_RESULT_END |
| 1519 | |
| 1520 | CONFIG test:collector:success:test status running |
| 1521 | |
| 1522 | FUNCTION_RESULT_BEGIN 4-restart 200 application/json |
| 1523 | {"status":200,"message":""} |
| 1524 | FUNCTION_RESULT_END |
| 1525 | |
| 1526 | CONFIG test:collector:success:test status running |
| 1527 | `, |
| 1528 | } |
| 1529 | }, |
| 1530 | }, |
| 1531 | } |
| 1532 | |
| 1533 | for name, test := range tests { |
| 1534 | t.Run(name, func(t *testing.T) { |
| 1535 | sim := test.createSim() |
| 1536 | sim.run(t) |
| 1537 | }) |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | func TestManager_Run_Dyncfg_Remove(t *testing.T) { |
| 1542 | tests := map[string]struct { |
| 1543 | createSim func() *runSim |
| 1544 | }{ |
| 1545 | "[remove] non-existing": { |
| 1546 | createSim: func() *runSim { |
| 1547 | cfg := prepareDyncfgCfg("success", "test") |
| 1548 | |
| 1549 | return &runSim{ |
| 1550 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1551 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1552 | UID: "1-remove", |
| 1553 | Args: []string{mgr.dyncfgJobID(cfg), "remove"}, |
| 1554 | })) |
| 1555 | }, |
| 1556 | wantDiscovered: nil, |
| 1557 | wantSeen: nil, |
| 1558 | wantExposed: nil, |
| 1559 | wantRunning: nil, |
| 1560 | wantDyncfg: ` |
| 1561 | |
| 1562 | FUNCTION_RESULT_BEGIN 1-remove 404 application/json |
| 1563 | {"status":404,"errorMessage":"job not found."} |
| 1564 | FUNCTION_RESULT_END |
| 1565 | `, |
| 1566 | } |
| 1567 | }, |
| 1568 | }, |
| 1569 | "[remove] non-dyncfg": { |
| 1570 | createSim: func() *runSim { |
| 1571 | stockCfg := prepareStockCfg("success", "stock") |
| 1572 | userCfg := prepareUserCfg("success", "user") |
| 1573 | discCfg := prepareDiscoveredCfg("success", "discovered") |
| 1574 | |
| 1575 | return &runSim{ |
| 1576 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 1577 | sendConfGroup(in, stockCfg.Source(), stockCfg) |
| 1578 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1579 | UID: "1-enable", |
| 1580 | Args: []string{mgr.dyncfgJobID(stockCfg), "enable"}, |
| 1581 | })) |
| 1582 | |
| 1583 | sendConfGroup(in, userCfg.Source(), userCfg) |
| 1584 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1585 | UID: "2-enable", |
| 1586 | Args: []string{mgr.dyncfgJobID(userCfg), "enable"}, |
| 1587 | })) |
| 1588 | |
| 1589 | sendConfGroup(in, discCfg.Source(), discCfg) |
| 1590 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1591 | UID: "3-enable", |
| 1592 | Args: []string{mgr.dyncfgJobID(discCfg), "enable"}, |
| 1593 | })) |
| 1594 | |
| 1595 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1596 | UID: "1-remove", |
| 1597 | Args: []string{mgr.dyncfgJobID(stockCfg), "remove"}, |
| 1598 | })) |
| 1599 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1600 | UID: "2-remove", |
| 1601 | Args: []string{mgr.dyncfgJobID(userCfg), "remove"}, |
| 1602 | })) |
| 1603 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1604 | UID: "3-remove", |
| 1605 | Args: []string{mgr.dyncfgJobID(discCfg), "remove"}, |
| 1606 | })) |
| 1607 | }, |
| 1608 | wantDiscovered: []confgroup.Config{ |
| 1609 | stockCfg, |
| 1610 | userCfg, |
| 1611 | discCfg, |
| 1612 | }, |
| 1613 | wantSeen: []confgroup.Config{ |
| 1614 | stockCfg, |
| 1615 | userCfg, |
| 1616 | discCfg, |
| 1617 | }, |
| 1618 | wantExposed: []wantExposedEntry{ |
| 1619 | {cfg: stockCfg, status: dyncfg.StatusRunning}, |
| 1620 | {cfg: userCfg, status: dyncfg.StatusRunning}, |
| 1621 | {cfg: discCfg, status: dyncfg.StatusRunning}, |
| 1622 | }, |
| 1623 | wantRunning: []string{stockCfg.FullName(), userCfg.FullName(), discCfg.FullName()}, |
| 1624 | wantDyncfg: ` |
| 1625 | CONFIG test:collector:success:stock create accepted job /collectors/test/Jobs stock 'type=stock,module=success,job=stock' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 1626 | |
| 1627 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 1628 | {"status":200,"message":""} |
| 1629 | FUNCTION_RESULT_END |
| 1630 | |
| 1631 | CONFIG test:collector:success:stock status running |
| 1632 | |
| 1633 | CONFIG test:collector:success:user create accepted job /collectors/test/Jobs user 'type=user,module=success,job=user' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 1634 | |
| 1635 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 1636 | {"status":200,"message":""} |
| 1637 | FUNCTION_RESULT_END |
| 1638 | |
| 1639 | CONFIG test:collector:success:user status running |
| 1640 | |
| 1641 | CONFIG test:collector:success:discovered create accepted job /collectors/test/Jobs discovered 'type=discovered,module=success,job=discovered' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 1642 | |
| 1643 | FUNCTION_RESULT_BEGIN 3-enable 200 application/json |
| 1644 | {"status":200,"message":""} |
| 1645 | FUNCTION_RESULT_END |
| 1646 | |
| 1647 | CONFIG test:collector:success:discovered status running |
| 1648 | |
| 1649 | FUNCTION_RESULT_BEGIN 1-remove 405 application/json |
| 1650 | {"status":405,"errorMessage":"removing jobs of type 'stock' is not supported, only 'dyncfg' jobs can be removed."} |
| 1651 | FUNCTION_RESULT_END |
| 1652 | |
| 1653 | FUNCTION_RESULT_BEGIN 2-remove 405 application/json |
| 1654 | {"status":405,"errorMessage":"removing jobs of type 'user' is not supported, only 'dyncfg' jobs can be removed."} |
| 1655 | FUNCTION_RESULT_END |
| 1656 | |
| 1657 | FUNCTION_RESULT_BEGIN 3-remove 405 application/json |
| 1658 | {"status":405,"errorMessage":"removing jobs of type 'discovered' is not supported, only 'dyncfg' jobs can be removed."} |
| 1659 | FUNCTION_RESULT_END |
| 1660 | `, |
| 1661 | } |
| 1662 | }, |
| 1663 | }, |
| 1664 | "[remove] not enabled dyncfg:ok": { |
| 1665 | createSim: func() *runSim { |
| 1666 | cfg := prepareDyncfgCfg("success", "test") |
| 1667 | |
| 1668 | return &runSim{ |
| 1669 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1670 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1671 | UID: "1-add", |
| 1672 | Source: "type=dyncfg", |
| 1673 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1674 | Payload: []byte("{}"), |
| 1675 | })) |
| 1676 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1677 | UID: "2-remove", |
| 1678 | Args: []string{mgr.dyncfgJobID(cfg), "remove"}, |
| 1679 | })) |
| 1680 | }, |
| 1681 | wantDiscovered: nil, |
| 1682 | wantSeen: nil, |
| 1683 | wantExposed: nil, |
| 1684 | wantRunning: nil, |
| 1685 | wantDyncfg: ` |
| 1686 | |
| 1687 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1688 | {"status":202,"message":""} |
| 1689 | FUNCTION_RESULT_END |
| 1690 | |
| 1691 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1692 | |
| 1693 | FUNCTION_RESULT_BEGIN 2-remove 200 application/json |
| 1694 | {"status":200,"message":""} |
| 1695 | FUNCTION_RESULT_END |
| 1696 | |
| 1697 | CONFIG test:collector:success:test delete |
| 1698 | `, |
| 1699 | } |
| 1700 | }, |
| 1701 | }, |
| 1702 | "[remove] enabled dyncfg:ok": { |
| 1703 | createSim: func() *runSim { |
| 1704 | cfg := prepareDyncfgCfg("success", "test") |
| 1705 | |
| 1706 | return &runSim{ |
| 1707 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1708 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1709 | UID: "1-add", |
| 1710 | Source: "type=dyncfg", |
| 1711 | Args: []string{mgr.dyncfgModID(cfg.Module()), "add", cfg.Name()}, |
| 1712 | Payload: []byte("{}"), |
| 1713 | })) |
| 1714 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1715 | UID: "2-enable", |
| 1716 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 1717 | })) |
| 1718 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1719 | UID: "3-remove", |
| 1720 | Args: []string{mgr.dyncfgJobID(cfg), "remove"}, |
| 1721 | })) |
| 1722 | }, |
| 1723 | wantDiscovered: nil, |
| 1724 | wantSeen: nil, |
| 1725 | wantExposed: nil, |
| 1726 | wantRunning: nil, |
| 1727 | wantDyncfg: ` |
| 1728 | |
| 1729 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1730 | {"status":202,"message":""} |
| 1731 | FUNCTION_RESULT_END |
| 1732 | |
| 1733 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1734 | |
| 1735 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 1736 | {"status":200,"message":""} |
| 1737 | FUNCTION_RESULT_END |
| 1738 | |
| 1739 | CONFIG test:collector:success:test status running |
| 1740 | |
| 1741 | FUNCTION_RESULT_BEGIN 3-remove 200 application/json |
| 1742 | {"status":200,"message":""} |
| 1743 | FUNCTION_RESULT_END |
| 1744 | |
| 1745 | CONFIG test:collector:success:test delete |
| 1746 | `, |
| 1747 | } |
| 1748 | }, |
| 1749 | }, |
| 1750 | } |
| 1751 | |
| 1752 | for name, test := range tests { |
| 1753 | t.Run(name, func(t *testing.T) { |
| 1754 | sim := test.createSim() |
| 1755 | sim.run(t) |
| 1756 | }) |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | func TestManager_Run_Dyncfg_Update(t *testing.T) { |
| 1761 | tests := map[string]struct { |
| 1762 | createSim func() *runSim |
| 1763 | }{ |
| 1764 | "[update] non-existing": { |
| 1765 | createSim: func() *runSim { |
| 1766 | cfg := prepareDyncfgCfg("success", "test") |
| 1767 | |
| 1768 | return &runSim{ |
| 1769 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1770 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1771 | UID: "1-update", |
| 1772 | Args: []string{mgr.dyncfgJobID(cfg), "update"}, |
| 1773 | Payload: []byte("{}"), |
| 1774 | })) |
| 1775 | }, |
| 1776 | wantDiscovered: nil, |
| 1777 | wantSeen: nil, |
| 1778 | wantExposed: nil, |
| 1779 | wantRunning: nil, |
| 1780 | wantDyncfg: ` |
| 1781 | |
| 1782 | FUNCTION_RESULT_BEGIN 1-update 404 application/json |
| 1783 | {"status":404,"errorMessage":"job not found."} |
| 1784 | FUNCTION_RESULT_END |
| 1785 | `, |
| 1786 | } |
| 1787 | }, |
| 1788 | }, |
| 1789 | "[update] enabled dyncfg:ok with dyncfg:ok": { |
| 1790 | createSim: func() *runSim { |
| 1791 | origCfg := prepareDyncfgCfg("success", "test"). |
| 1792 | Set("option_str", "1") |
| 1793 | updCfg := prepareDyncfgCfg("success", "test"). |
| 1794 | Set("option_str", "2") |
| 1795 | origBs, _ := json.Marshal(origCfg) |
| 1796 | updBs, _ := json.Marshal(updCfg) |
| 1797 | |
| 1798 | return &runSim{ |
| 1799 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1800 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1801 | UID: "1-add", |
| 1802 | Source: "type=dyncfg", |
| 1803 | Args: []string{mgr.dyncfgModID(origCfg.Module()), "add", origCfg.Name()}, |
| 1804 | Payload: origBs, |
| 1805 | })) |
| 1806 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1807 | UID: "2-enable", |
| 1808 | Args: []string{mgr.dyncfgJobID(origCfg), "enable"}, |
| 1809 | })) |
| 1810 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1811 | UID: "3-update", |
| 1812 | Source: "type=dyncfg", |
| 1813 | Args: []string{mgr.dyncfgJobID(origCfg), "update"}, |
| 1814 | Payload: updBs, |
| 1815 | })) |
| 1816 | }, |
| 1817 | wantDiscovered: nil, |
| 1818 | wantSeen: []confgroup.Config{ |
| 1819 | updCfg, |
| 1820 | }, |
| 1821 | wantExposed: []wantExposedEntry{ |
| 1822 | {cfg: updCfg, status: dyncfg.StatusRunning}, |
| 1823 | }, |
| 1824 | wantRunning: []string{updCfg.FullName()}, |
| 1825 | wantDyncfg: ` |
| 1826 | |
| 1827 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1828 | {"status":202,"message":""} |
| 1829 | FUNCTION_RESULT_END |
| 1830 | |
| 1831 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1832 | |
| 1833 | FUNCTION_RESULT_BEGIN 2-enable 200 application/json |
| 1834 | {"status":200,"message":""} |
| 1835 | FUNCTION_RESULT_END |
| 1836 | |
| 1837 | CONFIG test:collector:success:test status running |
| 1838 | |
| 1839 | FUNCTION_RESULT_BEGIN 3-update 200 application/json |
| 1840 | {"status":200,"message":""} |
| 1841 | FUNCTION_RESULT_END |
| 1842 | |
| 1843 | CONFIG test:collector:success:test status running |
| 1844 | `, |
| 1845 | } |
| 1846 | }, |
| 1847 | }, |
| 1848 | "[update] disabled dyncfg:ok with dyncfg:ok": { |
| 1849 | createSim: func() *runSim { |
| 1850 | origCfg := prepareDyncfgCfg("success", "test"). |
| 1851 | Set("option_str", "1") |
| 1852 | updCfg := prepareDyncfgCfg("success", "test"). |
| 1853 | Set("option_str", "2") |
| 1854 | origBs, _ := json.Marshal(origCfg) |
| 1855 | updBs, _ := json.Marshal(updCfg) |
| 1856 | |
| 1857 | return &runSim{ |
| 1858 | do: func(mgr *Manager, _ chan []*confgroup.Group) { |
| 1859 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1860 | UID: "1-add", |
| 1861 | Source: "type=dyncfg", |
| 1862 | Args: []string{mgr.dyncfgModID(origCfg.Module()), "add", origCfg.Name()}, |
| 1863 | Payload: origBs, |
| 1864 | })) |
| 1865 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1866 | UID: "2-disable", |
| 1867 | Args: []string{mgr.dyncfgJobID(origCfg), "disable"}, |
| 1868 | })) |
| 1869 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1870 | UID: "3-update", |
| 1871 | Source: "type=dyncfg", |
| 1872 | Args: []string{mgr.dyncfgJobID(origCfg), "update"}, |
| 1873 | Payload: updBs, |
| 1874 | })) |
| 1875 | }, |
| 1876 | wantDiscovered: nil, |
| 1877 | wantSeen: []confgroup.Config{ |
| 1878 | updCfg, |
| 1879 | }, |
| 1880 | wantExposed: []wantExposedEntry{ |
| 1881 | {cfg: updCfg, status: dyncfg.StatusDisabled}, |
| 1882 | }, |
| 1883 | wantRunning: nil, |
| 1884 | wantDyncfg: ` |
| 1885 | |
| 1886 | FUNCTION_RESULT_BEGIN 1-add 202 application/json |
| 1887 | {"status":202,"message":""} |
| 1888 | FUNCTION_RESULT_END |
| 1889 | |
| 1890 | CONFIG test:collector:success:test create accepted job /collectors/test/Jobs dyncfg 'type=dyncfg' 'schema get enable disable update restart test userconfig remove' 0x0000 0x0000 |
| 1891 | |
| 1892 | FUNCTION_RESULT_BEGIN 2-disable 200 application/json |
| 1893 | {"status":200,"message":""} |
| 1894 | FUNCTION_RESULT_END |
| 1895 | |
| 1896 | CONFIG test:collector:success:test status disabled |
| 1897 | |
| 1898 | FUNCTION_RESULT_BEGIN 3-update 200 application/json |
| 1899 | {"status":200,"message":""} |
| 1900 | FUNCTION_RESULT_END |
| 1901 | |
| 1902 | CONFIG test:collector:success:test status disabled |
| 1903 | `, |
| 1904 | } |
| 1905 | }, |
| 1906 | }, |
| 1907 | } |
| 1908 | |
| 1909 | for name, test := range tests { |
| 1910 | t.Run(name, func(t *testing.T) { |
| 1911 | sim := test.createSim() |
| 1912 | sim.run(t) |
| 1913 | }) |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | func sendConfGroup(in chan []*confgroup.Group, src string, configs ...confgroup.Config) { |
| 1918 | in <- prepareCfgGroups(src, configs...) |
| 1919 | in <- prepareCfgGroups("_") |
| 1920 | } |
| 1921 | |
| 1922 | func prepareCfgGroups(src string, configs ...confgroup.Config) []*confgroup.Group { |
| 1923 | return []*confgroup.Group{{Configs: configs, Source: src}} |
| 1924 | } |
| 1925 | |
| 1926 | func prepareStockCfg(module, job string) confgroup.Config { |
| 1927 | return confgroup.Config{}. |
| 1928 | SetSourceType(confgroup.TypeStock). |
| 1929 | SetProvider("test"). |
| 1930 | SetSource(fmt.Sprintf("type=stock,module=%s,job=%s", module, job)). |
| 1931 | SetModule(module). |
| 1932 | SetName(job) |
| 1933 | } |
| 1934 | |
| 1935 | func prepareUserCfg(module, job string) confgroup.Config { |
| 1936 | return confgroup.Config{}. |
| 1937 | SetSourceType(confgroup.TypeUser). |
| 1938 | SetProvider("test"). |
| 1939 | SetSource(fmt.Sprintf("type=user,module=%s,job=%s", module, job)). |
| 1940 | SetModule(module). |
| 1941 | SetName(job) |
| 1942 | } |
| 1943 | |
| 1944 | func prepareDiscoveredCfg(module, job string) confgroup.Config { |
| 1945 | return confgroup.Config{}. |
| 1946 | SetSourceType(confgroup.TypeDiscovered). |
| 1947 | SetProvider("test"). |
| 1948 | SetSource(fmt.Sprintf("type=discovered,module=%s,job=%s", module, job)). |
| 1949 | SetModule(module). |
| 1950 | SetName(job) |
| 1951 | } |
| 1952 | |
| 1953 | func prepareDyncfgCfg(module, job string) confgroup.Config { |
| 1954 | return confgroup.Config{}. |
| 1955 | SetSourceType(confgroup.TypeDyncfg). |
| 1956 | SetProvider("dyncfg"). |
| 1957 | SetSource("type=dyncfg"). |
| 1958 | SetModule(module). |
| 1959 | SetName(job) |
| 1960 | } |
| 1961 | |
| 1962 | func prepareFunctionOnlyCfg(module, job string) confgroup.Config { |
| 1963 | return confgroup.Config{}. |
| 1964 | SetSourceType(confgroup.TypeUser). |
| 1965 | SetProvider("test"). |
| 1966 | SetSource(fmt.Sprintf("type=user,module=%s,job=%s", module, job)). |
| 1967 | SetModule(module). |
| 1968 | SetName(job). |
| 1969 | Set("function_only", true) |
| 1970 | } |
| 1971 | |
| 1972 | func TestManager_Run_FunctionOnly(t *testing.T) { |
| 1973 | tests := map[string]struct { |
| 1974 | createSim func() *runSim |
| 1975 | }{ |
| 1976 | "function_only config for module without methods => error": { |
| 1977 | createSim: func() *runSim { |
| 1978 | cfg := prepareFunctionOnlyCfg("nofuncs", "test") |
| 1979 | |
| 1980 | return &runSim{ |
| 1981 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 1982 | sendConfGroup(in, cfg.Source(), cfg) |
| 1983 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 1984 | UID: "1-enable", |
| 1985 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 1986 | })) |
| 1987 | }, |
| 1988 | wantDiscovered: []confgroup.Config{cfg}, |
| 1989 | wantSeen: []confgroup.Config{ |
| 1990 | cfg, |
| 1991 | }, |
| 1992 | wantExposed: []wantExposedEntry{ |
| 1993 | {cfg: cfg, status: dyncfg.StatusFailed}, |
| 1994 | }, |
| 1995 | wantRunning: nil, |
| 1996 | wantDyncfg: ` |
| 1997 | CONFIG test:collector:nofuncs:test create accepted job /collectors/test/Jobs user 'type=user,module=nofuncs,job=test' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 1998 | |
| 1999 | FUNCTION_RESULT_BEGIN 1-enable 400 application/json |
| 2000 | {"status":400,"errorMessage":"invalid configuration: failed to apply configuration: function_only is set but nofuncs module has no methods defined"} |
| 2001 | FUNCTION_RESULT_END |
| 2002 | |
| 2003 | CONFIG test:collector:nofuncs:test status failed |
| 2004 | `, |
| 2005 | } |
| 2006 | }, |
| 2007 | }, |
| 2008 | "function_only config for module with methods => ok": { |
| 2009 | createSim: func() *runSim { |
| 2010 | cfg := prepareFunctionOnlyCfg("withfuncs", "test") |
| 2011 | |
| 2012 | return &runSim{ |
| 2013 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 2014 | sendConfGroup(in, cfg.Source(), cfg) |
| 2015 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 2016 | UID: "1-enable", |
| 2017 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 2018 | })) |
| 2019 | }, |
| 2020 | wantDiscovered: []confgroup.Config{cfg}, |
| 2021 | wantSeen: []confgroup.Config{ |
| 2022 | cfg, |
| 2023 | }, |
| 2024 | wantExposed: []wantExposedEntry{ |
| 2025 | {cfg: cfg, status: dyncfg.StatusRunning}, |
| 2026 | }, |
| 2027 | wantRunning: []string{cfg.FullName()}, |
| 2028 | wantDyncfg: ` |
| 2029 | CONFIG test:collector:withfuncs:test create accepted job /collectors/test/Jobs user 'type=user,module=withfuncs,job=test' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 2030 | |
| 2031 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 2032 | {"status":200,"message":""} |
| 2033 | FUNCTION_RESULT_END |
| 2034 | |
| 2035 | CONFIG test:collector:withfuncs:test status running |
| 2036 | `, |
| 2037 | } |
| 2038 | }, |
| 2039 | }, |
| 2040 | "FunctionOnly module => ok": { |
| 2041 | createSim: func() *runSim { |
| 2042 | cfg := prepareUserCfg("funconly", "test") |
| 2043 | |
| 2044 | return &runSim{ |
| 2045 | do: func(mgr *Manager, in chan []*confgroup.Group) { |
| 2046 | sendConfGroup(in, cfg.Source(), cfg) |
| 2047 | mgr.dyncfgConfig(dyncfg.NewFunction(functions.Function{ |
| 2048 | UID: "1-enable", |
| 2049 | Args: []string{mgr.dyncfgJobID(cfg), "enable"}, |
| 2050 | })) |
| 2051 | }, |
| 2052 | wantDiscovered: []confgroup.Config{cfg}, |
| 2053 | wantSeen: []confgroup.Config{ |
| 2054 | cfg, |
| 2055 | }, |
| 2056 | wantExposed: []wantExposedEntry{ |
| 2057 | {cfg: cfg, status: dyncfg.StatusRunning}, |
| 2058 | }, |
| 2059 | wantRunning: []string{cfg.FullName()}, |
| 2060 | wantDyncfg: ` |
| 2061 | CONFIG test:collector:funconly:test create accepted job /collectors/test/Jobs user 'type=user,module=funconly,job=test' 'schema get enable disable update restart test userconfig' 0x0000 0x0000 |
| 2062 | |
| 2063 | FUNCTION_RESULT_BEGIN 1-enable 200 application/json |
| 2064 | {"status":200,"message":""} |
| 2065 | FUNCTION_RESULT_END |
| 2066 | |
| 2067 | CONFIG test:collector:funconly:test status running |
| 2068 | `, |
| 2069 | } |
| 2070 | }, |
| 2071 | }, |
| 2072 | } |
| 2073 | |
| 2074 | for name, test := range tests { |
| 2075 | t.Run(name, func(t *testing.T) { |
| 2076 | sim := test.createSim() |
| 2077 | sim.run(t) |
| 2078 | }) |
| 2079 | } |
| 2080 | } |