1// Copyright 2016 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package push
15
16import (
17	"bytes"
18	"io/ioutil"
19	"net/http"
20	"net/http/httptest"
21	"testing"
22
23	"github.com/prometheus/common/expfmt"
24
25	"github.com/prometheus/client_golang/prometheus"
26)
27
28func TestPush(t *testing.T) {
29
30	var (
31		lastMethod string
32		lastBody   []byte
33		lastPath   string
34	)
35
36	// Fake a Pushgateway that always responds with 202.
37	pgwOK := httptest.NewServer(
38		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39			lastMethod = r.Method
40			var err error
41			lastBody, err = ioutil.ReadAll(r.Body)
42			if err != nil {
43				t.Fatal(err)
44			}
45			lastPath = r.URL.EscapedPath()
46			w.Header().Set("Content-Type", `text/plain; charset=utf-8`)
47			w.WriteHeader(http.StatusAccepted)
48		}),
49	)
50	defer pgwOK.Close()
51
52	// Fake a Pushgateway that always responds with 500.
53	pgwErr := httptest.NewServer(
54		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
55			http.Error(w, "fake error", http.StatusInternalServerError)
56		}),
57	)
58	defer pgwErr.Close()
59
60	metric1 := prometheus.NewCounter(prometheus.CounterOpts{
61		Name: "testname1",
62		Help: "testhelp1",
63	})
64	metric2 := prometheus.NewGauge(prometheus.GaugeOpts{
65		Name:        "testname2",
66		Help:        "testhelp2",
67		ConstLabels: prometheus.Labels{"foo": "bar", "dings": "bums"},
68	})
69
70	reg := prometheus.NewRegistry()
71	reg.MustRegister(metric1)
72	reg.MustRegister(metric2)
73
74	mfs, err := reg.Gather()
75	if err != nil {
76		t.Fatal(err)
77	}
78
79	buf := &bytes.Buffer{}
80	enc := expfmt.NewEncoder(buf, expfmt.FmtProtoDelim)
81
82	for _, mf := range mfs {
83		if err := enc.Encode(mf); err != nil {
84			t.Fatal(err)
85		}
86	}
87	wantBody := buf.Bytes()
88
89	// Push some Collectors, all good.
90	if err := New(pgwOK.URL, "testjob").
91		Collector(metric1).
92		Collector(metric2).
93		Push(); err != nil {
94		t.Fatal(err)
95	}
96	if lastMethod != "PUT" {
97		t.Error("want method PUT for Push, got", lastMethod)
98	}
99	if !bytes.Equal(lastBody, wantBody) {
100		t.Errorf("got body %v, want %v", lastBody, wantBody)
101	}
102	if lastPath != "/metrics/job/testjob" {
103		t.Error("unexpected path:", lastPath)
104	}
105
106	// Add some Collectors, with nil grouping, all good.
107	if err := New(pgwOK.URL, "testjob").
108		Collector(metric1).
109		Collector(metric2).
110		Add(); err != nil {
111		t.Fatal(err)
112	}
113	if lastMethod != "POST" {
114		t.Error("want method POST for Add, got", lastMethod)
115	}
116	if !bytes.Equal(lastBody, wantBody) {
117		t.Errorf("got body %v, want %v", lastBody, wantBody)
118	}
119	if lastPath != "/metrics/job/testjob" {
120		t.Error("unexpected path:", lastPath)
121	}
122
123	// Push some Collectors with a broken PGW.
124	if err := New(pgwErr.URL, "testjob").
125		Collector(metric1).
126		Collector(metric2).
127		Push(); err == nil {
128		t.Error("push to broken Pushgateway succeeded")
129	} else {
130		if got, want := err.Error(), "unexpected status code 500 while pushing to "+pgwErr.URL+"/metrics/job/testjob: fake error\n"; got != want {
131			t.Errorf("got error %q, want %q", got, want)
132		}
133	}
134
135	// Push some Collectors with invalid grouping or job.
136	if err := New(pgwOK.URL, "testjob").
137		Grouping("foo", "bums").
138		Collector(metric1).
139		Collector(metric2).
140		Push(); err == nil {
141		t.Error("push with grouping contained in metrics succeeded")
142	}
143	if err := New(pgwOK.URL, "test/job").
144		Collector(metric1).
145		Collector(metric2).
146		Push(); err == nil {
147		t.Error("push with invalid job value succeeded")
148	}
149	if err := New(pgwOK.URL, "testjob").
150		Grouping("foobar", "bu/ms").
151		Collector(metric1).
152		Collector(metric2).
153		Push(); err == nil {
154		t.Error("push with invalid grouping succeeded")
155	}
156	if err := New(pgwOK.URL, "testjob").
157		Grouping("foo-bar", "bums").
158		Collector(metric1).
159		Collector(metric2).
160		Push(); err == nil {
161		t.Error("push with invalid grouping succeeded")
162	}
163
164	// Push registry, all good.
165	if err := New(pgwOK.URL, "testjob").
166		Gatherer(reg).
167		Push(); err != nil {
168		t.Fatal(err)
169	}
170	if lastMethod != "PUT" {
171		t.Error("want method PUT for Push, got", lastMethod)
172	}
173	if !bytes.Equal(lastBody, wantBody) {
174		t.Errorf("got body %v, want %v", lastBody, wantBody)
175	}
176
177	// Add registry, all good.
178	if err := New(pgwOK.URL, "testjob").
179		Grouping("a", "x").
180		Grouping("b", "y").
181		Gatherer(reg).
182		Add(); err != nil {
183		t.Fatal(err)
184	}
185	if lastMethod != "POST" {
186		t.Error("want method POST for Add, got", lastMethod)
187	}
188	if !bytes.Equal(lastBody, wantBody) {
189		t.Errorf("got body %v, want %v", lastBody, wantBody)
190	}
191	if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
192		t.Error("unexpected path:", lastPath)
193	}
194}
195