Skip to content

Commit

Permalink
Add tests for database and distributed architecture
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Tantsur <[email protected]>
  • Loading branch information
dtantsur committed Mar 26, 2024
1 parent 4638c17 commit 86d7f6b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 15 deletions.
2 changes: 2 additions & 0 deletions pkg/ironic/ironic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ func ensureIronicDaemonSet(cctx ControllerContext, ironic *metal3api.Ironic, db
if deploy.ObjectMeta.CreationTimestamp.IsZero() {
cctx.Logger.Info("creating a new ironic daemon set")
}
cctx.Logger.Info("Before", "Deploy", deploy.Spec)
matchLabels := map[string]string{metal3api.IronicOperatorLabel: ironicDeploymentName(ironic)}
deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: matchLabels}
mergePodTemplates(&deploy.Spec.Template, template)
cctx.Logger.Info("After", "Deploy", deploy.Spec)
return controllerutil.SetControllerReference(ironic, deploy, cctx.Scheme)
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/functional.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ make install deploy IMG="${IMG}"

kubectl wait --for=condition=Available --timeout=60s \
-n ironic-standalone-operator-system deployment/ironic-standalone-operator-controller-manager
cd test && go test
cd test && go test -timeout 30m
120 changes: 106 additions & 14 deletions test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -82,6 +83,62 @@ var _ = BeforeSuite(func() {

})

func WaitForIronic(name types.NamespacedName) *metal3api.Ironic {
GinkgoHelper()

ironic := &metal3api.Ironic{}

By("waiting for Ironic deployment")

Eventually(func() bool {
err := k8sClient.Get(ctx, name, ironic)
Expect(err).NotTo(HaveOccurred())

cond := meta.FindStatusCondition(ironic.Status.Conditions, string(metal3api.IronicStatusAvailable))
if cond != nil && cond.Status == metav1.ConditionTrue {
return true
}

GinkgoWriter.Printf("Current status of Ironic: %+v\n", ironic)
return false
}).WithTimeout(10 * time.Minute).WithPolling(10 * time.Second).Should(BeTrue())

return ironic
}

func VerifyIronic(ironic *metal3api.Ironic) {
GinkgoHelper()

By("checking the service")

svc, err := clientset.CoreV1().Services(ironic.Namespace).Get(ctx, ironic.Name, metav1.GetOptions{})
GinkgoWriter.Printf("Ironic service: %+v\n", svc)
Expect(err).NotTo(HaveOccurred())
}

func DeleteAndWait(ironic *metal3api.Ironic) {
GinkgoHelper()

By("deleting Ironic")

name := types.NamespacedName{
Name: ironic.Name,
Namespace: ironic.Namespace,
}
err := k8sClient.Delete(ctx, ironic)
Expect(err).NotTo(HaveOccurred())

Eventually(func() bool {
err := k8sClient.Get(ctx, name, ironic)
if err != nil && k8serrors.IsNotFound(err) {
return true
}
GinkgoWriter.Println("Ironic", name, "not deleted yet, error is", err)
Expect(err).NotTo(HaveOccurred())
return false
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
}

var _ = Describe("Ironic object tests", func() {
var namespace string

Expand All @@ -93,6 +150,15 @@ var _ = Describe("Ironic object tests", func() {
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
_ = clientset.CoreV1().Namespaces().Delete(ctx, namespace, metav1.DeleteOptions{})
Eventually(func() bool {
_, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
return true
}
GinkgoWriter.Println("Namespace", namespace, "not deleted yet, error is", err)
Expect(err).NotTo(HaveOccurred())
return false
}).WithTimeout(2 * time.Minute).WithPolling(5 * time.Second).Should(BeTrue())
})
})

Expand All @@ -102,33 +168,59 @@ var _ = Describe("Ironic object tests", func() {
Namespace: namespace,
}

ironic := metal3api.Ironic{
ironic := &metal3api.Ironic{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
},
}
err := k8sClient.Create(ctx, &ironic)
err := k8sClient.Create(ctx, ironic)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
DeleteAndWait(ironic)
})

var cond *metav1.Condition
for attempt := 0; attempt < 10; attempt += 1 {
err = k8sClient.Get(ctx, name, &ironic)
Expect(err).NotTo(HaveOccurred())
ironic = WaitForIronic(name)
VerifyIronic(ironic)
})

cond = meta.FindStatusCondition(ironic.Status.Conditions, string(metal3api.IronicStatusAvailable))
if cond != nil && cond.Status == metav1.ConditionTrue {
break
}
It("creates distributed Ironic", func() {
name := types.NamespacedName{
Name: "test-ironic",
Namespace: namespace,
}

time.Sleep(5 * time.Second)
ironicDb := &metal3api.IronicDatabase{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-db", name.Name),
Namespace: name.Namespace,
},
}

if cond == nil || cond.Status != metav1.ConditionTrue {
Fail("Ironic deployment never succeded")
err := k8sClient.Create(ctx, ironicDb)
Expect(err).NotTo(HaveOccurred())

ironic := &metal3api.Ironic{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
},
Spec: metal3api.IronicSpec{
DatabaseRef: corev1.LocalObjectReference{
Name: ironicDb.Name,
},
Distributed: true,
},
}
})
err = k8sClient.Create(ctx, ironic)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(func() {
DeleteAndWait(ironic)
})

ironic = WaitForIronic(name)
VerifyIronic(ironic)
})
})

var _ = AfterSuite(func() {
Expand Down

0 comments on commit 86d7f6b

Please sign in to comment.