From 87104ca4a1d3d7d0cbaedbb32347a2d29ae7076d Mon Sep 17 00:00:00 2001 From: Silke Hofstra Date: Thu, 10 Oct 2019 19:28:45 +0200 Subject: [PATCH] fixup! Check TLS version in TLS test --- prober/tcp_test.go | 14 ++++++++++++-- prober/utils_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/prober/tcp_test.go b/prober/tcp_test.go index 1a1568085..e2fcbd4d3 100644 --- a/prober/tcp_test.go +++ b/prober/tcp_test.go @@ -170,14 +170,24 @@ func TestTCPConnectionWithTLS(t *testing.T) { } <-ch - // Check the probe_ssl_earliest_cert_expiry. + // Check the resulting metrics. mfs, err := registry.Gather() if err != nil { t.Fatal(err) } + + // Check labels + expectedLabels := map[string]map[string]string{ + "probe_tls_version_info": { + "version": "TLS 1.2", + }, + } + checkRegistryLabels(expectedLabels, mfs, t) + + // Check values expectedResults := map[string]float64{ "probe_ssl_earliest_cert_expiry": float64(certExpiry.Unix()), - "probe_tls_version": 1.2, + "probe_tls_version_info": 1, } checkRegistryResults(expectedResults, mfs, t) } diff --git a/prober/utils_test.go b/prober/utils_test.go index 38b95ee97..326da5d81 100644 --- a/prober/utils_test.go +++ b/prober/utils_test.go @@ -45,6 +45,35 @@ func checkRegistryResults(expRes map[string]float64, mfs []*dto.MetricFamily, t } } +// Check if expected labels are in the registry +func checkRegistryLabels(expRes map[string]map[string]string, mfs []*dto.MetricFamily, t *testing.T) { + results := make(map[string]map[string]string) + for _, mf := range mfs { + result := make(map[string]string) + for _, metric := range mf.Metric { + for _, l := range metric.GetLabel() { + result[l.GetName()] = l.GetValue() + } + } + results[mf.GetName()] = result + } + + for metric, labelValues := range expRes { + if _, ok := results[metric]; !ok { + t.Fatalf("Expected metric %v not found in returned metrics", metric) + } + for name, exp := range labelValues { + val, ok := results[metric][name] + if !ok { + t.Fatalf("Expected label %v for metric %v not found in returned metrics", val, name) + } + if val != exp { + t.Fatalf("Expected: %v{%q=%q}, got: %v{%q=%q}", metric, name, exp, metric, name, val) + } + } + } +} + // Create test certificate with specified expiry date // Certificate will be self-signed and use localhost/127.0.0.1 // Generated certificate and key are returned in PEM encoding