Skip to content

Commit

Permalink
tests refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Raghavendra Talur <[email protected]>
  • Loading branch information
raghavendra-talur committed Dec 11, 2024
1 parent 3409c8f commit a539220
Showing 1 changed file with 169 additions and 121 deletions.
290 changes: 169 additions & 121 deletions internal/controller/vrg_kubeobjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,158 +14,206 @@ import (

Recipe "github.com/ramendr/recipe/api/v1alpha1"
)
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

var _ = Describe("VRG_KubeObjectProtection", func() {
const namespaceName = "my-ns"
package controllers

Check failure on line 20 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

syntax error: non-declaration statement outside function body

Check failure on line 20 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

expected declaration, found 'package' (typecheck)

Check failure on line 20 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

syntax error: non-declaration statement outside function body

Check failure on line 20 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Unit tests

expected declaration, found 'package'

var hook *Recipe.Hook
var group *Recipe.Group
import (

Check failure on line 22 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

syntax error: imports must appear before other declarations (typecheck)

Check failure on line 22 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

imports must appear before other declarations (typecheck)

Check failure on line 22 in internal/controller/vrg_kubeobjects_test.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

syntax error: imports must appear before other declarations) (typecheck)
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

BeforeEach(func() {
duration := 30

hook = &Recipe.Hook{
Namespace: namespaceName,
Name: "hook-single",
Type: "exec",
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"myapp": "testapp",
},
"github.com/ramendr/ramen/internal/controller/kubeobjects"
Recipe "github.com/ramendr/recipe/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const testNamespaceName = "my-ns"

// testData holds all the test fixtures
type testData struct {
hook *Recipe.Hook
group *Recipe.Group
}

// newTestData creates a new instance of test data with default values
func newTestData() *testData {
duration := 30
essential := new(bool)

hook := &Recipe.Hook{
Namespace: testNamespaceName,
Name: "hook-single",
Type: "exec",
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"myapp": "testapp",
},
},
SinglePodOnly: false,
Ops: []*Recipe.Operation{
{
Name: "checkpoint",
Container: "main",
Timeout: duration,
Command: "bash /scripts/checkpoint.sh",
},
SinglePodOnly: false,
Ops: []*Recipe.Operation{
},
Chks: []*Recipe.Check{},
Essential: essential,
}

group := &Recipe.Group{
Name: "test-group",
BackupRef: "test-backup-ref",
Type: "resource",
IncludedNamespaces: []string{testNamespaceName},
IncludedResourceTypes: []string{"deployment", "replicaset"},
ExcludedResourceTypes: nil,
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Name: "checkpoint",
Container: "main",
Timeout: duration,
Command: "bash /scripts/checkpoint.sh",
Key: "test",
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{"empty-on-backup notin", "ignore-on-backup"},
},
},
Chks: []*Recipe.Check{},
Essential: new(bool),
}

group = &Recipe.Group{
Name: "test-group",
BackupRef: "test-backup-ref",
Type: "resource",
IncludedNamespaces: []string{namespaceName},
IncludedResourceTypes: []string{"deployment", "replicaset"},
ExcludedResourceTypes: nil,
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "test",
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{"empty-on-backup notin", "ignore-on-backup"},
},
}

return &testData{
hook: hook,
group: group,
}
}

// getExpectedHookCaptureSpec returns the expected CaptureSpec for a hook
func getExpectedHookCaptureSpec(hook *Recipe.Hook, opName string) *kubeobjects.CaptureSpec {
return &kubeobjects.CaptureSpec{
Name: hook.Name + "-" + opName,
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: []string{hook.Namespace},
IncludedResources: []string{"pod"},
ExcludedResources: []string{},
Hook: kubeobjects.HookSpec{
Name: hook.Name,
Namespace: hook.Namespace,
Type: hook.Type,
LabelSelector: hook.LabelSelector,
Op: kubeobjects.Operation{
Name: hook.Ops[0].Name,
Command: hook.Ops[0].Command,
Container: hook.Ops[0].Container,
},
},
IsHook: true,
},
}
LabelSelector: hook.LabelSelector,
IncludeClusterResources: new(bool),
},
}
}

// getExpectedHookRecoverSpec returns the expected RecoverSpec for a hook
func getExpectedHookRecoverSpec(hook *Recipe.Hook, opName string) *kubeobjects.RecoverSpec {
return &kubeobjects.RecoverSpec{
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: []string{hook.Namespace},
IncludedResources: []string{"pod"},
ExcludedResources: []string{},
Hook: kubeobjects.HookSpec{
Name: hook.Name,
Type: hook.Type,
Namespace: hook.Namespace,
LabelSelector: hook.LabelSelector,
Op: kubeobjects.Operation{
Name: hook.Ops[0].Name,
Command: hook.Ops[0].Command,
Container: hook.Ops[0].Container,
},
},
IsHook: true,
},
LabelSelector: hook.LabelSelector,
IncludeClusterResources: new(bool),
},
}
}

