Skip to content

Commit 4661c1f

Browse files
committed
update unit-test
1 parent 75006a8 commit 4661c1f

File tree

1 file changed

+117
-48
lines changed

1 file changed

+117
-48
lines changed

pkg/router/router_test.go

Lines changed: 117 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package router
22

33
import (
4-
"os"
5-
"path/filepath"
4+
"reflect"
65
"testing"
76

87
"github.com/google/go-cmp/cmp"
@@ -12,14 +11,54 @@ import (
1211
)
1312

1413
func TestPorts(t *testing.T) {
14+
defaultPorts := func() []corev1.ServicePort {
15+
return []corev1.ServicePort{
16+
{
17+
Name: "http",
18+
Port: 8443,
19+
},
20+
{
21+
Name: "rw-default",
22+
Port: 3306,
23+
TargetPort: intstr.IntOrString{
24+
IntVal: 6446,
25+
},
26+
},
27+
{
28+
Name: "read-write",
29+
Port: 6446,
30+
},
31+
{
32+
Name: "read-only",
33+
Port: 6447,
34+
},
35+
{
36+
Name: "x-read-write",
37+
Port: 6448,
38+
},
39+
{
40+
Name: "x-read-only",
41+
Port: 6449,
42+
},
43+
{
44+
Name: "x-default",
45+
Port: 33060,
46+
},
47+
{
48+
Name: "rw-admin",
49+
Port: 33062,
50+
},
51+
}
52+
}
53+
1554
tests := []struct {
16-
name string
17-
specifiedPorts []corev1.ServicePort
18-
expectedPortsFile string
55+
name string
56+
specifiedPorts []corev1.ServicePort
57+
expectedPorts []corev1.ServicePort
1958
}{
2059
{
21-
name: "default ports",
22-
expectedPortsFile: "default-ports.yaml",
60+
name: "default ports",
61+
expectedPorts: defaultPorts(),
2362
},
2463
{
2564
name: "additional ports",
@@ -37,7 +76,23 @@ func TestPorts(t *testing.T) {
3776
},
3877
},
3978
},
40-
expectedPortsFile: "add-ports.yaml",
79+
expectedPorts: updateObject(defaultPorts(), func(ports []corev1.ServicePort) []corev1.ServicePort {
80+
ports = append(ports, []corev1.ServicePort{
81+
{
82+
Name: "additional port",
83+
Port: 4308,
84+
},
85+
{
86+
Name: "additional port with target port",
87+
Port: 1337,
88+
TargetPort: intstr.IntOrString{
89+
Type: intstr.Int,
90+
IntVal: 20,
91+
},
92+
},
93+
}...)
94+
return ports
95+
}),
4196
},
4297
{
4398
name: "modified ports with additional ports",
@@ -67,7 +122,37 @@ func TestPorts(t *testing.T) {
67122
},
68123
},
69124
},
70-
expectedPortsFile: "mod-add-ports.yaml",
125+
expectedPorts: updateObject(defaultPorts(), func(ports []corev1.ServicePort) []corev1.ServicePort {
126+
for i, v := range ports {
127+
if v.Name == "http" {
128+
ports[i].Port = 5555
129+
continue
130+
}
131+
if v.Name == "rw-default" {
132+
ports[i].Port = 6666
133+
ports[i].TargetPort = intstr.IntOrString{
134+
Type: intstr.Int,
135+
IntVal: 30,
136+
}
137+
continue
138+
}
139+
}
140+
ports = append(ports, []corev1.ServicePort{
141+
{
142+
Name: "additional port",
143+
Port: 4308,
144+
},
145+
{
146+
Name: "additional port with target port",
147+
Port: 1337,
148+
TargetPort: intstr.IntOrString{
149+
Type: intstr.Int,
150+
IntVal: 20,
151+
},
152+
},
153+
}...)
154+
return ports
155+
}),
71156
},
72157
{
73158
name: "modified port with default targetPort",
@@ -77,55 +162,39 @@ func TestPorts(t *testing.T) {
77162
Port: 6666,
78163
},
79164
},
80-
expectedPortsFile: "mod-def-targetport-ports.yaml",
165+
expectedPorts: updateObject(defaultPorts(), func(ports []corev1.ServicePort) []corev1.ServicePort {
166+
for i, v := range ports {
167+
if v.Name == "rw-default" {
168+
ports[i].Port = 6666
169+
break
170+
}
171+
}
172+
return ports
173+
}),
81174
},
82175
}
83176

84177
for _, tt := range tests {
85178
t.Run(tt.name, func(t *testing.T) {
86179
got := ports(tt.specifiedPorts)
87-
data, err := yaml.Marshal(got)
88-
if err != nil {
89-
t.Fatal(err)
180+
if !reflect.DeepEqual(got, tt.expectedPorts) {
181+
gotBytes, err := yaml.Marshal(got)
182+
if err != nil {
183+
t.Fatalf("error marshaling got: %v", err)
184+
}
185+
wantBytes, err := yaml.Marshal(tt.expectedPorts)
186+
if err != nil {
187+
t.Fatalf("error marshaling want: %v", err)
188+
}
189+
t.Fatal(cmp.Diff(string(wantBytes), string(gotBytes)))
90190
}
91-
err = os.WriteFile(filepath.Join("testdata", "ports", tt.expectedPortsFile), data, 0666)
92-
if err != nil {
93-
t.Fatal(err)
94-
}
95-
expected := expectedObject[[]corev1.ServicePort](t, filepath.Join("ports", tt.expectedPortsFile))
96-
compareObj(t, got, expected)
97191
})
98192
}
99193
}
100194

101-
func expectedObject[T any](t *testing.T, path string) T {
102-
t.Helper()
103-
104-
data, err := os.ReadFile(filepath.Join("testdata", path))
105-
if err != nil {
106-
t.Fatal(err)
107-
}
108-
109-
obj := new(T)
110-
if err := yaml.Unmarshal(data, obj); err != nil {
111-
t.Fatal(err)
112-
}
113-
114-
return *obj
115-
}
116-
117-
func compareObj[T any](t *testing.T, got, want T) {
118-
t.Helper()
119-
120-
gotBytes, err := yaml.Marshal(got)
121-
if err != nil {
122-
t.Fatalf("error marshaling got: %v", err)
123-
}
124-
wantBytes, err := yaml.Marshal(want)
125-
if err != nil {
126-
t.Fatalf("error marshaling want: %v", err)
127-
}
128-
if string(gotBytes) != string(wantBytes) {
129-
t.Fatal(cmp.Diff(string(wantBytes), string(gotBytes)))
195+
func updateObject[T any](obj T, updateFuncs ...func(obj T) T) T {
196+
for _, f := range updateFuncs {
197+
obj = f(obj)
130198
}
199+
return obj
131200
}

0 commit comments

Comments
 (0)