Skip to content

Commit

Permalink
refactor tests (#90)
Browse files Browse the repository at this point in the history
* improve tests with parallel execution and rm sleep

* fix the tests to run on kindcluster
  • Loading branch information
priyakumarank authored and Azadehkhojandi committed Aug 21, 2019
1 parent 1e26f3a commit f5fd2cb
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 220 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ all: manager

# Run tests
test: generate fmt vet manifests
TEST_USE_EXISTING_CLUSTER=false go test -v -coverprofile=coverage.txt -covermode count ./api/... ./controllers/... ./resourcemanager/eventhubs/... ./resourcemanager/resourcegroups/... 2>&1 | tee testlogs.txt
TEST_USE_EXISTING_CLUSTER=false go test -v -coverprofile=coverage.txt -covermode count ./api/... ./controllers/... ./pkg/resourcemanager/eventhubs/... ./pkg/resourcemanager/resourcegroups/... 2>&1 | tee testlogs.txt
go-junit-report < testlogs.txt > report.xml
go tool cover -html=coverage.txt -o cover.html
# Run tests with existing cluster
test-existing: generate fmt vet manifests
TEST_USE_EXISTING_CLUSTER=true go test -v -coverprofile=coverage-existing.txt -covermode count ./api/... ./controllers/... ./resourcemanager/eventhubs/... ./resourcemanager/resourcegroups/... 2>&1 | tee testlogs-existing.txt
TEST_USE_EXISTING_CLUSTER=true go test -v -coverprofile=coverage-existing.txt -covermode count ./api/... ./controllers/... ./pkg/resourcemanager/eventhubs/... ./pkg/resourcemanager/resourcegroups/... 2>&1 | tee testlogs-existing.txt
go-junit-report < testlogs-existing.txt > report-existing.xml
go tool cover -html=coverage-existing.txt -o cover-existing.html

Expand Down
1 change: 1 addition & 0 deletions api/v1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var k8sClient client.Client
var testEnv *envtest.Environment

func TestAPIs(t *testing.T) {
t.Parallel()
RegisterFailHandler(Fail)

RunSpecsWithDefaultAndCustomReporters(t,
Expand Down
118 changes: 30 additions & 88 deletions controllers/eventhub_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ package controllers

import (
"context"

azurev1 "github.com/Azure/azure-service-operator/api/v1"
helpers "github.com/Azure/azure-service-operator/pkg/helpers"
resoucegroupsresourcemanager "github.com/Azure/azure-service-operator/pkg/resourcemanager/resourcegroups"

"time"

. "github.com/onsi/ginkgo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"time"

. "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -48,87 +49,45 @@ var _ = Describe("EventHub Controller", func() {
// Avoid adding tests for vanilla CRUD operations because they would
// test Kubernetes API server, which isn't the goal here.
Context("Create and Delete", func() {
if os.Getenv("TEST_USE_EXISTING_CLUSTER") == "true" {
It("should validate eventhubnamespaces exist before creating eventhubs", func() {

resourceGroupName := "t-rg-dev-eh-" + helpers.RandomString(10)
eventhubNamespaceName := "t-ns-dev-eh-" + helpers.RandomString(10)
eventhubName := "t-eh-" + helpers.RandomString(10)

// Create the EventHub object and expect the Reconcile to be created
eventhubInstance := &azurev1.Eventhub{
ObjectMeta: metav1.ObjectMeta{
Name: eventhubName,
Namespace: "default",
},
Spec: azurev1.EventhubSpec{
Location: "westus",
Namespace: eventhubNamespaceName,
ResourceGroup: resourceGroupName,
Properties: azurev1.EventhubProperties{
MessageRetentionInDays: 7,
PartitionCount: 1,
},
},
}

k8sClient.Create(context.Background(), eventhubInstance)

time.Sleep(60 * time.Second)

eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"}

Eventually(func() bool {
_ = k8sClient.Get(context.Background(), eventhubNamespacedName, eventhubInstance)
return eventhubInstance.IsSubmitted()
}, timeout,
).Should(BeFalse())

})
}

It("should create and delete eventhubs", func() {
It("should validate eventhubnamespaces exist before creating eventhubs", func() {

resourceGroupName := "t-rg-dev-eh-" + helpers.RandomString(10)
eventhubNamespaceName := "t-ns-dev-eh-" + helpers.RandomString(10)
eventhubName := "t-eh-" + helpers.RandomString(10)

var err error

// Create the Resourcegroup object and expect the Reconcile to be created
resourceGroupInstance := &azurev1.ResourceGroup{
// Create the EventHub object and expect the Reconcile to be created
eventhubInstance := &azurev1.Eventhub{
ObjectMeta: metav1.ObjectMeta{
Name: resourceGroupName,
Name: eventhubName,
Namespace: "default",
},
Spec: azurev1.ResourceGroupSpec{
Location: "westus",
Spec: azurev1.EventhubSpec{
Location: "westus",
Namespace: "t-ns-dev-eh-" + helpers.RandomString(10),
ResourceGroup: "t-rg-dev-eh-" + helpers.RandomString(10),
Properties: azurev1.EventhubProperties{
MessageRetentionInDays: 7,
PartitionCount: 1,
},
},
}

err = k8sClient.Create(context.Background(), resourceGroupInstance)
Expect(apierrors.IsInvalid(err)).To(Equal(false))
Expect(err).NotTo(HaveOccurred())
k8sClient.Create(context.Background(), eventhubInstance)

time.Sleep(30 * time.Second)
eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"}

// Create the Eventhub namespace object and expect the Reconcile to be created
eventhubNamespaceInstance := &azurev1.EventhubNamespace{
ObjectMeta: metav1.ObjectMeta{
Name: eventhubNamespaceName,
Namespace: "default",
},
Spec: azurev1.EventhubNamespaceSpec{
Location: "westus",
ResourceGroup: resourceGroupName,
},
}
Eventually(func() bool {
_ = k8sClient.Get(context.Background(), eventhubNamespacedName, eventhubInstance)
return eventhubInstance.IsSubmitted()
}, timeout,
).Should(BeFalse())
})

err = k8sClient.Create(context.Background(), eventhubNamespaceInstance)
Expect(apierrors.IsInvalid(err)).To(Equal(false))
Expect(err).NotTo(HaveOccurred())
It("should create and delete eventhubs", func() {

resourceGroupName = "t-rg-dev-controller"
eventhubNamespaceName = "t-ns-dev-eh-ns"
eventhubName := "t-eh-" + helpers.RandomString(10)

time.Sleep(30 * time.Second)
var err error

// Create the EventHub object and expect the Reconcile to be created
eventhubInstance := &azurev1.Eventhub{
Expand All @@ -151,12 +110,6 @@ var _ = Describe("EventHub Controller", func() {
Expect(apierrors.IsInvalid(err)).To(Equal(false))
Expect(err).NotTo(HaveOccurred())

time.Sleep(30 * time.Second)
// The instance object may not be a valid object because it might be missing some required fields.
// Please modify the instance object by adding required fields and then remove the following if statement.
Expect(apierrors.IsInvalid(err)).To(Equal(false))
Expect(err).NotTo(HaveOccurred())

eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"}

Eventually(func() bool {
Expand All @@ -171,8 +124,6 @@ var _ = Describe("EventHub Controller", func() {
}, timeout,
).Should(BeTrue())

time.Sleep(2 * time.Second)

//create secret in k8s
csecret := &v1.Secret{
TypeMeta: metav1.TypeMeta{
Expand All @@ -197,29 +148,20 @@ var _ = Describe("EventHub Controller", func() {
err = k8sClient.Create(context.Background(), csecret)
Expect(err).NotTo(HaveOccurred())

time.Sleep(2 * time.Second)

//get secret from k8s
secret := &v1.Secret{}
err = k8sClient.Get(context.Background(), types.NamespacedName{Name: eventhubName, Namespace: eventhubInstance.Namespace}, secret)
Expect(err).NotTo(HaveOccurred())
Expect(secret.Data).To(Equal(csecret.Data))
Expect(secret.ObjectMeta).To(Equal(csecret.ObjectMeta))

time.Sleep(2 * time.Second)

k8sClient.Delete(context.Background(), eventhubInstance)
Eventually(func() bool {
_ = k8sClient.Get(context.Background(), eventhubNamespacedName, eventhubInstance)
return eventhubInstance.IsBeingDeleted()
}, timeout,
).Should(BeTrue())

time.Sleep(2 * time.Second)

_, err = resoucegroupsresourcemanager.DeleteGroup(context.Background(), resourceGroupName)
Expect(err).NotTo(HaveOccurred())

})
})
})
Loading

0 comments on commit f5fd2cb

Please sign in to comment.