target_test.go 9.3 KB
Newer Older
1
// Copyright 2013 The Prometheus Authors
2 3 4 5 6 7 8 9 10 11 12 13
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

14
package scrape
15 16

import (
17 18
	"crypto/tls"
	"crypto/x509"
19
	"fmt"
20
	"io/ioutil"
21 22
	"net/http"
	"net/http/httptest"
Fabian Reinartz committed
23
	"net/url"
24
	"reflect"
25
	"strings"
26 27
	"testing"
	"time"
28

29
	"github.com/prometheus/common/model"
30

31
	config_util "github.com/prometheus/common/config"
32
	"github.com/prometheus/prometheus/pkg/labels"
33 34
)

35
const (
36
	caCertPath = "testdata/ca.cer"
37 38
)

39
func TestTargetLabels(t *testing.T) {
40 41
	target := newTestTarget("example.com:80", 0, labels.FromStrings("job", "some_job", "foo", "bar"))
	want := labels.FromStrings(model.JobLabel, "some_job", "foo", "bar")
42
	got := target.Labels()
43 44 45 46 47
	if !reflect.DeepEqual(want, got) {
		t.Errorf("want base labels %v, got %v", want, got)
	}
}

48 49
func TestTargetOffset(t *testing.T) {
	interval := 10 * time.Second
50
	jitter := uint64(0)
51 52 53 54 55

	offsets := make([]time.Duration, 10000)

	// Calculate offsets for 10000 different targets.
	for i := range offsets {
56 57 58
		target := newTestTarget("example.com:80", 0, labels.FromStrings(
			"label", fmt.Sprintf("%d", i),
		))
59
		offsets[i] = target.offset(interval, jitter)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	}

	// Put the offsets into buckets and validate that they are all
	// within bounds.
	bucketSize := 1 * time.Second
	buckets := make([]int, interval/bucketSize)

	for _, offset := range offsets {
		if offset < 0 || offset >= interval {
			t.Fatalf("Offset %v out of bounds", offset)
		}

		bucket := offset / bucketSize
		buckets[bucket]++
	}

	t.Log(buckets)

78
	// Calculate whether the number of targets per bucket
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	// does not differ more than a given tolerance.
	avg := len(offsets) / len(buckets)
	tolerance := 0.15

	for _, bucket := range buckets {
		diff := bucket - avg
		if diff < 0 {
			diff = -diff
		}

		if float64(diff)/float64(avg) > tolerance {
			t.Fatalf("Bucket out of tolerance bounds")
		}
	}
}

Fabian Reinartz committed
95 96 97 98 99
func TestTargetURL(t *testing.T) {
	params := url.Values{
		"abc": []string{"foo", "bar", "baz"},
		"xyz": []string{"hoo"},
	}
100
	labels := labels.FromMap(map[string]string{
Fabian Reinartz committed
101 102 103 104 105
		model.AddressLabel:     "example.com:1234",
		model.SchemeLabel:      "https",
		model.MetricsPathLabel: "/metricz",
		"__param_abc":          "overwrite",
		"__param_cde":          "huu",
106
	})
Fabian Reinartz committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	target := NewTarget(labels, labels, params)

	// The reserved labels are concatenated into a full URL. The first value for each
	// URL query parameter can be set/modified via labels as well.
	expectedParams := url.Values{
		"abc": []string{"overwrite", "bar", "baz"},
		"cde": []string{"huu"},
		"xyz": []string{"hoo"},
	}
	expectedURL := url.URL{
		Scheme:   "https",
		Host:     "example.com:1234",
		Path:     "/metricz",
		RawQuery: expectedParams.Encode(),
	}

	if u := target.URL(); !reflect.DeepEqual(u.String(), expectedURL.String()) {
124
		t.Fatalf("Expected URL %q, but got %q", expectedURL.String(), u.String())
Fabian Reinartz committed
125 126
	}
}
127

128 129 130
func newTestTarget(targetURL string, deadline time.Duration, lbls labels.Labels) *Target {
	lb := labels.NewBuilder(lbls)
	lb.Set(model.SchemeLabel, "http")
131
	lb.Set(model.AddressLabel, strings.TrimPrefix(targetURL, "http://"))
132
	lb.Set(model.MetricsPathLabel, "/metrics")
133

134
	return &Target{labels: lb.Labels()}
135
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

func TestNewHTTPBearerToken(t *testing.T) {
	server := httptest.NewServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				expected := "Bearer 1234"
				received := r.Header.Get("Authorization")
				if expected != received {
					t.Fatalf("Authorization header was not set correctly: expected '%v', got '%v'", expected, received)
				}
			},
		),
	)
	defer server.Close()

151
	cfg := config_util.HTTPClientConfig{
152
		BearerToken: "1234",
153
	}
154
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
}

func TestNewHTTPBearerTokenFile(t *testing.T) {
	server := httptest.NewServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				expected := "Bearer 12345"
				received := r.Header.Get("Authorization")
				if expected != received {
					t.Fatalf("Authorization header was not set correctly: expected '%v', got '%v'", expected, received)
				}
			},
		),
	)
	defer server.Close()

178
	cfg := config_util.HTTPClientConfig{
179 180
		BearerTokenFile: "testdata/bearertoken.txt",
	}
