aboutsummaryrefslogtreecommitdiff
path: root/collector/ipvs_linux_test.go
blob: 43c68395b4da6a0dc4bbfc15d357d745aef51d3e (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2015 The Prometheus Authors
// 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.

package collector

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/go-kit/kit/log"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"gopkg.in/alecthomas/kingpin.v2"
)

func TestIPVSCollector(t *testing.T) {
	testcases := []struct {
		labels  string
		expects []string
		err     error
	}{
		{
			"<none>",
			[]string{
				prometheus.NewDesc("node_ipvs_connections_total", "The total number of connections made.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_packets_total", "The total number of incoming packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_packets_total", "The total number of outgoing packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_bytes_total", "The total amount of incoming data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_bytes_total", "The total amount of outgoing data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_active", "The current active connections by local and remote address.", []string{"local_address", "local_port", "remote_address", "remote_port", "proto", "local_mark"}, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_inactive", "The current inactive connections by local and remote address.", []string{"local_address", "local_port", "remote_address", "remote_port", "proto", "local_mark"}, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_weight", "The current backend weight by local and remote address.", []string{"local_address", "local_port", "remote_address", "remote_port", "proto", "local_mark"}, nil).String(),
			},
			nil,
		},
		{
			"",
			[]string{
				prometheus.NewDesc("node_ipvs_connections_total", "The total number of connections made.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_packets_total", "The total number of incoming packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_packets_total", "The total number of outgoing packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_bytes_total", "The total amount of incoming data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_bytes_total", "The total amount of outgoing data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_active", "The current active connections by local and remote address.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_inactive", "The current inactive connections by local and remote address.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_weight", "The current backend weight by local and remote address.", nil, nil).String(),
			},
			nil,
		},
		{
			"local_port",
			[]string{
				prometheus.NewDesc("node_ipvs_connections_total", "The total number of connections made.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_packets_total", "The total number of incoming packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_packets_total", "The total number of outgoing packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_bytes_total", "The total amount of incoming data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_bytes_total", "The total amount of outgoing data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_active", "The current active connections by local and remote address.", []string{"local_port"}, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_inactive", "The current inactive connections by local and remote address.", []string{"local_port"}, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_weight", "The current backend weight by local and remote address.", []string{"local_port"}, nil).String(),
			},
			nil,
		},
		{
			"local_address,local_port",
			[]string{
				prometheus.NewDesc("node_ipvs_connections_total", "The total number of connections made.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_packets_total", "The total number of incoming packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_packets_total", "The total number of outgoing packets.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_incoming_bytes_total", "The total amount of incoming data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_outgoing_bytes_total", "The total amount of outgoing data.", nil, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_active", "The current active connections by local and remote address.", []string{"local_address", "local_port"}, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_connections_inactive", "The current inactive connections by local and remote address.", []string{"local_address", "local_port"}, nil).String(),
				prometheus.NewDesc("node_ipvs_backend_weight", "The current backend weight by local and remote address.", []string{"local_address", "local_port"}, nil).String(),
			},
			nil,
		},
		{
			"invalid_label",
			nil,
			errors.New(`unknown IPVS backend labels: "invalid_label"`),
		},
		{
			"invalid_label,bad_label",
			nil,
			errors.New(`unknown IPVS backend labels: "bad_label, invalid_label"`),
		},
	}
	for _, test := range testcases {
		t.Run(test.labels, func(t *testing.T) {
			args := []string{"--path.procfs", "fixtures/proc"}
			if test.labels != "<none>" {
				args = append(args, "--collector.ipvs.backend-labels="+test.labels)
			}
			if _, err := kingpin.CommandLine.Parse(args); err != nil {
				t.Fatal(err)
			}
			collector, err := newIPVSCollector(log.NewNopLogger())
			if err != nil {
				if test.err == nil {
					t.Fatal(err)
				}
				if !strings.Contains(err.Error(), test.err.Error()) {
					t.Fatalf("expect error: %v contains %v", err, test.err)
				}
				return
			}
			if test.err != nil {
				t.Fatalf("expect error: %v but got no error", test.err)
			}

			sink := make(chan prometheus.Metric)
			go func() {
				err = collector.Update(sink)
				if err != nil {
					panic(fmt.Sprintf("failed to update collector: %v", err))
				}
			}()
			for _, expected := range test.expects {
				got := (<-sink).Desc().String()
				if expected != got {
					t.Fatalf("Expected '%s' but got '%s'", expected, got)
				}
			}
		})
	}
}

// mock collector
type miniCollector struct {
	c Collector
}

func (c miniCollector) Collect(ch chan<- prometheus.Metric) {
	c.c.Update(ch)
}

func (c miniCollector) Describe(ch chan<- *prometheus.Desc) {
	prometheus.NewGauge(prometheus.GaugeOpts{
		Namespace: "fake",
		Subsystem: "fake",
		Name:      "fake",
		Help:      "fake",
	}).Describe(ch)
}

func TestIPVSCollectorResponse(t *testing.T) {
	testcases := []struct {
		labels      string
		metricsFile string
	}{
		{"<none>", "fixtures/ip_vs_result.txt"},
		{"", "fixtures/ip_vs_result_lbs_none.txt"},
		{"local_port", "fixtures/ip_vs_result_lbs_local_port.txt"},
		{"local_address,local_port", "fixtures/ip_vs_result_lbs_local_address_local_port.txt"},
	}
	for _, test := range testcases {
		t.Run(test.labels, func(t *testing.T) {
			args := []string{"--path.procfs", "fixtures/proc"}
			if test.labels != "<none>" {
				args = append(args, "--collector.ipvs.backend-labels="+test.labels)
			}
			if _, err := kingpin.CommandLine.Parse(args); err != nil {
				t.Fatal(err)
			}
			collector, err := NewIPVSCollector(log.NewNopLogger())
			if err != nil {
				t.Fatal(err)
			}
			registry := prometheus.NewRegistry()
			registry.MustRegister(miniCollector{c: collector})

			rw := httptest.NewRecorder()
			promhttp.InstrumentMetricHandler(registry, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})).ServeHTTP(rw, &http.Request{})

			wantMetrics, err := ioutil.ReadFile(test.metricsFile)
			if err != nil {
				t.Fatalf("unable to read input test file %s: %s", test.metricsFile, err)
			}

			wantLines := strings.Split(string(wantMetrics), "\n")
			gotLines := strings.Split(string(rw.Body.String()), "\n")
			gotLinesIdx := 0

			// Until the Prometheus Go client library offers better testability
			// (https://github.com/prometheus/client_golang/issues/58), we simply compare
			// verbatim text-format metrics outputs, but ignore any lines we don't have
			// in the fixture. Put differently, we are only testing that each line from
			// the fixture is present, in the order given.
		wantLoop:
			for _, want := range wantLines {
				for _, got := range gotLines[gotLinesIdx:] {
					if want == got {
						// this is a line we are interested in, and it is correct
						continue wantLoop
					} else {
						gotLinesIdx++
					}
				}
				// if this point is reached, the line we want was missing
				t.Fatalf("Missing expected output line(s), first missing line is %s", want)
			}
		})
	}
}