Skip to content

Commit

Permalink
add unit tests for bcp types
Browse files Browse the repository at this point in the history
  • Loading branch information
roothorp committed Jun 6, 2024
1 parent a2fb53a commit 0341cc2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
122 changes: 121 additions & 1 deletion pkg/api/v1/atlasbackupcompliancepolicy_types_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,129 @@
package v1

import "testing"
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
"github.com/stretchr/testify/assert"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestBackupCompliancePolicyToAtlas(t *testing.T) {
t.Run("Can convert Compliance Policy to Atlas", func(t *testing.T) {
in := AtlasBackupCompliancePolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "my-bcp",
Namespace: "test-ns",
Labels: map[string]string{
"test": "label",
},
},
Spec: AtlasBackupCompliancePolicySpec{
AuthorizedEmail: "[email protected]",
AuthorizedUserFirstName: "James",
AuthorizedUserLastName: "Bond",
CopyProtectionEnabled: true,
EncryptionAtRestEnabled: false,
PITEnabled: true,
RestoreWindowDays: 24,
ScheduledPolicyItems: []AtlasBackupPolicyItem{
{
FrequencyType: "monthly",
FrequencyInterval: 2,
RetentionUnit: "months",
RetentionValue: 4,
},
},
OnDemandPolicy: AtlasOnDemandPolicy{
RetentionUnit: "days",
RetentionValue: 14,
},
},
}
out := in.ToAtlas("testProjectID")

want := admin.DataProtectionSettings20231001{
AuthorizedEmail: "[email protected]",
AuthorizedUserFirstName: "James",
AuthorizedUserLastName: "Bond",
CopyProtectionEnabled: pointer.MakePtr(true),
EncryptionAtRestEnabled: pointer.MakePtr(false),
PitEnabled: pointer.MakePtr(true),
ProjectId: pointer.MakePtr("testProjectID"),
RestoreWindowDays: pointer.MakePtr(24),
ScheduledPolicyItems: &[]admin.BackupComplianceScheduledPolicyItem{
{
FrequencyType: "monthly",
FrequencyInterval: 2,
RetentionUnit: "months",
RetentionValue: 4,
},
},
OnDemandPolicyItem: &admin.BackupComplianceOnDemandPolicyItem{
FrequencyInterval: 0,
FrequencyType: "ondemand",
RetentionUnit: "days",
RetentionValue: 14,
},
}

assert.Equal(t, "", cmp.Diff(*out, want))
})
}

func TestBackupCompliancePolicyFromAtlas(t *testing.T) {
t.Run("Can convert Compliance Policy from Atlas", func(t *testing.T) {
in := &admin.DataProtectionSettings20231001{
AuthorizedEmail: "[email protected]",
AuthorizedUserFirstName: "James",
AuthorizedUserLastName: "Bond",
CopyProtectionEnabled: pointer.MakePtr(true),
EncryptionAtRestEnabled: pointer.MakePtr(false),
PitEnabled: pointer.MakePtr(true),
ProjectId: pointer.MakePtr("testProjectID"),
RestoreWindowDays: pointer.MakePtr(24),
ScheduledPolicyItems: &[]admin.BackupComplianceScheduledPolicyItem{
{
FrequencyType: "monthly",
FrequencyInterval: 2,
RetentionUnit: "months",
RetentionValue: 4,
},
},
OnDemandPolicyItem: &admin.BackupComplianceOnDemandPolicyItem{
FrequencyInterval: 0,
FrequencyType: "ondemand",
RetentionUnit: "days",
RetentionValue: 14,
},
}

out := NewBCPFromAtlas(in)

want := AtlasBackupCompliancePolicySpec{
AuthorizedEmail: "[email protected]",
AuthorizedUserFirstName: "James",
AuthorizedUserLastName: "Bond",
CopyProtectionEnabled: true,
EncryptionAtRestEnabled: false,
PITEnabled: true,
RestoreWindowDays: 24,
ScheduledPolicyItems: []AtlasBackupPolicyItem{
{
FrequencyType: "monthly",
FrequencyInterval: 2,
RetentionUnit: "months",
RetentionValue: 4,
},
},
OnDemandPolicy: AtlasOnDemandPolicy{
RetentionUnit: "days",
RetentionValue: 14,
},
}

assert.Equal(t, "", cmp.Diff(*out, want))
})
}
1 change: 1 addition & 0 deletions test/e2e/backup_compliance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var _ = Describe("Backup Compliance Configuration", Label("backup-compliance"),
})
})

//nolint:dupl
AfterEach(func() {
GinkgoWriter.Write([]byte("\n"))
GinkgoWriter.Write([]byte("===============================================\n"))
Expand Down

0 comments on commit 0341cc2

Please sign in to comment.