Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics_test.go « upstream « internal « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29a9e09777c49b8375a5c3c6c4bcf80ded464dc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package upstream

import (
	"io"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/prometheus/client_golang/prometheus/testutil"
	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab/workhorse/internal/config"
)

func TestInstrumentGeoProxyRoute(t *testing.T) {
	const (
		remote = `\A/remote\z`
		local  = `\A/local\z`
		main   = ""
	)

	u := newUpstream(config.Config{}, logrus.StandardLogger(), func(u *upstream) {
		u.Routes = []routeEntry{
			handleRouteWithMatchers(u, remote, withGeoProxy()),
			handleRouteWithMatchers(u, local),
			handleRouteWithMatchers(u, main),
		}
	})
	ts := httptest.NewServer(u)
	defer ts.Close()

	testCases := []testCase{
		{"remote", "/remote", remote},
		{"local", "/local", local},
		{"main", "/", main},
	}

	httpGeoProxiedRequestsTotal.Reset()

	runTestCases(t, ts, testCases)

	require.Equal(t, 1, testutil.CollectAndCount(httpGeoProxiedRequestsTotal))
	require.InDelta(t, 1, testutil.ToFloat64(httpGeoProxiedRequestsTotal.WithLabelValues("200", "get", remote)), 0.1)
	require.InDelta(t, 0, testutil.ToFloat64(httpGeoProxiedRequestsTotal.WithLabelValues("200", "get", local)), 0.1)
	require.InDelta(t, 0, testutil.ToFloat64(httpGeoProxiedRequestsTotal.WithLabelValues("200", "get", main)), 0.1)
}

func handleRouteWithMatchers(u *upstream, regex string, matchers ...func(*routeOptions)) routeEntry {
	handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		io.WriteString(w, regex)
	})
	return u.route("", regex, handler, matchers...)
}