func TestLoadHostfile(t *testing.T) { hostfile := hostess.NewHostfile() hostfile.Read() if !strings.Contains(string(hostfile.GetData()), domain) { t.Errorf("Expected to find %s", domain) } hostfile.Parse() on := enabled if runtime.GOOS == "windows" { on = false } hostname := hostess.NewHostname(domain, ip, on) found := hostfile.Hosts.Contains(hostname) if !found { t.Errorf("Expected to find %#v", hostname) } }
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 # 8.8.8.8 google.com 10.37.12.18 devsite.com m.devsite.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)) } }