github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/scraper/catalogue_test.go (about)

     1  package scraper
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/prometheus/prometheus/prompb"
     9  )
    10  
    11  // TestCatalogueFromReaderDNSGoldenIncludesSerial proves the DNS golden file is
    12  // exercising the SOA branch that emits probe_dns_serial.
    13  func TestCatalogueFromReaderDNSGoldenIncludesSerial(t *testing.T) {
    14  	fh, err := os.Open(filepath.Join("testdata", "dns.txt"))
    15  	if err != nil {
    16  		t.Fatalf("open dns golden file: %v", err)
    17  	}
    18  	defer fh.Close()
    19  
    20  	catalogue, err := CatalogueFromReader(fh)
    21  	if err != nil {
    22  		t.Fatalf("catalogue from reader: %v", err)
    23  	}
    24  
    25  	labels, ok := catalogue["probe_dns_serial"]
    26  	if !ok {
    27  		t.Fatalf("probe_dns_serial missing from DNS catalogue")
    28  	}
    29  
    30  	if len(labels) != 0 {
    31  		t.Fatalf("probe_dns_serial should not carry raw MetricFamily labels in scraper golden output, got %v", labels)
    32  	}
    33  }
    34  
    35  // TestCatalogueFromTimeseries verifies that catalogue extraction merges label
    36  // names across repeated series for the same metric family.
    37  func TestCatalogueFromTimeseries(t *testing.T) {
    38  	catalogue := CatalogueFromTimeseries(TimeSeries{
    39  		{Labels: []prompb.Label{{Name: "__name__", Value: "probe_success"}, {Name: configVersionLabelName, Value: "1"}, {Name: "job", Value: "j"}}},
    40  		{Labels: []prompb.Label{{Name: "__name__", Value: "probe_success"}, {Name: configVersionLabelName, Value: "2"}, {Name: "probe", Value: "p"}}},
    41  	})
    42  
    43  	expected := []string{configVersionLabelName, "job", "probe"}
    44  
    45  	got := catalogue["probe_success"]
    46  	if len(got) != len(expected) {
    47  		t.Fatalf("unexpected labels: %v", got)
    48  	}
    49  
    50  	for i, label := range expected {
    51  		if got[i] != label {
    52  			t.Fatalf("unexpected labels: %v", got)
    53  		}
    54  	}
    55  }
    56  
    57  // TestCompareMetricCatalogue covers the mismatch reporting we use for both the
    58  // fixture and runtime catalogue contract tests.
    59  func TestCompareMetricCatalogue(t *testing.T) {
    60  	expected := MetricLabelCatalogue{
    61  		"probe_success":    {configVersionLabelName},
    62  		"probe_dns_serial": {},
    63  	}
    64  	observed := MetricLabelCatalogue{
    65  		"probe_success":    {configVersionLabelName, "unexpected_label"},
    66  		"probe_other":      {},
    67  		"probe_dns_serial": {},
    68  	}
    69  
    70  	result := CompareMetricCatalogue(expected, observed)
    71  	if result.Success() {
    72  		t.Fatalf("expected mismatch")
    73  	}
    74  
    75  	if len(result.MissingMetrics) != 0 {
    76  		t.Fatalf("expected no missing metrics, got %v", result.MissingMetrics)
    77  	}
    78  
    79  	if len(result.UnexpectedMetrics) != 1 || result.UnexpectedMetrics[0] != "probe_other" {
    80  		t.Fatalf("unexpected metrics mismatch: %v", result.UnexpectedMetrics)
    81  	}
    82  
    83  	mismatch, ok := result.LabelMismatches["probe_success"]
    84  	if !ok {
    85  		t.Fatalf("expected probe_success label mismatch")
    86  	}
    87  
    88  	if len(mismatch.MissingLabels) != 0 {
    89  		t.Fatalf("expected no missing labels, got %v", mismatch.MissingLabels)
    90  	}
    91  
    92  	if len(mismatch.UnexpectedLabels) != 1 || mismatch.UnexpectedLabels[0] != "unexpected_label" {
    93  		t.Fatalf("unexpected labels mismatch: %v", mismatch.UnexpectedLabels)
    94  	}
    95  }