traefik/pkg/provider/kubernetes/k8s/event_handler_test.go

229 lines
4.8 KiB
Go
Raw Normal View History

package k8s
import (
"testing"
"github.com/stretchr/testify/assert"
2024-06-21 12:56:03 +00:00
discoveryv1 "k8s.io/api/discovery/v1"
2023-04-03 08:06:06 +00:00
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Test_detectChanges(t *testing.T) {
2024-06-21 12:56:03 +00:00
portA := int32(80)
portB := int32(8080)
tests := []struct {
name string
oldObj interface{}
newObj interface{}
want bool
}{
{
name: "With nil values",
want: true,
},
{
2024-06-21 12:56:03 +00:00
name: "With empty endpointslice",
oldObj: &discoveryv1.EndpointSlice{},
newObj: &discoveryv1.EndpointSlice{},
},
{
name: "With old nil",
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{},
want: true,
},
{
name: "With new nil",
2024-06-21 12:56:03 +00:00
oldObj: &discoveryv1.EndpointSlice{},
want: true,
},
{
name: "With same version",
2024-06-21 12:56:03 +00:00
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
},
},
{
name: "With different version",
2024-06-21 12:56:03 +00:00
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
},
},
},
{
name: "Ingress With same version",
2023-04-03 08:06:06 +00:00
oldObj: &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
},
2023-04-03 08:06:06 +00:00
newObj: &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
},
},
{
name: "Ingress With different version",
2023-04-03 08:06:06 +00:00
oldObj: &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
},
2023-04-03 08:06:06 +00:00
newObj: &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
},
},
want: true,
},
{
name: "With same annotations",
2024-06-21 12:56:03 +00:00
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
Annotations: map[string]string{
"test-annotation": "_",
},
},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{
"test-annotation": "_",
},
},
},
},
{
name: "With different annotations",
2024-06-21 12:56:03 +00:00
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
Annotations: map[string]string{
"test-annotation": "V",
},
},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{
"test-annotation": "X",
},
},
},
},
{
2024-06-21 12:56:03 +00:00
name: "With same endpoints and ports",
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
2024-06-21 12:56:03 +00:00
Endpoints: []discoveryv1.Endpoint{},
Ports: []discoveryv1.EndpointPort{},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
2024-06-21 12:56:03 +00:00
Endpoints: []discoveryv1.Endpoint{},
Ports: []discoveryv1.EndpointPort{},
},
},
{
2024-06-21 12:56:03 +00:00
name: "With different len of endpoints",
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
2024-06-21 12:56:03 +00:00
Endpoints: []discoveryv1.Endpoint{},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
},
2024-06-21 12:56:03 +00:00
Endpoints: []discoveryv1.Endpoint{{}},
},
want: true,
},
{
2024-06-21 12:56:03 +00:00
name: "With different endpoints",
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
2024-06-21 12:56:03 +00:00
Endpoints: []discoveryv1.Endpoint{{
Addresses: []string{"10.10.10.10"},
}},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
},
2024-06-21 12:56:03 +00:00
Endpoints: []discoveryv1.Endpoint{{
Addresses: []string{"10.10.10.11"},
}},
},
want: true,
},
{
2024-06-21 12:56:03 +00:00
name: "With different len of ports",
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
2024-06-21 12:56:03 +00:00
Ports: []discoveryv1.EndpointPort{},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
},
2024-06-21 12:56:03 +00:00
Ports: []discoveryv1.EndpointPort{{}},
},
want: true,
},
{
2024-06-21 12:56:03 +00:00
name: "With different ports",
oldObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
2024-06-21 12:56:03 +00:00
Ports: []discoveryv1.EndpointPort{{
Port: &portA,
}},
},
2024-06-21 12:56:03 +00:00
newObj: &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
},
2024-06-21 12:56:03 +00:00
Ports: []discoveryv1.EndpointPort{{
Port: &portB,
}},
},
want: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.want, objChanged(test.oldObj, test.newObj))
})
}
}