Skip to content

Commit

Permalink
Add ServiceBus topic CRUD test
Browse files Browse the repository at this point in the history
  • Loading branch information
Porges committed May 27, 2021
1 parent 21cda86 commit 1268c88
Show file tree
Hide file tree
Showing 6 changed files with 3,124 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/Azure/azure-service-operator/hack/generated/pkg/testcommon"
)

func Test_ServiceBus_Namespace_CRUD(t *testing.T) {
func Test_ServiceBus_Basic_CRUD(t *testing.T) {
t.Parallel()

g := NewGomegaWithT(t)
Expand All @@ -42,24 +42,23 @@ func Test_ServiceBus_Namespace_CRUD(t *testing.T) {
},
}

err = testContext.KubeClient.Create(ctx, namespace)
g.Expect(err).ToNot(HaveOccurred())

// It should be created in Kubernetes
g.Eventually(namespace).Should(testContext.Match.BeProvisioned(ctx))

// Run sub-tests
t.Run("Queue CRUD", func(t *testing.T) {
ServiceBus_Queue_CRUD(t, testContext, namespace.ObjectMeta)
})
// Create
g.Expect(testContext.KubeClient.Create(ctx, namespace)).To(Succeed())
g.Eventually(namespace, remainingTime(t)).Should(testContext.Match.BeProvisioned(ctx))

g.Expect(namespace.Status.Id).ToNot(BeNil())
armId := *namespace.Status.Id

RunParallelSubtests(t,
subtest{
name: "Queue CRUD",
test: func(t *testing.T) { ServiceBus_Queue_CRUD(t, testContext, namespace.ObjectMeta) },
},
)

// Delete
err = testContext.KubeClient.Delete(ctx, namespace)
g.Expect(err).ToNot(HaveOccurred())
g.Eventually(namespace).Should(testContext.Match.BeDeleted(ctx))
g.Expect(testContext.KubeClient.Delete(ctx, namespace)).To(Succeed())
g.Eventually(namespace, remainingTime(t)).Should(testContext.Match.BeDeleted(ctx))

// Ensure that the resource was really deleted in Azure
exists, retryAfter, err := testContext.AzureClient.HeadResource(ctx, armId, "2018-01-01-preview")
Expand All @@ -70,7 +69,6 @@ func Test_ServiceBus_Namespace_CRUD(t *testing.T) {

func ServiceBus_Queue_CRUD(t *testing.T, testContext testcommon.KubePerTestContext, sbNamespace metav1.ObjectMeta) {
ctx := context.Background()

g := NewGomegaWithT(t)

queue := &servicebus.NamespacesQueue{
Expand All @@ -82,17 +80,15 @@ func ServiceBus_Queue_CRUD(t *testing.T, testContext testcommon.KubePerTestConte
}

// Create
err := testContext.KubeClient.Create(ctx, queue)
g.Expect(err).ToNot(HaveOccurred())
g.Eventually(queue).Should(testContext.Match.BeProvisioned(ctx))
g.Expect(testContext.KubeClient.Create(ctx, queue)).To(Succeed())
g.Eventually(queue, remainingTime(t)).Should(testContext.Match.BeProvisioned(ctx))

g.Expect(queue.Status.Id).ToNot(BeNil())

// Just a basic assertion on a property
// a basic assertion on a property
g.Expect(queue.Status.Properties.SizeInBytes).ToNot(BeNil())
g.Expect(*queue.Status.Properties.SizeInBytes).To(Equal(0))

err = testContext.KubeClient.Delete(ctx, queue)
g.Expect(err).ToNot(HaveOccurred())
g.Eventually(queue).Should(testContext.Match.BeDeleted(ctx))
g.Expect(testContext.KubeClient.Delete(ctx, queue)).To(Succeed())
g.Eventually(queue, remainingTime(t)).Should(testContext.Match.BeDeleted(ctx))
}
99 changes: 99 additions & 0 deletions hack/generated/controllers/crd_servicebus_standard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package controllers_test

import (
"context"
"testing"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

servicebus "github.com/Azure/azure-service-operator/hack/generated/_apis/microsoft.servicebus/v1alpha1api20180101preview"
"github.com/Azure/azure-service-operator/hack/generated/pkg/testcommon"
)

func Test_ServiceBus_Standard_CRUD(t *testing.T) {
t.Parallel()

g := NewGomegaWithT(t)
ctx := context.Background()
testContext, err := testContext.ForTest(t)
g.Expect(err).ToNot(HaveOccurred())

rg, err := testContext.CreateNewTestResourceGroup(testcommon.WaitForCreation)
g.Expect(err).ToNot(HaveOccurred())

zoneRedundant := false
namespace := &servicebus.Namespace{
ObjectMeta: testContext.MakeObjectMetaWithName(testContext.Namer.GenerateName("sbstandard")),
Spec: servicebus.Namespaces_Spec{
Location: testContext.AzureRegion,
Owner: testcommon.AsOwner(rg.ObjectMeta),
Sku: &servicebus.SBSku{
Name: servicebus.SBSkuNameStandard,
},
Properties: servicebus.SBNamespaceProperties{
ZoneRedundant: &zoneRedundant,
},
},
}

// Create
g.Expect(testContext.KubeClient.Create(ctx, namespace)).To(Succeed())
g.Eventually(namespace, remainingTime(t)).Should(testContext.Match.BeProvisioned(ctx))

g.Expect(namespace.Status.Id).ToNot(BeNil())
armId := *namespace.Status.Id

RunParallelSubtests(t,
subtest{
name: "Queue CRUD",
test: func(t *testing.T) { ServiceBus_Queue_CRUD(t, testContext, namespace.ObjectMeta) },
},
subtest{
name: "Topic CRUD",
test: func(t *testing.T) { ServiceBus_Topic_CRUD(t, testContext, namespace.ObjectMeta) },
},
)

// Delete
g.Expect(testContext.KubeClient.Delete(ctx, namespace)).To(Succeed())
g.Eventually(namespace, remainingTime(t)).Should(testContext.Match.BeDeleted(ctx))

// Ensure that the resource was really deleted in Azure
exists, retryAfter, err := testContext.AzureClient.HeadResource(ctx, armId, "2018-01-01-preview")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(retryAfter).To(BeZero())
g.Expect(exists).To(BeFalse())
}

// Topics can only be created in Standard or Premium SKUs
func ServiceBus_Topic_CRUD(t *testing.T, testContext testcommon.KubePerTestContext, sbNamespace metav1.ObjectMeta) {
ctx := context.Background()
g := NewGomegaWithT(t)

topic := &servicebus.NamespacesTopic{
ObjectMeta: testContext.MakeObjectMeta("topic"),
Spec: servicebus.NamespacesTopics_Spec{
Location: &testContext.AzureRegion,
Owner: testcommon.AsOwner(sbNamespace),
},
}

// Create
g.Expect(testContext.KubeClient.Create(ctx, topic)).To(Succeed())
g.Eventually(topic, remainingTime(t)).Should(testContext.Match.BeProvisioned(ctx))

g.Expect(topic.Status.Id).ToNot(BeNil())

// a basic assertion on a property
g.Expect(topic.Status.Properties.SizeInBytes).ToNot(BeNil())
g.Expect(*topic.Status.Properties.SizeInBytes).To(Equal(0))

g.Expect(testContext.KubeClient.Delete(ctx, topic)).To(Succeed())
g.Eventually(topic, remainingTime(t)).Should(testContext.Match.BeDeleted(ctx))
}
23 changes: 23 additions & 0 deletions hack/generated/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,26 @@ func remainingTime(t *testing.T) time.Duration {

return DefaultResourceTimeout
}

type subtest struct {
name string
test func(t *testing.T)
}

func RunParallelSubtests(t *testing.T, tests ...subtest) {
// this looks super weird but is correct.
// parallel subtests do not run until their parent test completes,
// and then the parent test does not finish until all its subtests finish.
// so "subtests" will run and complete, then all the subtests will run
// in parallel, and then "subtests" will finish. ¯\_(ツ)_/¯
// See: https://blog.golang.org/subtests#TOC_7.2.
t.Run("subtests", func(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
test.test(t)
})
}
})
}
Loading

0 comments on commit 1268c88

Please sign in to comment.