haproxy_test: extend tests to cover name globbing

This commit is contained in:
Marko Crnic 2016-09-08 10:36:22 +02:00 committed by Cameron Sparr
parent 5c87b92976
commit 56b627dfe2
1 changed files with 35 additions and 19 deletions

View File

@ -111,36 +111,52 @@ func TestHaproxyGeneratesMetricsWithoutAuthentication(t *testing.T) {
func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) { func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) {
var randomNumber int64 var randomNumber int64
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber) var sockets [5]net.Listener
sock, err := net.Listen("unix", fmt.Sprintf("/tmp/test-haproxy%d.sock", randomNumber)) _globmask := "/tmp/test-haproxy*.sock"
if err != nil { _badmask := "/tmp/test-fail-haproxy*.sock"
t.Fatal("Cannot initialize socket ")
for i := 0; i < 5; i++ {
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber)
sockname := fmt.Sprintf("/tmp/test-haproxy%d.sock", randomNumber)
sock, err := net.Listen("unix", sockname)
if err != nil {
t.Fatal("Cannot initialize socket ")
}
sockets[i] = sock
defer sock.Close()
s := statServer{}
go s.serverSocket(sock)
} }
defer sock.Close()
s := statServer{}
go s.serverSocket(sock)
r := &haproxy{ r := &haproxy{
Servers: []string{sock.Addr().String()}, Servers: []string{_globmask},
} }
var acc testutil.Accumulator var acc testutil.Accumulator
err = r.Gather(&acc) err := r.Gather(&acc)
require.NoError(t, err) require.NoError(t, err)
tags := map[string]string{
"proxy": "be_app",
"server": sock.Addr().String(),
"sv": "host0",
}
fields := HaproxyGetFieldValues() fields := HaproxyGetFieldValues()
acc.AssertContainsTaggedFields(t, "haproxy", fields, tags) for _, sock := range sockets {
assert.Equal(t, len(socks), 5, "Failed to glob all sockets") tags := map[string]string{
"proxy": "be_app",
"server": sock.Addr().String(),
"sv": "host0",
}
acc.AssertContainsTaggedFields(t, "haproxy", fields, tags)
}
// This mask should not match any socket
r.Servers = []string{_badmask}
err = r.Gather(&acc)
require.Error(t, err)
} }
//When not passing server config, we default to localhost //When not passing server config, we default to localhost