Skip to content

Commit

Permalink
api: add test to cover multiple certificates with same issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Sep 13, 2024
1 parent f5fe425 commit e15eff2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/rpaas/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func (m *k8sRpaasManager) UpdateCertManagerRequest(ctx context.Context, instance
instance.Spec.DynamicCertificates.CertManager = nil
}

if index, found := findCertManagerRequestByName(instance, in.Name); found {
if index, found := findCertManagerRequestByName(instance, in.Name); found && in.Name != "" {
instance.Spec.DynamicCertificates.CertManagerRequests[index] = newRequest
} else if index, found := findCertManagerRequestByIssuer(instance, in.Issuer); found {
} else if index, found := findCertManagerRequestByIssuer(instance, in.Issuer); found && in.Name == "" {
instance.Spec.DynamicCertificates.CertManagerRequests[index] = newRequest
} else {
instance.Spec.DynamicCertificates.CertManagerRequests = append(instance.Spec.DynamicCertificates.CertManagerRequests, newRequest)
Expand Down
61 changes: 61 additions & 0 deletions internal/pkg/rpaas/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,67 @@ func Test_k8sRpaasManager_UpdateCertManagerRequest(t *testing.T) {
}
}

func Test_k8sRpaasManager_UpdateCertManagerRequestWithManyCertificates(t *testing.T) {
cfg := config.RpaasConfig{
EnableCertManager: true,
}

oldCfg := config.Get()
config.Set(cfg)
defer func() { config.Set(oldCfg) }()

instance := &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-1",
Namespace: "rpaasv2",
},
}

issuer := &cmv1.ClusterIssuer{
ObjectMeta: metav1.ObjectMeta{
Name: "default-issuer",
Annotations: map[string]string{
allowedDNSZonesAnnotation: "example.com,example.org",
},
},
}

client := fake.NewClientBuilder().
WithScheme(rpaasruntime.NewScheme()).
WithRuntimeObjects(instance, issuer).
Build()

manager := &k8sRpaasManager{cli: client}

err := manager.UpdateCertManagerRequest(context.TODO(), instance.Name, clientTypes.CertManager{
Issuer: "default-issuer",
Name: "my-instance-1.example.com",
DNSNames: []string{"my-instance-1.example.com"},
})

require.NoError(t, err)

err = manager.UpdateCertManagerRequest(context.TODO(), instance.Name, clientTypes.CertManager{
Issuer: "default-issuer",
Name: "my-instance-2.example.com",
DNSNames: []string{"my-instance-2.example.com"},
})
require.NoError(t, err)

updatedInstance := &v1alpha1.RpaasInstance{}
err = client.Get(context.TODO(), types.NamespacedName{
Name: "my-instance-1",
Namespace: "rpaasv2",
}, updatedInstance)
require.NoError(t, err)

assert.Len(t, updatedInstance.Spec.DynamicCertificates.CertManagerRequests, 2)
assert.Equal(t, []v1alpha1.CertManager{
{Name: "my-instance-1.example.com", Issuer: "default-issuer", DNSNames: []string{"my-instance-1.example.com"}},
{Name: "my-instance-2.example.com", Issuer: "default-issuer", DNSNames: []string{"my-instance-2.example.com"}},
}, updatedInstance.Spec.DynamicCertificates.CertManagerRequests)
}

func Test_k8sRpaasManager_DeleteCertManagerRequestByIssuer(t *testing.T) {
resources := []runtime.Object{
&v1alpha1.RpaasInstance{
Expand Down

0 comments on commit e15eff2

Please sign in to comment.