Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions test/integration/konnectExtension_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package integration

import (
"testing"

"github.com/google/uuid"
testutils "github.com/kong/kong-operator/pkg/utils/test"

Check failure on line 7 in test/integration/konnectExtension_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gci)
"github.com/kong/kong-operator/test"
"github.com/kong/kong-operator/test/helpers"
"github.com/kong/kong-operator/test/helpers/deploy"
konnectv1alpha1 "github.com/kong/kubernetes-configuration/v2/api/konnect/v1alpha1"
konnectv1alpha2 "github.com/kong/kubernetes-configuration/v2/api/konnect/v1alpha2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestKonnectExtensionKonnectControlPlaneNotFound(t *testing.T) {
ns, _ := helpers.SetupTestEnv(t, GetCtx(), GetEnv())

// Let's generate a unique test ID that we can refer to in Konnect entities.
// Using only the first 8 characters of the UUID to keep the ID short enough for Konnect to accept it as a part
// of an entity name.
testID := uuid.NewString()[:8]
t.Logf("Running Konnect extensions test with ID: %s", testID)

// Create an APIAuth for test.
clientNamespaced := client.NewNamespacedClient(GetClients().MgrClient, ns.Name)

konnectExtension := deploy.KonnectExtension(
t, ctx, clientNamespaced,
deploy.WithKonnectExtensionKonnectNamespacedRefControlPlaneRef(&konnectv1alpha2.KonnectGatewayControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "controlplane-not-found",
Namespace: ns.Name,
},
}),
)

t.Logf("Waiting for KonnectExtension %s/%s to have expected conditions set to False", konnectExtension.Namespace, konnectExtension.Name)
require.EventuallyWithT(t, func(t *assert.CollectT) {
ok, msg := checkKonnectExtensionConditions(t,
konnectExtension,
helpers.CheckAllConditionsFalse,
konnectv1alpha1.ControlPlaneRefValidConditionType,
konnectv1alpha2.KonnectExtensionReadyConditionType)
assert.Truef(t, ok, "condition check failed: %s, conditions: %+v", msg, konnectExtension.Status.Conditions)
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)
}

func TestKonnectExtensionControlPlaneRotation(t *testing.T) {
t.Skip("TODO: adapt to ControlPlane v2alpha1 https://github.com/kong/kong-operator/issues/1730")

ns, _ := helpers.SetupTestEnv(t, GetCtx(), GetEnv())

// Let's generate a unique test ID that we can refer to in Konnect entities.
// Using only the first 8 characters of the UUID to keep the ID short enough for Konnect to accept it as a part
// of an entity name.
testID := uuid.NewString()[:8]
t.Logf("Running Konnect extensions test with ID: %s", testID)

// Create an APIAuth for test.
clientNamespaced := client.NewNamespacedClient(GetClients().MgrClient, ns.Name)

authCfg := deploy.KonnectAPIAuthConfiguration(t, GetCtx(), clientNamespaced,
deploy.WithTestIDLabel(testID),
func(obj client.Object) {
authCfg := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration)
authCfg.Spec.Type = konnectv1alpha1.KonnectAPIAuthTypeToken
authCfg.Spec.Token = test.KonnectAccessToken()
authCfg.Spec.ServerURL = test.KonnectServerURL()
},
)

// Create a Konnect control plane for the KonnectExtension to attach to.
cp := deploy.KonnectGatewayControlPlane(t, GetCtx(), clientNamespaced, authCfg,
deploy.WithTestIDLabel(testID),
)

t.Logf("Waiting for Konnect ID to be assigned to ControlPlane %s/%s", cp.Namespace, cp.Name)
require.EventuallyWithT(t, func(t *assert.CollectT) {
err := GetClients().MgrClient.Get(GetCtx(), k8stypes.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, cp)
require.NoError(t, err)
assertKonnectEntityProgrammed(t, cp)
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)

konnectExtension := deploy.KonnectExtension(
t, ctx, clientNamespaced,
deploy.WithKonnectExtensionKonnectNamespacedRefControlPlaneRef(cp),
)

t.Logf("Waiting for KonnectExtension %s/%s to have expected conditions set to True", konnectExtension.Namespace, konnectExtension.Name)
require.EventuallyWithT(t, func(t *assert.CollectT) {
ok, msg := checkKonnectExtensionConditions(t,
konnectExtension,
helpers.CheckAllConditionsTrue,
konnectv1alpha1.ControlPlaneRefValidConditionType,
konnectv1alpha1.DataPlaneCertificateProvisionedConditionType,
konnectv1alpha2.KonnectExtensionReadyConditionType)
assert.Truef(t, ok, "condition check failed: %s, conditions: %+v", msg, konnectExtension.Status.Conditions)
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)

t.Logf("waiting for status.konnect and status.dataPlaneClientAuth to be set for KonnectExtension %s/%s", konnectExtension.Namespace, konnectExtension.Name)
require.EventuallyWithT(t,
checkKonnectExtensionStatus(konnectExtension, cp.GetKonnectID(), ""),
testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)

t.Logf("deleting Konnect control plane %s/%s", cp.Namespace, cp.Name)
deleteObjectAndWaitForDeletionFn(t, cp.DeepCopy())()

// Create a Konnect control plane for the KonnectExtension to attach to.
cp = deploy.KonnectGatewayControlPlane(t, GetCtx(), clientNamespaced, authCfg,
deploy.WithTestIDLabel(testID),
deploy.WithName(cp.Name), // Reuse the same name to ensure the KonnectExtension is recreated with the same name.
)
t.Cleanup(deleteObjectAndWaitForDeletionFn(t, cp.DeepCopy()))

t.Logf("Waiting for Konnect ID to be assigned to ControlPlane %s/%s", cp.Namespace, cp.Name)
require.EventuallyWithT(t, func(t *assert.CollectT) {
err := GetClients().MgrClient.Get(GetCtx(), k8stypes.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, cp)
require.NoError(t, err)
assertKonnectEntityProgrammed(t, cp)
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)

t.Logf("Waiting for KonnectExtension %s/%s to have expected conditions set to True", konnectExtension.Namespace, konnectExtension.Name)
require.EventuallyWithT(t, func(t *assert.CollectT) {
ok, msg := checkKonnectExtensionConditions(t,
konnectExtension,
helpers.CheckAllConditionsTrue,
konnectv1alpha1.ControlPlaneRefValidConditionType,
konnectv1alpha1.DataPlaneCertificateProvisionedConditionType,
konnectv1alpha2.KonnectExtensionReadyConditionType)
assert.Truef(t, ok, "condition check failed: %s, conditions: %+v", msg, konnectExtension.Status.Conditions)
}, testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)

t.Logf("waiting for status.konnect and status.dataPlaneClientAuth to be properly updated for KonnectExtension %s/%s", konnectExtension.Namespace, konnectExtension.Name)
require.EventuallyWithT(t,
checkKonnectExtensionStatus(konnectExtension, cp.GetKonnectID(), ""),
testutils.ObjectUpdateTimeout, testutils.ObjectUpdateTick)

// delete the KonnectExtension first to avoid the ControlPlane gets deleted first and
// the KonnectExtension gets stuck in deletion.
deleteObjectAndWaitForDeletionFn(t, konnectExtension.DeepCopy())()
}
Loading
Loading