Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integration tests by ensuring necessary pods are created #839

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/integration/controllers/etcd/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ var _ = Describe("Etcd Controller", func() {

testutils.SetStatefulSetPodsUpdated(sts)
testutils.SetStatefulSetReady(sts)
testutils.CreateStsPods(ctx, k8sClient, sts)
DeferCleanup(func() {
testutils.DeleteStsPods(ctx, k8sClient, sts)
})

err = k8sClient.Status().Update(ctx, sts)
Expect(err).NotTo(HaveOccurred())

Expand Down
28 changes: 27 additions & 1 deletion test/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
package utils

import (
"os"

. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
gomegatypes "github.com/onsi/gomega/types"
"k8s.io/apimachinery/pkg/api/resource"
"os"
)

func MatchFinalizer(finalizer string) gomegatypes.GomegaMatcher {
Expand Down Expand Up @@ -58,3 +59,28 @@ func SwitchDirectory(path string) func() {
}
}
}

// mergeStringMaps merges the content of the newMaps with the oldMap. If a key already exists then
// it gets overwritten by the last value with the same key.
func mergeStringMaps(oldMap map[string]string, newMaps ...map[string]string) map[string]string {
var out map[string]string

if oldMap != nil {
out = make(map[string]string)
}
for k, v := range oldMap {
out[k] = v
}

for _, newMap := range newMaps {
if newMap != nil && out == nil {
out = make(map[string]string)
}

for k, v := range newMap {
out[k] = v
}
}

return out
}
64 changes: 64 additions & 0 deletions test/utils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package utils
import (
"context"
"fmt"
"strconv"

druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -128,3 +130,65 @@ func CreateStatefulSet(name, namespace string, etcdUID types.UID, replicas int32
},
}
}

func CreateStsPods(ctx context.Context, cl client.Client, sts *appsv1.StatefulSet) {
stsReplicas := *sts.Spec.Replicas
for i := 0; i < int(stsReplicas); i++ {
podName := fmt.Sprintf("%s-%d", sts.Name, i)

podLabels := mergeStringMaps(sts.Spec.Template.Labels, map[string]string{
"apps.kubernetes.io/pod-index": strconv.Itoa(i),
"statefulset.kubernetes.io/pod-name": podName,
"controller-revision-hash": sts.Status.UpdateRevision,
})

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: sts.Namespace,
Labels: podLabels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: appsv1.SchemeGroupVersion.Version,
Kind: "StatefulSet",
BlockOwnerDeletion: pointer.Bool(true),
Name: sts.Name,
UID: sts.UID,
},
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "etcd",
Image: "etcd-wrapper:latest",
},
},
},
}

ExpectWithOffset(1, cl.Create(ctx, pod)).To(Succeed())
// Update pod status and set ready condition to true
pod.Status.Conditions = []corev1.PodCondition{
{
Type: corev1.PodReady,
Status: corev1.ConditionTrue,
},
}
ExpectWithOffset(1, cl.Status().Update(ctx, pod)).To(Succeed())
}
}

func DeleteStsPods(ctx context.Context, cl client.Client, sts *appsv1.StatefulSet) {
stsReplicas := *sts.Spec.Replicas
for i := 0; i < int(stsReplicas); i++ {
podName := fmt.Sprintf("%s-%d", sts.Name, i)
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: sts.Namespace,
},
}
ExpectWithOffset(1, cl.Delete(ctx, pod)).To(Succeed())
}
}