add etchosts
Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)
This commit is contained in:
parent
ecd6b47eaf
commit
05227e1d6f
3 changed files with 109 additions and 0 deletions
1
networkfs/MAINTAINERS
Normal file
1
networkfs/MAINTAINERS
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Victor Vieux <victor.vieux@docker.com> (@vieux)
|
34
networkfs/etchosts/etchosts.go
Normal file
34
networkfs/etchosts/etchosts.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package etchosts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultContent = map[string]string{
|
||||||
|
"localhost": "127.0.0.1",
|
||||||
|
"localhost ip6-localhost ip6-loopback": "::1",
|
||||||
|
"ip6-localnet": "fe00::0",
|
||||||
|
"ip6-mcastprefix": "ff00::0",
|
||||||
|
"ip6-allnodes": "ff02::1",
|
||||||
|
"ip6-allrouters": "ff02::2",
|
||||||
|
}
|
||||||
|
|
||||||
|
func Build(path, IP, hostname, domainname string) error {
|
||||||
|
content := bytes.NewBuffer(nil)
|
||||||
|
if IP != "" {
|
||||||
|
if domainname != "" {
|
||||||
|
content.WriteString(fmt.Sprintf("%s\t%s.%s %s\n", IP, hostname, domainname, hostname))
|
||||||
|
} else {
|
||||||
|
content.WriteString(fmt.Sprintf("%s\t%s\n", IP, hostname))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for hosts, ip := range defaultContent {
|
||||||
|
if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
||||||
|
}
|
74
networkfs/etchosts/etchosts_test.go
Normal file
74
networkfs/etchosts/etchosts_test.go
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
package etchosts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildHostnameDomainname(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "10.11.12.13\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildHostname(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), "10.11.12.13", "testhostname", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := "10.11.12.13\ttesthostname\n"; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildNoIP(t *testing.T) {
|
||||||
|
file, err := ioutil.TempFile("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
|
||||||
|
err = Build(file.Name(), "", "testhostname", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if expected := ""; !bytes.Contains(content, []byte(expected)) {
|
||||||
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue