Skip to content

Commit

Permalink
move certmanager test helper into certmanager module
Browse files Browse the repository at this point in the history
  • Loading branch information
stuggi committed Oct 11, 2023
1 parent e0907a2 commit 4e0cfd0
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 62 deletions.
39 changes: 39 additions & 0 deletions modules/certmanager/test/functional/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package functional

import (
"k8s.io/apimachinery/pkg/types"
)

type Names struct {
Namespace string
IssuerName types.NamespacedName
SelfSignedIssuerName types.NamespacedName
CAName types.NamespacedName
CertName types.NamespacedName
}

func CreateNames(namespace string) Names {
return Names{
Namespace: namespace,
SelfSignedIssuerName: types.NamespacedName{Namespace: namespace, Name: "selfsigned"},
CAName: types.NamespacedName{Namespace: namespace, Name: "ca"},
IssuerName: types.NamespacedName{Namespace: namespace, Name: "issuer"},
CertName: types.NamespacedName{Namespace: namespace, Name: "cert"},
}
}
45 changes: 13 additions & 32 deletions modules/certmanager/test/functional/certmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,15 @@ limitations under the License.
package functional

import (
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/openstack-k8s-operators/lib-common/modules/certmanager"

certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
certmgrmetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("certmanager module", func() {
var namespace string

BeforeEach(func() {
// NOTE(gibi): We need to create a unique namespace for each test run
// as namespaces cannot be deleted in a locally running envtest. See
// https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation
namespace = uuid.New().String()
th.CreateNamespace(namespace)
// We still request the delete of the Namespace to properly cleanup if
// we run the test in an existing cluster.
DeferCleanup(th.DeleteNamespace, namespace)

})

It("creates selfsigned issuer", func() {
i := certmanager.NewIssuer(
certmanager.SelfSignedIssuer(
Expand All @@ -53,7 +36,7 @@ var _ = Describe("certmanager module", func() {

_, err := i.CreateOrPatch(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
issuer := th.GetIssuer(types.NamespacedName{Namespace: namespace, Name: "selfsigned"})
issuer := thCertmanager.GetIssuer(names.SelfSignedIssuerName)
Expect(issuer.Spec.SelfSigned).NotTo(BeNil())
Expect(issuer.Labels["f"]).To(Equal("l"))

Expand All @@ -72,18 +55,17 @@ var _ = Describe("certmanager module", func() {

_, err := i.CreateOrPatch(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
issuer := th.GetIssuer(types.NamespacedName{Namespace: namespace, Name: "ca"})
issuer := thCertmanager.GetIssuer(names.CAName)
Expect(issuer.Spec.CA).NotTo(BeNil())
Expect(issuer.Spec.CA.SecretName).To(Equal("secret"))
Expect(issuer.Labels["f"]).To(Equal("l"))
})

It("deletes issuer", func() {
issuerName := types.NamespacedName{Namespace: namespace, Name: "issuer"}
i := certmanager.NewIssuer(
certmanager.CAIssuer(
issuerName.Name,
issuerName.Namespace,
names.IssuerName.Name,
names.IssuerName.Namespace,
map[string]string{"f": "l"},
"secret",
),
Expand All @@ -92,18 +74,18 @@ var _ = Describe("certmanager module", func() {

_, err := i.CreateOrPatch(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
issuer := th.GetIssuer(issuerName)
issuer := thCertmanager.GetIssuer(names.IssuerName)
Expect(issuer).NotTo(BeNil())
err = i.Delete(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
th.AssertIssuerDoesNotExist(issuerName)
thCertmanager.AssertIssuerDoesNotExist(names.IssuerName)
})

It("creates certificate", func() {
c := certmanager.NewCertificate(
certmanager.Cert(
"cert",
namespace,
names.CertName.Name,
names.CertName.Namespace,
map[string]string{"f": "l"},
certmgrv1.CertificateSpec{
CommonName: "keystone-public-openstack.apps-crc.testing",
Expand All @@ -123,18 +105,17 @@ var _ = Describe("certmanager module", func() {

_, err := c.CreateOrPatch(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
cert := th.GetCert(types.NamespacedName{Namespace: namespace, Name: "cert"})
cert := thCertmanager.GetCert(names.CertName)
Expect(cert.Spec.CommonName).To(Equal("keystone-public-openstack.apps-crc.testing"))
Expect(cert.Spec.SecretName).To(Equal("secret"))
Expect(cert.Labels["f"]).To(Equal("l"))
})

It("deletes certificate", func() {
certName := types.NamespacedName{Namespace: namespace, Name: "cert"}
c := certmanager.NewCertificate(
certmanager.Cert(
certName.Name,
certName.Namespace,
names.CertName.Name,
names.CertName.Namespace,
map[string]string{"f": "l"},
certmgrv1.CertificateSpec{
CommonName: "keystone-public-openstack.apps-crc.testing",
Expand All @@ -154,10 +135,10 @@ var _ = Describe("certmanager module", func() {

_, err := c.CreateOrPatch(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
cert := th.GetCert(certName)
cert := thCertmanager.GetCert(names.CertName)
Expect(cert).NotTo(BeNil())
err = c.Delete(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
th.AssertIssuerDoesNotExist(certName)
thCertmanager.AssertIssuerDoesNotExist(names.CertName)
})
})
55 changes: 37 additions & 18 deletions modules/certmanager/test/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -40,8 +41,9 @@ import (
certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
corev1 "k8s.io/api/core/v1"

certmanager_test "github.com/openstack-k8s-operators/lib-common/modules/certmanager/test/helpers"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
. "github.com/openstack-k8s-operators/lib-common/modules/test/helpers"
common_test "github.com/openstack-k8s-operators/lib-common/modules/test/helpers"
//+kubebuilder:scaffold:imports
)

Expand All @@ -59,14 +61,17 @@ const (
)

var (
cfg *rest.Config
cClient client.Client
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
logger logr.Logger
h *helper.Helper
th *TestHelper
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
logger logr.Logger
h *helper.Helper
th *common_test.TestHelper
thCertmanager *certmanager_test.TestHelper
namespace string
names Names
)

func TestCommon(t *testing.T) {
Expand All @@ -90,6 +95,7 @@ var _ = BeforeSuite(func() {
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "test", "openshift_crds", "cert-manager", "v1"),
},

ErrorIfCRDPathMissing: true,
}
var err error
Expand All @@ -107,23 +113,23 @@ var _ = BeforeSuite(func() {

logger = ctrl.Log.WithName("---Test---")

cClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(cClient).NotTo(BeNil())

client, err := kubernetes.NewForConfig(cfg)
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(client).NotTo(BeNil())

th = NewTestHelper(ctx, cClient, timeout, interval, logger)
Expect(k8sClient).NotTo(BeNil())
th = common_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger)
Expect(th).NotTo(BeNil())
thCertmanager = certmanager_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger)
Expect(thCertmanager).NotTo(BeNil())

kclient, err := kubernetes.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred(), "failed to create kclient")

// NOTE(gibi): helper.Helper needs an object that is being reconciled
// we are not really doing reconciliation in this test but still we need to
// provide a valid object. It is used as controller reference for certain
// objects created in the test. So we provide a simple one, a Namespace.
genericObject := th.CreateNamespace("generic-object")
h, err = helper.NewHelper(genericObject, cClient, client, testEnv.Scheme, ctrl.Log)
h, err = helper.NewHelper(genericObject, k8sClient, kclient, testEnv.Scheme, ctrl.Log)
Expect(err).NotTo(HaveOccurred())
Expect(h).NotTo(BeNil())

Expand All @@ -139,3 +145,16 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})

var _ = BeforeEach(func() {
// NOTE(gibi): We need to create a unique namespace for each test run
// as namespaces cannot be deleted in a locally running envtest. See
// https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation
namespace = uuid.New().String()
th.CreateNamespace(namespace)
// We still request the delete of the Namespace to properly cleanup if
// we run the test in an existing cluster.
DeferCleanup(th.DeleteNamespace, namespace)

names = CreateNames(namespace)
})
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,39 @@ limitations under the License.
package helpers

import (
"context"
"time"

certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/onsi/gomega"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

base "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers"
)

// TestHelper is a collection of helpers for testing operators. It extends the
// generic TestHelper from modules/test.
type TestHelper struct {
*base.TestHelper
}

// NewTestHelper returns a TestHelper
func NewTestHelper(
ctx context.Context,
k8sClient client.Client,
timeout time.Duration,
interval time.Duration,
logger logr.Logger,
) *TestHelper {
helper := &TestHelper{}
helper.TestHelper = base.NewTestHelper(ctx, k8sClient, timeout, interval, logger)
return helper
}

// GetIssuer waits for and retrieves a Issuer resource from the Kubernetes cluster
//
// Example:
Expand Down
9 changes: 2 additions & 7 deletions modules/test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ module github.com/openstack-k8s-operators/lib-common/modules/test
go 1.19

require (
github.com/cert-manager/cert-manager v1.11.5
github.com/go-logr/logr v1.2.4
github.com/onsi/gomega v1.28.0
github.com/openstack-k8s-operators/lib-common/modules/common v0.1.1-0.20230919113507-d74c2f31d216
golang.org/x/mod v0.12.0
k8s.io/apimachinery v0.26.9
sigs.k8s.io/controller-runtime v0.14.6
)

require (
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 // indirect
sigs.k8s.io/gateway-api v0.6.0 // indirect
)
require github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 // indirect

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down Expand Up @@ -50,7 +45,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.26.9 // indirect
k8s.io/apiextensions-apiserver v0.26.9 // indirect
k8s.io/apimachinery v0.26.9 // indirect
k8s.io/client-go v0.26.9 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
Expand Down
5 changes: 0 additions & 5 deletions modules/test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cert-manager/cert-manager v1.11.5 h1:K2LurvwIE4hIhODQZnkOW6ljYe3lVMAliS/to+gI05o=
github.com/cert-manager/cert-manager v1.11.5/go.mod h1:zNOyoTEwdn9Rtj5Or2pjBY1Bqwtw4vBElP2fKSP8/g8=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down Expand Up @@ -270,7 +268,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
k8s.io/api v0.26.9 h1:s8Y+G1u2JM55b90+Yo2RVb3PGT/hkWNVPN4idPERxJg=
k8s.io/api v0.26.9/go.mod h1:W/W4fEWRVzPD36820LlVUQfNBiSbiq0VPWRFJKwzmUg=
k8s.io/apiextensions-apiserver v0.26.9 h1:aJqWRuBj9i9J6tIDniqUDYM5QCRajTKXK/GO+zEccGQ=
k8s.io/apiextensions-apiserver v0.26.9/go.mod h1:L1uysxOP2kC1vkZTlHGUlUl5WSpa7e4GHJmGEZY7yLg=
k8s.io/apimachinery v0.26.9 h1:5yAV9cFR7Z4gIorKcAjWnx4uxtxiFsERwq4Pvmx0CCg=
k8s.io/apimachinery v0.26.9/go.mod h1:qYzLkrQ9lhrZRh0jNKo2cfvf/R1/kQONnSiyB7NUJU0=
k8s.io/client-go v0.26.9 h1:TGWi/6guEjIgT0Hg871Gsmx0qFuoGyGFjlFedrk7It0=
Expand All @@ -283,8 +280,6 @@ k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSn
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/controller-runtime v0.14.6 h1:oxstGVvXGNnMvY7TAESYk+lzr6S3V5VFxQ6d92KcwQA=
sigs.k8s.io/controller-runtime v0.14.6/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0=
sigs.k8s.io/gateway-api v0.6.0 h1:v2FqrN2ROWZLrSnI2o91taHR8Sj3s+Eh3QU7gLNWIqA=
sigs.k8s.io/gateway-api v0.6.0/go.mod h1:EYJT+jlPWTeNskjV0JTki/03WX1cyAnBhwBJfYHpV/0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
Expand Down

0 comments on commit 4e0cfd0

Please sign in to comment.