Skip to content

Commit

Permalink
Add testutils package to avoid duplicating code
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKeller committed Aug 4, 2023
1 parent 7454d89 commit 0a793bd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 76 deletions.
13 changes: 12 additions & 1 deletion pkg/controller/keyvault/event_mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package keyvault

import (
"context"
"github.com/Azure/aks-app-routing-operator/pkg/controller/metrics"
"github.com/Azure/aks-app-routing-operator/pkg/controller/testutils"
"testing"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -56,9 +58,14 @@ func TestEventMirrorHappyPath(t *testing.T) {
events: recorder,
}

beforeErrCount := testutils.GetErrMetricCount(t, eventMirrorControllerName)
beforeReconcileCount := testutils.GetReconcileMetricCount(t, eventMirrorControllerName, metrics.LabelSuccess)
_, err := e.Reconcile(ctx, req)
require.NoError(t, err)

require.Equal(t, testutils.GetErrMetricCount(t, eventMirrorControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, eventMirrorControllerName, metrics.LabelSuccess), beforeReconcileCount)

assert.Equal(t, "Warning FailedMount test keyvault event involvedObject{kind=Ingress,apiVersion=networking.k8s.io/v1}", <-recorder.Events)
}

Expand Down Expand Up @@ -107,10 +114,14 @@ func TestEventMirrorServiceOwnerHappyPath(t *testing.T) {
events: recorder,
}

beforeErrCount := getErrMetricBeforeCount(t)
beforeErrCount := testutils.GetErrMetricCount(t, eventMirrorControllerName)
beforeReconcileCount := testutils.GetReconcileMetricCount(t, eventMirrorControllerName, metrics.LabelSuccess)
_, err := e.Reconcile(ctx, req)
require.NoError(t, err)

require.Equal(t, testutils.GetErrMetricCount(t, eventMirrorControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, eventMirrorControllerName, metrics.LabelSuccess), beforeReconcileCount)

assert.Equal(t, "Warning FailedMount test keyvault event involvedObject{kind=Service,apiVersion=v1}", <-recorder.Events)
assert.Equal(t, "Warning FailedMount test keyvault event involvedObject{kind=Ingress,apiVersion=networking.k8s.io/v1}", <-recorder.Events)
}
100 changes: 25 additions & 75 deletions pkg/controller/keyvault/ingress_secret_provider_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package keyvault
import (
"context"
"fmt"
"github.com/Azure/aks-app-routing-operator/pkg/controller/testutils"
"net/url"
"testing"

"github.com/go-logr/logr"
promDTO "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
netv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -54,13 +54,13 @@ func TestIngressSecretProviderClassReconcilerIntegration(t *testing.T) {

// Create the secret provider class
req := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: ing.Namespace, Name: ing.Name}}
beforeErrCount := getErrMetricBeforeCount(t, ingressSecretProviderControllerName)
beforeRequestCount := getReconcileMetricBeforeCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
beforeErrCount := testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
beforeRequestCount := testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
_, err := i.Reconcile(ctx, req)
require.NoError(t, err)

require.Equal(t, getErrMetricAfterCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, getReconcileMetricAfterCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)
require.Equal(t, testutils.GetErrMetricCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)

// Prove it exists
spc := &secv1.SecretProviderClass{}
Expand Down Expand Up @@ -91,24 +91,24 @@ func TestIngressSecretProviderClassReconcilerIntegration(t *testing.T) {
assert.Equal(t, expected.Spec, spc.Spec)

// Check for idempotence
beforeErrCount = getErrMetricBeforeCount(t, ingressSecretProviderControllerName)
beforeRequestCount = getReconcileMetricBeforeCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
beforeErrCount = testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
beforeRequestCount = testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
_, err = i.Reconcile(ctx, req)
require.NoError(t, err)
require.Equal(t, getErrMetricAfterCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, getReconcileMetricAfterCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)
require.Equal(t, testutils.GetErrMetricCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)

// Remove the cert's version from the ingress
ing.Annotations = map[string]string{
"kubernetes.azure.com/tls-cert-keyvault-uri": "https://testvault.vault.azure.net/certificates/testcert",
}
require.NoError(t, i.client.Update(ctx, ing))
beforeErrCount = getErrMetricBeforeCount(t, ingressSecretProviderControllerName)
beforeRequestCount = getReconcileMetricBeforeCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
beforeErrCount = testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
beforeRequestCount = testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
_, err = i.Reconcile(ctx, req)
require.NoError(t, err)
require.Equal(t, getErrMetricAfterCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, getReconcileMetricAfterCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)
require.Equal(t, testutils.GetErrMetricCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)

// Prove the objectVersion property was removed
require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(spc), spc))
Expand All @@ -118,23 +118,23 @@ func TestIngressSecretProviderClassReconcilerIntegration(t *testing.T) {
// Remove the cert annotation from the ingress
ing.Annotations = map[string]string{}
require.NoError(t, i.client.Update(ctx, ing))
beforeErrCount = getErrMetricBeforeCount(t, ingressSecretProviderControllerName)
beforeRequestCount = getReconcileMetricBeforeCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
beforeErrCount = testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
beforeRequestCount = testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
_, err = i.Reconcile(ctx, req)
require.NoError(t, err)
require.Equal(t, getErrMetricAfterCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, getReconcileMetricAfterCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)
require.Equal(t, testutils.GetErrMetricCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)

// Prove secret class was removed
require.True(t, errors.IsNotFound(c.Get(ctx, client.ObjectKeyFromObject(spc), spc)))

// Check for idempotence
beforeErrCount = getErrMetricBeforeCount(t, ingressSecretProviderControllerName)
beforeRequestCount = getReconcileMetricBeforeCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
beforeErrCount = testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
beforeRequestCount = testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess)
_, err = i.Reconcile(ctx, req)
require.NoError(t, err)
require.Equal(t, getErrMetricAfterCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, getReconcileMetricAfterCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)
require.Equal(t, testutils.GetErrMetricCount(t, ingressSecretProviderControllerName), beforeErrCount)
require.Greater(t, testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelSuccess), beforeRequestCount)
}

