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"}
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)
}
}
}
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)
}
}
}
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)
}
}
}
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{
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{
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{
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{
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{
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)
}
}
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) {
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) {
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) {
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) {
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) {
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)
}
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) {
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{
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{
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{
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{
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{
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)
}
--- /dev/null
+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)
+ }
+ }
+}