generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic (CRUD) serverless operations to main test scenario (#898)
- Loading branch information
Showing
7 changed files
with
321 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package serverless | ||
|
||
import ( | ||
"github.com/kyma-project/serverless/components/operator/api/v1alpha1" | ||
"github.com/kyma-project/serverless/tests/operator/utils" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func Create(utils *utils.TestUtils) error { | ||
serverlessObj := fixServerless(utils) | ||
|
||
return utils.Client.Create(utils.Ctx, serverlessObj) | ||
} | ||
|
||
func fixServerless(testUtils *utils.TestUtils) *v1alpha1.Serverless { | ||
return &v1alpha1.Serverless{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: testUtils.ServerlessName, | ||
Namespace: testUtils.Namespace, | ||
}, | ||
Spec: v1alpha1.ServerlessSpec{ | ||
DockerRegistry: &v1alpha1.DockerRegistry{ | ||
EnableInternal: utils.PtrFromVal(false), | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package serverless | ||
|
||
import "github.com/kyma-project/serverless/tests/operator/utils" | ||
|
||
func Delete(utils *utils.TestUtils) error { | ||
serverless := fixServerless(utils) | ||
|
||
return utils.Client.Delete(utils.Ctx, serverless) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package deployment | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kyma-project/serverless/components/operator/api/v1alpha1" | ||
"github.com/kyma-project/serverless/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, serverless *v1alpha1.Serverless) error { | ||
var deploy appsv1.Deployment | ||
objectKey := client.ObjectKey{ | ||
Name: testutils.ServerlessCtrlDeployName, | ||
Namespace: testutils.Namespace, | ||
} | ||
|
||
err := testutils.Client.Get(testutils.Ctx, objectKey, &deploy) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return verifyDeployEnvs(&deploy, serverless) | ||
} | ||
|
||
func verifyDeployEnvs(deploy *appsv1.Deployment, serverless *v1alpha1.Serverless) error { | ||
expectedEnvs := []corev1.EnvVar{ | ||
{ | ||
Name: "APP_FUNCTION_TRACE_COLLECTOR_ENDPOINT", | ||
Value: serverless.Status.TracingEndpoint, | ||
}, | ||
{ | ||
Name: "APP_FUNCTION_PUBLISHER_PROXY_ADDRESS", | ||
Value: serverless.Status.EventingEndpoint, | ||
}, | ||
{ | ||
Name: "APP_FUNCTION_TARGET_CPU_UTILIZATION_PERCENTAGE", | ||
Value: serverless.Status.CPUUtilizationPercentage, | ||
}, | ||
{ | ||
Name: "APP_FUNCTION_REQUEUE_DURATION", | ||
Value: serverless.Status.RequeueDuration, | ||
}, | ||
{ | ||
Name: "APP_FUNCTION_BUILD_EXECUTOR_ARGS", | ||
Value: serverless.Status.BuildExecutorArgs, | ||
}, | ||
{ | ||
Name: "APP_FUNCTION_BUILD_MAX_SIMULTANEOUS_JOBS", | ||
Value: serverless.Status.BuildMaxSimultaneousJobs, | ||
}, | ||
{ | ||
Name: "APP_HEALTHZ_LIVENESS_TIMEOUT", | ||
Value: serverless.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package serverless | ||
|
||
import ( | ||
"github.com/kyma-project/serverless/components/operator/api/v1alpha1" | ||
"github.com/kyma-project/serverless/tests/operator/utils" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func Update(testutils *utils.TestUtils) error { | ||
var serverless v1alpha1.Serverless | ||
objectKey := client.ObjectKey{ | ||
Name: testutils.ServerlessName, | ||
Namespace: testutils.Namespace, | ||
} | ||
|
||
if err := testutils.Client.Get(testutils.Ctx, objectKey, &serverless); err != nil { | ||
return err | ||
} | ||
|
||
serverless.Spec = testutils.ServerlessUpdateSpec | ||
|
||
return testutils.Client.Update(testutils.Ctx, &serverless) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package serverless | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kyma-project/serverless/components/operator/api/v1alpha1" | ||
"github.com/kyma-project/serverless/tests/operator/serverless/deployment" | ||
"github.com/kyma-project/serverless/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 serverless v1alpha1.Serverless | ||
objectKey := client.ObjectKey{ | ||
Name: utils.ServerlessName, | ||
Namespace: utils.Namespace, | ||
} | ||
|
||
if err := utils.Client.Get(utils.Ctx, objectKey, &serverless); err != nil { | ||
return err | ||
} | ||
|
||
if err := verifyState(utils, &serverless); err != nil { | ||
return err | ||
} | ||
|
||
if err := verifyStatus(&serverless); err != nil { | ||
return err | ||
} | ||
|
||
return deployment.VerifyCtrlMngrEnvs(utils, &serverless) | ||
} | ||
|
||
// check if all data from the spec is reflected in the status | ||
func verifyStatus(serverless *v1alpha1.Serverless) error { | ||
status := serverless.Status | ||
spec := serverless.Spec | ||
|
||
if err := isSpecValueReflectedInStatus(spec.TargetCPUUtilizationPercentage, status.CPUUtilizationPercentage); err != nil { | ||
return err | ||
} | ||
|
||
if err := isSpecValueReflectedInStatus(spec.FunctionRequeueDuration, status.RequeueDuration); err != nil { | ||
return err | ||
} | ||
|
||
if err := isSpecValueReflectedInStatus(spec.FunctionBuildExecutorArgs, status.BuildExecutorArgs); err != nil { | ||
return err | ||
} | ||
|
||
if err := isSpecValueReflectedInStatus(spec.FunctionBuildMaxSimultaneousJobs, status.BuildMaxSimultaneousJobs); err != nil { | ||
return err | ||
} | ||
|
||
if err := isSpecValueReflectedInStatus(spec.HealthzLivenessTimeout, status.HealthzLivenessTimeout); err != nil { | ||
return err | ||
} | ||
|
||
if err := isSpecValueReflectedInStatus(spec.DefaultBuildJobPreset, status.DefaultBuildJobPreset); err != nil { | ||
return err | ||
} | ||
|
||
if err := isSpecValueReflectedInStatus(spec.DefaultRuntimePodPreset, status.DefaultRuntimePodPreset); err != nil { | ||
return err | ||
} | ||
|
||
if spec.Eventing != nil { | ||
if err := isSpecValueReflectedInStatus(spec.Eventing.Endpoint, status.EventingEndpoint); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if spec.Tracing != nil { | ||
if err := isSpecValueReflectedInStatus(spec.Tracing.Endpoint, status.TracingEndpoint); 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, serverless *v1alpha1.Serverless) error { | ||
if serverless.Status.State != v1alpha1.StateReady { | ||
return fmt.Errorf("serverless '%s' in '%s' state", utils.ServerlessName, serverless.Status.State) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/avast/retry-go" | ||
) | ||
|
||
func WithRetry(utils *TestUtils, f func(utils *TestUtils) error) error { | ||
return retry.Do( | ||
func() error { | ||
return f(utils) | ||
}, | ||
retry.Delay(1*time.Second), | ||
retry.DelayType(retry.FixedDelay), | ||
retry.Attempts(100), | ||
retry.Context(utils.Ctx), | ||
retry.LastErrorOnly(true), | ||
) | ||
} |