Skip to content

Commit

Permalink
CLOUDP-201374: Fix backup schedule repeatedly updating (#1136)
Browse files Browse the repository at this point in the history
  • Loading branch information
roothorp authored Sep 26, 2023
1 parent d63b7d4 commit 84d669c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/api/v1/custom_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestAtlasCustomRoles_ToAtlas(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.spec.ToAtlas()
if reflect.DeepEqual(got, tt.want) {
if !reflect.DeepEqual(got, tt.want) {
g, _ := json.MarshalIndent(got, "", " ")
w, _ := json.MarshalIndent(tt.want, "", " ")
fmt.Println("GOT", string(g))
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/atlasdeployment/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ func normalizeBackupSchedule(s *mongodbatlas.CloudProviderSnapshotBackupPolicy)
s.Links = nil
s.NextSnapshot = ""
if len(s.Policies) > 0 && len(s.Policies[0].PolicyItems) > 0 {
s.Policies[0].PolicyItems[0].ID = ""
for i := range s.Policies[0].PolicyItems {
s.Policies[0].PolicyItems[i].ID = ""
}
}
s.UpdateSnapshots = nil
}
Expand Down
146 changes: 146 additions & 0 deletions pkg/controller/atlasdeployment/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,149 @@ func Test_backupScheduleManagedByAtlas(t *testing.T) {
assert.False(t, result)
})
}

func Test_backupSchedulesAreEqual(t *testing.T) {
examplePolicy := mongodbatlas.CloudProviderSnapshotBackupPolicy{
ClusterID: "testID",
ClusterName: "testName",
ReferenceHourOfDay: toptr.MakePtr[int64](12),
ReferenceMinuteOfHour: toptr.MakePtr[int64](59),
RestoreWindowDays: toptr.MakePtr[int64](4),
UpdateSnapshots: toptr.MakePtr[bool](false),
NextSnapshot: "test123",
Policies: []mongodbatlas.Policy{
{
ID: "testID",
PolicyItems: []mongodbatlas.PolicyItem{
{
ID: "testID1",
FrequencyInterval: 10,
FrequencyType: "testFreq1",
RetentionUnit: "testRet1",
RetentionValue: 21,
},
{
ID: "testID2",
FrequencyInterval: 20,
FrequencyType: "testFreq2",
RetentionUnit: "testRet2",
RetentionValue: 450,
},
},
},
},
AutoExportEnabled: toptr.MakePtr[bool](true),
Export: &mongodbatlas.Export{
ExportBucketID: "testID",
FrequencyType: "testFreq",
},
UseOrgAndGroupNamesInExportPrefix: toptr.MakePtr[bool](false),
Links: []*mongodbatlas.Link{
{
Rel: "abc",
Href: "xyz",
},
},
CopySettings: []mongodbatlas.CopySetting{
{
CloudProvider: toptr.MakePtr[string]("testString"),
RegionName: toptr.MakePtr[string]("testString"),
ReplicationSpecID: toptr.MakePtr[string]("testString"),
ShouldCopyOplogs: toptr.MakePtr[bool](true),
Frequencies: []string{"testString"},
},
},
DeleteCopiedBackups: []mongodbatlas.DeleteCopiedBackup{
{
CloudProvider: toptr.MakePtr[string]("testString"),
RegionName: toptr.MakePtr[string]("testString"),
ReplicationSpecID: toptr.MakePtr[string]("testString"),
},
},
}

t.Run("should return true when backups are both empty", func(t *testing.T) {
res, err := backupSchedulesAreEqual(&mongodbatlas.CloudProviderSnapshotBackupPolicy{}, &mongodbatlas.CloudProviderSnapshotBackupPolicy{})
assert.NoError(t, err)
assert.True(t, res)
})
t.Run("should return true when backups are identical", func(t *testing.T) {
res, err := backupSchedulesAreEqual(&examplePolicy, &examplePolicy)
assert.NoError(t, err)
assert.True(t, res)
})
t.Run("should return true when backups are identical after normalization", func(t *testing.T) {
firstPolicy := &mongodbatlas.CloudProviderSnapshotBackupPolicy{
ClusterID: clusterID,
Links: []*mongodbatlas.Link{
{
Href: "policy1",
Rel: "policy1",
},
},
NextSnapshot: "policy1 NextSnapshot",
Policies: []mongodbatlas.Policy{
{
ID: "policy ID",
PolicyItems: []mongodbatlas.PolicyItem{
{
ID: "policy1 item 1 id",
FrequencyInterval: 1,
FrequencyType: "testFreq1",
RetentionUnit: "testRet1",
RetentionValue: 1,
},
{
ID: "policy 1 item 2 id",
FrequencyInterval: 2,
FrequencyType: "testFreq2",
RetentionUnit: "testRet2",
RetentionValue: 2,
},
},
},
},
}
secondPolicy := &mongodbatlas.CloudProviderSnapshotBackupPolicy{
ClusterID: clusterID,
Links: []*mongodbatlas.Link{
{
Href: "policy2",
Rel: "policy2",
},
},
NextSnapshot: "policy2 NextSnapshot",
Policies: []mongodbatlas.Policy{
{
ID: "policy ID",
PolicyItems: []mongodbatlas.PolicyItem{
{
ID: "policy2 item 1 id",
FrequencyInterval: 1,
FrequencyType: "testFreq1",
RetentionUnit: "testRet1",
RetentionValue: 1,
},
{
ID: "policy 2 item 2 id",
FrequencyInterval: 2,
FrequencyType: "testFreq2",
RetentionUnit: "testRet2",
RetentionValue: 2,
},
},
},
},
}
res, err := backupSchedulesAreEqual(firstPolicy, secondPolicy)
assert.NoError(t, err)
assert.True(t, res)
})
t.Run("should return false when backups differ", func(t *testing.T) {
changedPolicy := examplePolicy
changedPolicy.ClusterName = "different name"
res, err := backupSchedulesAreEqual(&examplePolicy, &changedPolicy)
assert.NoError(t, err)
assert.False(t, res)
})
}

0 comments on commit 84d669c

Please sign in to comment.