1
1
package router
2
2
3
3
import (
4
- "os"
5
- "path/filepath"
4
+ "reflect"
6
5
"testing"
7
6
8
7
"github.com/google/go-cmp/cmp"
@@ -12,14 +11,54 @@ import (
12
11
)
13
12
14
13
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
+
15
54
tests := []struct {
16
- name string
17
- specifiedPorts []corev1.ServicePort
18
- expectedPortsFile string
55
+ name string
56
+ specifiedPorts []corev1.ServicePort
57
+ expectedPorts []corev1. ServicePort
19
58
}{
20
59
{
21
- name : "default ports" ,
22
- expectedPortsFile : "default-ports.yaml" ,
60
+ name : "default ports" ,
61
+ expectedPorts : defaultPorts () ,
23
62
},
24
63
{
25
64
name : "additional ports" ,
@@ -37,7 +76,23 @@ func TestPorts(t *testing.T) {
37
76
},
38
77
},
39
78
},
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
+ }),
41
96
},
42
97
{
43
98
name : "modified ports with additional ports" ,
@@ -67,7 +122,37 @@ func TestPorts(t *testing.T) {
67
122
},
68
123
},
69
124
},
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
+ }),
71
156
},
72
157
{
73
158
name : "modified port with default targetPort" ,
@@ -77,55 +162,39 @@ func TestPorts(t *testing.T) {
77
162
Port : 6666 ,
78
163
},
79
164
},
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
+ }),
81
174
},
82
175
}
83
176
84
177
for _ , tt := range tests {
85
178
t .Run (tt .name , func (t * testing.T ) {
86
179
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 )))
90
190
}
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 )
97
191
})
98
192
}
99
193
}
100
194
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 )
130
198
}
199
+ return obj
131
200
}
0 commit comments