Skip to content

Commit

Permalink
test: before suite setup and after suite cleanup
Browse files Browse the repository at this point in the history
Added the test for before suite setup (Installation and deployment of
LVMO) and after suite cleanup (Cleaning up of cluster).

Signed-off-by: riya-singhal31 <riyasinghalji@gmail.com>
  • Loading branch information
riya-singhal31 committed Mar 31, 2022
1 parent 3e00b44 commit 10709c7
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ vet: ## Run go vet against code.
go vet ./...

test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -cover `go list ./... | grep -v "e2e"`


OPERATOR_NAMESPACE ?= openshift-storage
Expand Down Expand Up @@ -302,3 +302,14 @@ catalog-push: ## Push a catalog image.
lvm-must-gather:
@echo "Building the lvm-must-gather image"
$(IMAGE_BUILD_CMD) build -f must-gather/Dockerfile -t "${MUST_GATHER_FULL_IMAGE_NAME}" must-gather/

# Variables required to run and build LVM end to end tests.
LVM_OPERATOR_INSTALL ?= true
LVM_OPERATOR_UNINSTALL ?= true

# Build and run lvm tests.
e2e-test:
@echo "build and run e2e tests"
cd e2e/lvm && ginkgo build
cd e2e/lvm && ./lvm.test --lvm-catalog-image=$(CATALOG_IMG) --lvm-subscription-channel=$(CHANNELS) --lvm-operator-install=$(LVM_OPERATOR_INSTALL) --lvm-operator-uninstall=$(LVM_OPERATOR_UNINSTALL)

12 changes: 12 additions & 0 deletions e2e/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package e2e

import (
"fmt"

"github.com/onsi/ginkgo/v2"
)

//nolint:errcheck
func debug(msg string, args ...interface{}) {
ginkgo.GinkgoWriter.Write([]byte(fmt.Sprintf(msg, args...)))
}
39 changes: 39 additions & 0 deletions e2e/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package e2e

import (
"flag"
"fmt"

deploymanager "github.com/red-hat-storage/lvm-operator/pkg/deploymanager"
)

// TestNamespace is the namespace we run all the tests in.
const TestNamespace = "lvm-endtoendtest"

var lvmOperatorInstall bool
var lvmOperatorUninstall bool

// LVMCatalogSourceImage is the LVM CatalogSource container image to use in the deployment
var LvmCatalogSourceImage string

// LvmSubscriptionChannel is the name of the lvm subscription channel
var LvmSubscriptionChannel string

// DeployManager is the suite global DeployManager
var DeployManagerObj *deploymanager.DeployManager

// SuiteFailed indicates whether any test in the current suite has failed
var SuiteFailed = false

func init() {
flag.StringVar(&LvmCatalogSourceImage, "lvm-catalog-image", "", "The LVM CatalogSource container image to use in the deployment")
flag.StringVar(&LvmSubscriptionChannel, "lvm-subscription-channel", "", "The subscription channel to revise updates from")
flag.BoolVar(&lvmOperatorInstall, "lvm-operator-install", true, "Install the LVM operator before starting tests")
flag.BoolVar(&lvmOperatorUninstall, "lvm-operator-uninstall", true, "Uninstall the LVM cluster and operator after test completion")

d, err := deploymanager.NewDeployManager()
if err != nil {
panic(fmt.Sprintf("failed to initialize DeployManager: %v", err))
}
DeployManagerObj = d
}
34 changes: 34 additions & 0 deletions e2e/lvm/lvm_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lvm_test

import (
"flag"
"fmt"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

tests "github.com/red-hat-storage/lvm-operator/e2e"
)

func TestLvm(t *testing.T) {
flag.Parse()
RegisterFailHandler(Fail)
RunSpecs(t, "Lvm Suite")
}

var _ = BeforeSuite(func() {
tests.BeforeTestSuiteSetup()
})

var _ = AfterSuite(func() {
tests.AfterTestSuiteCleanup()
})

var _ = Describe("lvmtest", func() {
Context("Run a dummy test", func() {
It("Should do nothing", func() {
fmt.Println("Do nothing")
})
})
})
47 changes: 47 additions & 0 deletions e2e/setup_teardown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package e2e

import "github.com/onsi/gomega"

// BeforeTestSuiteSetup is the function called to initialize the test environment.
func BeforeTestSuiteSetup() {

SuiteFailed = true
if lvmOperatorInstall {
debug("BeforeTestSuite: deploying LVM Operator\n")
err := DeployManagerObj.DeployLVMWithOLM(LvmCatalogSourceImage, LvmSubscriptionChannel)
gomega.Expect(err).To(gomega.BeNil())
}

debug("BeforeTestSuite: starting LVM Cluster\n")
err := DeployManagerObj.StartLVMCluster()
gomega.Expect(err).To(gomega.BeNil())

debug("BeforeTestSuite: creating Namespace %s\n", TestNamespace)
err = DeployManagerObj.CreateNamespace(TestNamespace)
gomega.Expect(err).To(gomega.BeNil())

SuiteFailed = false

debug("------------------------------\n")

}

