test: ensure ipv6 compatability (#5110)
This commit is contained in:
1 parent
eb33649ded
commit
29efd5e6c6
5 files changed
+314
-10
No files matched your search
@@ -496,7 +496,7 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
expPublicAddress: ":9202",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ip",
|
||||
name: "setting public address directly with ipv4",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
@@ -511,7 +511,7 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
expPublicAddress: "127.0.0.1:9202",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ip:port",
|
||||
name: "setting public address directly with ipv4:port",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
@@ -525,6 +525,66 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
expErrStr: "",
|
||||
expPublicAddress: "127.0.0.1:8080",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ipv6",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860:0:0:0:0:8888]",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860:0:0:0:0:8888]:9202",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ipv6:port",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860:0:0:0:0:8888]:8080",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860:0:0:0:0:8888]:8080",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with abbreviated ipv6",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860::8888]",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860::8888]:9202",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with abbreviated ipv6:port",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860::8888]:8080",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860::8888]:8080",
|
||||
},
|
||||
{
|
||||
name: "setting public address to env var",
|
||||
inputConfig: &config.Config{
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -57,6 +58,12 @@ const (
|
||||
WorkerAuthReqFile = "auth_request_token"
|
||||
)
|
||||
|
||||
var (
|
||||
// This regular expression is used to find all instances of square brackets within a string.
|
||||
// This regular expression is used to remove the square brackets from an IPv6 address.
|
||||
squareBrackets = regexp.MustCompile("\\[|\\]")
|
||||
)
|
||||
|
||||
func init() {
|
||||
metric.InitializeBuildInfo(prometheus.DefaultRegisterer)
|
||||
}
|
||||
@@ -845,7 +852,12 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
|
||||
return fmt.Errorf("Error splitting public adddress host/port: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// remove the square brackets from the ipv6 address because the method
|
||||
// net.JoinHostPort() will add a second pair of square brackets.
|
||||
host = squareBrackets.ReplaceAllString(host, "")
|
||||
conf.Worker.PublicAddr = net.JoinHostPort(host, port)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user