Refactor backup cleanup test code slightly (#4080)

Switch to using functions that always
return a new instance of the struct
in question. Upcoming tests were
having issues with state carrying
over between individual tests.

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [x] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #3217

#### Test Plan

- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2023-08-22 08:29:38 -07:00 committed by GitHub
parent 4ace4bee76
commit 9255013d6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -137,89 +137,113 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
backupTag, _ := makeTagKV(TagBackupCategory) backupTag, _ := makeTagKV(TagBackupCategory)
// Current backup and snapshots. // Current backup and snapshots.
bupCurrent := &backup.Backup{ bupCurrent := func() *backup.Backup {
BaseModel: model.BaseModel{ return &backup.Backup{
ID: model.StableID("current-bup-id"), BaseModel: model.BaseModel{
ModelStoreID: manifest.ID("current-bup-msid"), ID: model.StableID("current-bup-id"),
}, ModelStoreID: manifest.ID("current-bup-msid"),
SnapshotID: "current-snap-msid", },
StreamStoreID: "current-deets-msid", SnapshotID: "current-snap-msid",
StreamStoreID: "current-deets-msid",
}
} }
snapCurrent := &manifest.EntryMetadata{ snapCurrent := func() *manifest.EntryMetadata {
ID: "current-snap-msid", return &manifest.EntryMetadata{
Labels: map[string]string{ ID: "current-snap-msid",
backupTag: "0", Labels: map[string]string{
}, backupTag: "0",
},
}
} }
deetsCurrent := &manifest.EntryMetadata{ deetsCurrent := func() *manifest.EntryMetadata {
ID: "current-deets-msid", return &manifest.EntryMetadata{
ID: "current-deets-msid",
}
} }
// Legacy backup with details in separate model. // Legacy backup with details in separate model.
bupLegacy := &backup.Backup{ bupLegacy := func() *backup.Backup {
BaseModel: model.BaseModel{ return &backup.Backup{
ID: model.StableID("legacy-bup-id"), BaseModel: model.BaseModel{
ModelStoreID: manifest.ID("legacy-bup-msid"), ID: model.StableID("legacy-bup-id"),
}, ModelStoreID: manifest.ID("legacy-bup-msid"),
SnapshotID: "legacy-snap-msid", },
DetailsID: "legacy-deets-msid", SnapshotID: "legacy-snap-msid",
DetailsID: "legacy-deets-msid",
}
} }
snapLegacy := &manifest.EntryMetadata{ snapLegacy := func() *manifest.EntryMetadata {
ID: "legacy-snap-msid", return &manifest.EntryMetadata{
Labels: map[string]string{ ID: "legacy-snap-msid",
backupTag: "0", Labels: map[string]string{
}, backupTag: "0",
},
}
} }
deetsLegacy := &model.BaseModel{ deetsLegacy := func() *model.BaseModel {
ID: "legacy-deets-id", return &model.BaseModel{
ModelStoreID: "legacy-deets-msid", ID: "legacy-deets-id",
ModelStoreID: "legacy-deets-msid",
}
} }
// Incomplete backup missing data snapshot. // Incomplete backup missing data snapshot.
bupNoSnapshot := &backup.Backup{ bupNoSnapshot := func() *backup.Backup {
BaseModel: model.BaseModel{ return &backup.Backup{
ID: model.StableID("ns-bup-id"), BaseModel: model.BaseModel{
ModelStoreID: manifest.ID("ns-bup-id-msid"), ID: model.StableID("ns-bup-id"),
}, ModelStoreID: manifest.ID("ns-bup-id-msid"),
StreamStoreID: "ns-deets-msid", },
StreamStoreID: "ns-deets-msid",
}
} }
deetsNoSnapshot := &manifest.EntryMetadata{ deetsNoSnapshot := func() *manifest.EntryMetadata {
ID: "ns-deets-msid", return &manifest.EntryMetadata{
ID: "ns-deets-msid",
}
} }
// Legacy incomplete backup missing data snapshot. // Legacy incomplete backup missing data snapshot.
bupLegacyNoSnapshot := &backup.Backup{ bupLegacyNoSnapshot := func() *backup.Backup {
BaseModel: model.BaseModel{ return &backup.Backup{
ID: model.StableID("ns-legacy-bup-id"), BaseModel: model.BaseModel{
ModelStoreID: manifest.ID("ns-legacy-bup-id-msid"), ID: model.StableID("ns-legacy-bup-id"),
}, ModelStoreID: manifest.ID("ns-legacy-bup-id-msid"),
DetailsID: "ns-legacy-deets-msid", },
DetailsID: "ns-legacy-deets-msid",
}
} }
deetsLegacyNoSnapshot := &model.BaseModel{ deetsLegacyNoSnapshot := func() *model.BaseModel {
ID: "ns-legacy-deets-id", return &model.BaseModel{
ModelStoreID: "ns-legacy-deets-msid", ID: "ns-legacy-deets-id",
ModelStoreID: "ns-legacy-deets-msid",
}
} }
// Incomplete backup missing details. // Incomplete backup missing details.
bupNoDetails := &backup.Backup{ bupNoDetails := func() *backup.Backup {
BaseModel: model.BaseModel{ return &backup.Backup{
ID: model.StableID("nssid-bup-id"), BaseModel: model.BaseModel{
ModelStoreID: manifest.ID("nssid-bup-msid"), ID: model.StableID("nssid-bup-id"),
}, ModelStoreID: manifest.ID("nssid-bup-msid"),
SnapshotID: "nssid-snap-msid", },
SnapshotID: "nssid-snap-msid",
}
} }
snapNoDetails := &manifest.EntryMetadata{ snapNoDetails := func() *manifest.EntryMetadata {
ID: "nssid-snap-msid", return &manifest.EntryMetadata{
Labels: map[string]string{ ID: "nssid-snap-msid",
backupTag: "0", Labels: map[string]string{
}, backupTag: "0",
},
}
} }
// Get some stable time so that we can do everything relative to this in the // Get some stable time so that we can do everything relative to this in the
@ -268,16 +292,16 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "OnlyCompleteBackups Noops", name: "OnlyCompleteBackups Noops",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
deetsCurrent, deetsCurrent(),
snapLegacy, snapLegacy(),
}, },
detailsModels: []*model.BaseModel{ detailsModels: []*model.BaseModel{
deetsLegacy, deetsLegacy(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
{bup: bupLegacy}, {bup: bupLegacy()},
}, },
time: baseTime, time: baseTime,
expectErr: assert.NoError, expectErr: assert.NoError,
@ -285,24 +309,24 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "MissingFieldsInBackup CausesCleanup", name: "MissingFieldsInBackup CausesCleanup",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapNoDetails, snapNoDetails(),
deetsNoSnapshot, deetsNoSnapshot(),
}, },
detailsModels: []*model.BaseModel{ detailsModels: []*model.BaseModel{
deetsLegacyNoSnapshot, deetsLegacyNoSnapshot(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupNoSnapshot}, {bup: bupNoSnapshot()},
{bup: bupLegacyNoSnapshot}, {bup: bupLegacyNoSnapshot()},
{bup: bupNoDetails}, {bup: bupNoDetails()},
}, },
expectDeleteIDs: []manifest.ID{ expectDeleteIDs: []manifest.ID{
manifest.ID(bupNoSnapshot.ModelStoreID), manifest.ID(bupNoSnapshot().ModelStoreID),
manifest.ID(bupLegacyNoSnapshot.ModelStoreID), manifest.ID(bupLegacyNoSnapshot().ModelStoreID),
manifest.ID(bupNoDetails.ModelStoreID), manifest.ID(bupNoDetails().ModelStoreID),
manifest.ID(deetsLegacyNoSnapshot.ModelStoreID), manifest.ID(deetsLegacyNoSnapshot().ModelStoreID),
snapNoDetails.ID, snapNoDetails().ID,
deetsNoSnapshot.ID, deetsNoSnapshot().ID,
}, },
time: baseTime, time: baseTime,
expectErr: assert.NoError, expectErr: assert.NoError,
@ -310,20 +334,20 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "MissingSnapshot CausesCleanup", name: "MissingSnapshot CausesCleanup",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
deetsCurrent, deetsCurrent(),
}, },
detailsModels: []*model.BaseModel{ detailsModels: []*model.BaseModel{
deetsLegacy, deetsLegacy(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
{bup: bupLegacy}, {bup: bupLegacy()},
}, },
expectDeleteIDs: []manifest.ID{ expectDeleteIDs: []manifest.ID{
manifest.ID(bupCurrent.ModelStoreID), manifest.ID(bupCurrent().ModelStoreID),
deetsCurrent.ID, deetsCurrent().ID,
manifest.ID(bupLegacy.ModelStoreID), manifest.ID(bupLegacy().ModelStoreID),
manifest.ID(deetsLegacy.ModelStoreID), manifest.ID(deetsLegacy().ModelStoreID),
}, },
time: baseTime, time: baseTime,
expectErr: assert.NoError, expectErr: assert.NoError,
@ -331,38 +355,39 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "MissingDetails CausesCleanup", name: "MissingDetails CausesCleanup",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
snapLegacy, snapLegacy(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
{bup: bupLegacy}, {bup: bupLegacy()},
}, },
expectDeleteIDs: []manifest.ID{ expectDeleteIDs: []manifest.ID{
manifest.ID(bupCurrent.ModelStoreID), manifest.ID(bupCurrent().ModelStoreID),
manifest.ID(bupLegacy.ModelStoreID), manifest.ID(bupLegacy().ModelStoreID),
snapCurrent.ID, snapCurrent().ID,
snapLegacy.ID, snapLegacy().ID,
}, },
time: baseTime, time: baseTime,
expectErr: assert.NoError, expectErr: assert.NoError,
}, },
// Tests with various errors from Storer.
{ {
name: "SnapshotsListError Fails", name: "SnapshotsListError Fails",
snapshotFetchErr: assert.AnError, snapshotFetchErr: assert.AnError,
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
}, },
expectErr: assert.Error, expectErr: assert.Error,
}, },
{ {
name: "LegacyDetailsListError Fails", name: "LegacyDetailsListError Fails",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
}, },
detailsModelListErr: assert.AnError, detailsModelListErr: assert.AnError,
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
}, },
time: baseTime, time: baseTime,
expectErr: assert.Error, expectErr: assert.Error,
@ -370,8 +395,8 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "BackupIDsListError Fails", name: "BackupIDsListError Fails",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
deetsCurrent, deetsCurrent(),
}, },
backupListErr: assert.AnError, backupListErr: assert.AnError,
time: baseTime, time: baseTime,
@ -380,22 +405,22 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "BackupModelGetErrorNotFound CausesCleanup", name: "BackupModelGetErrorNotFound CausesCleanup",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
deetsCurrent, deetsCurrent(),
snapLegacy, snapLegacy(),
snapNoDetails, snapNoDetails(),
}, },
detailsModels: []*model.BaseModel{ detailsModels: []*model.BaseModel{
deetsLegacy, deetsLegacy(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
{ {
bup: bupLegacy, bup: bupLegacy(),
err: data.ErrNotFound, err: data.ErrNotFound,
}, },
{ {
bup: bupNoDetails, bup: bupNoDetails(),
err: data.ErrNotFound, err: data.ErrNotFound,
}, },
}, },
@ -404,11 +429,11 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
// delete operation should ignore missing models though so there's no // delete operation should ignore missing models though so there's no
// issue. // issue.
expectDeleteIDs: []manifest.ID{ expectDeleteIDs: []manifest.ID{
snapLegacy.ID, snapLegacy().ID,
manifest.ID(deetsLegacy.ModelStoreID), manifest.ID(deetsLegacy().ModelStoreID),
manifest.ID(bupLegacy.ModelStoreID), manifest.ID(bupLegacy().ModelStoreID),
snapNoDetails.ID, snapNoDetails().ID,
manifest.ID(bupNoDetails.ModelStoreID), manifest.ID(bupNoDetails().ModelStoreID),
}, },
time: baseTime, time: baseTime,
expectErr: assert.NoError, expectErr: assert.NoError,
@ -416,21 +441,21 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "BackupModelGetError Fails", name: "BackupModelGetError Fails",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
deetsCurrent, deetsCurrent(),
snapLegacy, snapLegacy(),
snapNoDetails, snapNoDetails(),
}, },
detailsModels: []*model.BaseModel{ detailsModels: []*model.BaseModel{
deetsLegacy, deetsLegacy(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
{ {
bup: bupLegacy, bup: bupLegacy(),
err: assert.AnError, err: assert.AnError,
}, },
{bup: bupNoDetails}, {bup: bupNoDetails()},
}, },
time: baseTime, time: baseTime,
expectErr: assert.Error, expectErr: assert.Error,
@ -438,34 +463,35 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "DeleteError Fails", name: "DeleteError Fails",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
snapCurrent, snapCurrent(),
deetsCurrent, deetsCurrent(),
snapLegacy, snapLegacy(),
snapNoDetails, snapNoDetails(),
}, },
detailsModels: []*model.BaseModel{ detailsModels: []*model.BaseModel{
deetsLegacy, deetsLegacy(),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: bupCurrent}, {bup: bupCurrent()},
{bup: bupLegacy}, {bup: bupLegacy()},
{bup: bupNoDetails}, {bup: bupNoDetails()},
}, },
expectDeleteIDs: []manifest.ID{ expectDeleteIDs: []manifest.ID{
snapNoDetails.ID, snapNoDetails().ID,
manifest.ID(bupNoDetails.ModelStoreID), manifest.ID(bupNoDetails().ModelStoreID),
}, },
deleteErr: assert.AnError, deleteErr: assert.AnError,
time: baseTime, time: baseTime,
expectErr: assert.Error, expectErr: assert.Error,
}, },
// Tests dealing with buffer times.
{ {
name: "MissingSnapshot BarelyTooYoungForCleanup Noops", name: "MissingSnapshot BarelyTooYoungForCleanup Noops",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
manifestWithTime(baseTime, deetsCurrent), manifestWithTime(baseTime, deetsCurrent()),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: backupWithTime(baseTime, bupCurrent)}, {bup: backupWithTime(baseTime, bupCurrent())},
}, },
time: baseTime.Add(24 * time.Hour), time: baseTime.Add(24 * time.Hour),
buffer: 24 * time.Hour, buffer: 24 * time.Hour,
@ -474,14 +500,14 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "MissingSnapshot BarelyOldEnough CausesCleanup", name: "MissingSnapshot BarelyOldEnough CausesCleanup",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
manifestWithTime(baseTime, deetsCurrent), manifestWithTime(baseTime, deetsCurrent()),
}, },
backups: []backupRes{ backups: []backupRes{
{bup: backupWithTime(baseTime, bupCurrent)}, {bup: backupWithTime(baseTime, bupCurrent())},
}, },
expectDeleteIDs: []manifest.ID{ expectDeleteIDs: []manifest.ID{
deetsCurrent.ID, deetsCurrent().ID,
manifest.ID(bupCurrent.ModelStoreID), manifest.ID(bupCurrent().ModelStoreID),
}, },
time: baseTime.Add((24 * time.Hour) + time.Second), time: baseTime.Add((24 * time.Hour) + time.Second),
buffer: 24 * time.Hour, buffer: 24 * time.Hour,
@ -490,12 +516,12 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
{ {
name: "BackupGetErrorNotFound TooYoung Noops", name: "BackupGetErrorNotFound TooYoung Noops",
snapshots: []*manifest.EntryMetadata{ snapshots: []*manifest.EntryMetadata{
manifestWithTime(baseTime, snapCurrent), manifestWithTime(baseTime, snapCurrent()),
manifestWithTime(baseTime, deetsCurrent), manifestWithTime(baseTime, deetsCurrent()),
}, },
backups: []backupRes{ backups: []backupRes{
{ {
bup: backupWithTime(baseTime, bupCurrent), bup: backupWithTime(baseTime, bupCurrent()),
err: data.ErrNotFound, err: data.ErrNotFound,
}, },
}, },