Skip to content

Commit

Permalink
Adjust CRUD operator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKalke committed Apr 30, 2024
1 parent 16b88af commit 4c69b2a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 69 deletions.
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
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.1
github.com/onsi/gomega v1.32.0
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-20210720184732-4bb14d4b1be1 // 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
40 changes: 7 additions & 33 deletions tests/operator/dockerregistry/deployment/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ 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 {
func VerifyDockerregistryDeployment(testutils *utils.TestUtils) error {
var deploy appsv1.Deployment
objectKey := client.ObjectKey{
Name: testutils.CtrlDeployName,
Name: testutils.DockerregistryDeployName,
Namespace: testutils.Namespace,
}

Expand All @@ -22,37 +19,14 @@ func VerifyCtrlMngrEnvs(testutils *utils.TestUtils, dockerRegistry *v1alpha1.Doc
return err
}

return verifyDeployEnvs(&deploy, dockerRegistry)
return verifyDeployReadiness(&deploy)
}

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
}
func verifyDeployReadiness(deploy *appsv1.Deployment) error {

for _, env := range in.Env {
if env.Name == expected.Name {
// return true if value is the same
return env.Value == expected.Value
}
if deploy.Status.Replicas != 0 && deploy.Status.Replicas == deploy.Status.ReadyReplicas {
return nil
}

return false
return fmt.Errorf("dockerregistry replicas ready '%s' in total '%s'", deploy.Status.ReadyReplicas, deploy.Status.Replicas)
}
27 changes: 1 addition & 26 deletions tests/operator/dockerregistry/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,10 @@ func Verify(utils *utils.TestUtils) error {
return err
}

if err := verifyStatus(&dockerRegistry); err != nil {
if err := deployment.VerifyDockerregistryDeployment(utils); 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
}

Expand Down
6 changes: 3 additions & 3 deletions tests/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func main() {
Client: client,
Logger: log,

Name: "default-test",
CtrlDeployName: "serverless-ctrl-mngr",
RegistryName: "dockerregistry-docker-registry",
Name: "default-test",
DockerregistryDeployName: "internal-docker-registry",
RegistryName: "dockerregistry-docker-registry",
UpdateSpec: v1alpha1.DockerRegistrySpec{
HealthzLivenessTimeout: "20",
},
Expand Down
10 changes: 5 additions & 5 deletions tests/operator/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type TestUtils struct {
Logger *zap.SugaredLogger
Client client.Client

Namespace string
Name string
CtrlDeployName string
RegistryName string
UpdateSpec v1alpha1.DockerRegistrySpec
Namespace string
Name string
DockerregistryDeployName string
RegistryName string
UpdateSpec v1alpha1.DockerRegistrySpec
}

0 comments on commit 4c69b2a

Please sign in to comment.