This commit is contained in:
Abhishek Pandey 2023-08-22 08:49:49 +05:30
parent 277e7e1b3e
commit 7396ab1094

View File

@ -5,8 +5,10 @@ import (
"testing" "testing"
"github.com/alcionai/clues" "github.com/alcionai/clues"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"golang.org/x/exp/maps"
"github.com/alcionai/corso/src/internal/common/ptr" "github.com/alcionai/corso/src/internal/common/ptr"
evmock "github.com/alcionai/corso/src/internal/events/mock" evmock "github.com/alcionai/corso/src/internal/events/mock"
@ -14,6 +16,7 @@ import (
"github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/internal/tester"
"github.com/alcionai/corso/src/internal/tester/tconfig" "github.com/alcionai/corso/src/internal/tester/tconfig"
"github.com/alcionai/corso/src/internal/version" "github.com/alcionai/corso/src/internal/version"
"github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/control" "github.com/alcionai/corso/src/pkg/control"
ctrlTD "github.com/alcionai/corso/src/pkg/control/testdata" ctrlTD "github.com/alcionai/corso/src/pkg/control/testdata"
"github.com/alcionai/corso/src/pkg/count" "github.com/alcionai/corso/src/pkg/count"
@ -1003,369 +1006,369 @@ func (suite *ExchangeRestoreNightlyIntgSuite) TestRestore_Run_exchangeWithAdvanc
checkRestoreCounts(t, ctr1, 0, 0, countItemsInRestore) checkRestoreCounts(t, ctr1, 0, 0, countItemsInRestore)
}) })
// // skip restore // skip restore
// suite.Run("skip collisions", func() { suite.Run("skip collisions", func() {
// t := suite.T() t := suite.T()
// ctx, flush := tester.NewContext(t) ctx, flush := tester.NewContext(t)
// defer flush() defer flush()
// mb := evmock.NewBus() mb := evmock.NewBus()
// ctr2 := count.New() ctr2 := count.New()
// restoreCfg.OnCollision = control.Skip restoreCfg.OnCollision = control.Skip
// ro, _ := prepNewTestRestoreOp( ro, _ := prepNewTestRestoreOp(
// t, t,
// ctx, ctx,
// bod.st, bod.st,
// bo.Results.BackupID, bo.Results.BackupID,
// mb, mb,
// ctr2, ctr2,
// sel, sel,
// opts, opts,
// restoreCfg) restoreCfg)
// deets := runAndCheckRestore(t, ctx, &ro, mb, false) deets := runAndCheckRestore(t, ctx, &ro, mb, false)
// assert.Zero( assert.Zero(
// t, t,
// len(deets.Entries), len(deets.Entries),
// "no items should have been restored") "no items should have been restored")
// checkRestoreCounts(t, ctr2, countItemsInRestore, 0, 0) checkRestoreCounts(t, ctr2, countItemsInRestore, 0, 0)
// result := map[string]string{} result := map[string]string{}
// for cat, ac := range testCategories { for cat, ac := range testCategories {
// suite.Run(cat.String(), func() { suite.Run(cat.String(), func() {
// t := suite.T() t := suite.T()
// ctx, flush := tester.NewContext(t) ctx, flush := tester.NewContext(t)
// defer flush() defer flush()
// m := filterCollisionKeyResults( m := filterCollisionKeyResults(
// t, t,
// ctx, ctx,
// userID, userID,
// containerIDs[cat], containerIDs[cat],
// GetItemsInContainerByCollisionKeyer[string](ac), GetItemsInContainerByCollisionKeyer[string](ac),
// collisionKeys[cat]) collisionKeys[cat])
// maps.Copy(result, m) maps.Copy(result, m)
// currentIDs, err := ac.GetItemIDsInContainer(ctx, userID, containerIDs[cat]) currentIDs, err := ac.GetItemIDsInContainer(ctx, userID, containerIDs[cat])
// require.NoError(t, err, clues.ToCore(err)) require.NoError(t, err, clues.ToCore(err))
// assert.Equal(t, itemIDs[cat], currentIDs, "ids are equal") assert.Equal(t, itemIDs[cat], currentIDs, "ids are equal")
// }) })
// }
// assert.Len(t, result, 0, "no new items should get added")
// })
// // replace restore
// suite.Run("replace collisions", func() {
// t := suite.T()
// ctx, flush := tester.NewContext(t)
// defer flush()
// mb := evmock.NewBus()
// ctr3 := count.New()
// restoreCfg.OnCollision = control.Replace
// ro, _ := prepNewTestRestoreOp(
// t,
// ctx,
// bod.st,
// bo.Results.BackupID,
// mb,
// ctr3,
// sel,
// opts,
// restoreCfg)
// deets := runAndCheckRestore(t, ctx, &ro, mb, false)
// filtEnts := []details.Entry{}
// for _, e := range deets.Entries {
// if e.Folder == nil {
// filtEnts = append(filtEnts, e)
// }
// }
// assert.Len(t, filtEnts, countItemsInRestore, "every item should have been replaced")
// checkRestoreCounts(t, ctr3, 0, countItemsInRestore, 0)
// result := map[string]string{}
// for cat, ac := range testCategories {
// suite.Run(cat.String(), func() {
// t := suite.T()
// ctx, flush := tester.NewContext(t)
// defer flush()
// m := filterCollisionKeyResults(
// t,
// ctx,
// userID,
// containerIDs[cat],
// GetItemsInContainerByCollisionKeyer[string](ac),
// collisionKeys[cat])
// maps.Copy(result, m)
// currentIDs, err := ac.GetItemIDsInContainer(ctx, userID, containerIDs[cat])
// require.NoError(t, err, clues.ToCore(err))
// assert.Equal(t, len(itemIDs[cat]), len(currentIDs), "count of ids are equal")
// for orig := range itemIDs[cat] {
// assert.NotContains(t, currentIDs, orig, "original item should not exist after replacement")
// }
// itemIDs[cat] = currentIDs
// })
// }
// assert.Len(t, result, 0, "all items should have been replaced")
// })
// // copy restore
// suite.Run("copy collisions", func() {
// t := suite.T()
// ctx, flush := tester.NewContext(t)
// defer flush()
// mb := evmock.NewBus()
// ctr4 := count.New()
// restoreCfg.OnCollision = control.Copy
// ro, _ := prepNewTestRestoreOp(
// t,
// ctx,
// bod.st,
// bo.Results.BackupID,
// mb,
// ctr4,
// sel,
// opts,
// restoreCfg)
// deets := runAndCheckRestore(t, ctx, &ro, mb, false)
// filtEnts := []details.Entry{}
// for _, e := range deets.Entries {
// if e.Folder == nil {
// filtEnts = append(filtEnts, e)
// }
// }
// assert.Len(t, filtEnts, countItemsInRestore, "every item should have been copied")
// checkRestoreCounts(t, ctr4, 0, 0, countItemsInRestore)
// result := map[string]string{}
// for cat, ac := range testCategories {
// suite.Run(cat.String(), func() {
// t := suite.T()
// ctx, flush := tester.NewContext(t)
// defer flush()
// m := filterCollisionKeyResults(
// t,
// ctx,
// userID,
// containerIDs[cat],
// GetItemsInContainerByCollisionKeyer[string](ac),
// collisionKeys[cat])
// maps.Copy(result, m)
// currentIDs, err := ac.GetItemIDsInContainer(ctx, userID, containerIDs[cat])
// require.NoError(t, err, clues.ToCore(err))
// assert.Equal(t, 2*len(itemIDs[cat]), len(currentIDs), "count of ids should be double from before")
// assert.Subset(t, maps.Keys(currentIDs), maps.Keys(itemIDs[cat]), "original item should exist after copy")
// })
// }
// // TODO: we have the option of modifying copy creations in exchange
// // so that the results don't collide. But we haven't made that
// // decision yet.
// assert.Len(t, result, 0, "no items should have been added as copies")
// })
} }
// func (suite *ExchangeRestoreNightlyIntgSuite) TestRestore_Run_exchangeAlternateProtectedResource() { assert.Len(t, result, 0, "no new items should get added")
// t := suite.T() })
// ctx, flush := tester.NewContext(t) // replace restore
// defer flush()
// // a backup is required to run restores suite.Run("replace collisions", func() {
t := suite.T()
// baseSel := selectors.NewExchangeBackup([]string{suite.its.user.ID}) ctx, flush := tester.NewContext(t)
// baseSel.Include( defer flush()
// // events cannot be run, for the same reason as incremental backups: the user needs
// // to have their account recycled.
// // base_sel.EventCalendars([]string{api.DefaultCalendar}, selectors.PrefixMatch()),
// baseSel.ContactFolders([]string{api.DefaultContacts}, selectors.PrefixMatch()),
// baseSel.MailFolders([]string{api.MailInbox}, selectors.PrefixMatch()))
// baseSel.DiscreteOwner = suite.its.user.ID mb := evmock.NewBus()
ctr3 := count.New()
// var ( restoreCfg.OnCollision = control.Replace
// mb = evmock.NewBus()
// opts = control.DefaultOptions()
// )
// bo, bod := prepNewTestBackupOp(t, ctx, mb, baseSel.Selector, opts, version.Backup) ro, _ := prepNewTestRestoreOp(
// defer bod.close(t, ctx) t,
ctx,
bod.st,
bo.Results.BackupID,
mb,
ctr3,
sel,
opts,
restoreCfg)
// runAndCheckBackup(t, ctx, &bo, mb, false) deets := runAndCheckRestore(t, ctx, &ro, mb, false)
filtEnts := []details.Entry{}
// rsel, err := baseSel.ToExchangeRestore() for _, e := range deets.Entries {
// require.NoError(t, err, clues.ToCore(err)) if e.Folder == nil {
filtEnts = append(filtEnts, e)
}
}
// var ( assert.Len(t, filtEnts, countItemsInRestore, "every item should have been replaced")
// restoreCfg = ctrlTD.DefaultRestoreConfig("exchange_restore_to_user")
// sel = rsel.Selector
// userID = suite.its.user.ID
// secondaryUserID = suite.its.secondaryUser.ID
// uid = userID
// acCont = suite.its.ac.Contacts()
// acMail = suite.its.ac.Mail()
// // acEvts = suite.its.ac.Events()
// firstCtr = count.New()
// )
// restoreCfg.OnCollision = control.Copy checkRestoreCounts(t, ctr3, 0, countItemsInRestore, 0)
// mb = evmock.NewBus()
// // first restore to the current user result := map[string]string{}
// ro1, _ := prepNewTestRestoreOp( for cat, ac := range testCategories {
// t, suite.Run(cat.String(), func() {
// ctx, t := suite.T()
// bod.st,
// bo.Results.BackupID,
// mb,
// firstCtr,
// sel,
// opts,
// restoreCfg)
// runAndCheckRestore(t, ctx, &ro1, mb, false) ctx, flush := tester.NewContext(t)
defer flush()
// // get all files in folder, use these as the base m := filterCollisionKeyResults(
// // set of files to compare against. t,
ctx,
userID,
containerIDs[cat],
GetItemsInContainerByCollisionKeyer[string](ac),
collisionKeys[cat])
maps.Copy(result, m)
// var ( currentIDs, err := ac.GetItemIDsInContainer(ctx, userID, containerIDs[cat])
// userItemIDs = map[path.CategoryType]map[string]struct{}{} require.NoError(t, err, clues.ToCore(err))
// userCollisionKeys = map[path.CategoryType]map[string]string{}
// )
// // --- contacts assert.Equal(t, len(itemIDs[cat]), len(currentIDs), "count of ids are equal")
// cat := path.ContactsCategory for orig := range itemIDs[cat] {
assert.NotContains(t, currentIDs, orig, "original item should not exist after replacement")
}
itemIDs[cat] = currentIDs
})
}
assert.Len(t, result, 0, "all items should have been replaced")
})
// copy restore
suite.Run("copy collisions", func() {
t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
mb := evmock.NewBus()
ctr4 := count.New()
restoreCfg.OnCollision = control.Copy
ro, _ := prepNewTestRestoreOp(
t,
ctx,
bod.st,
bo.Results.BackupID,
mb,
ctr4,
sel,
opts,
restoreCfg)
deets := runAndCheckRestore(t, ctx, &ro, mb, false)
filtEnts := []details.Entry{}
for _, e := range deets.Entries {
if e.Folder == nil {
filtEnts = append(filtEnts, e)
}
}
assert.Len(t, filtEnts, countItemsInRestore, "every item should have been copied")
checkRestoreCounts(t, ctr4, 0, 0, countItemsInRestore)
result := map[string]string{}
for cat, ac := range testCategories {
suite.Run(cat.String(), func() {
t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
m := filterCollisionKeyResults(
t,
ctx,
userID,
containerIDs[cat],
GetItemsInContainerByCollisionKeyer[string](ac),
collisionKeys[cat])
maps.Copy(result, m)
currentIDs, err := ac.GetItemIDsInContainer(ctx, userID, containerIDs[cat])
require.NoError(t, err, clues.ToCore(err))
assert.Equal(t, 2*len(itemIDs[cat]), len(currentIDs), "count of ids should be double from before")
assert.Subset(t, maps.Keys(currentIDs), maps.Keys(itemIDs[cat]), "original item should exist after copy")
})
}
// TODO: we have the option of modifying copy creations in exchange
// so that the results don't collide. But we haven't made that
// decision yet.
assert.Len(t, result, 0, "no items should have been added as copies")
})
}
func (suite *ExchangeRestoreNightlyIntgSuite) TestRestore_Run_exchangeAlternateProtectedResource() {
t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
// a backup is required to run restores
baseSel := selectors.NewExchangeBackup([]string{suite.its.user.ID})
baseSel.Include(
// events cannot be run, for the same reason as incremental backups: the user needs
// to have their account recycled.
// base_sel.EventCalendars([]string{api.DefaultCalendar}, selectors.PrefixMatch()),
baseSel.ContactFolders([]string{api.DefaultContacts}, selectors.PrefixMatch()),
baseSel.MailFolders([]string{api.MailInbox}, selectors.PrefixMatch()))
baseSel.DiscreteOwner = suite.its.user.ID
var (
mb = evmock.NewBus()
opts = control.DefaultOptions()
)
bo, bod := prepNewTestBackupOp(t, ctx, mb, baseSel.Selector, opts, version.Backup)
defer bod.close(t, ctx)
runAndCheckBackup(t, ctx, &bo, mb, false)
rsel, err := baseSel.ToExchangeRestore()
require.NoError(t, err, clues.ToCore(err))
var (
restoreCfg = ctrlTD.DefaultRestoreConfig("exchange_restore_to_user")
sel = rsel.Selector
userID = suite.its.user.ID
secondaryUserID = suite.its.secondaryUser.ID
uid = userID
acCont = suite.its.ac.Contacts()
acMail = suite.its.ac.Mail()
// acEvts = suite.its.ac.Events()
firstCtr = count.New()
)
restoreCfg.OnCollision = control.Copy
mb = evmock.NewBus()
// first restore to the current user
ro1, _ := prepNewTestRestoreOp(
t,
ctx,
bod.st,
bo.Results.BackupID,
mb,
firstCtr,
sel,
opts,
restoreCfg)
runAndCheckRestore(t, ctx, &ro1, mb, false)
// get all files in folder, use these as the base
// set of files to compare against.
var (
userItemIDs = map[path.CategoryType]map[string]struct{}{}
userCollisionKeys = map[path.CategoryType]map[string]string{}
)
// --- contacts
cat := path.ContactsCategory
userItemIDs[cat], userCollisionKeys[cat], _ = getCollKeysAndItemIDs(
t,
ctx,
acCont,
uid,
"",
restoreCfg.Location)
// --- events
// cat = path.EventsCategory
// userItemIDs[cat], userCollisionKeys[cat], _ = getCollKeysAndItemIDs( // userItemIDs[cat], userCollisionKeys[cat], _ = getCollKeysAndItemIDs(
// t, // t,
// ctx, // ctx,
// acCont, // acEvts,
// uid, // uid,
// "", // "",
// restoreCfg.Location) // restoreCfg.Location)
// // --- events // --- mail
// // cat = path.EventsCategory cat = path.EmailCategory
// // userItemIDs[cat], userCollisionKeys[cat], _ = getCollKeysAndItemIDs( userItemIDs[cat], userCollisionKeys[cat], _ = getCollKeysAndItemIDs(
// // t, t,
// // ctx, ctx,
// // acEvts, acMail,
// // uid, uid,
// // "", "",
// // restoreCfg.Location) restoreCfg.Location,
api.MailInbox)
// // --- mail // then restore to the secondary user
// cat = path.EmailCategory
// userItemIDs[cat], userCollisionKeys[cat], _ = getCollKeysAndItemIDs(
// t,
// ctx,
// acMail,
// uid,
// "",
// restoreCfg.Location,
// api.MailInbox)
// // then restore to the secondary user uid = secondaryUserID
mb = evmock.NewBus()
secondCtr := count.New()
restoreCfg.ProtectedResource = uid
// uid = secondaryUserID ro2, _ := prepNewTestRestoreOp(
// mb = evmock.NewBus() t,
// secondCtr := count.New() ctx,
// restoreCfg.ProtectedResource = uid bod.st,
bo.Results.BackupID,
mb,
secondCtr,
sel,
opts,
restoreCfg)
// ro2, _ := prepNewTestRestoreOp( runAndCheckRestore(t, ctx, &ro2, mb, false)
// t,
// ctx,
// bod.st,
// bo.Results.BackupID,
// mb,
// secondCtr,
// sel,
// opts,
// restoreCfg)
// runAndCheckRestore(t, ctx, &ro2, mb, false) var (
secondaryItemIDs = map[path.CategoryType]map[string]struct{}{}
secondaryCollisionKeys = map[path.CategoryType]map[string]string{}
)
// var ( // --- contacts
// secondaryItemIDs = map[path.CategoryType]map[string]struct{}{} cat = path.ContactsCategory
// secondaryCollisionKeys = map[path.CategoryType]map[string]string{} secondaryItemIDs[cat], secondaryCollisionKeys[cat], _ = getCollKeysAndItemIDs(
// ) t,
ctx,
acCont,
uid,
"",
restoreCfg.Location)
// // --- contacts // --- events
// cat = path.ContactsCategory // cat = path.EventsCategory
// secondaryItemIDs[cat], secondaryCollisionKeys[cat], _ = getCollKeysAndItemIDs( // secondaryItemIDs[cat], secondaryCollisionKeys[cat], _ = getCollKeysAndItemIDs(
// t, // t,
// ctx, // ctx,
// acCont, // acEvts,
// uid, // uid,
// "", // "",
// restoreCfg.Location) // restoreCfg.Location)
// // --- events // --- mail
// // cat = path.EventsCategory cat = path.EmailCategory
// // secondaryItemIDs[cat], secondaryCollisionKeys[cat], _ = getCollKeysAndItemIDs( secondaryItemIDs[cat], secondaryCollisionKeys[cat], _ = getCollKeysAndItemIDs(
// // t, t,
// // ctx, ctx,
// // acEvts, acMail,
// // uid, uid,
// // "", "",
// // restoreCfg.Location) restoreCfg.Location,
api.MailInbox)
// // --- mail // compare restore results
// cat = path.EmailCategory for _, cat := range []path.CategoryType{path.ContactsCategory, path.EmailCategory, path.EventsCategory} {
// secondaryItemIDs[cat], secondaryCollisionKeys[cat], _ = getCollKeysAndItemIDs( assert.Equal(t, len(userItemIDs[cat]), len(secondaryItemIDs[cat]))
// t, assert.ElementsMatch(t, maps.Keys(userCollisionKeys[cat]), maps.Keys(secondaryCollisionKeys[cat]))
// ctx, }
// acMail, }
// uid,
// "",
// restoreCfg.Location,
// api.MailInbox)
// // compare restore results
// for _, cat := range []path.CategoryType{path.ContactsCategory, path.EmailCategory, path.EventsCategory} {
// assert.Equal(t, len(userItemIDs[cat]), len(secondaryItemIDs[cat]))
// assert.ElementsMatch(t, maps.Keys(userCollisionKeys[cat]), maps.Keys(secondaryCollisionKeys[cat]))
// }
// }
func getCollKeysAndItemIDs( func getCollKeysAndItemIDs(
t *testing.T, t *testing.T,