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 <fabiofalci@gmail.com> (github: fabiofalci)
This commit is contained in:
Fabio Falci 2014-06-26 12:03:23 +01:00 committed by Vincent Demeester
parent 7297bfab30
commit 9fdc86ac55
2 changed files with 18 additions and 7 deletions

View File

@ -137,7 +137,16 @@ func ValidateIp4Address(val string) (string, error) {
return "", fmt.Errorf("%s is not an ip4 address", val) 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]`) alpha := regexp.MustCompile(`[a-zA-Z]`)
if alpha.FindString(val) == "" { if alpha.FindString(val) == "" {
return "", fmt.Errorf("%s is not a valid domain", val) return "", fmt.Errorf("%s is not a valid domain", val)

View File

@ -23,8 +23,9 @@ func TestValidateIP4(t *testing.T) {
} }
func TestValidateDomain(t *testing.T) { func TestValidateDnsSearch(t *testing.T) {
valid := []string{ valid := []string{
`.`,
`a`, `a`,
`a.`, `a.`,
`1.foo`, `1.foo`,
@ -49,7 +50,8 @@ func TestValidateDomain(t *testing.T) {
invalid := []string{ invalid := []string{
``, ``,
`.`, ` `,
` `,
`17`, `17`,
`17.`, `17.`,
`.17`, `.17`,
@ -65,14 +67,14 @@ func TestValidateDomain(t *testing.T) {
} }
for _, domain := range valid { for _, domain := range valid {
if ret, err := ValidateDomain(domain); err != nil || ret == "" { if ret, err := ValidateDnsSearch(domain); err != nil || ret == "" {
t.Fatalf("ValidateDomain(`"+domain+"`) got %s %s", ret, err) t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
} }
} }
for _, domain := range invalid { for _, domain := range invalid {
if ret, err := ValidateDomain(domain); err == nil || ret != "" { if ret, err := ValidateDnsSearch(domain); err == nil || ret != "" {
t.Fatalf("ValidateDomain(`"+domain+"`) got %s %s", ret, err) t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
} }
} }
} }