Skip to content

Commit

Permalink
e2e: adding testcases with recipe
Browse files Browse the repository at this point in the history
Signed-off-by: Annaraya Narasagond <annarayanarasagond@gmail.com>
  • Loading branch information
asn1809 committed Jan 2, 2025
1 parent e6f4395 commit 6ab815a
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 34 deletions.
225 changes: 224 additions & 1 deletion e2e/deployers/discoveredapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@
package deployers

import (
"context"
"fmt"
"os"
"os/exec"

"github.com/ramendr/ramen/e2e/types"
"github.com/ramendr/ramen/e2e/util"
recipe "github.com/ramendr/recipe/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type DiscoveredApp struct{}
const timeout = 300

type DiscoveredApp struct {
IncludeRecipe bool
IncludeHooks bool
}

func (d DiscoveredApp) GetName() string {
if d.IncludeRecipe {
if d.IncludeHooks {
return "disapp-recipe-hooks"
}

return "disapp-recipe"
}

return "disapp"
}

Expand Down Expand Up @@ -68,6 +87,16 @@ func (d DiscoveredApp) Deploy(ctx types.Context) error {

log.Info("Workload deployed")

// recipe needs to be created based on flags
if d.IncludeRecipe {
recipeName := ctx.Name() + "-recipe"
if err := createRecipe(recipeName, appNamespace, d.IncludeHooks); err != nil {
log.Info("recipe creation failed")
}

log.Info("recipe created on both dr clusters")
}

return nil
}

Expand Down Expand Up @@ -96,6 +125,22 @@ func (d DiscoveredApp) Undeploy(ctx types.Context) error {
return err
}

if d.IncludeRecipe {
recipeName := ctx.Name() + "-recipe"

log.Infof("Deleting recipe on cluster %q", drpolicy.Spec.DRClusters[0])

if err := deleteRecipe(util.Ctx.C1.Client, recipeName, appNamespace); err != nil {
return err
}

log.Infof("Deleting recipe on cluster %q", drpolicy.Spec.DRClusters[1])

if err := deleteRecipe(util.Ctx.C2.Client, recipeName, appNamespace); err != nil {
return err
}
}

log.Infof("Deleting namespace %q on cluster %q", appNamespace, drpolicy.Spec.DRClusters[0])

// delete namespace on both clusters
Expand All @@ -117,3 +162,181 @@ func (d DiscoveredApp) Undeploy(ctx types.Context) error {
func (d DiscoveredApp) IsDiscovered() bool {
return true
}

func createRecipe(name, namespace string, includeHooks bool) error {
var recipe *recipe.Recipe
if includeHooks {
recipe = getRecipeWithHooks(name, namespace)
} else {
recipe = getRecipeWithoutHooks(name, namespace)
}

err := util.Ctx.C1.Client.Create(context.Background(), recipe)
if err != nil {
if !errors.IsAlreadyExists(err) {
return err
}

util.Ctx.Log.Info("recipe " + name + " already exists" + " in the cluster " + "C1")
}

err = util.Ctx.C2.Client.Create(context.Background(), recipe)
if err != nil {
if !errors.IsAlreadyExists(err) {
return err
}

util.Ctx.Log.Info("recipe " + name + " already exists" + " in the cluster " + "C2")
}

return nil
}

func getRecipeWithoutHooks(name, namespace string) *recipe.Recipe {
return &recipe.Recipe{
TypeMeta: metav1.TypeMeta{
Kind: "Recipe",
APIVersion: "ramendr.openshift.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: recipe.RecipeSpec{
AppType: "busybox",
Groups: []*recipe.Group{
{
Name: "rg1",
Type: "resource",
BackupRef: "rg1",
IncludedNamespaces: []string{
namespace,
},
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "appname",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"busybox"},
},
},
},
},
},
Workflows: []*recipe.Workflow{
{
Name: "backup",
Sequence: []map[string]string{
{
"group": "rg1",
},
},
},
{
Name: "restore",
Sequence: []map[string]string{
{
"group": "rg1",
},
},
},
},
},
}
}

