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
var sockets [5]net.Listener
_globmask := "/tmp/test-haproxy*.sock"
_badmask := "/tmp/test-fail-haproxy*.sock"
for i := 0; i < 5; i++ {
binary.Read(rand.Reader, binary.LittleEndian, &randomNumber) binary.Read(rand.Reader, binary.LittleEndian, &randomNumber)
sock, err := net.Listen("unix", fmt.Sprintf("/tmp/test-haproxy%d.sock", randomNumber)) sockname := fmt.Sprintf("/tmp/test-haproxy%d.sock", randomNumber)
sock, err := net.Listen("unix", sockname)
if err != nil { if err != nil {
t.Fatal("Cannot initialize socket ") t.Fatal("Cannot initialize socket ")
} }
sockets[i] = sock
defer sock.Close() defer sock.Close()
s := statServer{} s := statServer{}
go s.serverSocket(sock) 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)
fields := HaproxyGetFieldValues()
for _, sock := range sockets {
tags := map[string]string{ tags := map[string]string{
"proxy": "be_app", "proxy": "be_app",
"server": sock.Addr().String(), "server": sock.Addr().String(),
"sv": "host0", "sv": "host0",
} }
fields := HaproxyGetFieldValues()
acc.AssertContainsTaggedFields(t, "haproxy", fields, tags) acc.AssertContainsTaggedFields(t, "haproxy", fields, tags)
assert.Equal(t, len(socks), 5, "Failed to glob all sockets") }
// 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