// getExpectedGroupCaptureSpec returns the expected CaptureSpec for a group
func getExpectedGroupCaptureSpec(group *Recipe.Group) *kubeobjects.CaptureSpec {
return &kubeobjects.CaptureSpec{
Name: group.Name,
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: group.IncludedNamespaces,
IncludedResources: group.IncludedResourceTypes,
ExcludedResources: group.ExcludedResourceTypes,
},
LabelSelector: group.LabelSelector,
IncludeClusterResources: group.IncludeClusterResources,
OrLabelSelectors: []*metav1.LabelSelector{},
},
}
}

// getExpectedGroupRecoverSpec returns the expected RecoverSpec for a group
func getExpectedGroupRecoverSpec(group *Recipe.Group) *kubeobjects.RecoverSpec {
return &kubeobjects.RecoverSpec{
BackupName: group.BackupRef,
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: group.IncludedNamespaces,
IncludedResources: group.IncludedResourceTypes,
ExcludedResources: group.ExcludedResourceTypes,
},
LabelSelector: group.LabelSelector,
IncludeClusterResources: group.IncludeClusterResources,
OrLabelSelectors: []*metav1.LabelSelector{},
},
}
}

var _ = Describe("VRG_KubeObjectProtection", func() {
var td *testData

BeforeEach(func() {
td = newTestData()
})

Context("Conversion", func() {
It("Hook to CaptureSpec", func() {
targetCaptureSpec := &kubeobjects.CaptureSpec{
Name: hook.Name + "-" + hook.Ops[0].Name,
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: []string{namespaceName},
IncludedResources: []string{"pod"},
ExcludedResources: []string{},
Hook: kubeobjects.HookSpec{
Name: hook.Name,
Namespace: namespaceName,
Type: hook.Type,
LabelSelector: hook.LabelSelector,
Op: kubeobjects.Operation{
Name: hook.Ops[0].Name,
Command: hook.Ops[0].Command,
Container: hook.Ops[0].Container,
},
},
IsHook: true,
},
LabelSelector: hook.LabelSelector,
IncludeClusterResources: new(bool),
},
}
converted, err := convertRecipeHookToCaptureSpec(*hook, hook.Ops[0].Name)
expected := getExpectedHookCaptureSpec(td.hook, td.hook.Ops[0].Name)
converted, err := convertRecipeHookToCaptureSpec(*td.hook, td.hook.Ops[0].Name)

Expect(err).To(BeNil())
Expect(converted).To(Equal(targetCaptureSpec))
Expect(converted).To(Equal(expected))
})

It("Hook to RecoverSpec", func() {
targetRecoverSpec := &kubeobjects.RecoverSpec{
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: []string{namespaceName},
IncludedResources: []string{"pod"},
ExcludedResources: []string{},
Hook: kubeobjects.HookSpec{
Name: hook.Name,
Type: hook.Type,
Namespace: namespaceName,
// Timeout: hook.Ops[0].Timeout,
LabelSelector: hook.LabelSelector,
Op: kubeobjects.Operation{
Name: hook.Ops[0].Name,
Command: hook.Ops[0].Command,
Container: hook.Ops[0].Container,
},
},
IsHook: true,
},
LabelSelector: hook.LabelSelector,
IncludeClusterResources: new(bool),
},
}
converted, err := convertRecipeHookToRecoverSpec(*hook, hook.Ops[0].Name)
expected := getExpectedHookRecoverSpec(td.hook, td.hook.Ops[0].Name)
converted, err := convertRecipeHookToRecoverSpec(*td.hook, td.hook.Ops[0].Name)

Expect(err).To(BeNil())
Expect(converted).To(Equal(targetRecoverSpec))
Expect(converted).To(Equal(expected))
})

It("Group to CaptureSpec", func() {
targetCaptureSpec := &kubeobjects.CaptureSpec{
Name: group.Name,
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: group.IncludedNamespaces,
IncludedResources: group.IncludedResourceTypes,
ExcludedResources: group.ExcludedResourceTypes,
},
LabelSelector: group.LabelSelector,
IncludeClusterResources: group.IncludeClusterResources,
OrLabelSelectors: []*metav1.LabelSelector{},
},
}
converted, err := convertRecipeGroupToCaptureSpec(*group)
expected := getExpectedGroupCaptureSpec(td.group)
converted, err := convertRecipeGroupToCaptureSpec(*td.group)

Expect(err).To(BeNil())
Expect(converted).To(Equal(targetCaptureSpec))
Expect(converted).To(Equal(expected))
})

It("Group to RecoverSpec", func() {
targetRecoverSpec := &kubeobjects.RecoverSpec{
BackupName: group.BackupRef,
Spec: kubeobjects.Spec{
KubeResourcesSpec: kubeobjects.KubeResourcesSpec{
IncludedNamespaces: group.IncludedNamespaces,
IncludedResources: group.IncludedResourceTypes,
ExcludedResources: group.ExcludedResourceTypes,
},
LabelSelector: group.LabelSelector,
IncludeClusterResources: group.IncludeClusterResources,
OrLabelSelectors: []*metav1.LabelSelector{},
},
}
converted, err := convertRecipeGroupToRecoverSpec(*group)
expected := getExpectedGroupRecoverSpec(td.group)
converted, err := convertRecipeGroupToRecoverSpec(*td.group)

Expect(err).To(BeNil())
Expect(converted).To(Equal(targetRecoverSpec))
Expect(converted).To(Equal(expected))
})
})
})

0 comments on commit a539220

Please sign in to comment.