Skip to content

Commit

Permalink
move (and adjust) tests/operator from serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
anoipm committed Apr 30, 2024
1 parent 39d4875 commit 16b88af
Show file tree
Hide file tree
Showing 14 changed files with 462 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/operator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
PROJECT_ROOT = ../..
include ${PROJECT_ROOT}/hack/help.mk

.PHONY: test
test: ## Run integration test.
go run main.go

.PHONY: cluster-info
cluster-info: ## Print useful info about the cluster regarding integration run
@echo "####################### Operator Logs #######################"
@kubectl logs -n kyma-system -l app.kubernetes.io/component=dockerregistry-operator.kyma-project.io --tail=-1 || true
@echo ""

@echo "####################### DockerRegistry CR #######################"
@kubectl get dockerregistry -A -oyaml || true
@echo ""

@echo "####################### Pods #######################"
@kubectl get pods -A || true
@echo ""
23 changes: 23 additions & 0 deletions tests/operator/dockerregistry/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dockerregistry

import (
"github.com/kyma-project/docker-registry/components/operator/api/v1alpha1"
"github.com/kyma-project/docker-registry/tests/operator/utils"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Create(utils *utils.TestUtils) error {
dockerRegistryObj := fixDockerRegistry(utils)

return utils.Client.Create(utils.Ctx, dockerRegistryObj)
}

func fixDockerRegistry(testUtils *utils.TestUtils) *v1alpha1.DockerRegistry {
return &v1alpha1.DockerRegistry{
ObjectMeta: v1.ObjectMeta{
Name: testUtils.Name,
Namespace: testUtils.Namespace,
},
Spec: v1alpha1.DockerRegistrySpec{},
}
}
9 changes: 9 additions & 0 deletions tests/operator/dockerregistry/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dockerregistry

import "github.com/kyma-project/docker-registry/tests/operator/utils"

func Delete(utils *utils.TestUtils) error {
dockerRegistry := fixDockerRegistry(utils)

return utils.Client.Delete(utils.Ctx, dockerRegistry)
}
58 changes: 58 additions & 0 deletions tests/operator/dockerregistry/deployment/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package deployment

import (
"fmt"

"github.com/kyma-project/docker-registry/components/operator/api/v1alpha1"
"github.com/kyma-project/docker-registry/tests/operator/utils"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func VerifyCtrlMngrEnvs(testutils *utils.TestUtils, dockerRegistry *v1alpha1.DockerRegistry) error {
var deploy appsv1.Deployment
objectKey := client.ObjectKey{
Name: testutils.CtrlDeployName,
Namespace: testutils.Namespace,
}

err := testutils.Client.Get(testutils.Ctx, objectKey, &deploy)
if err != nil {
return err
}

return verifyDeployEnvs(&deploy, dockerRegistry)
}

func verifyDeployEnvs(deploy *appsv1.Deployment, dockerRegistry *v1alpha1.DockerRegistry) error {
expectedEnvs := []corev1.EnvVar{
{
Name: "APP_HEALTHZ_LIVENESS_TIMEOUT",
Value: dockerRegistry.Status.HealthzLivenessTimeout,
},
}
for _, expectedEnv := range expectedEnvs {
if !isEnvReflected(expectedEnv, &deploy.Spec.Template.Spec.Containers[0]) {
return fmt.Errorf("env '%s' with value '%s' not found in deployment", expectedEnv.Name, expectedEnv.Value)
}
}

return nil
}

func isEnvReflected(expected corev1.EnvVar, in *corev1.Container) bool {
if expected.Value == "" {
// return true if value is not overrided
return true
}

for _, env := range in.Env {
if env.Name == expected.Name {
// return true if value is the same
return env.Value == expected.Value
}
}

return false
}
23 changes: 23 additions & 0 deletions tests/operator/dockerregistry/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dockerregistry

import (
"github.com/kyma-project/docker-registry/components/operator/api/v1alpha1"
"github.com/kyma-project/docker-registry/tests/operator/utils"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func Update(testutils *utils.TestUtils) error {
var dockerregistry v1alpha1.DockerRegistry
objectKey := client.ObjectKey{
Name: testutils.Name,
Namespace: testutils.Namespace,
}

if err := testutils.Client.Get(testutils.Ctx, objectKey, &dockerregistry); err != nil {
return err
}

dockerregistry.Spec = testutils.UpdateSpec

return testutils.Client.Update(testutils.Ctx, &dockerregistry)
}
75 changes: 75 additions & 0 deletions tests/operator/dockerregistry/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dockerregistry

import (
"fmt"

"github.com/kyma-project/docker-registry/components/operator/api/v1alpha1"
"github.com/kyma-project/docker-registry/tests/operator/dockerregistry/deployment"
"github.com/kyma-project/docker-registry/tests/operator/utils"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func VerifyDeletion(utils *utils.TestUtils) error {
err := Verify(utils)
if !errors.IsNotFound(err) {
return err
}

return nil
}

func Verify(utils *utils.TestUtils) error {
var dockerRegistry v1alpha1.DockerRegistry
objectKey := client.ObjectKey{
Name: utils.Name,
Namespace: utils.Namespace,
}

if err := utils.Client.Get(utils.Ctx, objectKey, &dockerRegistry); err != nil {
return err
}

if err := verifyState(utils, &dockerRegistry); err != nil {
return err
}

if err := verifyStatus(&dockerRegistry); err != nil {
return err
}

return deployment.VerifyCtrlMngrEnvs(utils, &dockerRegistry)
}

// check if all data from the spec is reflected in the status
func verifyStatus(dockerRegistry *v1alpha1.DockerRegistry) error {
status := dockerRegistry.Status
spec := dockerRegistry.Spec

if err := isSpecValueReflectedInStatus(spec.HealthzLivenessTimeout, status.HealthzLivenessTimeout); err != nil {
return err
}

return nil
}

func isSpecValueReflectedInStatus(specValue string, statusValue string) error {
if specValue == "" {
// value is not set in the spec, so value in the status may be empty or defauled
return nil
}

if specValue != statusValue {
return fmt.Errorf("value '%s' not found in status", specValue)
}

return nil
}

func verifyState(utils *utils.TestUtils, dockerRegistry *v1alpha1.DockerRegistry) error {
if dockerRegistry.Status.State != v1alpha1.StateReady {
return fmt.Errorf("dockerregistry '%s' in '%s' state", utils.Name, dockerRegistry.Status.State)
}

return nil
}
20 changes: 20 additions & 0 deletions tests/operator/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package logger

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func New() (*zap.SugaredLogger, error) {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.TimeKey = "timestamp"
config.Encoding = "json"
config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("Jan 02 15:04:05.000000000")

logger, err := config.Build()
if err != nil {
return nil, err
}

return logger.Sugar(), nil
}
104 changes: 104 additions & 0 deletions tests/operator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"fmt"
"os"
"time"

"github.com/google/uuid"
"github.com/kyma-project/docker-registry/components/operator/api/v1alpha1"
"github.com/kyma-project/docker-registry/tests/operator/dockerregistry"
"github.com/kyma-project/docker-registry/tests/operator/logger"
"github.com/kyma-project/docker-registry/tests/operator/namespace"
"github.com/kyma-project/docker-registry/tests/operator/utils"
)

var (
testTimeout = time.Minute * 10
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()

log, err := logger.New()
if err != nil {
fmt.Printf("%s: %s\n", "unable to setup logger", err)
os.Exit(1)
}

log.Info("Configuring test essentials")
client, err := utils.GetKuberentesClient()
if err != nil {
log.Error(err)
os.Exit(1)
}

log.Info("Start scenario")
err = runScenario(&utils.TestUtils{
Namespace: fmt.Sprintf("dockerregistry-test-%s", uuid.New().String()),
Ctx: ctx,
Client: client,
Logger: log,

Name: "default-test",
CtrlDeployName: "serverless-ctrl-mngr",
RegistryName: "dockerregistry-docker-registry",
UpdateSpec: v1alpha1.DockerRegistrySpec{
HealthzLivenessTimeout: "20",
},
})
if err != nil {
log.Error(err)
os.Exit(1)
}
}

func runScenario(testutil *utils.TestUtils) error {
// create test namespace
testutil.Logger.Infof("Creating namespace '%s'", testutil.Namespace)
if err := namespace.Create(testutil); err != nil {
return err
}

// create Docker Registry
testutil.Logger.Infof("Creating dockerregistry '%s'", testutil.Name)
if err := dockerregistry.Create(testutil); err != nil {
return err
}

// verify Docker Registry
testutil.Logger.Infof("Verifying dockerregistry '%s'", testutil.Name)
if err := utils.WithRetry(testutil, dockerregistry.Verify); err != nil {
return err
}

// update Docker Registry with other spec
testutil.Logger.Infof("Updating dockerregistry '%s'", testutil.Name)
if err := dockerregistry.Update(testutil); err != nil {
return err
}

// verify Docker Registry
testutil.Logger.Infof("Verifying dockerregistry '%s'", testutil.Name)
if err := utils.WithRetry(testutil, dockerregistry.Verify); err != nil {
return err
}

// delete Docker Registry
testutil.Logger.Infof("Deleting dockerregistry '%s'", testutil.Name)
if err := dockerregistry.Delete(testutil); err != nil {
return err
}

// verify Docker Registry deletion
testutil.Logger.Infof("Verifying dockerregistry '%s' deletion", testutil.Name)
if err := utils.WithRetry(testutil, dockerregistry.VerifyDeletion); err != nil {
return err
}

// cleanup
testutil.Logger.Infof("Deleting namespace '%s'", testutil.Namespace)
return namespace.Delete(testutil)
}
21 changes: 21 additions & 0 deletions tests/operator/namespace/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package namespace

import (
"github.com/kyma-project/docker-registry/tests/operator/utils"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Create(utils *utils.TestUtils) error {
namespace := fixNamespace(utils)

return utils.Client.Create(utils.Ctx, namespace)
}

func fixNamespace(utils *utils.TestUtils) *v1.Namespace {
return &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: utils.Namespace,
},
}
}
9 changes: 9 additions & 0 deletions tests/operator/namespace/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package namespace

import "github.com/kyma-project/docker-registry/tests/operator/utils"

func Delete(utils *utils.TestUtils) error {
namespace := fixNamespace(utils)

return utils.Client.Delete(utils.Ctx, namespace)
}
Loading

0 comments on commit 16b88af

Please sign in to comment.