Skip to content

Commit

Permalink
test: add unit test for emitFinalizeCleanup()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-ibra committed Dec 18, 2023
1 parent ff6acc9 commit 3ccd439
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 7 deletions.
27 changes: 20 additions & 7 deletions internal/controller/validatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"context"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -107,7 +109,10 @@ func (r *ValidatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

err = removeFinalizer(ctx, r.Client, vc, CleanupFinalizer)
r.emitFinalizeCleanup()

if emitErr := r.emitFinalizeCleanup(); emitErr != nil {
r.Log.Error(emitErr, "Failed to emit FinalizeCleanup request")
}

Check warning on line 115 in internal/controller/validatorconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/validatorconfig_controller.go#L114-L115

Added lines #L114 - L115 were not covered by tests

return ctrl.Result{}, err
}
Expand Down Expand Up @@ -383,27 +388,35 @@ func isConditionTrue(vc *v1alpha1.ValidatorConfig, chartName string, conditionTy
return vc.Status.Conditions[idx], vc.Status.Conditions[idx].Status == corev1.ConditionTrue
}

func (r *ValidatorConfigReconciler) emitFinalizeCleanup() {
grpcEnabled := os.Getenv("CLEANUP_GRPC_ENABLED")
func (r *ValidatorConfigReconciler) emitFinalizeCleanup() error {
grpcEnabled := os.Getenv("CLEANUP_GRPC_SERVER_ENABLED")
if grpcEnabled != "true" {
r.Log.V(0).Info("Cleanup job gRPC server is not enabled")
return
return nil
}

host := os.Getenv("CLEANUP_GRPC_SERVER_HOST")
if host == "" {
return errors.New("CLEANUP_GRPC_SERVER_HOST is invalid")
}

port := os.Getenv("CLEANUP_GRPC_SERVER_PORT")
_, err := strconv.Atoi(port)
if err != nil {
return fmt.Errorf("CLEANUP_GRPC_SERVER_PORT is invalid: %w", err)
}

url := fmt.Sprintf("https://%s:%s", host, port)

client := cleanupv1connect.NewCleanupServiceClient(
http.DefaultClient,
url,
)
_, err := client.FinalizeCleanup(
_, err = client.FinalizeCleanup(
context.Background(),
connect.NewRequest(&cleanv1.FinalizeCleanupRequest{}),
)
if err != nil {
r.Log.Error(err, "FinalizeCleanup request failed", "url", url)
return fmt.Errorf("FinalizeCleanup request to %s failed: %w", url, err)
}
return nil

Check warning on line 421 in internal/controller/validatorconfig_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/validatorconfig_controller.go#L421

Added line #L421 was not covered by tests
}
73 changes: 73 additions & 0 deletions internal/controller/validatorconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,76 @@ func TestConfigureHelmBasicAuth(t *testing.T) {
}
}
}

func TestEmitFinalizeCleanup(t *testing.T) {
cs := []struct {
name string
reconciler ValidatorConfigReconciler
env map[string]string
expected error
}{
{
name: "CLEANUP_GRPC_SERVER_ENABLED is empty",
reconciler: ValidatorConfigReconciler{},
env: map[string]string{},
expected: errors.New("CLEANUP_GRPC_SERVER_HOST is empty"),
},
{
name: "CLEANUP_GRPC_SERVER_ENABLED is disabled",
reconciler: ValidatorConfigReconciler{},
env: map[string]string{"CLEANUP_GRPC_SERVER_ENABLED": "false"},
expected: nil,
},
{
name: "CLEANUP_GRPC_SERVER_HOST is empty",
reconciler: ValidatorConfigReconciler{},
env: map[string]string{
"CLEANUP_GRPC_SERVER_ENABLED": "true",
"CLEANUP_GRPC_SERVER_PORT": "1234",
},
expected: errors.New("CLEANUP_GRPC_SERVER_HOST is invalid"),
},
{
name: "CLEANUP_GRPC_SERVER_PORT is empty",
reconciler: ValidatorConfigReconciler{},
env: map[string]string{
"CLEANUP_GRPC_SERVER_ENABLED": "true",
"CLEANUP_GRPC_SERVER_HOST": "localhost",
},
expected: errors.New(`CLEANUP_GRPC_SERVER_PORT is invalid: strconv.Atoi: parsing "": invalid syntax`),
},
{
name: "CLEANUP_GRPC_SERVER_PORT is invalid",
reconciler: ValidatorConfigReconciler{},
env: map[string]string{
"CLEANUP_GRPC_SERVER_ENABLED": "true",
"CLEANUP_GRPC_SERVER_HOST": "localhost",
"CLEANUP_GRPC_SERVER_PORT": "abcd",
},
expected: errors.New(`CLEANUP_GRPC_SERVER_PORT is invalid: strconv.Atoi: parsing "abcd": invalid syntax`),
},
{
name: "Request fails",
reconciler: ValidatorConfigReconciler{},
env: map[string]string{
"CLEANUP_GRPC_SERVER_ENABLED": "true",
"CLEANUP_GRPC_SERVER_HOST": "localhost",
"CLEANUP_GRPC_SERVER_PORT": "1234",
},
expected: errors.New(`FinalizeCleanup request to https://localhost:1234 failed: unavailable: dial tcp [::1]:1234: connect: connection refused`),
},
}
for _, c := range cs {
t.Log(c.name)

os.Clearenv()
for k, v := range c.env {
os.Setenv(k, v)
}

err := c.reconciler.emitFinalizeCleanup()
if err != nil && !reflect.DeepEqual(err.Error(), c.expected.Error()) {
t.Errorf("expected (%v), got (%v)", c.expected, err)
}
}
}

0 comments on commit 3ccd439

Please sign in to comment.