Skip to content

Commit

Permalink
http: use a method to get local address
Browse files Browse the repository at this point in the history
And limit its export to testing.
  • Loading branch information
vincentbernat committed Aug 21, 2022
1 parent 18ee3ea commit 3e3bcbd
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 28 deletions.
6 changes: 2 additions & 4 deletions common/http/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ type Component struct {
durations *reporter.HistogramVec
sizes *reporter.HistogramVec
}

// Local address used by the HTTP server. Only valid after Start().
Address net.Addr
address net.Addr

// GinRouter is the router exposed for /api
GinRouter *gin.Engine
Expand Down Expand Up @@ -142,7 +140,7 @@ func (c *Component) Start() error {
if err != nil {
return fmt.Errorf("unable to listen to %v: %w", c.config.Listen, err)
}
c.Address = listener.Addr()
c.address = listener.Addr()

// Start serving requests
c.t.Go(func() error {
Expand Down
6 changes: 3 additions & 3 deletions common/http/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestHandler(t *testing.T) {
}))

// Check the HTTP server is running and answering metrics
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/test",
ContentType: "text/plain; charset=utf-8",
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestGinRouter(t *testing.T) {
})
})

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/test",
ContentType: "application/json; charset=utf-8",
Expand All @@ -81,7 +81,7 @@ func TestGinRouterPanic(t *testing.T) {
panic("heeeelp")
})

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/test",
StatusCode: 500,
Expand Down
6 changes: 6 additions & 0 deletions common/http/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package http

