Skip to content

Commit

Permalink
Merge pull request #365 from stuggi/move_certmanager_test_helpers
Browse files Browse the repository at this point in the history
move certmanager test helper into certmanager module
  • Loading branch information
stuggi authored Oct 11, 2023
2 parents 7fd4e4c + d04ab52 commit e8a0540
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 404 deletions.
1 change: 0 additions & 1 deletion modules/certmanager/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.28.0
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20231009135225-e0907a2df5d8
github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20231009135225-e0907a2df5d8
go.uber.org/zap v1.26.0
k8s.io/api v0.26.9
k8s.io/apimachinery v0.26.9
Expand Down
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 := th.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 := th.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 := th.GetIssuer(names.IssuerName)
Expect(issuer).NotTo(BeNil())
err = i.Delete(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
th.AssertIssuerDoesNotExist(issuerName)
th.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 := th.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 := th.GetCert(names.CertName)
Expect(cert).NotTo(BeNil())
err = c.Delete(ctx, h)
Expect(err).ShouldNot(HaveOccurred())
th.AssertIssuerDoesNotExist(certName)
th.AssertIssuerDoesNotExist(names.CertName)
})
})
51 changes: 33 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,8 @@ 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"
//+kubebuilder:scaffold:imports
)

Expand All @@ -59,14 +60,16 @@ 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 *certmanager_test.TestHelper
namespace string
names Names
)

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

ErrorIfCRDPathMissing: true,
}
var err error
Expand All @@ -107,23 +111,21 @@ 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 = certmanager_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger)
Expect(th).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 +141,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
2 changes: 1 addition & 1 deletion modules/test/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetCRDDirFromModule(t *testing.T) {
// NOTE(gibi): this is not a good use case as k8s.io does not have
// CRDs stored. But in lib-common we have no go.mod deps on a repo
// that has such CRDs to test on.
path, err := GetCRDDirFromModule("k8s.io/api", "go.mod", "bases")
path, err := GetCRDDirFromModule("k8s.io/api", "../common/go.mod", "bases")
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(path).Should(MatchRegexp("/.*/k8s.io/api@v.*/bases"))
})
Expand Down
51 changes: 6 additions & 45 deletions modules/test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,22 @@ 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/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/onsi/ginkgo/v2 v2.13.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // 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/client-go v0.26.9 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/openstack-k8s-operators/lib-common/modules/common => ../common
Expand Down
Loading

0 comments on commit e8a0540

Please sign in to comment.