From 74071a4fba0eeafab6202bbab2555df811265190 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) --- networkfs/resolvconf/resolvconf.go | 6 ++++-- networkfs/resolvconf/resolvconf_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/networkfs/resolvconf/resolvconf.go b/networkfs/resolvconf/resolvconf.go index d6854fb..38ae564 100644 --- a/networkfs/resolvconf/resolvconf.go +++ b/networkfs/resolvconf/resolvconf.go @@ -78,8 +78,10 @@ func Build(path string, dns, dnsSearch []string) error { } } if len(dnsSearch) > 0 { - if _, err := content.WriteString("search " + strings.Join(dnsSearch, " ") + "\n"); err != nil { - return err + if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." { + if _, err := content.WriteString("search " + searchString + "\n"); err != nil { + return err + } } } diff --git a/networkfs/resolvconf/resolvconf_test.go b/networkfs/resolvconf/resolvconf_test.go index fd20712..6187acb 100644 --- a/networkfs/resolvconf/resolvconf_test.go +++ b/networkfs/resolvconf/resolvconf_test.go @@ -131,3 +131,28 @@ func TestBuild(t *testing.T) { t.Fatalf("Expected to find '%s' got '%s'", expected, content) } } + +func TestBuildWithZeroLengthDomainSearch(t *testing.T) { + file, err := ioutil.TempFile("", "") + if err != nil { + t.Fatal(err) + } + defer os.Remove(file.Name()) + + err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"."}) + if err != nil { + t.Fatal(err) + } + + content, err := ioutil.ReadFile(file.Name()) + if err != nil { + t.Fatal(err) + } + + if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\n"; !bytes.Contains(content, []byte(expected)) { + t.Fatalf("Expected to find '%s' got '%s'", expected, content) + } + if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) { + t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content) + } +}