github.com/dlintw/docker@v1.5.0-rc4/pkg/networkfs/etchosts/etchosts_test.go (about) 1 package etchosts 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func TestBuildDefault(t *testing.T) { 11 file, err := ioutil.TempFile("", "") 12 if err != nil { 13 t.Fatal(err) 14 } 15 defer os.Remove(file.Name()) 16 17 // check that /etc/hosts has consistent ordering 18 for i := 0; i <= 5; i++ { 19 err = Build(file.Name(), "", "", "", nil) 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 content, err := ioutil.ReadFile(file.Name()) 25 if err != nil { 26 t.Fatal(err) 27 } 28 expected := "127.0.0.1\tlocalhost\n::1\tlocalhost ip6-localhost ip6-loopback\nfe00::0\tip6-localnet\nff00::0\tip6-mcastprefix\nff02::1\tip6-allnodes\nff02::2\tip6-allrouters\n" 29 30 if expected != string(content) { 31 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 32 } 33 } 34 } 35 36 func TestBuildHostnameDomainname(t *testing.T) { 37 file, err := ioutil.TempFile("", "") 38 if err != nil { 39 t.Fatal(err) 40 } 41 defer os.Remove(file.Name()) 42 43 err = Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", nil) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 content, err := ioutil.ReadFile(file.Name()) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 if expected := "10.11.12.13\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) { 54 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 55 } 56 } 57 58 func TestBuildHostname(t *testing.T) { 59 file, err := ioutil.TempFile("", "") 60 if err != nil { 61 t.Fatal(err) 62 } 63 defer os.Remove(file.Name()) 64 65 err = Build(file.Name(), "10.11.12.13", "testhostname", "", nil) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 content, err := ioutil.ReadFile(file.Name()) 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 if expected := "10.11.12.13\ttesthostname\n"; !bytes.Contains(content, []byte(expected)) { 76 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 77 } 78 } 79 80 func TestBuildNoIP(t *testing.T) { 81 file, err := ioutil.TempFile("", "") 82 if err != nil { 83 t.Fatal(err) 84 } 85 defer os.Remove(file.Name()) 86 87 err = Build(file.Name(), "", "testhostname", "", nil) 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 content, err := ioutil.ReadFile(file.Name()) 93 if err != nil { 94 t.Fatal(err) 95 } 96 97 if expected := ""; !bytes.Contains(content, []byte(expected)) { 98 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 99 } 100 } 101 102 func TestUpdate(t *testing.T) { 103 file, err := ioutil.TempFile("", "") 104 if err != nil { 105 t.Fatal(err) 106 } 107 defer os.Remove(file.Name()) 108 109 if err := Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", nil); err != nil { 110 t.Fatal(err) 111 } 112 113 content, err := ioutil.ReadFile(file.Name()) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 if expected := "10.11.12.13\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) { 119 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 120 } 121 122 if err := Update(file.Name(), "1.1.1.1", "testhostname"); err != nil { 123 t.Fatal(err) 124 } 125 126 content, err = ioutil.ReadFile(file.Name()) 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 if expected := "1.1.1.1\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) { 132 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 133 } 134 }