From 89b67db4472401f309ed2989c934bfaf64d139ad Mon Sep 17 00:00:00 2001 From: Fabio Falci Date: Thu, 26 Jun 2014 12:03:23 +0100 Subject: [PATCH] Relax dns search to accept empty domain In that case /etc/resolv.conf will be generated with no search option. Usage: --dns-search=. Docker-DCO-1.1-Signed-off-by: Fabio Falci (github: fabiofalci) Upstream-commit: 9fdc86ac55646015a8421645e1cab7b51bc58380 Component: cli --- components/cli/opts/opts.go | 11 ++++++++++- components/cli/opts/opts_test.go | 14 ++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/components/cli/opts/opts.go b/components/cli/opts/opts.go index 67f1c8fd48..d17b57e07c 100644 --- a/components/cli/opts/opts.go +++ b/components/cli/opts/opts.go @@ -137,7 +137,16 @@ func ValidateIp4Address(val string) (string, error) { return "", fmt.Errorf("%s is not an ip4 address", val) } -func ValidateDomain(val string) (string, error) { +// Validates domain for resolvconf search configuration. +// A zero length domain is represented by . +func ValidateDnsSearch(val string) (string, error) { + if val = strings.Trim(val, " "); val == "." { + return val, nil + } + return validateDomain(val) +} + +func validateDomain(val string) (string, error) { alpha := regexp.MustCompile(`[a-zA-Z]`) if alpha.FindString(val) == "" { return "", fmt.Errorf("%s is not a valid domain", val) diff --git a/components/cli/opts/opts_test.go b/components/cli/opts/opts_test.go index 299cbfe503..b18088b934 100644 --- a/components/cli/opts/opts_test.go +++ b/components/cli/opts/opts_test.go @@ -23,8 +23,9 @@ func TestValidateIP4(t *testing.T) { } -func TestValidateDomain(t *testing.T) { +func TestValidateDnsSearch(t *testing.T) { valid := []string{ + `.`, `a`, `a.`, `1.foo`, @@ -49,7 +50,8 @@ func TestValidateDomain(t *testing.T) { invalid := []string{ ``, - `.`, + ` `, + ` `, `17`, `17.`, `.17`, @@ -65,14 +67,14 @@ func TestValidateDomain(t *testing.T) { } for _, domain := range valid { - if ret, err := ValidateDomain(domain); err != nil || ret == "" { - t.Fatalf("ValidateDomain(`"+domain+"`) got %s %s", ret, err) + if ret, err := ValidateDnsSearch(domain); err != nil || ret == "" { + t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err) } } for _, domain := range invalid { - if ret, err := ValidateDomain(domain); err == nil || ret != "" { - t.Fatalf("ValidateDomain(`"+domain+"`) got %s %s", ret, err) + if ret, err := ValidateDnsSearch(domain); err == nil || ret != "" { + t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err) } } }