diff --git a/pkg/promunifi/collector.go b/pkg/promunifi/collector.go index 1c7613f6..a663e48e 100644 --- a/pkg/promunifi/collector.go +++ b/pkg/promunifi/collector.go @@ -240,9 +240,8 @@ func (u *promUnifi) DebugOutput() (bool, error) { } // check the port - parts := strings.Split(u.HTTPListen, ":") - if len(parts) != 2 { - return false, fmt.Errorf("invalid listen address: %s (must be of the form \"IP:Port\"", u.HTTPListen) + if _, _, err := net.SplitHostPort(u.HTTPListen); err != nil { + return false, fmt.Errorf("invalid listen address: %s (must be of the form \"IP:Port\")", u.HTTPListen) } // Skip network binding check during health checks to avoid "address already in use" diff --git a/pkg/promunifi/collector_test.go b/pkg/promunifi/collector_test.go index 113ac66e..c26e0f63 100644 --- a/pkg/promunifi/collector_test.go +++ b/pkg/promunifi/collector_test.go @@ -20,3 +20,32 @@ func TestDebugOutputAcceptsUnsetHTTPListen(t *testing.T) { assert.True(t, ok) assert.Equal(t, defaultHTTPListen, u.HTTPListen) } + +func TestDebugOutputAcceptsAnIPv6HTTPListen(t *testing.T) { + poller.SetHealthCheckMode(true) + t.Cleanup(func() { poller.SetHealthCheckMode(false) }) + + tests := []struct { + listen string + valid bool + }{ + {"0.0.0.0:9130", true}, + {"[::]:9130", true}, + {":9130", true}, + {"0.0.0.0", false}, + {"0.0.0.0:9130:9131", false}, + } + + for _, test := range tests { + u := &promUnifi{Config: &Config{HTTPListen: test.listen}} + + ok, err := u.DebugOutput() + assert.Equal(t, test.valid, ok, test.listen) + + if test.valid { + require.NoError(t, err, test.listen) + } else { + require.Error(t, err, test.listen) + } + } +}