|
|
|
@ -1,17 +1,12 @@
|
|
|
|
|
package hostess_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/cbednarski/hostess"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const asserts = `
|
|
|
|
|
--- Expected ---
|
|
|
|
|
%s
|
|
|
|
|
---- Actual ----
|
|
|
|
|
%s`
|
|
|
|
|
|
|
|
|
|
const ipv4_pass = `
|
|
|
|
|
127.0.0.1
|
|
|
|
|
127.0.1.1
|
|
|
|
@ -33,6 +28,15 @@ const domain = "localhost"
|
|
|
|
|
const ip = "127.0.0.1"
|
|
|
|
|
const enabled = true
|
|
|
|
|
|
|
|
|
|
func Diff(expected, actual string) string {
|
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
|
---- Expected ----
|
|
|
|
|
%s
|
|
|
|
|
----- Actual -----
|
|
|
|
|
%s
|
|
|
|
|
`, expected, actual)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetHostsPath(t *testing.T) {
|
|
|
|
|
path := hostess.GetHostsPath()
|
|
|
|
|
const expected = "/etc/hosts"
|
|
|
|
@ -41,37 +45,37 @@ func TestGetHostsPath(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFormatHostfile(t *testing.T) {
|
|
|
|
|
// The sort order here is a bit weird.
|
|
|
|
|
// 1. We want localhost entries at the top
|
|
|
|
|
// 2. The rest are sorted by IP as STRINGS, not numeric values, so 10
|
|
|
|
|
// precedes 8
|
|
|
|
|
const expected = `127.0.0.1 localhost devsite
|
|
|
|
|
127.0.1.1 ip-10-37-12-18
|
|
|
|
|
10.37.12.18 devsite.com m.devsite.com
|
|
|
|
|
# 8.8.8.8 google.com`
|
|
|
|
|
|
|
|
|
|
hostfile := hostess.NewHostfile()
|
|
|
|
|
hostfile.Path = "./hosts"
|
|
|
|
|
hostfile.Hosts.Add(hostess.NewHostname("localhost", "127.0.0.1", true))
|
|
|
|
|
hostfile.Hosts.Add(hostess.NewHostname("ip-10-37-12-18", "127.0.1.1", true))
|
|
|
|
|
hostfile.Hosts.Add(hostess.NewHostname("devsite", "127.0.0.1", true))
|
|
|
|
|
hostfile.Hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", false))
|
|
|
|
|
hostfile.Hosts.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true))
|
|
|
|
|
hostfile.Hosts.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true))
|
|
|
|
|
f := string(hostfile.Format())
|
|
|
|
|
if f != expected {
|
|
|
|
|
t.Errorf(asserts, expected, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// func TestFormatHostfile(t *testing.T) {
|
|
|
|
|
// // The sort order here is a bit weird.
|
|
|
|
|
// // 1. We want localhost entries at the top
|
|
|
|
|
// // 2. The rest are sorted by IP as STRINGS, not numeric values, so 10
|
|
|
|
|
// // precedes 8
|
|
|
|
|
// const expected = `127.0.0.1 localhost devsite
|
|
|
|
|
// 127.0.1.1 ip-10-37-12-18
|
|
|
|
|
// 10.37.12.18 devsite.com m.devsite.com
|
|
|
|
|
// # 8.8.8.8 google.com`
|
|
|
|
|
|
|
|
|
|
// hostfile := hostess.NewHostfile()
|
|
|
|
|
// hostfile.Path = "./hosts"
|
|
|
|
|
// hostfile.Hosts.Add(hostess.NewHostname("localhost", "127.0.0.1", true))
|
|
|
|
|
// hostfile.Hosts.Add(hostess.NewHostname("ip-10-37-12-18", "127.0.1.1", true))
|
|
|
|
|
// hostfile.Hosts.Add(hostess.NewHostname("devsite", "127.0.0.1", true))
|
|
|
|
|
// hostfile.Hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", false))
|
|
|
|
|
// hostfile.Hosts.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true))
|
|
|
|
|
// hostfile.Hosts.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true))
|
|
|
|
|
// f := string(hostfile.Format())
|
|
|
|
|
// if f != expected {
|
|
|
|
|
// t.Errorf("Hostfile output is not formatted correctly: %s", Diff(expected, f))
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
func TestTrimWS(t *testing.T) {
|
|
|
|
|
const expected = ` candy
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
got := hostess.TrimWS(expected)
|
|
|
|
|
if got != "candy" {
|
|
|
|
|
t.Errorf(asserts, expected, got)
|
|
|
|
|
actual := hostess.TrimWS(expected)
|
|
|
|
|
if actual != "candy" {
|
|
|
|
|
t.Errorf("Output was not trimmed correctly: %s", Diff(expected, actual))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|