Skip to content

Commit

Permalink
tests: fix Konnect entities test, remove KongSNI when KongCertificate…
Browse files Browse the repository at this point in the history
… is deleted
  • Loading branch information
pmalek committed Sep 30, 2024
1 parent 850d40d commit a74d957
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 92 deletions.
7 changes: 2 additions & 5 deletions controller/konnect/reconciler_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
}
}
}
return ctrl.Result{}, err
return ctrl.Result{}, nil
} else if res.Requeue {
return res, nil
}
Expand Down Expand Up @@ -543,10 +543,7 @@ func updateStatusWithCondition[T interface {
if k8serrors.IsConflict(err) {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, fmt.Errorf(
"failed to update status with %s condition: %w",
conditions.KonnectEntityAPIAuthConfigurationResolvedRefConditionType, err,
)
return ctrl.Result{}, fmt.Errorf("failed to update status with %s condition: %w", conditionType, err)
}

return ctrl.Result{}, nil
Expand Down
2 changes: 1 addition & 1 deletion controller/konnect/watch_kongsni.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func kongSNIRefersToKonnectGatewayControlPlane(
Name: sni.Spec.CertificateRef.Name,
}
cert := configurationv1alpha1.KongCertificate{}
if err := cl.Get(context.Background(), certNN, &cert); client.IgnoreNotFound(err) != nil {
if err := cl.Get(context.Background(), certNN, &cert); err != nil {
return true
}

Expand Down
174 changes: 88 additions & 86 deletions test/envtest/konnect_entities_sni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,92 +50,94 @@ func TestKongSNI(t *testing.T) {
apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced)
cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth)

t.Log("Creating KongCertificate and setting it to Programmed")
createdCert := deployKongCertificateAttachedToCP(t, ctx, clientNamespaced, cp)
createdCert.Status = configurationv1alpha1.KongCertificateStatus{
Konnect: &konnectalpha1.KonnectEntityStatusWithControlPlaneRef{
KonnectEntityStatus: konnectEntityStatus("cert-12345"),
ControlPlaneID: cp.Status.GetKonnectID(),
},
Conditions: []metav1.Condition{
{
Type: conditions.KonnectEntityProgrammedConditionType,
Status: metav1.ConditionTrue,
Reason: conditions.KonnectEntityProgrammedReasonProgrammed,
ObservedGeneration: createdCert.GetGeneration(),
LastTransitionTime: metav1.Now(),
t.Run("adding, patching and deleting KongSNI", func(t *testing.T) {
t.Log("Creating KongCertificate and setting it to Programmed")
createdCert := deployKongCertificateAttachedToCP(t, ctx, clientNamespaced, cp)
createdCert.Status = configurationv1alpha1.KongCertificateStatus{
Konnect: &konnectalpha1.KonnectEntityStatusWithControlPlaneRef{
KonnectEntityStatus: konnectEntityStatus("cert-12345"),
ControlPlaneID: cp.Status.GetKonnectID(),
},
},
}
require.NoError(t, clientNamespaced.Status().Update(ctx, createdCert))

t.Log("Setting up a watch for KongSNI events")
w := setupWatch[configurationv1alpha1.KongSNIList](t, ctx, cl, client.InNamespace(ns.Name))

t.Log("Setting up SDK for creating SNI")
sdk.SNIsSDK.EXPECT().CreateSniWithCertificate(
mock.Anything,
mock.MatchedBy(func(req sdkkonnectops.CreateSniWithCertificateRequest) bool {
return req.ControlPlaneID == cp.Status.ID &&
req.CertificateID == createdCert.GetKonnectID() &&
req.SNIWithoutParents.Name == "test.kong-sni.example.com"
}),
).Return(&sdkkonnectops.CreateSniWithCertificateResponse{
Sni: &sdkkonnectcomp.Sni{
ID: lo.ToPtr("sni-12345"),
},
}, nil)

t.Log("Creating KongSNI")
createdSNI := deploySNIAttachedToCertificate(t, ctx,
clientNamespaced,
"test.kong-sni.example.com", nil,
createdCert,
)
Conditions: []metav1.Condition{
{
Type: conditions.KonnectEntityProgrammedConditionType,
Status: metav1.ConditionTrue,
Reason: conditions.KonnectEntityProgrammedReasonProgrammed,
ObservedGeneration: createdCert.GetGeneration(),
LastTransitionTime: metav1.Now(),
},
},
}
require.NoError(t, clientNamespaced.Status().Update(ctx, createdCert))

t.Log("Setting up a watch for KongSNI events")
w := setupWatch[configurationv1alpha1.KongSNIList](t, ctx, cl, client.InNamespace(ns.Name))

t.Log("Setting up SDK for creating SNI")
sdk.SNIsSDK.EXPECT().CreateSniWithCertificate(
mock.Anything,
mock.MatchedBy(func(req sdkkonnectops.CreateSniWithCertificateRequest) bool {
return req.ControlPlaneID == cp.Status.ID &&
req.CertificateID == createdCert.GetKonnectID() &&
req.SNIWithoutParents.Name == "test.kong-sni.example.com"
}),
).Return(&sdkkonnectops.CreateSniWithCertificateResponse{
Sni: &sdkkonnectcomp.Sni{
ID: lo.ToPtr("sni-12345"),
},
}, nil)

t.Log("Creating KongSNI")
createdSNI := deploySNIAttachedToCertificate(t, ctx,
clientNamespaced,
"test.kong-sni.example.com", nil,
createdCert,
)

t.Log("Waiting for SNI to be programmed and get Konnect ID")
watchFor(t, ctx, w, watch.Modified, func(s *configurationv1alpha1.KongSNI) bool {
return s.GetKonnectID() == "sni-12345" && lo.ContainsBy(s.Status.Conditions,
func(c metav1.Condition) bool {
return c.Type == "Programmed" && c.Status == metav1.ConditionTrue
})
}, "SNI didn't get Programmed status condition or didn't get the correct (sni-12345) Konnect ID assigned")

t.Log("Set up SDK for SNI update")
sdk.SNIsSDK.EXPECT().UpsertSniWithCertificate(
mock.Anything,
mock.MatchedBy(func(req sdkkonnectops.UpsertSniWithCertificateRequest) bool {
return req.CertificateID == createdCert.GetKonnectID() &&
req.ControlPlaneID == cp.Status.ID &&
req.SNIWithoutParents.Name == "test2.kong-sni.example.com"
}),
).Return(&sdkkonnectops.UpsertSniWithCertificateResponse{}, nil)

t.Log("Patching KongSNI")
sniToPatch := createdSNI.DeepCopy()
sniToPatch.Spec.KongSNIAPISpec.Name = "test2.kong-sni.example.com"
require.NoError(t, clientNamespaced.Patch(ctx, sniToPatch, client.MergeFrom(createdSNI)))

t.Log("Waiting for KongSNI to be updated in the SDK")
assert.EventuallyWithT(t, func(c *assert.CollectT) {
assert.True(c, factory.SDK.SNIsSDK.AssertExpectations(t))
}, waitTime, tickTime)

t.Log("Setting up SDK for deleting SNI")
sdk.SNIsSDK.EXPECT().DeleteSniWithCertificate(
mock.Anything,
sdkkonnectops.DeleteSniWithCertificateRequest{
ControlPlaneID: cp.Status.ID,
CertificateID: createdCert.GetKonnectID(),
SNIID: "sni-12345",
},
).Return(&sdkkonnectops.DeleteSniWithCertificateResponse{}, nil)

t.Log("Deleting KongSNI")
require.NoError(t, clientNamespaced.Delete(ctx, createdSNI))

t.Log("Waiting for SNI to be programmed and got Konnect ID")
watchFor(t, ctx, w, watch.Modified, func(s *configurationv1alpha1.KongSNI) bool {
return s.GetKonnectID() == "sni-12345" && lo.ContainsBy(s.Status.Conditions,
func(c metav1.Condition) bool {
return c.Type == "Programmed" && c.Status == metav1.ConditionTrue
})
}, "SNI didn't get Programmed status condition or didn't get the correct (sni-12345) Konnect ID assigned")

t.Log("Set up SDK for SNI update")
sdk.SNIsSDK.EXPECT().UpsertSniWithCertificate(
mock.Anything,
mock.MatchedBy(func(req sdkkonnectops.UpsertSniWithCertificateRequest) bool {
return req.CertificateID == createdCert.GetKonnectID() &&
req.ControlPlaneID == cp.Status.ID &&
req.SNIWithoutParents.Name == "test2.kong-sni.example.com"
}),
).Return(&sdkkonnectops.UpsertSniWithCertificateResponse{}, nil)

t.Log("Patching KongSNI")
sniToPatch := createdSNI.DeepCopy()
sniToPatch.Spec.KongSNIAPISpec.Name = "test2.kong-sni.example.com"
require.NoError(t, clientNamespaced.Patch(ctx, sniToPatch, client.MergeFrom(createdSNI)))

t.Log("Waiting for KongSNI to be updated in the SDK")
assert.EventuallyWithT(t, func(c *assert.CollectT) {
assert.True(c, factory.SDK.SNIsSDK.AssertExpectations(t))
}, waitTime, tickTime)

t.Log("Setting up SDK for deleting SNI")
sdk.SNIsSDK.EXPECT().DeleteSniWithCertificate(
mock.Anything,
sdkkonnectops.DeleteSniWithCertificateRequest{
ControlPlaneID: cp.Status.ID,
CertificateID: createdCert.GetKonnectID(),
SNIID: "sni-12345",
},
).Return(&sdkkonnectops.DeleteSniWithCertificateResponse{}, nil)

t.Log("Deleting KongSNI")
require.NoError(t, clientNamespaced.Delete(ctx, createdSNI))

t.Log("Waiting for SNI to be deleted in SDK")
assert.EventuallyWithT(t, func(c *assert.CollectT) {
assert.True(c, factory.SDK.SNIsSDK.AssertExpectations(t))
}, waitTime, tickTime)
t.Log("Waiting for SNI to be deleted in SDK")
assert.EventuallyWithT(t, func(c *assert.CollectT) {
assert.True(c, factory.SDK.SNIsSDK.AssertExpectations(t))
}, waitTime, tickTime)
})
}

0 comments on commit a74d957

Please sign in to comment.