// AfterTestSuiteCleanup is the function called to tear down the test environment.
func AfterTestSuiteCleanup() {

debug("\n------------------------------\n")

debug("AfterTestSuite: deleting Namespace %s\n", TestNamespace)
err := DeployManagerObj.DeleteNamespaceAndWait(TestNamespace)
gomega.Expect(err).To(gomega.BeNil())

if lvmOperatorUninstall {
debug("AfterTestSuite: deleting default LVM CLuster\n")
err := DeployManagerObj.DeleteLVMCluster()
gomega.Expect(err).To(gomega.BeNil())

debug("AfterTestSuite: uninstalling LVM Operator\n")
err = DeployManagerObj.UninstallLVM(LvmCatalogSourceImage, LvmSubscriptionChannel)
gomega.Expect(err).To(gomega.BeNil(), "error uninstalling LVM: %v", err)
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cybozu-go/log v1.6.0 // indirect
github.com/cybozu-go/netutil v1.2.0 // indirect
Expand All @@ -55,12 +56,16 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo/v2 v2.1.3 // indirect
github.com/operator-framework/api v0.14.0 // indirect
github.com/operator-framework/operator-lifecycle-manager v0.20.0 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.28.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.2.1 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
Expand Down Expand Up @@ -284,6 +287,7 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v0.0.0-20161128191214-064e2069ce9c/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -425,6 +429,8 @@ github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
Expand All @@ -440,6 +446,10 @@ github.com/openshift/build-machinery-go v0.0.0-20210712174854-1bb7fd1518d3/go.mo
github.com/openshift/client-go v0.0.0-20210831095141-e19a065e79f7 h1:iKVU5Tga76kiCWpq9giPi0TfI/gZcFoYb7/x+1SkgwM=
github.com/openshift/client-go v0.0.0-20210831095141-e19a065e79f7/go.mod h1:D6P8RkJzwdkBExQdYUnkWcePMLBiTeCCr8eQIQ7y8Dk=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/operator-framework/api v0.14.0 h1:5nk8fQL8l+dDxi11hZi0T7nqhhoIQLn+qL2DhMEGnoE=
github.com/operator-framework/api v0.14.0/go.mod h1:r/erkmp9Kc1Al4dnxmRkJYc0uCtD5FohN9VuJ5nTxz0=
github.com/operator-framework/operator-lifecycle-manager v0.20.0 h1:h8SPePMO492krrRnamt5AepqD4nSWb3RRZdvZdN8x6I=
github.com/operator-framework/operator-lifecycle-manager v0.20.0/go.mod h1:sml7etyu98h87eikzA6IKay6BRCzagkwYdcbuisdBTk=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
Expand Down Expand Up @@ -496,6 +506,7 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand Down
39 changes: 39 additions & 0 deletions pkg/deploymanager/LVMCluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package deploymanager

import (
"context"

v1alpha1 "github.com/red-hat-storage/lvm-operator/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (t *DeployManager) GenerateLVMCluster() *v1alpha1.LVMCluster {
lvmClusterRes := &v1alpha1.LVMCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "lvmcluster-sample",
Namespace: InstallNamespace,
},
Spec: v1alpha1.LVMClusterSpec{
Storage: v1alpha1.Storage{
DeviceClasses: []v1alpha1.DeviceClass{
{
Name: "vg1",
},
},
},
},
}
return lvmClusterRes
}

// Creates a sample CR.
func (t *DeployManager) StartLVMCluster() error {
lvmClusterRes := t.GenerateLVMCluster()
return t.crClient.Create(context.TODO(), lvmClusterRes)
}

// Deletes a sample CR.
func (t *DeployManager) DeleteLVMCluster() error {
lvmClusterRes := t.GenerateLVMCluster()
return t.crClient.Delete(context.TODO(), lvmClusterRes)
}
63 changes: 63 additions & 0 deletions pkg/deploymanager/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package deploymanager

import (
"context"
"fmt"
"time"

k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilwait "k8s.io/apimachinery/pkg/util/wait"
)

// CreateNamespace creates a namespace in the cluster, ignoring if it already exists.
func (t *DeployManager) CreateNamespace(namespace string) error {
label := make(map[string]string)
// Label required for monitoring this namespace
label["openshift.io/cluster-monitoring"] = "true"
ns := &k8sv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Labels: label,
},
}
_, err := t.k8sClient.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
if err != nil && !errors.IsAlreadyExists(err) {
return err
}
return nil
}

// DeleteNamespaceAndWait deletes a namespace and waits on it to terminate.
func (t *DeployManager) DeleteNamespaceAndWait(namespace string) error {
err := t.k8sClient.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}

lastReason := ""
timeout := 600 * time.Second
interval := 10 * time.Second

// Wait for namespace to terminate
err = utilwait.PollImmediate(interval, timeout, func() (done bool, err error) {
_, err = t.k8sClient.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
lastReason = fmt.Sprintf("Error talking to k8s apiserver: %v", err)
return false, nil
}
if err == nil {
lastReason = "Waiting on namespace to be deleted"
return false, nil
}

return true, nil
})

if err != nil {
return fmt.Errorf("%v: %s", err, lastReason)
}

return nil
}
Loading

0 comments on commit 10709c7

Please sign in to comment.