Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Jul 14, 2015
1 parent eef3104 commit 1c543a2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 35 deletions.
6 changes: 3 additions & 3 deletions proxy/common_test.go
Expand Up @@ -4,6 +4,8 @@ import (
"reflect"
"regexp"
"testing"

"github.com/stretchr/testify/require"
)

func TestFindStringNamedSubmatch(t *testing.T) {
Expand All @@ -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)
}
9 changes: 4 additions & 5 deletions proxy/proxy.go
Expand Up @@ -2,7 +2,6 @@ package proxy

import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
31 changes: 9 additions & 22 deletions proxy/proxy_test.go
Expand Up @@ -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) {
Expand All @@ -34,26 +30,17 @@ 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)
}
}

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)
}
7 changes: 3 additions & 4 deletions site/proxy.md
Expand Up @@ -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/'`

Expand Down
3 changes: 2 additions & 1 deletion weave
Expand Up @@ -36,7 +36,8 @@ weave launch-router [--password <password>] [--nickname <nickname>]
[--no-discovery] [--init-peer-count <count>] <peer> ...
weave launch-dns [<addr>]
weave launch-proxy [-H <endpoint>] [--with-dns | --without-dns]
[--no-default-ipalloc] [--no-rewrite-hosts] [--hostname]
[--no-default-ipalloc] [--no-rewrite-hosts]
[--hostname </regexp/replacement/>]
weave connect [--replace] [<peer> ...]
weave forget <peer> ...
weave run [--with-dns | --without-dns] [<addr> ...]
Expand Down

0 comments on commit 1c543a2

Please sign in to comment.