kubernetes/pkg/controller/endpointslicemirroring/endpointslice_tracker_test.go
Rob Scott 8691466059
Adding EndpointSliceMirroring controller
This will mirror custom Endpoints to EndpointSlices to ensure that
applications will not need to maintain both separately.
2020-07-06 12:43:33 -07:00

175 lines
4.8 KiB
Go

/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package endpointslicemirroring
import (
"testing"
discovery "k8s.io/api/discovery/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestEndpointSliceTrackerUpdate(t *testing.T) {
epSlice1 := &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "example-1",
Namespace: "ns1",
ResourceVersion: "rv1",
Labels: map[string]string{discovery.LabelServiceName: "svc1"},
},
}
epSlice1DifferentNS := epSlice1.DeepCopy()
epSlice1DifferentNS.Namespace = "ns2"
epSlice1DifferentService := epSlice1.DeepCopy()
epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2"
epSlice1DifferentRV := epSlice1.DeepCopy()
epSlice1DifferentRV.ResourceVersion = "rv2"
testCases := map[string]struct {
updateParam *discovery.EndpointSlice
checksParam *discovery.EndpointSlice
expectHas bool
expectStale bool
}{
"same slice": {
updateParam: epSlice1,
checksParam: epSlice1,
expectHas: true,
expectStale: false,
},
"different namespace": {
updateParam: epSlice1,
checksParam: epSlice1DifferentNS,
expectHas: false,
expectStale: true,
},
"different service": {
updateParam: epSlice1,
checksParam: epSlice1DifferentService,
expectHas: false,
expectStale: true,
},
"different resource version": {
updateParam: epSlice1,
checksParam: epSlice1DifferentRV,
expectHas: true,
expectStale: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
esTracker := newEndpointSliceTracker()
esTracker.update(tc.updateParam)
if esTracker.has(tc.checksParam) != tc.expectHas {
t.Errorf("tc.tracker.has(%+v) == %t, expected %t", tc.checksParam, esTracker.has(tc.checksParam), tc.expectHas)
}
if esTracker.stale(tc.checksParam) != tc.expectStale {
t.Errorf("tc.tracker.stale(%+v) == %t, expected %t", tc.checksParam, esTracker.stale(tc.checksParam), tc.expectStale)
}
})
}
}
func TestEndpointSliceTrackerDelete(t *testing.T) {
epSlice1 := &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: "example-1",
Namespace: "ns1",
ResourceVersion: "rv1",
Labels: map[string]string{discovery.LabelServiceName: "svc1"},
},
}
epSlice1DifferentNS := epSlice1.DeepCopy()
epSlice1DifferentNS.Namespace = "ns2"
epSlice1DifferentService := epSlice1.DeepCopy()
epSlice1DifferentService.Labels[discovery.LabelServiceName] = "svc2"
epSlice1DifferentRV := epSlice1.DeepCopy()
epSlice1DifferentRV.ResourceVersion = "rv2"
testCases := map[string]struct {
deleteParam *discovery.EndpointSlice
checksParam *discovery.EndpointSlice
expectHas bool
expectStale bool
}{
"same slice": {
deleteParam: epSlice1,
checksParam: epSlice1,
expectHas: false,
expectStale: true,
},
"different namespace": {
deleteParam: epSlice1DifferentNS,
checksParam: epSlice1DifferentNS,
expectHas: false,
expectStale: true,
},
"different namespace, check original ep slice": {
deleteParam: epSlice1DifferentNS,
checksParam: epSlice1,
expectHas: true,
expectStale: false,
},
"different service": {
deleteParam: epSlice1DifferentService,
checksParam: epSlice1DifferentService,
expectHas: false,
expectStale: true,
},
"different service, check original ep slice": {
deleteParam: epSlice1DifferentService,
checksParam: epSlice1,
expectHas: true,
expectStale: false,
},
"different resource version": {
deleteParam: epSlice1DifferentRV,
checksParam: epSlice1DifferentRV,
expectHas: false,
expectStale: true,
},
"different resource version, check original ep slice": {
deleteParam: epSlice1DifferentRV,
checksParam: epSlice1,
expectHas: false,
expectStale: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
esTracker := newEndpointSliceTracker()
esTracker.update(epSlice1)
esTracker.delete(tc.deleteParam)
if esTracker.has(tc.checksParam) != tc.expectHas {
t.Errorf("esTracker.has(%+v) == %t, expected %t", tc.checksParam, esTracker.has(tc.checksParam), tc.expectHas)
}
if esTracker.stale(tc.checksParam) != tc.expectStale {
t.Errorf("esTracker.stale(%+v) == %t, expected %t", tc.checksParam, esTracker.stale(tc.checksParam), tc.expectStale)
}
})
}
}