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