@@ -17,6 +17,7 @@ limitations under the License.
17
17
package apiserver
18
18
19
19
import (
20
+ "fmt"
20
21
"testing"
21
22
22
23
"github.com/davecgh/go-spew/spew"
@@ -31,13 +32,18 @@ import (
31
32
listers "k8s.io/kube-aggregator/pkg/client/listers/apiregistration/internalversion"
32
33
)
33
34
35
+ const (
36
+ testServicePort = 1234
37
+ testServicePortName = "testPort"
38
+ )
39
+
34
40
func newEndpoints (namespace , name string ) * v1.Endpoints {
35
41
return & v1.Endpoints {
36
42
ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : name },
37
43
}
38
44
}
39
45
40
- func newEndpointsWithAddress (namespace , name string ) * v1.Endpoints {
46
+ func newEndpointsWithAddress (namespace , name string , port int32 , portName string ) * v1.Endpoints {
41
47
return & v1.Endpoints {
42
48
ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : name },
43
49
Subsets : []v1.EndpointSubset {
@@ -47,18 +53,24 @@ func newEndpointsWithAddress(namespace, name string) *v1.Endpoints {
47
53
IP : "val" ,
48
54
},
49
55
},
56
+ Ports : []v1.EndpointPort {
57
+ {
58
+ Name : portName ,
59
+ Port : port ,
60
+ },
61
+ },
50
62
},
51
63
},
52
64
}
53
65
}
54
66
55
- func newService (namespace , name string ) * v1.Service {
67
+ func newService (namespace , name string , port int32 , portName string ) * v1.Service {
56
68
return & v1.Service {
57
69
ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : name },
58
70
Spec : v1.ServiceSpec {
59
71
Type : v1 .ServiceTypeClusterIP ,
60
72
Ports : []v1.ServicePort {
61
- {Port : 443 },
73
+ {Port : port , Name : portName },
62
74
},
63
75
},
64
76
}
@@ -77,6 +89,7 @@ func newRemoteAPIService(name string) *apiregistration.APIService {
77
89
Service : & apiregistration.ServiceReference {
78
90
Namespace : "foo" ,
79
91
Name : "bar" ,
92
+ Port : testServicePort ,
80
93
},
81
94
},
82
95
}
@@ -107,7 +120,7 @@ func TestSync(t *testing.T) {
107
120
name : "no service" ,
108
121
apiServiceName : "remote.group" ,
109
122
apiServices : []* apiregistration.APIService {newRemoteAPIService ("remote.group" )},
110
- services : []* v1.Service {newService ("foo" , "not-bar" )},
123
+ services : []* v1.Service {newService ("foo" , "not-bar" , testServicePort , testServicePortName )},
111
124
expectedAvailability : apiregistration.APIServiceCondition {
112
125
Type : apiregistration .Available ,
113
126
Status : apiregistration .ConditionFalse ,
@@ -128,19 +141,19 @@ func TestSync(t *testing.T) {
128
141
},
129
142
},
130
143
}},
131
- endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" )},
144
+ endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
132
145
expectedAvailability : apiregistration.APIServiceCondition {
133
146
Type : apiregistration .Available ,
134
147
Status : apiregistration .ConditionFalse ,
135
148
Reason : "ServicePortError" ,
136
- Message : `service/bar in "foo" is not listening on port 443` ,
149
+ Message : fmt . Sprintf ( `service/bar in "foo" is not listening on port %d` , testServicePort ) ,
137
150
},
138
151
},
139
152
{
140
153
name : "no endpoints" ,
141
154
apiServiceName : "remote.group" ,
142
155
apiServices : []* apiregistration.APIService {newRemoteAPIService ("remote.group" )},
143
- services : []* v1.Service {newService ("foo" , "bar" )},
156
+ services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
144
157
expectedAvailability : apiregistration.APIServiceCondition {
145
158
Type : apiregistration .Available ,
146
159
Status : apiregistration .ConditionFalse ,
@@ -152,21 +165,34 @@ func TestSync(t *testing.T) {
152
165
name : "missing endpoints" ,
153
166
apiServiceName : "remote.group" ,
154
167
apiServices : []* apiregistration.APIService {newRemoteAPIService ("remote.group" )},
155
- services : []* v1.Service {newService ("foo" , "bar" )},
168
+ services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
156
169
endpoints : []* v1.Endpoints {newEndpoints ("foo" , "bar" )},
157
170
expectedAvailability : apiregistration.APIServiceCondition {
158
171
Type : apiregistration .Available ,
159
172
Status : apiregistration .ConditionFalse ,
160
173
Reason : "MissingEndpoints" ,
161
- Message : `endpoints for service/bar in "foo" have no addresses` ,
174
+ Message : `endpoints for service/bar in "foo" have no addresses with port name "testPort"` ,
175
+ },
176
+ },
177
+ {
178
+ name : "wrong endpoint port name" ,
179
+ apiServiceName : "remote.group" ,
180
+ apiServices : []* apiregistration.APIService {newRemoteAPIService ("remote.group" )},
181
+ services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
182
+ endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , "wrongName" )},
183
+ expectedAvailability : apiregistration.APIServiceCondition {
184
+ Type : apiregistration .Available ,
185
+ Status : apiregistration .ConditionFalse ,
186
+ Reason : "MissingEndpoints" ,
187
+ Message : fmt .Sprintf (`endpoints for service/bar in "foo" have no addresses with port name "%s"` , testServicePortName ),
162
188
},
163
189
},
164
190
{
165
191
name : "remote" ,
166
192
apiServiceName : "remote.group" ,
167
193
apiServices : []* apiregistration.APIService {newRemoteAPIService ("remote.group" )},
168
- services : []* v1.Service {newService ("foo" , "bar" )},
169
- endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" )},
194
+ services : []* v1.Service {newService ("foo" , "bar" , testServicePort , testServicePortName )},
195
+ endpoints : []* v1.Endpoints {newEndpointsWithAddress ("foo" , "bar" , testServicePort , testServicePortName )},
170
196
expectedAvailability : apiregistration.APIServiceCondition {
171
197
Type : apiregistration .Available ,
172
198
Status : apiregistration .ConditionTrue ,
0 commit comments