2014-05-05 18:05:14 -04:00
|
|
|
package etchosts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-11-06 14:36:09 -05:00
|
|
|
func TestBuildDefault(t *testing.T) {
|
|
|
|
file, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
// check that /etc/hosts has consistent ordering
|
|
|
|
for i := 0; i <= 5; i++ {
|
|
|
|
err = Build(file.Name(), "", "", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(file.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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"
|
|
|
|
|
|
|
|
if expected != string(content) {
|
|
|
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 18:05:14 -04:00
|
|
|
func TestBuildHostnameDomainname(t *testing.T) {
|
|
|
|
file, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2014-05-06 10:42:22 -04:00
|
|
|
err = Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", nil)
|
2014-05-05 18:05:14 -04:00
|
|
|
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())
|
|
|
|
|
2014-05-06 10:42:22 -04:00
|
|
|
err = Build(file.Name(), "10.11.12.13", "testhostname", "", nil)
|
2014-05-05 18:05:14 -04:00
|
|
|
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())
|
|
|
|
|
2014-05-06 10:42:22 -04:00
|
|
|
err = Build(file.Name(), "", "testhostname", "", nil)
|
2014-05-05 18:05:14 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 01:23:56 -04:00
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
file, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
if err := Build(file.Name(), "10.11.12.13", "testhostname", "testdomainname", nil); 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Update(file.Name(), "1.1.1.1", "testhostname"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(file.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := "1.1.1.1\ttesthostname.testdomainname testhostname\n"; !bytes.Contains(content, []byte(expected)) {
|
|
|
|
t.Fatalf("Expected to find '%s' got '%s'", expected, content)
|
|
|
|
}
|
|
|
|
}
|