MCPcopy Create free account
hub / github.com/prometheus/common / TestCreateError

Function TestCreateError

expfmt/text_create_test.go:628–690  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

626}
627
628func TestCreateError(t *testing.T) {
629 scenarios := []struct {
630 in *dto.MetricFamily
631 err string
632 }{
633 // 0: No metric.
634 {
635 in: &dto.MetricFamily{
636 Name: proto.String("name"),
637 Help: proto.String("doc string"),
638 Type: dto.MetricType_COUNTER.Enum(),
639 Metric: []*dto.Metric{},
640 },
641 err: "MetricFamily has no metrics",
642 },
643 // 1: No metric name.
644 {
645 in: &dto.MetricFamily{
646 Help: proto.String("doc string"),
647 Type: dto.MetricType_UNTYPED.Enum(),
648 Metric: []*dto.Metric{
649 {
650 Untyped: &dto.Untyped{
651 Value: proto.Float64(math.Inf(-1)),
652 },
653 },
654 },
655 },
656 err: "MetricFamily has no name",
657 },
658 // 2: Wrong type.
659 {
660 in: &dto.MetricFamily{
661 Name: proto.String("name"),
662 Help: proto.String("doc string"),
663 Type: dto.MetricType_COUNTER.Enum(),
664 Metric: []*dto.Metric{
665 {
666 Untyped: &dto.Untyped{
667 Value: proto.Float64(math.Inf(-1)),
668 },
669 },
670 },
671 },
672 err: "expected counter in metric",
673 },
674 }
675
676 for i, scenario := range scenarios {
677 var out bytes.Buffer
678 _, err := MetricFamilyToText(&out, scenario.in)
679 if err == nil {
680 t.Errorf("%d. expected error, got nil", i)
681 continue
682 }
683 if expected, got := scenario.err, err.Error(); strings.Index(got, expected) != 0 {
684 t.Errorf(
685 "%d. expected error starting with %q, got %q",

Callers

nothing calls this directly

Calls 3

MetricFamilyToTextFunction · 0.85
ErrorMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected