From 5cd6734305f1cc2b352b14739d22ad466d4cc890 Mon Sep 17 00:00:00 2001 From: conorbroderick Date: Wed, 17 May 2017 16:41:53 +0100 Subject: [PATCH] Added utility methods for checking probe results --- dns_test.go | 135 ++++++++++++++------------------------------------ http_test.go | 67 ++++++------------------- tcp_test.go | 70 +++++++------------------- utils_test.go | 20 ++++++++ 4 files changed, 90 insertions(+), 202 deletions(-) create mode 100644 utils_test.go diff --git a/dns_test.go b/dns_test.go index 9736434..aaafa5e 100644 --- a/dns_test.go +++ b/dns_test.go @@ -14,17 +14,14 @@ package main import ( - "bytes" "net" "net/http/httptest" - "regexp" "runtime" "testing" "time" "github.com/miekg/dns" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/expfmt" ) var PROTOCOLS = [...]string{"udp", "tcp"} @@ -127,27 +124,16 @@ func TestRecursiveDNSResponse(t *testing.T) { if result != test.ShouldSucceed { t.Fatalf("Test %d had unexpected result: %v", i, result) } - mfs, err := registry.Gather() if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - - for _, re := range []*regexp.Regexp{ - regexp.MustCompile("probe_dns_answer_rrs 2"), - regexp.MustCompile("probe_dns_authority_rrs 0"), - regexp.MustCompile("probe_dns_additional_rrs 0"), - } { - if !re.Match(buf.Bytes()) { - t.Errorf("Did not find expected output in test %d: %q", i, re) - } + expectedResults := map[string]float64{ + "probe_dns_answer_rrs": 2, + "probe_dns_authority_rrs": 0, + "probe_dns_additional_rrs": 0, } + checkRegistryResults(expectedResults, mfs, t) } } } @@ -264,27 +250,16 @@ func TestAuthoritativeDNSResponse(t *testing.T) { if result != test.ShouldSucceed { t.Fatalf("Test %d had unexpected result: %v", i, result) } - mfs, err := registry.Gather() if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - - for _, re := range []*regexp.Regexp{ - regexp.MustCompile("probe_dns_answer_rrs 1"), - regexp.MustCompile("probe_dns_authority_rrs 2"), - regexp.MustCompile("probe_dns_additional_rrs 3"), - } { - if !re.Match(buf.Bytes()) { - t.Errorf("Did not find expected output in test %d: %q", i, re) - } + expectedResults := map[string]float64{ + "probe_dns_answer_rrs": 1, + "probe_dns_authority_rrs": 2, + "probe_dns_additional_rrs": 3, } + checkRegistryResults(expectedResults, mfs, t) } } } @@ -336,22 +311,12 @@ func TestServfailDNSResponse(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - - for _, re := range []*regexp.Regexp{ - regexp.MustCompile("probe_dns_answer_rrs 0"), - regexp.MustCompile("probe_dns_authority_rrs 0"), - regexp.MustCompile("probe_dns_additional_rrs 0"), - } { - if !re.Match(buf.Bytes()) { - t.Errorf("Did not find expected output in test %d: %q", i, re) - } + expectedResults := map[string]float64{ + "probe_dns_answer_rrs": 0, + "probe_dns_authority_rrs": 0, + "probe_dns_additional_rrs": 0, } + checkRegistryResults(expectedResults, mfs, t) } } } @@ -392,16 +357,11 @@ func TestDNSProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_ip_protocol 4") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv4, got %s", buf.String()) + + expectedResults := map[string]float64{ + "probe_ip_protocol": 4, } + checkRegistryResults(expectedResults, mfs, t) // Force IPv6 module = Module{ @@ -421,15 +381,10 @@ func TestDNSProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) // Prefer IPv6 module = Module{ @@ -450,15 +405,10 @@ func TestDNSProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) // Prefer IPv4 module = Module{ @@ -479,15 +429,11 @@ func TestDNSProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 4") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv4, got %s", buf.String()) + + expectedResults = map[string]float64{ + "probe_ip_protocol": 4, } + checkRegistryResults(expectedResults, mfs, t) // Prefer none module = Module{ @@ -507,15 +453,11 @@ func TestDNSProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) // No protocol module = Module{ @@ -540,15 +482,10 @@ func TestDNSProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) } } diff --git a/http_test.go b/http_test.go index 57f4df3..c08f7e4 100644 --- a/http_test.go +++ b/http_test.go @@ -14,18 +14,15 @@ package main import ( - "bytes" "fmt" "net/http" "net/http/httptest" - "regexp" "strings" "testing" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/config" - "github.com/prometheus/common/expfmt" ) func TestHTTPStatusCodes(t *testing.T) { @@ -77,21 +74,15 @@ func TestRedirectFollowed(t *testing.T) { if !result { t.Fatalf("Redirect test failed unexpectedly, got %s", body) } + mfs, err := registry.Gather() if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_http_redirects 1") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected one redirect, got %s", body) + expectedResults := map[string]float64{ + "probe_http_redirects": 1, } - + checkRegistryResults(expectedResults, mfs, t) } func TestRedirectNotFollowed(t *testing.T) { @@ -147,17 +138,10 @@ func TestFailIfNotSSL(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_http_ssl 0") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected HTTP without SSL, got %s", body) + expectedResults := map[string]float64{ + "probe_http_ssl": 0, } - + checkRegistryResults(expectedResults, mfs, t) } func TestFailIfMatchesRegexp(t *testing.T) { @@ -330,16 +314,10 @@ func TestFailIfSelfSignedCA(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_http_ssl 0") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected HTTP without SSL because of CA failure, got %s", body) + expectedResults := map[string]float64{ + "probe_http_ssl": 0, } + checkRegistryResults(expectedResults, mfs, t) } func TestSucceedIfSelfSignedCA(t *testing.T) { @@ -361,16 +339,10 @@ func TestSucceedIfSelfSignedCA(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_http_ssl 1") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected HTTP with SSL, got %s", body) + expectedResults := map[string]float64{ + "probe_http_ssl": 1, } + checkRegistryResults(expectedResults, mfs, t) } func TestTLSConfigIsIgnoredForPlainHTTP(t *testing.T) { @@ -392,15 +364,8 @@ func TestTLSConfigIsIgnoredForPlainHTTP(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_http_ssl 0") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected HTTP without SSL, got %s", body) + expectedResults := map[string]float64{ + "probe_http_ssl": 0, } - + checkRegistryResults(expectedResults, mfs, t) } diff --git a/tcp_test.go b/tcp_test.go index d697d44..2968e96 100644 --- a/tcp_test.go +++ b/tcp_test.go @@ -14,17 +14,14 @@ package main import ( - "bytes" "fmt" "net" "net/http/httptest" - "regexp" "runtime" "testing" "time" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/expfmt" ) func TestTCPConnection(t *testing.T) { @@ -199,16 +196,10 @@ func TestTCPConnectionProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - var buf bytes.Buffer - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re := regexp.MustCompile("probe_ip_protocol 4") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv4, got %s", buf.String()) + expectedResults := map[string]float64{ + "probe_ip_protocol": 4, } + checkRegistryResults(expectedResults, mfs, t) // Force IPv6 module = Module{ @@ -228,15 +219,10 @@ func TestTCPConnectionProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) // Prefer IPv4 module = Module{ @@ -257,15 +243,10 @@ func TestTCPConnectionProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 4") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv4, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 4, } + checkRegistryResults(expectedResults, mfs, t) // Prefer IPv6 module = Module{ @@ -286,15 +267,10 @@ func TestTCPConnectionProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) // Prefer nothing module = Module{ @@ -314,15 +290,10 @@ func TestTCPConnectionProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) // No protocol module = Module{ @@ -340,13 +311,8 @@ func TestTCPConnectionProtocol(t *testing.T) { if err != nil { t.Fatal(err) } - for _, mf := range mfs { - if _, err = expfmt.MetricFamilyToText(&buf, mf); err != nil { - t.Fatal(err) - } - } - re = regexp.MustCompile("probe_ip_protocol 6") - if !re.Match(buf.Bytes()) { - t.Errorf("Expected IPv6, got %s", buf.String()) + expectedResults = map[string]float64{ + "probe_ip_protocol": 6, } + checkRegistryResults(expectedResults, mfs, t) } diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..1cbc4f0 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,20 @@ +package main + +import ( + "testing" + + dto "github.com/prometheus/client_model/go" +) + +// Check if expected results are in the registry +func checkRegistryResults(expRes map[string]float64, mfs []*dto.MetricFamily, t *testing.T) { + res := make(map[string]float64) + for i := range mfs { + res[mfs[i].GetName()] = mfs[i].Metric[0].GetGauge().GetValue() + } + for k, v := range expRes { + if val, ok := res[k]; !ok || val != v { + t.Fatalf("Expected: %v: %v, got: %v: %v", k, v, k, val) + } + } +} -- 2.25.1