-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: before suite setup and after suite cleanup
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
1 parent
3e00b44
commit 10709c7
Showing
11 changed files
with
709 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.