github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/cast/types_test.go (about) 1 package cast 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/awesome-flow/flow/pkg/types" 9 ) 10 11 func TestCfgMapper(t *testing.T) { 12 actors := map[string]types.CfgBlockActor{ 13 "bar": types.CfgBlockActor{ 14 Builder: "builder", 15 Module: "module", 16 Params: map[string]types.Value{"baz": 42}, 17 }, 18 } 19 ppl := map[string]types.CfgBlockPipeline{ 20 "moo": types.CfgBlockPipeline{ 21 Connect: []string{"connect"}, 22 }, 23 } 24 sys := types.CfgBlockSystem{ 25 Admin: types.CfgBlockSystemAdmin{ 26 Bind: "123.45.67.89", 27 Enabled: true, 28 }, 29 Maxprocs: 42, 30 Metrics: types.CfgBlockSystemMetrics{ 31 Enabled: true, 32 Interval: 1e3, 33 Receiver: types.CfgBlockSystemMetricsReceiver{ 34 Params: map[string]types.Value{"p1": 1, "p2": "2", "p3": true}, 35 Type: "type", 36 }, 37 }, 38 } 39 40 tests := []struct { 41 name string 42 inputKV *types.KeyValue 43 wantKV *types.KeyValue 44 wantErr error 45 }{ 46 { 47 "Empty map", 48 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 49 &types.KeyValue{Key: types.NewKey("foo"), Value: types.Cfg{}}, 50 nil, 51 }, 52 { 53 "Nil-value", 54 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 55 nil, 56 fmt.Errorf("Cfg cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 57 }, 58 { 59 "Components defined", 60 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 61 "actors": actors, 62 }}, 63 &types.KeyValue{Key: types.NewKey("foo"), Value: types.Cfg{ 64 Actors: actors, 65 }}, 66 nil, 67 }, 68 { 69 "Pipeline defined", 70 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 71 "pipeline": ppl, 72 }}, 73 &types.KeyValue{Key: types.NewKey("foo"), Value: types.Cfg{ 74 Pipeline: ppl, 75 }}, 76 nil, 77 }, 78 { 79 "System defined", 80 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 81 "system": sys, 82 }}, 83 &types.KeyValue{Key: types.NewKey("foo"), Value: types.Cfg{ 84 System: sys, 85 }}, 86 nil, 87 }, 88 { 89 "Unknown keys defined", 90 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 91 "unknown1": "foobar", 92 "unknown2": "boobar", 93 }}, 94 nil, 95 fmt.Errorf("Cfg cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 96 }, 97 } 98 99 t.Parallel() 100 101 for _, testCase := range tests { 102 t.Run(testCase.name, func(t *testing.T) { 103 mpr := &CfgMapper{} 104 gotKV, gotErr := mpr.Map(testCase.inputKV) 105 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 106 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 107 } 108 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 109 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 110 } 111 }) 112 } 113 } 114 115 func TestCfgBlockSystemMapper(t *testing.T) { 116 adm := types.CfgBlockSystemAdmin{ 117 Bind: "123.45.67.89", 118 Enabled: true, 119 } 120 metrics := types.CfgBlockSystemMetrics{ 121 Enabled: true, 122 Interval: 1e3, 123 Receiver: types.CfgBlockSystemMetricsReceiver{ 124 Params: map[string]types.Value{"p1": "v1", "p2": "v2", "p3": "v3"}, 125 Type: "type", 126 }, 127 } 128 129 tests := []struct { 130 name string 131 inputKV *types.KeyValue 132 wantKV *types.KeyValue 133 wantErr error 134 }{ 135 { 136 "Empty map", 137 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 138 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystem{}}, 139 nil, 140 }, 141 { 142 "Nil-value", 143 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 144 nil, 145 fmt.Errorf("CfgBlockSystem cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 146 }, 147 { 148 "Maxprocs defined", 149 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 150 "maxprocs": 42, 151 }}, 152 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystem{ 153 Maxprocs: 42, 154 }}, 155 nil, 156 }, 157 { 158 "Admin defined", 159 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 160 "admin": adm, 161 }}, 162 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystem{ 163 Admin: adm, 164 }}, 165 nil, 166 }, 167 { 168 "Metrics defined", 169 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 170 "metrics": metrics, 171 }}, 172 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystem{ 173 Metrics: metrics, 174 }}, 175 nil, 176 }, 177 { 178 "Unknown keys defined", 179 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 180 "unknown1": "v1", 181 "unknown2": 42, 182 }}, 183 nil, 184 fmt.Errorf("CfgBlockSystem cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 185 }, 186 } 187 188 t.Parallel() 189 190 for _, testCase := range tests { 191 t.Run(testCase.name, func(t *testing.T) { 192 mpr := &CfgBlockSystemMapper{} 193 gotKV, gotErr := mpr.Map(testCase.inputKV) 194 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 195 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 196 } 197 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 198 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 199 } 200 }) 201 } 202 } 203 204 func TestCfgBlockSystemAdminMapper(t *testing.T) { 205 tests := []struct { 206 name string 207 inputKV *types.KeyValue 208 wantKV *types.KeyValue 209 wantErr error 210 }{ 211 { 212 "Empty map", 213 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 214 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemAdmin{}}, 215 nil, 216 }, 217 { 218 "Nil-value", 219 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 220 nil, 221 fmt.Errorf("CfgBlockSystemAdmin cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 222 }, 223 { 224 "Enabled defined", 225 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 226 "enabled": true, 227 }}, 228 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemAdmin{ 229 Enabled: true, 230 }}, 231 nil, 232 }, 233 { 234 "BindAddr defined", 235 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 236 "bind": "123.45.67.89", 237 }}, 238 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemAdmin{ 239 Bind: "123.45.67.89", 240 }}, 241 nil, 242 }, 243 { 244 "Unknown keys defined", 245 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 246 "unknown1": "v1", 247 "unknown2": 42, 248 }}, 249 nil, 250 fmt.Errorf("CfgBlockSystemAdmin cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 251 }, 252 } 253 254 t.Parallel() 255 256 for _, testCase := range tests { 257 t.Run(testCase.name, func(t *testing.T) { 258 mpr := &CfgBlockSystemAdminMapper{} 259 gotKV, gotErr := mpr.Map(testCase.inputKV) 260 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 261 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 262 } 263 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 264 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 265 } 266 }) 267 } 268 } 269 270 func TestCfgBlockSystemMetricsMapper(t *testing.T) { 271 rcv := types.CfgBlockSystemMetricsReceiver{ 272 Params: map[string]types.Value{"p1": "v1", "p2": 2, "p3": true}, 273 Type: "type", 274 } 275 276 tests := []struct { 277 name string 278 inputKV *types.KeyValue 279 wantKV *types.KeyValue 280 wantErr error 281 }{ 282 { 283 "Empty map", 284 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 285 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetrics{}}, 286 nil, 287 }, 288 { 289 "Nil-value", 290 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 291 nil, 292 fmt.Errorf("CfgBlockSystemMetrics cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 293 }, 294 { 295 "Enabled defined", 296 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 297 "enabled": true, 298 }}, 299 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetrics{ 300 Enabled: true, 301 }}, 302 nil, 303 }, 304 { 305 "Interval defined", 306 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 307 "interval": 1, 308 }}, 309 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetrics{ 310 Interval: 1, 311 }}, 312 nil, 313 }, 314 { 315 "Receiver defined", 316 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 317 "receiver": rcv, 318 }}, 319 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetrics{ 320 Receiver: rcv, 321 }}, 322 nil, 323 }, 324 { 325 "Unknown keys defined", 326 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 327 "unknown1": "v1", 328 "unknown2": 42, 329 }}, 330 nil, 331 fmt.Errorf("CfgBlockSystemMetrics cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 332 }, 333 } 334 335 t.Parallel() 336 337 for _, testCase := range tests { 338 t.Run(testCase.name, func(t *testing.T) { 339 mpr := &CfgBlockSystemMetricsMapper{} 340 gotKV, gotErr := mpr.Map(testCase.inputKV) 341 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 342 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 343 } 344 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 345 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 346 } 347 }) 348 } 349 } 350 351 func TestCfgBlockSystemMetricsReceiverMapper(t *testing.T) { 352 tests := []struct { 353 name string 354 inputKV *types.KeyValue 355 wantKV *types.KeyValue 356 wantErr error 357 }{ 358 { 359 "Empty map", 360 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 361 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetricsReceiver{}}, 362 nil, 363 }, 364 { 365 "Nil-value", 366 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 367 nil, 368 fmt.Errorf("CfgBlockSystemMetricsReceiver cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 369 }, 370 { 371 "Type defined", 372 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 373 "type": "type", 374 }}, 375 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetricsReceiver{ 376 Type: "type", 377 }}, 378 nil, 379 }, 380 { 381 "Params defined", 382 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 383 "params": map[string]types.Value{"p1": "v1", "p2": 2, "p3": true}, 384 }}, 385 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockSystemMetricsReceiver{ 386 Params: map[string]types.Value{"p1": "v1", "p2": 2, "p3": true}, 387 }}, 388 nil, 389 }, 390 { 391 "Unknown keys defined", 392 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 393 "unknown1": "v1", 394 "unknown2": 42, 395 }}, 396 nil, 397 fmt.Errorf("CfgBlockSystemMetricsReceiver cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 398 }, 399 } 400 401 t.Parallel() 402 403 for _, testCase := range tests { 404 t.Run(testCase.name, func(t *testing.T) { 405 mpr := &CfgBlockSystemMetricsReceiverMapper{} 406 gotKV, gotErr := mpr.Map(testCase.inputKV) 407 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 408 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 409 } 410 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 411 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 412 } 413 }) 414 } 415 } 416 417 func TestMapCfgBlockActorMapper(t *testing.T) { 418 p1 := map[string]types.Value{"p11": 11, "p12": "12"} 419 p2 := map[string]types.Value{"p21": 21, "p22": "22"} 420 comp1 := types.CfgBlockActor{ 421 Builder: "builder1", 422 Module: "module1", 423 Params: p1, 424 } 425 comp2 := types.CfgBlockActor{ 426 Builder: "builder2", 427 Module: "module2", 428 Params: p2, 429 } 430 tests := []struct { 431 name string 432 inputKV *types.KeyValue 433 wantKV *types.KeyValue 434 wantErr error 435 }{ 436 { 437 "Empty map", 438 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 439 &types.KeyValue{Key: types.NewKey("foo"), Value: make(map[string]types.CfgBlockActor)}, 440 nil, 441 }, 442 { 443 "Nil-value", 444 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 445 nil, 446 fmt.Errorf("map[string]CfgBlockActor cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 447 }, 448 { 449 "A set of components", 450 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 451 "bar": comp1, 452 "baz": comp2, 453 }}, 454 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.CfgBlockActor{ 455 "bar": comp1, 456 "baz": comp2, 457 }}, 458 nil, 459 }, 460 } 461 462 t.Parallel() 463 464 for _, testCase := range tests { 465 t.Run(testCase.name, func(t *testing.T) { 466 mpr := &MapCfgBlockActorMapper{} 467 gotKV, gotErr := mpr.Map(testCase.inputKV) 468 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 469 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 470 } 471 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 472 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 473 } 474 }) 475 } 476 } 477 478 func TestCfgBlockActorMapper(t *testing.T) { 479 params := map[string]types.Value{"p1": "v1", "p2": 2, "p3": true} 480 481 tests := []struct { 482 name string 483 inputKV *types.KeyValue 484 wantKV *types.KeyValue 485 wantErr error 486 }{ 487 { 488 "Empty map", 489 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 490 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockActor{}}, 491 nil, 492 }, 493 { 494 "Nil-value", 495 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 496 nil, 497 fmt.Errorf("CfgBlockActor cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 498 }, 499 { 500 "Builder defined", 501 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 502 "builder": "builder", 503 }}, 504 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockActor{ 505 Builder: "builder", 506 }}, 507 nil, 508 }, 509 { 510 "Module defined", 511 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 512 "module": "module", 513 }}, 514 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockActor{ 515 Module: "module", 516 }}, 517 nil, 518 }, 519 { 520 "Params defined", 521 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 522 "params": params, 523 }}, 524 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockActor{ 525 Params: params, 526 }}, 527 nil, 528 }, 529 { 530 "Unknown keys defined", 531 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 532 "unknown1": "v1", 533 "unknown2": 42, 534 }}, 535 nil, 536 fmt.Errorf("CfgBlockActor cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 537 }, 538 } 539 540 t.Parallel() 541 542 for _, testCase := range tests { 543 t.Run(testCase.name, func(t *testing.T) { 544 mpr := &CfgBlockActorMapper{} 545 gotKV, gotErr := mpr.Map(testCase.inputKV) 546 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 547 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 548 } 549 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 550 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 551 } 552 }) 553 } 554 } 555 556 func TestMapCfgBlockPipeline(t *testing.T) { 557 ppl1 := types.CfgBlockPipeline{ 558 Connect: []string{"connect1"}, 559 } 560 ppl2 := types.CfgBlockPipeline{ 561 Connect: []string{"connect2"}, 562 } 563 564 tests := []struct { 565 name string 566 inputKV *types.KeyValue 567 wantKV *types.KeyValue 568 wantErr error 569 }{ 570 { 571 "Empty map", 572 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 573 &types.KeyValue{Key: types.NewKey("foo"), Value: make(map[string]types.CfgBlockPipeline)}, 574 nil, 575 }, 576 { 577 "Nil-value", 578 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 579 nil, 580 fmt.Errorf("map[string]CfgBlockPipeline cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 581 }, 582 { 583 "A set of components", 584 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 585 "bar": ppl1, 586 "baz": ppl2, 587 }}, 588 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.CfgBlockPipeline{ 589 "bar": ppl1, 590 "baz": ppl2, 591 }}, 592 nil, 593 }, 594 } 595 596 t.Parallel() 597 598 for _, testCase := range tests { 599 t.Run(testCase.name, func(t *testing.T) { 600 mpr := &MapCfgBlockPipelineMapper{} 601 gotKV, gotErr := mpr.Map(testCase.inputKV) 602 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 603 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 604 } 605 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 606 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 607 } 608 }) 609 } 610 } 611 612 func TestCfgBlockPipelineMapper(t *testing.T) { 613 tests := []struct { 614 name string 615 inputKV *types.KeyValue 616 wantKV *types.KeyValue 617 wantErr error 618 }{ 619 { 620 "Empty map", 621 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 622 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockPipeline{}}, 623 nil, 624 }, 625 { 626 "Nil-value", 627 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 628 nil, 629 fmt.Errorf("CfgBlockPipeline cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 630 }, 631 { 632 "Connect defined", 633 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 634 "connect": []string{"connect"}, 635 }}, 636 &types.KeyValue{Key: types.NewKey("foo"), Value: types.CfgBlockPipeline{ 637 Connect: []string{"connect"}, 638 }}, 639 nil, 640 }, 641 { 642 "Unknown keys defined", 643 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{ 644 "unknown1": "v1", 645 "unknown2": 42, 646 }}, 647 nil, 648 fmt.Errorf("CfgBlockPipeline cast failed for key: %q: unknown attributes: [%s]", types.NewKey("foo"), "unknown1, unknown2"), 649 }, 650 } 651 652 t.Parallel() 653 654 for _, testCase := range tests { 655 t.Run(testCase.name, func(t *testing.T) { 656 mpr := &CfgBlockPipelineMapper{} 657 gotKV, gotErr := mpr.Map(testCase.inputKV) 658 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 659 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 660 } 661 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 662 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 663 } 664 }) 665 } 666 } 667 668 func TestArrStrMapper(t *testing.T) { 669 tests := []struct { 670 name string 671 inputKV *types.KeyValue 672 wantKV *types.KeyValue 673 wantErr error 674 }{ 675 { 676 "Empty list", 677 &types.KeyValue{Key: types.NewKey("foo"), Value: []interface{}{}}, 678 &types.KeyValue{Key: types.NewKey("foo"), Value: []string{}}, 679 nil, 680 }, 681 { 682 "Nil-value", 683 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 684 nil, 685 fmt.Errorf("[]string cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 686 }, 687 { 688 "A list", 689 &types.KeyValue{Key: types.NewKey("foo"), Value: []interface{}{"foo", "bar", "baz"}}, 690 &types.KeyValue{Key: types.NewKey("foo"), Value: []string{"foo", "bar", "baz"}}, 691 nil, 692 }, 693 } 694 695 t.Parallel() 696 697 for _, testCase := range tests { 698 t.Run(testCase.name, func(t *testing.T) { 699 mpr := &ArrStrMapper{} 700 gotKV, gotErr := mpr.Map(testCase.inputKV) 701 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 702 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 703 } 704 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 705 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 706 } 707 }) 708 } 709 } 710 711 func TestMapStrToStrMapper(t *testing.T) { 712 tests := []struct { 713 name string 714 inputKV *types.KeyValue 715 wantKV *types.KeyValue 716 wantErr error 717 }{ 718 { 719 "Empty map", 720 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{}}, 721 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]string{}}, 722 nil, 723 }, 724 { 725 "Nil-value", 726 &types.KeyValue{Key: types.NewKey("foo"), Value: nil}, 727 nil, 728 fmt.Errorf("map[string]string cast failed for key: %q, val: %#v: unknown value type", types.NewKey("foo"), nil), 729 }, 730 { 731 "A map", 732 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]types.Value{"foo": "bar", "baz": "moo"}}, 733 &types.KeyValue{Key: types.NewKey("foo"), Value: map[string]string{"foo": "bar", "baz": "moo"}}, 734 nil, 735 }, 736 } 737 738 t.Parallel() 739 740 for _, testCase := range tests { 741 t.Run(testCase.name, func(t *testing.T) { 742 mpr := &MapStrToStrMapper{} 743 gotKV, gotErr := mpr.Map(testCase.inputKV) 744 if !reflect.DeepEqual(gotErr, testCase.wantErr) { 745 t.Fatalf("Unexpected error: Map(%#v) = _, %s, want: %s", testCase.inputKV, gotErr, testCase.wantErr) 746 } 747 if testCase.wantKV != nil && !reflect.DeepEqual(gotKV, testCase.wantKV) { 748 t.Fatalf("Unexpected value: Map(%#v) = %#v, want: %#v", testCase.inputKV, gotKV, testCase.wantKV) 749 } 750 }) 751 } 752 }