-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdomains_test.go
More file actions
196 lines (175 loc) · 5.74 KB
/
domains_test.go
File metadata and controls
196 lines (175 loc) · 5.74 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package managerdriver
import (
"testing"
"github.com/stretchr/testify/assert"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
ingressv1alpha1 "github.com/ngrok/ngrok-operator/api/ingress/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/testutils"
)
func TestIngressToDomains_SkipsInternalDomains(t *testing.T) {
tests := []struct {
name string
hosts []string
expectedDomains []string
}{
{
name: "regular domain is included",
hosts: []string{"example.com"},
expectedDomains: []string{"example.com"},
},
{
name: "internal domain is skipped",
hosts: []string{"foo.internal"},
expectedDomains: []string{},
},
{
name: "mixed domains - only non-internal included",
hosts: []string{"example.com", "foo.internal", "bar.ngrok.io"},
expectedDomains: []string{"example.com", "bar.ngrok.io"},
},
{
name: "subdomain internal is skipped",
hosts: []string{"service.namespace.internal"},
expectedDomains: []string{},
},
{
name: "internal as subdomain is NOT skipped",
hosts: []string{"internal.example.com"},
expectedDomains: []string{"internal.example.com"},
},
{
name: "uppercase internal is skipped",
hosts: []string{"FOO.INTERNAL"},
expectedDomains: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ingress := testutils.NewTestIngressV1WithHosts("test-ingress", "test-namespace", tt.hosts...)
result := ingressToDomains(ingress, "", nil)
assert.Len(t, result, len(tt.expectedDomains))
for _, expectedDomain := range tt.expectedDomains {
_, found := result[expectedDomain]
assert.True(t, found, "expected domain %s to be in result", expectedDomain)
}
})
}
}
func TestGatewayToDomains_SkipsInternalDomains(t *testing.T) {
tests := []struct {
name string
hostnames []string
expectedDomains []string
}{
{
name: "regular domain is included",
hostnames: []string{"example.com"},
expectedDomains: []string{"example.com"},
},
{
name: "internal domain is skipped",
hostnames: []string{"foo.internal"},
expectedDomains: []string{},
},
{
name: "mixed domains - only non-internal included",
hostnames: []string{"example.com", "foo.internal", "bar.ngrok.io"},
expectedDomains: []string{"example.com", "bar.ngrok.io"},
},
{
name: "subdomain internal is skipped",
hostnames: []string{"service.namespace.internal"},
expectedDomains: []string{},
},
{
name: "internal as subdomain is NOT skipped",
hostnames: []string{"internal.example.com"},
expectedDomains: []string{"internal.example.com"},
},
{
name: "uppercase internal is skipped",
hostnames: []string{"FOO.INTERNAL"},
expectedDomains: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gateway := testutils.NewGatewayWithHostnames("test-gateway", "test-namespace", tt.hostnames...)
result := gatewayToDomains(gateway, "", nil)
assert.Len(t, result, len(tt.expectedDomains))
for _, expectedDomain := range tt.expectedDomains {
_, found := result[expectedDomain]
assert.True(t, found, "expected domain %s to be in result", expectedDomain)
}
})
}
}
func TestIngressToDomains_SkipsExistingDomains(t *testing.T) {
ingress := testutils.NewTestIngressV1WithHosts("test-ingress", "test-namespace", "example.com", "new.example.com")
existingDomains := map[string]ingressv1alpha1.Domain{
"example.com": {
ObjectMeta: metav1.ObjectMeta{Name: "example-com"},
Spec: ingressv1alpha1.DomainSpec{Domain: "example.com"},
},
}
result := ingressToDomains(ingress, "", existingDomains)
assert.Len(t, result, 1)
_, found := result["new.example.com"]
assert.True(t, found, "expected new.example.com to be in result")
_, found = result["example.com"]
assert.False(t, found, "expected example.com to be skipped as it exists")
}
func TestGatewayToDomains_SkipsExistingDomains(t *testing.T) {
gateway := testutils.NewGatewayWithHostnames("test-gateway", "test-namespace", "example.com", "new.example.com")
existingDomains := map[string]ingressv1alpha1.Domain{
"example.com": {
ObjectMeta: metav1.ObjectMeta{Name: "example-com"},
Spec: ingressv1alpha1.DomainSpec{Domain: "example.com"},
},
}
result := gatewayToDomains(gateway, "", existingDomains)
assert.Len(t, result, 1)
_, found := result["new.example.com"]
assert.True(t, found, "expected new.example.com to be in result")
_, found = result["example.com"]
assert.False(t, found, "expected example.com to be skipped as it exists")
}
func TestIngressToDomains_SkipsEmptyHost(t *testing.T) {
ingress := &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: "test-namespace",
},
Spec: netv1.IngressSpec{
Rules: []netv1.IngressRule{
{Host: ""},
{Host: "example.com"},
},
},
}
result := ingressToDomains(ingress, "", nil)
assert.Len(t, result, 1)
_, found := result["example.com"]
assert.True(t, found)
}
func TestGatewayToDomains_SkipsNilHostname(t *testing.T) {
gateway := &gatewayv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway",
Namespace: "test-namespace",
},
Spec: gatewayv1.GatewaySpec{
Listeners: []gatewayv1.Listener{
{Name: "no-hostname", Hostname: nil},
{Name: "with-hostname", Hostname: ptr.To(gatewayv1.Hostname("example.com"))},
},
},
}
result := gatewayToDomains(gateway, "", nil)
assert.Len(t, result, 1)
_, found := result["example.com"]
assert.True(t, found)
}