This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfunctions_test.go
112 lines (101 loc) · 3.05 KB
/
functions_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
package main
import (
"regexp"
"testing"
)
func TestServiceURL(t *testing.T) {
var testCases = []struct {
args []string
result string
}{
{[]string{"cloudbreak", "bridge.address", "", "http://", "9091", "8080"}, "http://cloudbreak:8080"},
{[]string{"cloudbreak", "bridge.address", "cloudbreak", "http://", "9091", "8080"}, "http://bridge.address:9091"},
{[]string{"datalake", "bridge.address", "cloudbreak,datalake,environment", "http://", "8086", "8080"}, "http://bridge.address:8086"},
{[]string{"datalake", "bridge.address", "cloudbreak,datalake-api,datalake-dr,environment", "http://", "8086", "8080"}, "http://datalake:8080"},
{[]string{"datalake", "bridge.address", "datalake,environment", "http://", "8086", "8080"}, "http://bridge.address:8086"},
{[]string{"datalake", "bridge.address", "cloudbreak,datalake", "http://", "8086", "8080"}, "http://bridge.address:8086"},
}
for _, c := range testCases {
out := catchStdOut(t, func() {
ServiceURL(c.args)
})
if out != c.result {
t.Errorf("Service URL generated:'%s' should be '%s'.", out, c.result)
}
}
}
func TestBinVersion(t *testing.T) {
Version = "version"
GitRevision = "gitrevision"
expected := "version-gitrevision\n"
out := catchStdOut(t, func() {
BinVersion([]string{})
})
if out != expected {
t.Errorf("Version should be '%s', was: '%s'", expected, out)
}
}
func TestGenerateCaddyFileSingle(t *testing.T) {
should := []string{`(?m)^\s*localhost:`}
out := catchStdOut(t, func() {
GenerateCaddyFile([]string{"localhost"})
})
t.Log(out)
for _, s := range should {
re := regexp.MustCompile(s)
if res := re.FindString(out); len(res) == 0 {
t.Errorf("Can't find service '%s' in output.", s)
}
}
}
func TestGenerateCaddyFileMultiple(t *testing.T) {
should := []string{`(?m)^\s*localhost:`, `(?m)^\s*localhost2:`}
out := catchStdOut(t, func() {
GenerateCaddyFile([]string{"localhost,localhost2"})
})
t.Log(out)
for _, s := range should {
re := regexp.MustCompile(s)
if res := re.FindString(out); len(res) == 0 {
t.Errorf("Can't find service '%s' in output.", s)
}
}
}
func TestHostFromURL(t *testing.T) {
var testCases = []struct {
args []string
result string
}{
{[]string{"foo.com:1234"}, "foo.com"},
{[]string{"foo.com:1234/bar/blah/"}, "foo.com"},
{[]string{"http://foo.com:1234"}, "foo.com"},
{[]string{"https://foo.com:1234/bar/blah/"}, "foo.com"},
}
for _, c := range testCases {
out := catchStdOut(t, func() {
HostFromURL(c.args)
})
if out != c.result {
t.Errorf("Host for URL '%s': got '%s', expected '%s'.", c.args, out, c.result)
}
}
}
func TestPortFromURL(t *testing.T) {
var testCases = []struct {
args []string
result string
}{
{[]string{"foo.com:1234"}, "1234"},
{[]string{"foo.com:1234/bar/blah/"}, "1234"},
{[]string{"http://foo.com:1234"}, "1234"},
{[]string{"https://foo.com:1234/bar/blah/"}, "1234"},
}
for _, c := range testCases {
out := catchStdOut(t, func() {
PortFromURL(c.args)
})
if out != c.result {
t.Errorf("Host for URL '%s': got '%s', expected '%s'.", c.args, out, c.result)
}
}
}