import (
"net"
"testing"

"akvorado/common/daemon"
Expand All @@ -26,3 +27,8 @@ func NewMock(t *testing.T, r *reporter.Reporter) *Component {
helpers.StartStop(t, c)
return c
}

// LocalAddr returns the address the HTTP server is listening to.
func (c *Component) LocalAddr() net.Addr {
return c.address
}
2 changes: 1 addition & 1 deletion console/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestServeAssets(t *testing.T) {
conf.ServeLiveFS = live
_, h, _, _ := NewMock(t, conf)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/",
ContentType: "text/html; charset=utf-8",
Expand Down
4 changes: 2 additions & 2 deletions console/authentication/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestUserHandler(t *testing.T) {
endpoint.GET("/avatar", c.UserAvatarHandlerFunc)

t.Run("default user configured", func(t *testing.T) {
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
Description: "user info, no user logged in",
URL: "/api/v0/console/user/info",
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestUserHandler(t *testing.T) {

t.Run("no default user", func(t *testing.T) {
c.config.DefaultUser.Login = ""
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
Description: "user info, no user logged in",
URL: "/api/v0/console/user/info",
Expand Down
2 changes: 1 addition & 1 deletion console/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestConfigHandler(t *testing.T) {
config := DefaultConfiguration()
config.Version = "1.2.3"
_, h, _, _ := NewMock(t, config)
helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/configuration",
JSONOutput: gin.H{
Expand Down
2 changes: 1 addition & 1 deletion console/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestServeDocs(t *testing.T) {
_, h, _, _ := NewMock(t, conf)

resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/console/docs/%s",
h.Address, tc.Path))
h.LocalAddr(), tc.Path))
if err != nil {
t.Fatalf("GET /api/v0/console/docs/%s:\n%+v", tc.Path, err)
}
Expand Down
2 changes: 1 addition & 1 deletion console/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ UNION DISTINCT
}).
Return(nil)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/filter/validate",
JSONInput: gin.H{"filter": `InIfName = "Gi0/0/0/1"`},
Expand Down
2 changes: 1 addition & 1 deletion console/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func TestGraphHandler(t *testing.T) {
SetArg(1, expectedSQL).
Return(nil)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
Description: "single direction",
URL: "/api/v0/console/graph",
Expand Down
2 changes: 1 addition & 1 deletion console/sankey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestSankeyHandler(t *testing.T) {
SetArg(1, expectedSQL).
Return(nil)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/sankey",
JSONInput: gin.H{
Expand Down
10 changes: 5 additions & 5 deletions console/widgets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestWidgetLastFlow(t *testing.T) {
return nil
})

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/widget/flow-last",
JSONOutput: gin.H{
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestFlowRate(t *testing.T) {
`SELECT COUNT(*)/300 AS rate FROM flows WHERE TimeReceived > date_sub(minute, 5, now())`).
Return(mockRow)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/widget/flow-rate",
JSONOutput: gin.H{
Expand All @@ -132,7 +132,7 @@ func TestWidgetExporters(t *testing.T) {
SetArg(1, expected).
Return(nil)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/widget/exporters",
JSONOutput: gin.H{
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestWidgetTop(t *testing.T) {
}),
)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/widget/top/src-port",
JSONOutput: gin.H{
Expand Down Expand Up @@ -245,7 +245,7 @@ ORDER BY Time WITH FILL
SetArg(1, expected).
Return(nil)

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/console/widget/graph?points=100",
JSONOutput: gin.H{
Expand Down
4 changes: 2 additions & 2 deletions inlet/core/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestCore(t *testing.T) {
t.Run("http flows", func(t *testing.T) {
c.httpFlowFlushDelay = 20 * time.Millisecond

resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/inlet/flows", c.d.HTTP.Address))
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/inlet/flows", c.d.HTTP.LocalAddr()))
if err != nil {
t.Fatalf("GET /api/v0/inlet/flows:\n%+v", err)
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestCore(t *testing.T) {
// Test HTTP flow clients with a limit
time.Sleep(10 * time.Millisecond)
t.Run("http flows with limit", func(t *testing.T) {
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/inlet/flows?limit=4", c.d.HTTP.Address))
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/api/v0/inlet/flows?limit=4", c.d.HTTP.LocalAddr()))
if err != nil {
t.Fatalf("GET /api/v0/inlet/flows:\n%+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion inlet/flow/schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ func TestHTTPEndpoints(t *testing.T) {
},
}

helpers.TestHTTPEndpoints(t, c.d.HTTP.Address, cases)
helpers.TestHTTPEndpoints(t, c.d.HTTP.LocalAddr(), cases)
}
4 changes: 2 additions & 2 deletions orchestrator/clickhouse/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestHTTPEndpoints(t *testing.T) {
},
}

helpers.TestHTTPEndpoints(t, c.d.HTTP.Address, cases)
helpers.TestHTTPEndpoints(t, c.d.HTTP.LocalAddr(), cases)
}

func TestAdditionalASNs(t *testing.T) {
Expand Down Expand Up @@ -91,5 +91,5 @@ func TestAdditionalASNs(t *testing.T) {
},
}

helpers.TestHTTPEndpoints(t, c.d.HTTP.Address, cases)
helpers.TestHTTPEndpoints(t, c.d.HTTP.LocalAddr(), cases)
}
2 changes: 1 addition & 1 deletion orchestrator/clickhouse/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *Component) getHTTPBaseURL(address string) (string, error) {
localAddr := conn.LocalAddr().(*net.UDPAddr)

// Combine with HTTP port
_, port, err := net.SplitHostPort(c.d.HTTP.Address.String())
_, port, err := net.SplitHostPort(c.d.HTTP.LocalAddr().String())
if err != nil {
return "", fmt.Errorf("cannot get HTTP port: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/clickhouse/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestGetHTTPBaseURL(t *testing.T) {
}
expectedURL := &url.URL{
Scheme: "http",
Host: http.Address.String(),
Host: http.LocalAddr().String(),
}
parsedURL.Host = parsedURL.Host[strings.LastIndex(parsedURL.Host, ":"):]
expectedURL.Host = expectedURL.Host[strings.LastIndex(expectedURL.Host, ":"):]
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestConfigurationEndpoint(t *testing.T) {
"bye": "Goodbye pal!",
})

helpers.TestHTTPEndpoints(t, h.Address, helpers.HTTPEndpointCases{
helpers.TestHTTPEndpoints(t, h.LocalAddr(), helpers.HTTPEndpointCases{
{
URL: "/api/v0/orchestrator/configuration/inlet",
ContentType: "application/x-yaml; charset=utf-8",
Expand Down

0 comments on commit 3e3bcbd

Please sign in to comment.