Skip to content

Commit

Permalink
operator crud tests (#16)
Browse files Browse the repository at this point in the history
* move (and adjust) tests/operator from serverless

* Adjust CRUD operator tests

* Add CRUD test to github action

* Add more logs for integration tests

* Adjust operator-verify

* Fix typo in operator-verify

* fix operator kustomization config

* remove detailed tests message

* cleanup

---------

Co-authored-by: MichalKalke <[email protected]>
Co-authored-by: Michał Kalke <[email protected]>
  • Loading branch information
3 people authored May 7, 2024
1 parent 57d31b1 commit ecd1db4
Show file tree
Hide file tree
Showing 18 changed files with 505 additions and 49 deletions.
140 changes: 95 additions & 45 deletions .github/workflows/operator-verify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,100 @@ jobs:
- name: run test
run: make -C components/operator test

operator-integration-test:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.SETUP_GO_VERSION }}
- name: create single cluster
uses: AbsaOSS/k3d-action@4e8b3239042be1dc0aed6c5eb80c13b18200fc79 #v2.4.0
with:
cluster-name: "k3dCluster"
args: >-
--agents 1
--image rancher/k3s:v1.29.3-k3s1
--port 80:80@loadbalancer
--port 443:443@loadbalancer
--wait
- name: run integration test (pre)
if: github.event_name == 'pull_request'
run: |
make -C components/operator deploy
make -C tests/operator test
env:
IMG: europe-docker.pkg.dev/kyma-project/dev/dockerregistry-operator:PR-${{ github.event.number }}
- name: run integration test (post)
if: github.event_name == 'push'
run: |
make -C components/operator deploy
make -C tests/operator test
env:
IMG: europe-docker.pkg.dev/kyma-project/prod/dockerregistry-operator:${{ github.sha }}
- name: collect cluster-info
if: ${{ always() }}
run: make -C tests/operator cluster-info


# upgrade-test: //TODO: change it to run dockerregistry verify (need to be implemented)
# runs-on: ubuntu-latest
# if: github.event_name == 'push'
# steps:
# - uses: actions/checkout@v4
# - name: create single cluster
# uses: AbsaOSS/k3d-action@4e8b3239042be1dc0aed6c5eb80c13b18200fc79 #v2.4.0
# with:
# cluster-name: "k3dCluster"
# args: >-
# --agents 1
# --image rancher/k3s:v1.28.6-k3s1
# --port 80:80@loadbalancer
# --port 443:443@loadbalancer
# --wait
# - name: upgrade test
# run: make -C hack upgrade-test
# env:
# IMG: europe-docker.pkg.dev/kyma-project/prod/dockerregistry-operator:${{ github.sha }}
operator-upgrade-test:
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.SETUP_GO_VERSION }}
- name: create single cluster
uses: AbsaOSS/k3d-action@4e8b3239042be1dc0aed6c5eb80c13b18200fc79 #v2.4.0
with:
cluster-name: "k3dCluster"
args: >-
--agents 1
--image rancher/k3s:v1.29.3-k3s1
--port 80:80@loadbalancer
--port 443:443@loadbalancer
--wait
- name: upgrade test
run: |
make -C components/operator deploy-release
make -C tests/operator test
make -C components/operator deploy
make -C tests/operator test
env:
IMG: europe-docker.pkg.dev/kyma-project/prod/dockerregistry-operator:${{ github.sha }}
- name: collect cluster-info
if: ${{ always() }}
run: make -C tests/operator cluster-info

# gardener-integration-test: //TODO: change it to run dockerregistry verify (need to be implemented)
# if: github.event_name == 'push'
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: save sa
# shell: bash
# run: 'echo "$GARDENER_SA" > /tmp/gardener-sa.yaml'
# env:
# GARDENER_SA: ${{ secrets.GARDENER_SA }}
# - name: provision gardener
# run: make -C hack provision-gardener
# env:
# GARDENER_SECRET_NAME: ${{ secrets.GARDENER_SECRET_NAME }}
# GARDENER_PROJECT: ${{ secrets.GARDENER_PROJECT }}
# GARDENER_SA_PATH: /tmp/gardener-sa.yaml
# - name: run test
# run: make -C hack integration-test-on-cluster
# env:
# IMG: europe-docker.pkg.dev/kyma-project/prod/dockerregistry-operator:${{ github.sha }}
# - name: deprovision gardener
## https://docs.github.com/en/actions/learn-github-actions/expressions#always
# if: ${{ always() }}
# run: make -C hack deprovision-gardener
# env:
# GARDENER_SA_PATH: /tmp/gardener-sa.yaml
gardener-integration-test:
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.SETUP_GO_VERSION }}
- name: save sa
shell: bash
run: 'echo "$GARDENER_SA" > /tmp/gardener-sa.yaml'
env:
GARDENER_SA: ${{ secrets.GARDENER_SA }}
- name: provision gardener
run: make -C hack provision-gardener
env:
GARDENER_SECRET_NAME: ${{ secrets.GARDENER_SECRET_NAME }}
GARDENER_PROJECT: ${{ secrets.GARDENER_PROJECT }}
GARDENER_SA_PATH: /tmp/gardener-sa.yaml
- name: run test
run: |
make -C components/operator deploy
make -C tests/operator test
env:
IMG: europe-docker.pkg.dev/kyma-project/prod/dockerregistry-operator:${{ github.sha }}
- name: deprovision gardener
if: ${{ always() }}
run: make -C hack deprovision-gardener
env:
GARDENER_SA_PATH: /tmp/gardener-sa.yaml
2 changes: 1 addition & 1 deletion components/operator/api/v1alpha1/dockerregistry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Endpoint struct {
// DockerRegistrySpec defines the desired state of DockerRegistry
type DockerRegistrySpec struct {
// Sets the timeout for the Function health check. The default value in seconds is `10`
HealthzLivenessTimeout string `json:"healthzLivenessTimeout,omitempty"`
HealthzLivenessTimeout string `json:"healthzLivenessTimeout,omitempty"` //TODO: probably it was only used by serverless so it could be removed
}

type State string
Expand Down
4 changes: 2 additions & 2 deletions config/operator/base/deployment/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: dockerregistry-operator
newTag: 2d332b272e2a
newName: europe-docker.pkg.dev/kyma-project/prod/dockerregistry-operator
newTag: main
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.21
toolchain go1.21.3

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -72,7 +74,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
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)
}
31 changes: 31 additions & 0 deletions tests/operator/dockerregistry/deployment/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package deployment

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

func VerifyDockerregistryDeployment(testutils *utils.TestUtils) error {
var deploy appsv1.Deployment
objectKey := client.ObjectKey{
Name: testutils.DockerregistryDeployName,
Namespace: testutils.Namespace,
}

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

return verifyDeployReadiness(&deploy)
}

func verifyDeployReadiness(deploy *appsv1.Deployment) error {
if deploy.Status.Replicas != 0 && deploy.Status.Replicas == deploy.Status.ReadyReplicas {
return nil
}

return fmt.Errorf("dockerregistry replicas ready '%d' in total '%d'", deploy.Status.ReadyReplicas, deploy.Status.Replicas)
}
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)
}
49 changes: 49 additions & 0 deletions tests/operator/dockerregistry/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 := deployment.VerifyDockerregistryDeployment(utils); err != nil {
return err
}

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
}
Loading

0 comments on commit ecd1db4

Please sign in to comment.