-
Notifications
You must be signed in to change notification settings - Fork 5
/
app_test.go
129 lines (118 loc) · 2.25 KB
/
app_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package candy
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_AppService_FindApps(t *testing.T) {
cases := []struct {
Name string
Hosts map[string]string
TLDs []string
WantApps []App
WantErr error
}{
{
Name: "valid hosts",
Hosts: map[string]string{
"app1": "8080",
"app2": "192.168.0.1:9090",
"app3": "https://192.168.0.2:9091",
"app4": "https://owenou.com",
"app5": "https://owenou.dev/path",
},
TLDs: []string{"test", "dev"},
WantApps: []App{
{
Host: "app1.test",
Addr: "127.0.0.1:8080",
},
{
Host: "app1.dev",
Addr: "127.0.0.1:8080",
},
{
Host: "app2.test",
Addr: "192.168.0.1:9090",
},
{
Host: "app2.dev",
Addr: "192.168.0.1:9090",
},
{
Host: "app3.test",
Addr: "192.168.0.2:9091",
},
{
Host: "app3.dev",
Addr: "192.168.0.2:9091",
},
{
Host: "app4.test",
Addr: "owenou.com",
},
{
Host: "app4.dev",
Addr: "owenou.com",
},
{
Host: "app5.test",
Addr: "owenou.dev",
},
{
Host: "app5.dev",
Addr: "owenou.dev",
},
},
WantErr: nil,
},
{
Name: "invalid hosts",
Hosts: map[string]string{
"app1": "invalid",
},
TLDs: []string{"test"},
WantApps: nil,
WantErr: nil,
},
{
Name: "ignore invalid hosts",
Hosts: map[string]string{
"app1": "invalid",
"app2": "8080",
},
TLDs: []string{"test"},
WantApps: []App{
{
Host: "app2.test",
Addr: "127.0.0.1:8080",
},
},
WantErr: nil,
},
}
for _, c := range cases {
cc := c
t.Run(cc.Name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
for k, v := range cc.Hosts {
if err := os.WriteFile(filepath.Join(dir, k), []byte(v), 0o0644); err != nil {
t.Fatalf("error writing test hosts: %s", err)
}
}
svc := NewAppService(AppServiceConfig{
TLDs: cc.TLDs,
HostRoot: dir,
})
gotApps, gotErr := svc.FindApps()
if !cmp.Equal(cc.WantErr, gotErr) {
t.Fatalf("mismatch error: want=%s got=%s", cc.WantErr, gotErr)
}
if diff := cmp.Diff(cc.WantApps, gotApps); diff != "" {
t.Fatalf("mismatch apps (-want +got): %s", diff)
}
})
}
}