dialer_test.go 10.3 KB
Newer Older
Michal Witkowski's avatar
Michal Witkowski committed
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
// Copyright 2016 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.

package conntrack_test

import (
	"context"
	"net"
	"net/http"
	"testing"

	"time"

	"github.com/mwitkow/go-conntrack"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/stretchr/testify/suite"
)

func TestDialerWrapper(t *testing.T) {
	suite.Run(t, &DialerTestSuite{})
}

type DialerTestSuite struct {
	suite.Suite

	serverListener net.Listener
	httpServer     http.Server
}

func (s *DialerTestSuite) SetupSuite() {
	var err error
	s.serverListener, err = net.Listen("tcp", "127.0.0.1:0")
	require.NoError(s.T(), err, "must be able to allocate a port for serverListener")
	s.httpServer = http.Server{
		Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
			resp.WriteHeader(http.StatusOK)
		}),
	}
	go func() {
		s.httpServer.Serve(s.serverListener)
	}()
}

func (s *DialerTestSuite) TestDialerMetricsArePreregistered() {
	conntrack.NewDialContextFunc() // dialer name = default
	conntrack.NewDialContextFunc(conntrack.DialWithName("foobar"))
	conntrack.PreRegisterDialerMetrics("something_manual")
	for testId, testCase := range []struct {
		metricName     string
		existingLabels []string
	}{
		{"net_conntrack_dialer_conn_attempted_total", []string{"default"}},
		{"net_conntrack_dialer_conn_attempted_total", []string{"foobar"}},
		{"net_conntrack_dialer_conn_attempted_total", []string{"something_manual"}},
		{"net_conntrack_dialer_conn_closed_total", []string{"default"}},
		{"net_conntrack_dialer_conn_closed_total", []string{"foobar"}},
		{"net_conntrack_dialer_conn_closed_total", []string{"something_manual"}},
		{"net_conntrack_dialer_conn_established_total", []string{"default"}},
		{"net_conntrack_dialer_conn_established_total", []string{"foobar"}},
		{"net_conntrack_dialer_conn_established_total", []string{"something_manual"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"default", "resolution"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"default", "refused"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"default", "timeout"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"default", "unknown"}},
	} {
		lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...))
		assert.NotEqual(s.T(), 0, lineCount, "metrics must exist for test case %d", testId)
	}
}

func (s *DialerTestSuite) TestDialerMetricsAreNotPreregisteredWithMonitoringOff() {
	conntrack.NewDialContextFunc(conntrack.DialWithName("nomon"), conntrack.DialWithoutMonitoring())
	for testId, testCase := range []struct {
		metricName     string
		existingLabels []string
	}{
		{"net_conntrack_dialer_conn_attempted_total", []string{"nomon"}},
		{"net_conntrack_dialer_conn_closed_total", []string{"nomon"}},
		{"net_conntrack_dialer_conn_established_total", []string{"nomon"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "resolution"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "refused"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "timeout"}},
		{"net_conntrack_dialer_conn_failed_total", []string{"nomon", "unknown"}},
	} {
		lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...))
Michal Witkowski's avatar
Michal Witkowski committed
87
		assert.Equal(s.T(), 0, lineCount, "metrics should not be registered exist for test case %d", testId)
Michal Witkowski's avatar
Michal Witkowski committed
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
	}
}

func (s *DialerTestSuite) TestDialerUnderNormalConnection() {
	dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithName("normal_conn"))

	beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "normal_conn")
	beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn")
	beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "normal_conn")

	conn, err := dialFunc(context.TODO(), "tcp", s.serverListener.Addr().String())
	require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here")
	assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "normal_conn"),
		"the attempted conn counter must be incremented after connection was opened")
	assert.Equal(s.T(), beforeEstablished+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn"),
		"the established conn counter must be incremented after connection was opened")
	assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "normal_conn"),
		"the closed conn counter must not be incremented after connection was opened")
	conn.Close()
	assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn"),
		"the closed conn counter must be incremented after connection was closed")
}

func (s *DialerTestSuite) TestDialerWithContextName() {
	dialFunc := conntrack.NewDialContextFunc()
	conntrack.PreRegisterDialerMetrics("ctx_conn")

	beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ctx_conn")
	beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ctx_conn")
	beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ctx_conn")

	conn, err := dialFunc(conntrack.DialNameToContext(context.TODO(), "ctx_conn"), "tcp", s.serverListener.Addr().String())
	require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here")
	assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ctx_conn"),
		"the attempted conn counter must be incremented after connection was opened")
	assert.Equal(s.T(), beforeEstablished+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ctx_conn"),
		"the established conn counter must be incremented after connection was opened")
	assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ctx_conn"),
		"the closed conn counter must not be incremented after connection was opened")
	conn.Close()
	assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ctx_conn"),
		"the closed conn counter must be incremented after connection was closed")
}

func (s *ListenerTestSuite) TestDialerTracingCapturedInPage() {
	dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithTracing())
	dialerName := "some_dialer"
	conn, err := dialFunc(conntrack.DialNameToContext(context.TODO(), dialerName), "tcp", s.serverListener.Addr().String())
	time.Sleep(5 * time.Millisecond)
	require.NoError(s.T(), err, "DialContext should successfully establish a conn here")
	assert.Contains(s.T(), fetchTraceEvents(s.T(), "net.ClientConn."+dialerName), conn.LocalAddr().String(),
		"the /debug/trace/events page must contain the live connection")
	time.Sleep(5 * time.Millisecond)
	conn.Close()
}

func (s *DialerTestSuite) TestDialerResolutionFailure() {
	dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithName("res_err"))

	beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "res_err")
	beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "res_err")
	beforeResolutionErrors := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "res_err", "resolution")
	beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "res_err")

	_, err := dialFunc(context.TODO(), "tcp", "dialer.test.wrong.domain.wrong:443")
	require.Error(s.T(), err, "NewDialContextFunc should fail here")
	assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "res_err"),
		"the attempted conn counter must be incremented after connection was opened")
	assert.Equal(s.T(), beforeEstablished, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "res_err"),
		"the established conn counter must not be incremented on a failure")
	assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "res_err"),
		"the closed conn counter must not be incremented on a failure")
	assert.Equal(s.T(), beforeResolutionErrors+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "res_err", "resolution"),
		"the failure counter for resolution error should be incremented")
}

func (s *DialerTestSuite) TestDialerRefusedFailure() {
	dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithName("ref_err"))

	beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ref_err")
	beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ref_err")
	beforeResolutionErrors := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "ref_err", "resolution")
	beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ref_err")

	_, err := dialFunc(context.TODO(), "tcp", "127.0.0.1:337") // 337 is a cool port, let's hope its unused.
	require.Error(s.T(), err, "NewDialContextFunc should fail here")
	assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ref_err"),
		"the attempted conn counter must be incremented after connection was opened")
	assert.Equal(s.T(), beforeEstablished, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ref_err"),
		"the established conn counter must not be incremented on a failure")
	assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ref_err"),
		"the closed conn counter must not be incremented on a failure")
	assert.Equal(s.T(), beforeResolutionErrors+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "ref_err", "refused"),
		"the failure counter for connection refused error should be incremented")
}

func (s *DialerTestSuite) TearDownSuite() {
	if s.serverListener != nil {
		s.T().Logf("stopped http.Server at: %v", s.serverListener.Addr().String())
		s.serverListener.Close()
	}
}