forked from cbednarski/hostess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostname_test.go
115 lines (100 loc) · 2.84 KB
/
hostname_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package hostess_test
import (
"net"
"testing"
"github.com/cbednarski/hostess"
)
func TestHostname(t *testing.T) {
h := hostess.MustHostname(domain, ip, enabled)
if h.Domain != domain {
t.Errorf("Domain should be %s", domain)
}
if !h.IP.Equal(net.ParseIP(ip)) {
t.Errorf("IP should be %s", ip)
}
if h.Enabled != enabled {
t.Errorf("Enabled should be %t", enabled)
}
}
func TestEqual(t *testing.T) {
a := hostess.MustHostname("localhost", "127.0.0.1", true)
b := hostess.MustHostname("localhost", "127.0.0.1", false)
c := hostess.MustHostname("localhost", "127.0.1.1", false)
if !a.Equal(b) {
t.Errorf("%+v and %+v should be equal", a, b)
}
if a.Equal(c) {
t.Errorf("%+v and %+v should not be equal", a, c)
}
}
func TestEqualIP(t *testing.T) {
a := hostess.MustHostname("localhost", "127.0.0.1", true)
c := hostess.MustHostname("localhost", "127.0.1.1", false)
ip := net.ParseIP("127.0.0.1")
if !a.EqualIP(ip) {
t.Errorf("%s and %s should be equal", a.IP, ip)
}
if a.EqualIP(c.IP) {
t.Errorf("%s and %s should not be equal", a.IP, c.IP)
}
}
func TestIsValid(t *testing.T) {
hostname := &hostess.Hostname{
Domain: "localhost",
IP: net.ParseIP("127.0.0.1"),
Enabled: true,
IPv6: true,
}
if !hostname.IsValid() {
t.Fatalf("%+v should be a valid hostname", hostname)
}
}
func TestIsValidBlank(t *testing.T) {
hostname := &hostess.Hostname{
Domain: "",
IP: net.ParseIP("127.0.0.1"),
Enabled: true,
IPv6: true,
}
if hostname.IsValid() {
t.Errorf("%+v should be invalid because the name is blank", hostname)
}
}
func TestIsValidBadIP(t *testing.T) {
hostname := &hostess.Hostname{
Domain: "localhost",
IP: net.ParseIP("localhost"),
Enabled: true,
IPv6: true,
}
if hostname.IsValid() {
t.Errorf("%+v should be invalid because the ip is malformed", hostname)
}
}
func TestFormatHostname(t *testing.T) {
hostname := hostess.MustHostname(domain, ip, enabled)
const exp_enabled = "127.0.0.1 localhost"
if hostname.Format() != exp_enabled {
t.Errorf("Hostname format doesn't match desired output: %s", Diff(hostname.Format(), exp_enabled))
}
hostname.Enabled = false
const exp_disabled = "# 127.0.0.1 localhost"
if hostname.Format() != exp_disabled {
t.Errorf("Hostname format doesn't match desired output: %s", Diff(hostname.Format(), exp_disabled))
}
}
func TestFormatEnabled(t *testing.T) {
hostname := hostess.MustHostname(domain, ip, enabled)
const expectedOn = "(On)"
if hostname.FormatEnabled() != expectedOn {
t.Errorf("Expected hostname to be turned %s", expectedOn)
}
const expectedHumanOn = "localhost -> 127.0.0.1 (On)"
if hostname.FormatHuman() != expectedHumanOn {
t.Errorf("Unexpected output%s", Diff(expectedHumanOn, hostname.FormatHuman()))
}
hostname.Enabled = false
if hostname.FormatEnabled() != "(Off)" {
t.Error("Expected hostname to be turned (Off)")
}
}