-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathservice_listener_test.go
108 lines (93 loc) · 2.63 KB
/
service_listener_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
package main
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
)
func TestLoadBalancerNameFromHostname(t *testing.T) {
scenarios := map[string]string{
"testpublic-1111111111.us-east-1.elb.amazonaws.com": "testpublic",
"internal-testinternal-2222222222.us-east-1.elb.amazonaws.com": "testinternal",
}
for hostname, elbName := range scenarios {
extractedName, err := loadBalancerNameFromHostname(hostname)
if err != nil {
t.Errorf("Expected %s to parse to %s, but got %v", hostname, elbName, err)
}
if extractedName != elbName {
t.Errorf("Expected %s but got %s for hostname %s", elbName, extractedName, hostname)
}
}
invalid := []string{
"nodashes",
"internal",
}
for _, bad := range invalid {
extractedName, err := loadBalancerNameFromHostname(bad)
if err == nil {
t.Errorf("Expected %s to parse to fail, but got %v", bad, extractedName)
}
}
}
func TestFindMostSpecificZoneForDomain(t *testing.T) {
demo := route53.HostedZone{
Name: aws.String("demo.com."),
}
demoSub := route53.HostedZone{
Name: aws.String("sub.demo.com."),
}
zones := []*route53.HostedZone{
&demo,
&demoSub,
}
scenarios := map[string]*route53.HostedZone{
".demo.com": &demo,
"test.demo.com": &demo,
"test.again.demo.com": &demo,
"sub.demo.com": &demoSub,
"test.sub.demo.com": &demoSub,
}
for domain, expectedZone := range scenarios {
actualZone, err := findMostSpecificZoneForDomain(domain, zones)
if err != nil {
t.Error("Expected no error to be raised", err)
return
}
if actualZone != expectedZone {
t.Errorf("Expected %s to eq %s for domain %s", *actualZone, *expectedZone, domain)
}
}
}
func TestDomainWithTrailingDot(t *testing.T) {
scenarios := map[string]string{
".test.com": ".test.com.",
"hello.goodbye.io.": "hello.goodbye.io.",
}
for withoutDot, withDot := range scenarios {
result := domainWithTrailingDot(withoutDot)
if result != withDot {
t.Errorf("Expected %s but got %s for hostname %s", withDot, result, withoutDot)
}
}
}
func TestGetTLD(t *testing.T) {
scenarios := map[string]string{
".test.com": "test.com",
"hello.goodbye.io": "goodbye.io",
"this.is.really.long.hello.goodbye.io": "goodbye.io",
}
for domain, tld := range scenarios {
result, err := getTLD(domain)
if err != nil {
t.Error("Unexpected error: ", err)
}
if result != tld {
t.Errorf("Expected %s but got %s for tld %s", tld, result, domain)
}
}
bad := "bad.domain"
_, err := getTLD(bad)
if err == nil {
t.Errorf("%s should cause error", bad)
}
}