diff --git a/proxy/common_test.go b/proxy/common_test.go index 6fd45d1607..56d575f3db 100644 --- a/proxy/common_test.go +++ b/proxy/common_test.go @@ -4,6 +4,8 @@ import ( "reflect" "regexp" "testing" + + "github.com/stretchr/testify/require" ) func TestFindStringNamedSubmatch(t *testing.T) { @@ -13,7 +15,5 @@ func TestFindStringNamedSubmatch(t *testing.T) { "barGroup": "bar", } namedSubmatches := findStringNamedSubmatch(re, "prefix foobar suffix") - if !reflect.DeepEqual(namedSubmatches, expectedResult) { - t.Error("Unexpected result", namedSubmatches) - } + require.True(t, reflect.DeepEqual(namedSubmatches, expectedResult), "Unexpected namedSubmatches: %s", namedSubmatches) } diff --git a/proxy/proxy.go b/proxy/proxy.go index b6b2cd1e46..e6371cedf3 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -2,7 +2,6 @@ package proxy import ( "crypto/tls" - "errors" "fmt" "net" "net/http" @@ -139,8 +138,8 @@ func unescapeBackSlashes(src string) string { func parseHostname(hostname string) (*regexp.Regexp, string, error) { namedSubmatches := findStringNamedSubmatch(substitutionRegexp, hostname) if len(namedSubmatches) < 1 { - errStr := fmt.Sprintf("Incorrect hostname expression: %s", hostname) - return nil, "", errors.New(errStr) + err := fmt.Errorf("Incorrect hostname expression: %s", hostname) + return nil, "", err } escapedPattern := namedSubmatches[substitutionPatternName] @@ -150,8 +149,8 @@ func parseHostname(hostname string) (*regexp.Regexp, string, error) { patternRegexp, err := regexp.Compile(unescapedPattern) if err != nil { - errStr := fmt.Sprintf("Incorrect hostname pattern '%s': %s", escapedPattern, err.Error()) - return nil, "", errors.New(errStr) + err := fmt.Errorf("Incorrect hostname pattern '%s': %s", escapedPattern, err.Error()) + return nil, "", err } return patternRegexp, unescapedReplacement, nil diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index e721f5046e..7721c27d46 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -2,23 +2,19 @@ package proxy import ( "testing" + + "github.com/stretchr/testify/require" ) func TestParseHostname(t *testing.T) { hostnameExpr := `/aws-[0-9]+-(.*)/my-app-$1/` patternRegexp, replacementStr, err := parseHostname(hostnameExpr) - if err != nil { - t.Error("Unexpected error", err) - } - if replacementStr != "my-app-$1" { - t.Error("Unexpected replacement string", replacementStr) - } + require.Nil(t, err, "Unexpected error") + require.Equal(t, "my-app-$1", replacementStr, "Unexpected replacement string") hostname := patternRegexp.ReplaceAllString("aws-17651276812-name", replacementStr) - if hostname != "my-app-name" { - t.Error("Unexpected replacement hostname", hostname) - } + require.Equal(t, "my-app-name", hostname, "Unexpected replacement hostname") } func TestParseWrongHostname(t *testing.T) { @@ -34,9 +30,7 @@ func TestParseWrongHostname(t *testing.T) { for _, hostnameExpr := range wrongHostnameExprs { _, _, err := parseHostname(hostnameExpr) - if err == nil { - t.Error("Should not succesully parse wrong hostname expression", hostnameExpr) - } + require.Error(t, err, "Should not parse wrong hostname expression %s", hostnameExpr) } } @@ -44,16 +38,9 @@ func TestEscapedHostname(t *testing.T) { hostnameExpr := `/aws\/[0-9]+\/(.*)/my\/app\/$1/` patternRegexp, replacementStr, err := parseHostname(hostnameExpr) - if err != nil { - t.Error("Unexpected error", err) - } - if replacementStr != "my/app/$1" { - t.Error("Unexpected replacement string", replacementStr) - } + require.Nil(t, err, "Unexpected error", err) + require.Equal(t, "my/app/$1", replacementStr, "Unexpected replacement string") hostname := patternRegexp.ReplaceAllString("aws/17651276812/name", replacementStr) - if hostname != "my/app/name" { - t.Error("Unexpected replacement hostname", hostname) - } - + require.Equal(t, "my/app/name", hostname, "Unexpected replacement hostname", hostname) } diff --git a/site/proxy.md b/site/proxy.md index 176ddf2690..42030929a4 100644 --- a/site/proxy.md +++ b/site/proxy.md @@ -135,10 +135,9 @@ for registration. However, there are situations in which the final container name is out of the user's control (e.g. when using Docker orchestrators which append control/namespacing identifiers to the original container names). -For those situations, the proxy provides a -`--hostname=/containerNamePattern/hostnameReplacement/` flag, where -`/containerNamePattern/hostnameReplacement/` is a regular expression -substitution command. +For those situations, the proxy provides a `--hostname=/regexp/replacement/` +flag, where `/regexp/replacement/` is a regular expression substitution command +which will be applied to the container name before passing it over to weaveDNS. For instance, if we launch the proxy using `--hostname='/^aws-[0-9]+-(.*)$/my-app-$1/'` diff --git a/weave b/weave index d48f79e410..e96b77657c 100755 --- a/weave +++ b/weave @@ -36,7 +36,8 @@ weave launch-router [--password ] [--nickname ] [--no-discovery] [--init-peer-count ] ... weave launch-dns [] weave launch-proxy [-H ] [--with-dns | --without-dns] - [--no-default-ipalloc] [--no-rewrite-hosts] [--hostname] + [--no-default-ipalloc] [--no-rewrite-hosts] + [--hostname ] weave connect [--replace] [ ...] weave forget ... weave run [--with-dns | --without-dns] [ ...]