Skip to content

Commit

Permalink
chore: Add tests for test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgough committed Jun 21, 2024
1 parent 4d35296 commit eb59047
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 57 deletions.
49 changes: 0 additions & 49 deletions internal/controller/thanosreceive_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controller

import (
"context"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -29,7 +28,6 @@ import (
"github.com/thanos-community/thanos-operator/internal/pkg/manifests/receive"
"github.com/thanos-community/thanos-operator/test/utils"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -336,50 +334,3 @@ config:

})
})

type expectApiResource string

const (
expectApiResourceDeployment expectApiResource = "Deployment"
expectApiResourceStatefulSet expectApiResource = "StatefulSet"
)

func validateExistenceOfRequiredNamedResources(expectResource expectApiResource, name, ns string) error {
sa := &corev1.ServiceAccount{}
if err := k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: ns,
}, sa); err != nil {
return err
}

svc := &corev1.Service{}
if err := k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: ns,
}, svc); err != nil {
return err
}

switch expectResource {
case expectApiResourceDeployment:
dep := &appsv1.Deployment{}
if err := k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: ns,
}, dep); err != nil {
return err
}
case expectApiResourceStatefulSet:
sts := &appsv1.StatefulSet{}
if err := k8sClient.Get(ctx, types.NamespacedName{
Name: name,
Namespace: ns,
}, sts); err != nil {
return err
}
default:
return fmt.Errorf("unexpected resource type")
}
return nil
}
4 changes: 2 additions & 2 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var _ = Describe("controller", Ordered, func() {

stsName := receive.IngesterNameFromParent(receiveName, hashringName)
Eventually(func() bool {
return utils.VerifyStsReplicasRunning(c, 1, stsName, namespace)
return utils.VerifyStatefulSetReplicasRunning(c, 1, stsName, namespace)
}, time.Minute*5, time.Second*10).Should(BeTrue())
})

Expand Down Expand Up @@ -259,7 +259,7 @@ var _ = Describe("controller", Ordered, func() {
defer cancelFn()

Eventually(func() error {
return utils.RemoteWrite(utils.DefaultRemoteWriteRequest())
return utils.RemoteWrite(utils.DefaultRemoteWriteRequest(), nil, nil)
}, time.Minute*1, time.Second*5).Should(Succeed())

})
Expand Down
47 changes: 46 additions & 1 deletion test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func VerifyServiceAccountExists(c client.Client, name string, namespace string)
return err == nil
}

func VerifyStsReplicasRunning(c client.Client, expect int, name string, namespace string) bool {
func VerifyStatefulSetReplicasRunning(c client.Client, expect int, name string, namespace string) bool {
sts := &appsv1.StatefulSet{}
err := c.Get(context.Background(), client.ObjectKey{
Name: name,
Expand Down Expand Up @@ -264,6 +264,18 @@ func VerifyDeploymentArgs(c client.Client, name string, namespace string, contai
return slices.Contains(deployment.Spec.Template.Spec.Containers[0].Args, containsArg)
}

func VerifyStatefulSetExists(c client.Client, name string, namespace string) bool {
sts := &appsv1.StatefulSet{}
err := c.Get(context.Background(), client.ObjectKey{
Name: name,
Namespace: namespace,
}, sts)
if err != nil {
return false
}
return true
}

func VerifyConfigMapContents(c client.Client, name, namespace, key, expect string) bool {
cm := &corev1.ConfigMap{}
if err := c.Get(context.Background(), types.NamespacedName{
Expand Down Expand Up @@ -414,3 +426,36 @@ func (s *setHeadersTransport) RoundTrip(req *http.Request) (*http.Response, erro
}
return s.RoundTripper.RoundTrip(req)
}

type ExpectApiResource string

const (
ExpectApiResourceDeployment ExpectApiResource = "Deployment"
ExpectApiResourceStatefulSet ExpectApiResource = "StatefulSet"
)

// VerifyExistenceOfRequiredNamedResources checks if the required resources exist in the cluster.
// This is a named Service, ServiceAccount, and either a Deployment or StatefulSet.
func VerifyExistenceOfRequiredNamedResources(c client.Client, expectResource ExpectApiResource, name, ns string) bool {
if VerifyServiceAccountExists(c, name, ns) == false {
return false
}

if VerifyServiceExists(c, name, ns) == false {
return false
}

switch expectResource {
case ExpectApiResourceDeployment:
if VerifyDeploymentExists(c, name, ns) == false {
return false
}
case ExpectApiResourceStatefulSet:
if VerifyStatefulSetExists(c, name, ns) == false {
return false
}
default:
return false
}
return true
}
Loading

0 comments on commit eb59047

Please sign in to comment.