func getRecipeWithHooks(name, namespace string) *recipe.Recipe {
return &recipe.Recipe{
TypeMeta: metav1.TypeMeta{
Kind: "Recipe",
APIVersion: "ramendr.openshift.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: recipe.RecipeSpec{
AppType: "busybox",
Groups: []*recipe.Group{
{
Name: "rg1",
Type: "resource",
BackupRef: "rg1",
IncludedNamespaces: []string{
namespace,
},
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "appname",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"busybox"},
},
},
},
},
},
Hooks: []*recipe.Hook{
getHookSpec(namespace, "backup"),
getHookSpec(namespace, "restore"),
},
Workflows: []*recipe.Workflow{
{
Name: "backup",
Sequence: []map[string]string{
{
"hook": "backup/check-replicas",
},
{
"group": "rg1",
},
},
},
{
Name: "restore",
Sequence: []map[string]string{
{
"group": "rg1",
},
{
"hook": "restore/check-replicas",
},
},
},
},
},
}
}

func getHookSpec(namespace, hookType string) *recipe.Hook {
return &recipe.Hook{
Name: hookType,
Type: "check",
Namespace: namespace,
NameSelector: "busybox",
SelectResource: "deployment",
Timeout: timeout,
Chks: []*recipe.Check{
{
Name: "check-replicas",
Condition: "{$.spec.replicas} == {$.status.readyReplicas}",
},
},
}
}

func deleteRecipe(client client.Client, name, namespace string) error {
r := &recipe.Recipe{}
key := k8stypes.NamespacedName{Namespace: namespace, Name: name}

err := client.Get(context.Background(), key, r)
if err != nil {
if !errors.IsNotFound(err) {
return err
}

return nil
}

return client.Delete(context.Background(), r)
}
11 changes: 11 additions & 0 deletions e2e/dractions/discovered.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func EnableProtectionDiscoveredApps(ctx types.Context) error {

drpc := generateDRPCDiscoveredApps(
name, managementNamespace, clusterName, drPolicyName, placementName, appname, appNamespace)

if v, ok := ctx.Deployer().(deployers.DiscoveredApp); ok {
if v.IncludeRecipe {
recipeName := name + "-recipe"
drpc.Spec.KubeObjectProtection.RecipeRef = &ramen.RecipeRef{
Namespace: appNamespace,
Name: recipeName,
}
}
}

if err = createDRPC(util.Ctx.Hub.Client, drpc); err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions e2e/exhaustive_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ const (
)

var (
Workloads = []types.Workload{}
subscription = &deployers.Subscription{}
appset = &deployers.ApplicationSet{}
discoveredApps = &deployers.DiscoveredApp{}
Deployers = []types.Deployer{subscription, appset, discoveredApps}
Workloads = []types.Workload{}
subscription = &deployers.Subscription{}
appset = &deployers.ApplicationSet{}
discoveredApps = &deployers.DiscoveredApp{}
discoveredAppsWithoutHook = &deployers.DiscoveredApp{IncludeRecipe: true, IncludeHooks: false}
discoveredAppsWithHook = &deployers.DiscoveredApp{IncludeRecipe: true, IncludeHooks: true}
Deployers = []types.Deployer{
subscription, appset, discoveredApps, discoveredAppsWithoutHook,
discoveredAppsWithHook,
}
)

func generateWorkloads([]types.Workload) {
Expand Down
13 changes: 7 additions & 6 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.7

require (
github.com/ramendr/ramen/api v0.0.0-00010101000000-000000000000
github.com/ramendr/recipe v0.0.0-20241009174526-5cecfd571447
github.com/spf13/viper v1.19.0
go.uber.org/zap v1.27.0
k8s.io/api v0.31.1
Expand All @@ -21,7 +22,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
Expand Down Expand Up @@ -57,22 +58,22 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.31.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240411171206-dc4e619f62f3 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38 // indirect
k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Expand Down
Loading

0 comments on commit 6ab815a

Please sign in to comment.