|
8 | 8 | "time"
|
9 | 9 |
|
10 | 10 | "github.com/stretchr/testify/assert"
|
| 11 | + "github.com/stretchr/testify/require" |
11 | 12 |
|
12 | 13 | "github.com/influxdata/telegraf/metric"
|
13 | 14 | )
|
@@ -468,3 +469,95 @@ func TestTemplate6(t *testing.T) {
|
468 | 469 | expS := "localhost.cpu0.us-west-2.cpu.FIELDNAME"
|
469 | 470 | assert.Equal(t, expS, mS)
|
470 | 471 | }
|
| 472 | + |
| 473 | +func TestClean(t *testing.T) { |
| 474 | + now := time.Unix(1234567890, 0) |
| 475 | + tests := []struct { |
| 476 | + name string |
| 477 | + metric_name string |
| 478 | + tags map[string]string |
| 479 | + fields map[string]interface{} |
| 480 | + expected string |
| 481 | + }{ |
| 482 | + { |
| 483 | + "Base metric", |
| 484 | + "cpu", |
| 485 | + map[string]string{"host": "localhost"}, |
| 486 | + map[string]interface{}{"usage_busy": float64(8.5)}, |
| 487 | + "localhost.cpu.usage_busy 8.5 1234567890\n", |
| 488 | + }, |
| 489 | + { |
| 490 | + "Dot and whitespace in tags", |
| 491 | + "cpu", |
| 492 | + map[string]string{"host": "localhost", "label.dot and space": "value with.dot"}, |
| 493 | + map[string]interface{}{"usage_busy": float64(8.5)}, |
| 494 | + "localhost.value_with_dot.cpu.usage_busy 8.5 1234567890\n", |
| 495 | + }, |
| 496 | + { |
| 497 | + "Field with space", |
| 498 | + "system", |
| 499 | + map[string]string{"host": "localhost"}, |
| 500 | + map[string]interface{}{"uptime_format": "20 days, 23:26"}, |
| 501 | + "", // yes nothing. graphite don't serialize string fields |
| 502 | + }, |
| 503 | + { |
| 504 | + "Allowed punct", |
| 505 | + "cpu", |
| 506 | + map[string]string{"host": "localhost", "tag": "-_:="}, |
| 507 | + map[string]interface{}{"usage_busy": float64(10)}, |
| 508 | + "localhost.-_:=.cpu.usage_busy 10 1234567890\n", |
| 509 | + }, |
| 510 | + { |
| 511 | + "Special conversions to hyphen", |
| 512 | + "cpu", |
| 513 | + map[string]string{"host": "localhost", "tag": "/@*"}, |
| 514 | + map[string]interface{}{"usage_busy": float64(10)}, |
| 515 | + "localhost.---.cpu.usage_busy 10 1234567890\n", |
| 516 | + }, |
| 517 | + { |
| 518 | + "Special drop chars", |
| 519 | + "cpu", |
| 520 | + map[string]string{"host": "localhost", "tag": `\no slash`}, |
| 521 | + map[string]interface{}{"usage_busy": float64(10)}, |
| 522 | + "localhost.no_slash.cpu.usage_busy 10 1234567890\n", |
| 523 | + }, |
| 524 | + { |
| 525 | + "Empty tag & value field", |
| 526 | + "cpu", |
| 527 | + map[string]string{"host": "localhost"}, |
| 528 | + map[string]interface{}{"value": float64(10)}, |
| 529 | + "localhost.cpu 10 1234567890\n", |
| 530 | + }, |
| 531 | + { |
| 532 | + "Unicode Letters allowed", |
| 533 | + "cpu", |
| 534 | + map[string]string{"host": "localhost", "tag": "μnicodε_letters"}, |
| 535 | + map[string]interface{}{"value": float64(10)}, |
| 536 | + "localhost.μnicodε_letters.cpu 10 1234567890\n", |
| 537 | + }, |
| 538 | + { |
| 539 | + "Other Unicode not allowed", |
| 540 | + "cpu", |
| 541 | + map[string]string{"host": "localhost", "tag": "“☢”"}, |
| 542 | + map[string]interface{}{"value": float64(10)}, |
| 543 | + "localhost.___.cpu 10 1234567890\n", |
| 544 | + }, |
| 545 | + { |
| 546 | + "Newline in tags", |
| 547 | + "cpu", |
| 548 | + map[string]string{"host": "localhost", "label": "some\nthing\nwith\nnewline"}, |
| 549 | + map[string]interface{}{"usage_busy": float64(8.5)}, |
| 550 | + "localhost.some_thing_with_newline.cpu.usage_busy 8.5 1234567890\n", |
| 551 | + }, |
| 552 | + } |
| 553 | + |
| 554 | + s := GraphiteSerializer{} |
| 555 | + for _, tt := range tests { |
| 556 | + t.Run(tt.name, func(t *testing.T) { |
| 557 | + m, err := metric.New(tt.metric_name, tt.tags, tt.fields, now) |
| 558 | + assert.NoError(t, err) |
| 559 | + actual, _ := s.Serialize(m) |
| 560 | + require.Equal(t, tt.expected, string(actual)) |
| 561 | + }) |
| 562 | + } |
| 563 | +} |
0 commit comments