181
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
182 183 184 185 186 187 188 189 190
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
}

191 192 193 194 195 196 197 198 199 200 201 202 203
func TestNewHTTPBasicAuth(t *testing.T) {
	server := httptest.NewServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				username, password, ok := r.BasicAuth()
				if !(ok && username == "user" && password == "password123") {
					t.Fatalf("Basic authorization header was not set correctly: expected '%v:%v', got '%v:%v'", "user", "password123", username, password)
				}
			},
		),
	)
	defer server.Close()

204 205
	cfg := config_util.HTTPClientConfig{
		BasicAuth: &config_util.BasicAuth{
206 207 208 209
			Username: "user",
			Password: "password123",
		},
	}
210
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
211 212 213 214 215 216 217 218 219
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
}

220 221 222 223 224 225 226 227 228
func TestNewHTTPCACert(t *testing.T) {
	server := httptest.NewUnstartedServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("Content-Type", `text/plain; version=0.0.4`)
				w.Write([]byte{})
			},
		),
	)
229
	server.TLS = newTLSConfig("server", t)
230 231 232
	server.StartTLS()
	defer server.Close()

233 234
	cfg := config_util.HTTPClientConfig{
		TLSConfig: config_util.TLSConfig{
235
			CAFile: caCertPath,
236
		},
237
	}
238
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
}

func TestNewHTTPClientCert(t *testing.T) {
	server := httptest.NewUnstartedServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("Content-Type", `text/plain; version=0.0.4`)
				w.Write([]byte{})
			},
		),
	)
257
	tlsConfig := newTLSConfig("server", t)
258 259 260 261 262 263
	tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
	tlsConfig.ClientCAs = tlsConfig.RootCAs
	server.TLS = tlsConfig
	server.StartTLS()
	defer server.Close()

264 265
	cfg := config_util.HTTPClientConfig{
		TLSConfig: config_util.TLSConfig{
266
			CAFile:   caCertPath,
267 268
			CertFile: "testdata/client.cer",
			KeyFile:  "testdata/client.key",
269 270
		},
	}
271
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
272 273 274 275 276 277 278 279 280
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
}

281 282 283 284 285 286 287 288 289 290 291 292 293
func TestNewHTTPWithServerName(t *testing.T) {
	server := httptest.NewUnstartedServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("Content-Type", `text/plain; version=0.0.4`)
				w.Write([]byte{})
			},
		),
	)
	server.TLS = newTLSConfig("servername", t)
	server.StartTLS()
	defer server.Close()

294 295
	cfg := config_util.HTTPClientConfig{
		TLSConfig: config_util.TLSConfig{
296
			CAFile:     caCertPath,
297 298 299
			ServerName: "prometheus.rocks",
		},
	}
300
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err != nil {
		t.Fatal(err)
	}
}

func TestNewHTTPWithBadServerName(t *testing.T) {
	server := httptest.NewUnstartedServer(
		http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("Content-Type", `text/plain; version=0.0.4`)
				w.Write([]byte{})
			},
		),
	)
	server.TLS = newTLSConfig("servername", t)
	server.StartTLS()
	defer server.Close()

323 324
	cfg := config_util.HTTPClientConfig{
		TLSConfig: config_util.TLSConfig{
325
			CAFile:     caCertPath,
326 327 328
			ServerName: "badname",
		},
	}
329
	c, err := config_util.NewClientFromConfig(cfg, "test", false, false)
330 331 332 333 334 335 336 337 338 339
	if err != nil {
		t.Fatal(err)
	}
	_, err = c.Get(server.URL)
	if err == nil {
		t.Fatal("Expected error, got nil.")
	}
}

func newTLSConfig(certName string, t *testing.T) *tls.Config {
340 341
	tlsConfig := &tls.Config{}
	caCertPool := x509.NewCertPool()
342
	caCert, err := ioutil.ReadFile(caCertPath)
343 344 345 346 347 348
	if err != nil {
		t.Fatalf("Couldn't set up TLS server: %v", err)
	}
	caCertPool.AppendCertsFromPEM(caCert)
	tlsConfig.RootCAs = caCertPool
	tlsConfig.ServerName = "127.0.0.1"
349 350 351
	certPath := fmt.Sprintf("testdata/%s.cer", certName)
	keyPath := fmt.Sprintf("testdata/%s.key", certName)
	cert, err := tls.LoadX509KeyPair(certPath, keyPath)
352
	if err != nil {
353
		t.Errorf("Unable to use specified server cert (%s) & key (%v): %s", certPath, keyPath, err)
354 355 356 357
	}
	tlsConfig.Certificates = []tls.Certificate{cert}
	return tlsConfig
}
358

359
func TestNewClientWithBadTLSConfig(t *testing.T) {
360 361
	cfg := config_util.HTTPClientConfig{
		TLSConfig: config_util.TLSConfig{
362 363 364 365 366
			CAFile:   "testdata/nonexistent_ca.cer",
			CertFile: "testdata/nonexistent_client.cer",
			KeyFile:  "testdata/nonexistent_client.key",
		},
	}
367
	_, err := config_util.NewClientFromConfig(cfg, "test", false, false)
368 369 370 371
	if err == nil {
		t.Fatalf("Expected error, got nil.")
	}
}