func TestIngressSecretProviderClassReconcilerInvalidURL(t *testing.T) {
Expand Down Expand Up @@ -166,17 +166,17 @@ func TestIngressSecretProviderClassReconcilerInvalidURL(t *testing.T) {
metrics.InitControllerMetrics(ingressSecretProviderControllerName)

// get the before value of the error metrics
beforeErrCount := getErrMetricBeforeCount(t, ingressSecretProviderControllerName)
beforeRequestCount := getReconcileMetricBeforeCount(t, metrics.LabelError, ingressSecretProviderControllerName)
beforeErrCount := testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
beforeRequestCount := testutils.GetReconcileMetricCount(t, metrics.LabelError, ingressSecretProviderControllerName)

req := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: ing.Namespace, Name: ing.Name}}
_, err := i.Reconcile(ctx, req)
require.NoError(t, err)

assert.Equal(t, "Warning InvalidInput error while processing Keyvault reference: invalid secret uri: inv@lid URL", <-recorder.Events)
//even though no error was returned, we should expect the error count to be incremented
afterErrCount := getErrMetricAfterCount(t, ingressSecretProviderControllerName)
afterRequestCount := getReconcileMetricBeforeCount(t, ingressSecretProviderControllerName, metrics.LabelError)
afterErrCount := testutils.GetErrMetricCount(t, ingressSecretProviderControllerName)
afterRequestCount := testutils.GetReconcileMetricCount(t, ingressSecretProviderControllerName, metrics.LabelError)

assert.Greater(t, afterErrCount, beforeErrCount)
assert.Greater(t, afterRequestCount, beforeRequestCount)
Expand Down Expand Up @@ -307,53 +307,3 @@ func TestIngressSecretProviderClassReconcilerBuildSPCCloud(t *testing.T) {
})
}
}

func getErrMetricBeforeCount(t *testing.T, controllerName string) float64 {
errMetric, err := metrics.AppRoutingReconcileErrors.GetMetricWithLabelValues(controllerName)
require.NoError(t, err)

metricProto := &promDTO.Metric{}

err = errMetric.Write(metricProto)
require.NoError(t, err)

beforeCount := metricProto.GetCounter().GetValue()
return beforeCount
}

func getErrMetricAfterCount(t *testing.T, controllerName string) float64 {
errMetric, err := metrics.AppRoutingReconcileErrors.GetMetricWithLabelValues(controllerName)
require.NoError(t, err)

metricProto := &promDTO.Metric{}

err = errMetric.Write(metricProto)
require.NoError(t, err)
afterCount := metricProto.GetCounter().GetValue()
return afterCount
}

func getReconcileMetricBeforeCount(t *testing.T, controllerName, label string) float64 {
errMetric, err := metrics.AppRoutingReconcileTotal.GetMetricWithLabelValues(controllerName, label)
require.NoError(t, err)

metricProto := &promDTO.Metric{}

err = errMetric.Write(metricProto)
require.NoError(t, err)

beforeCount := metricProto.GetCounter().GetValue()
return beforeCount
}

func getReconcileMetricAfterCount(t *testing.T, controllerName, label string) float64 {
errMetric, err := metrics.AppRoutingReconcileTotal.GetMetricWithLabelValues(controllerName, label)
require.NoError(t, err)

metricProto := &promDTO.Metric{}

err = errMetric.Write(metricProto)
require.NoError(t, err)
afterCount := metricProto.GetCounter().GetValue()
return afterCount
}
34 changes: 34 additions & 0 deletions pkg/controller/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package testutils

import (
"github.com/Azure/aks-app-routing-operator/pkg/controller/metrics"
promDTO "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
"testing"
)

func GetErrMetricCount(t *testing.T, controllerName string) float64 {
errMetric, err := metrics.AppRoutingReconcileErrors.GetMetricWithLabelValues(controllerName)
require.NoError(t, err)

metricProto := &promDTO.Metric{}

err = errMetric.Write(metricProto)
require.NoError(t, err)

beforeCount := metricProto.GetCounter().GetValue()
return beforeCount
}

func GetReconcileMetricCount(t *testing.T, controllerName, label string) float64 {
errMetric, err := metrics.AppRoutingReconcileTotal.GetMetricWithLabelValues(controllerName, label)
require.NoError(t, err)

metricProto := &promDTO.Metric{}

err = errMetric.Write(metricProto)
require.NoError(t, err)

beforeCount := metricProto.GetCounter().GetValue()
return beforeCount
}

0 comments on commit 0a793bd

Please sign in to comment.