From ce5d07822ad60e4341bf61c5bddde79ad2d2bcde Mon Sep 17 00:00:00 2001 From: Akshay Patidar Date: Sat, 19 Oct 2024 19:05:06 +0530 Subject: [PATCH] chore: add test cases Signed-off-by: Akshay Patidar --- controllers/autoneg_test.go | 72 ++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/controllers/autoneg_test.go b/controllers/autoneg_test.go index 0a4b6cd..8a0b92b 100644 --- a/controllers/autoneg_test.go +++ b/controllers/autoneg_test.go @@ -166,6 +166,14 @@ var statusTests = []struct { true, true, }, + { + "valid autoneg status without autoneg", + map[string]string{ + autonegStatusAnnotation: validStatus, + }, + true, + false, + }, } var oldStatusTests = []struct { @@ -256,12 +264,21 @@ var oldStatusTests = []struct { true, false, }, + { + "(legacy) valid autoneg status without autoneg", + map[string]string{ + oldAutonegStatusAnnotation: validStatus, + }, + true, + false, + }, } func TestGetStatuses(t *testing.T) { var serviceReconciler = ServiceReconciler{ - ServiceNameTemplate: "{namespace}-{name}-{port}-{hash}", - AllowServiceName: true, + ServiceNameTemplate: "{namespace}-{name}-{port}-{hash}", + AllowServiceName: true, + DeregisterNEGsOnAnnotationRemoval: true, } for _, st := range statusTests { _, valid, err := getStatuses("ns", "test", st.annotations, &serviceReconciler) @@ -282,8 +299,9 @@ func TestGetStatuses(t *testing.T) { func TestGetOldStatuses(t *testing.T) { var serviceReconciler = ServiceReconciler{ - ServiceNameTemplate: "{namespace}-{name}-{port}-{hash}", - AllowServiceName: true, + ServiceNameTemplate: "{namespace}-{name}-{port}-{hash}", + AllowServiceName: true, + DeregisterNEGsOnAnnotationRemoval: true, } for _, st := range oldStatusTests { _, valid, err := getStatuses("ns", "test", st.annotations, &serviceReconciler) @@ -340,6 +358,52 @@ func TestGetStatusesServiceNameAllowed(t *testing.T) { } } +func TestGetStatusesOnlyAutonegStatusAnnotation(t *testing.T) { + var serviceReconciler = ServiceReconciler{ + ServiceNameTemplate: "{namespace}-{name}-{port}-{hash}", + AllowServiceName: true, + DeregisterNEGsOnAnnotationRemoval: true, + } + statuses, valid, err := getStatuses("ns", "test", map[string]string{autonegStatusAnnotation: validAutonegStatus}, &serviceReconciler) + if err != nil { + t.Errorf("Expected no error, got one: %v", err) + } + if !valid { + t.Errorf("Expected autoneg status config, got none") + } + + if !statuses.newConfig { + t.Errorf("Expected new autoneg config") + } + if statuses.config.BackendServices != nil { + t.Errorf("Expected nil backend services") + } + +} + +func TestGetStatusesOnlyOldAutonegStatusAnnotation(t *testing.T) { + var serviceReconciler = ServiceReconciler{ + ServiceNameTemplate: "{namespace}-{name}-{port}-{hash}", + AllowServiceName: true, + DeregisterNEGsOnAnnotationRemoval: true, + } + statuses, valid, err := getStatuses("ns", "test", map[string]string{oldAutonegStatusAnnotation: validAutonegStatus}, &serviceReconciler) + if err != nil { + t.Errorf("Expected no error, got one: %v", err) + } + if !valid { + t.Errorf("Expected old autoneg status config, got none") + } + + if statuses.newConfig { + t.Errorf("Expected old autoneg config") + } + if statuses.oldConfig.Name != "" { + t.Errorf("Expected empty old autoneg config") + } + +} + func TestDefaultMaxRatePerEndpointWhenOverrideIsSet(t *testing.T) { var serviceReconciler = ServiceReconciler{ ServiceNameTemplate: "{namespace}-{name}-{port}",