From 064c1206a33f83211608cebb08aba693b1d1d9f0 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Mon, 16 Oct 2023 20:12:00 +0530 Subject: [PATCH] Update tests for GetRestoreResource --- src/cli/utils/groups.go | 3 +- src/internal/m365/mock/connector.go | 9 ++ src/internal/m365/restore.go | 1 + .../m365/service/exchange/restore_test.go | 84 +++++++++++++++++++ .../m365/service/groups/restore_test.go | 68 +++++++++++++++ .../m365/service/onedrive/restore_test.go | 63 ++++++++++++++ .../m365/service/sharepoint/restore_test.go | 84 +++++++++++++++++++ src/internal/operations/restore_test.go | 71 ---------------- 8 files changed, 311 insertions(+), 72 deletions(-) create mode 100644 src/internal/m365/service/exchange/restore_test.go create mode 100644 src/internal/m365/service/sharepoint/restore_test.go diff --git a/src/cli/utils/groups.go b/src/cli/utils/groups.go index f6cd2db18..7e6f259c3 100644 --- a/src/cli/utils/groups.go +++ b/src/cli/utils/groups.go @@ -69,7 +69,8 @@ func AddGroupsCategories(sel *selectors.GroupsBackup, cats []string) *selectors. func MakeGroupsOpts(cmd *cobra.Command) GroupsOpts { restoreCfg := makeBaseRestoreCfgOpts(cmd) - restoreCfg.SubServiceType = path.SharePointService // this is the only possibility as of now + restoreCfg.SubServiceType = path.SharePointService // this is the only possibility as of now + restoreCfg.SubService = append(flags.SiteIDFV, flags.WebURLFV...)[0] // we should always have just one return GroupsOpts{ diff --git a/src/internal/m365/mock/connector.go b/src/internal/m365/mock/connector.go index e10a48819..706362938 100644 --- a/src/internal/m365/mock/connector.go +++ b/src/internal/m365/mock/connector.go @@ -50,6 +50,15 @@ func (ctrl Controller) ProduceBackupCollections( return ctrl.Collections, ctrl.Exclude, ctrl.Err == nil, ctrl.Err } +func (ctrl Controller) GetRestoreResource( + ctx context.Context, + service path.ServiceType, + rc control.RestoreConfig, + orig idname.Provider, +) (path.ServiceType, idname.Provider, error) { + return service, orig, nil +} + func (ctrl *Controller) GetMetadataPaths( ctx context.Context, r kinject.RestoreProducer, diff --git a/src/internal/m365/restore.go b/src/internal/m365/restore.go index 881cb2034..8df8d35bc 100644 --- a/src/internal/m365/restore.go +++ b/src/internal/m365/restore.go @@ -53,6 +53,7 @@ func (ctrl *Controller) GetRestoreResource( } ctrl.IDNameLookup = idname.NewCache(map[string]string{pr.ID(): pr.Name()}) + return svc, pr, nil } diff --git a/src/internal/m365/service/exchange/restore_test.go b/src/internal/m365/service/exchange/restore_test.go new file mode 100644 index 000000000..5bce9af47 --- /dev/null +++ b/src/internal/m365/service/exchange/restore_test.go @@ -0,0 +1,84 @@ +package exchange + +import ( + "testing" + + "github.com/alcionai/clues" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/common/dttm" + "github.com/alcionai/corso/src/internal/common/idname" + "github.com/alcionai/corso/src/internal/tester" + "github.com/alcionai/corso/src/pkg/control" + "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/services/m365/api" +) + +type ExchangeRestoreUnitSuite struct { + tester.Suite +} + +func TestExchangeRestoreUnitSuite(t *testing.T) { + suite.Run(t, &ExchangeRestoreUnitSuite{Suite: tester.NewUnitSuite(t)}) +} + +func (suite *ExchangeRestoreUnitSuite) TestGetRestoreResource() { + var ( + id = "id" + name = "name" + cfgWithPR = control.DefaultRestoreConfig(dttm.HumanReadable) + ) + + cfgWithPR.ProtectedResource = id + + table := []struct { + name string + cfg control.RestoreConfig + orig idname.Provider + cache map[string]string + expectErr assert.ErrorAssertionFunc + expectProvider assert.ValueAssertionFunc + expectID string + expectName string + }{ + { + name: "use original", + cfg: control.DefaultRestoreConfig(dttm.HumanReadable), + orig: idname.NewProvider("oid", "oname"), + expectErr: assert.NoError, + expectID: "oid", + expectName: "oname", + }, + { + name: "use new", + cfg: cfgWithPR, + orig: idname.NewProvider("oid", "oname"), + cache: map[string]string{id: name}, + expectErr: assert.NoError, + expectID: id, + expectName: name, + }, + } + for _, test := range table { + suite.Run(test.name, func() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + svc, result, err := GetRestoreResource( + ctx, + api.Client{}, + test.cfg, + idname.NewCache(test.cache), + test.orig) + test.expectErr(t, err, clues.ToCore(err)) + require.NotNil(t, result) + assert.Equal(t, path.ExchangeService, svc) + assert.Equal(t, test.expectID, result.ID()) + assert.Equal(t, test.expectName, result.Name()) + }) + } +} diff --git a/src/internal/m365/service/groups/restore_test.go b/src/internal/m365/service/groups/restore_test.go index 262bc3159..2260c960f 100644 --- a/src/internal/m365/service/groups/restore_test.go +++ b/src/internal/m365/service/groups/restore_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/suite" "golang.org/x/exp/slices" + "github.com/alcionai/corso/src/internal/common/dttm" "github.com/alcionai/corso/src/internal/common/idname" "github.com/alcionai/corso/src/internal/common/ptr" "github.com/alcionai/corso/src/internal/data" @@ -65,6 +66,73 @@ func (suite *GroupsUnitSuite) TestConsumeRestoreCollections_noErrorOnGroups() { assert.NoError(t, err, "Groups Channels restore") } +func (suite *GroupsUnitSuite) TestGetRestoreResource() { + var ( + sid = "site-id" + sname = "site-name" + nsid = "new-site-id" + nsname = "new-site-name" + cfgWithoutPR = control.DefaultRestoreConfig(dttm.HumanReadable) + cfgWithPR = control.DefaultRestoreConfig(dttm.HumanReadable) + ) + + cfgWithoutPR.SubService.Type = path.SharePointService + cfgWithoutPR.SubService.ID = sid + cfgWithPR.ProtectedResource = nsid + cfgWithPR.SubService.Type = path.SharePointService + cfgWithPR.SubService.ID = sid + + table := []struct { + name string + cfg control.RestoreConfig + orig idname.Provider + cache map[string]string + expectErr assert.ErrorAssertionFunc + expectProvider assert.ValueAssertionFunc + expectID string + expectName string + }{ + { + name: "use original", + cfg: cfgWithoutPR, + orig: idname.NewProvider("oid", "oname"), + cache: map[string]string{sid: sname}, + expectErr: assert.NoError, + expectID: sid, + expectName: sname, + }, + { + name: "use new", + cfg: cfgWithPR, + orig: idname.NewProvider("oid", "oname"), + cache: map[string]string{nsid: nsname}, + expectErr: assert.NoError, + expectID: nsid, + expectName: nsname, + }, + } + for _, test := range table { + suite.Run(test.name, func() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + svc, result, err := GetRestoreResource( + ctx, + api.Client{}, + test.cfg, + idname.NewCache(test.cache), + test.orig) + test.expectErr(t, err, clues.ToCore(err)) + require.NotNil(t, result) + assert.Equal(t, path.SharePointService, svc) + assert.Equal(t, test.expectID, result.ID()) + assert.Equal(t, test.expectName, result.Name()) + }) + } +} + type groupsIntegrationSuite struct { tester.Suite resource string diff --git a/src/internal/m365/service/onedrive/restore_test.go b/src/internal/m365/service/onedrive/restore_test.go index 0af13eccb..aecf2a16c 100644 --- a/src/internal/m365/service/onedrive/restore_test.go +++ b/src/internal/m365/service/onedrive/restore_test.go @@ -8,9 +8,13 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "github.com/alcionai/corso/src/internal/common/dttm" + "github.com/alcionai/corso/src/internal/common/idname" "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/internal/version" + "github.com/alcionai/corso/src/pkg/control" "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/services/m365/api" ) type RestoreUnitSuite struct { @@ -315,3 +319,62 @@ func (suite *RestoreUnitSuite) TestAugmentRestorePaths_DifferentRestorePath() { }) } } + +func (suite *RestoreUnitSuite) TestGetRestoreResource() { + var ( + id = "id" + name = "name" + cfgWithPR = control.DefaultRestoreConfig(dttm.HumanReadable) + ) + + cfgWithPR.ProtectedResource = id + + table := []struct { + name string + cfg control.RestoreConfig + orig idname.Provider + cache map[string]string + expectErr assert.ErrorAssertionFunc + expectProvider assert.ValueAssertionFunc + expectID string + expectName string + }{ + { + name: "use original", + cfg: control.DefaultRestoreConfig(dttm.HumanReadable), + orig: idname.NewProvider("oid", "oname"), + expectErr: assert.NoError, + expectID: "oid", + expectName: "oname", + }, + { + name: "use new", + cfg: cfgWithPR, + orig: idname.NewProvider("oid", "oname"), + cache: map[string]string{id: name}, + expectErr: assert.NoError, + expectID: id, + expectName: name, + }, + } + for _, test := range table { + suite.Run(test.name, func() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + svc, result, err := GetRestoreResource( + ctx, + api.Client{}, + test.cfg, + idname.NewCache(test.cache), + test.orig) + test.expectErr(t, err, clues.ToCore(err)) + require.NotNil(t, result) + assert.Equal(t, path.OneDriveService, svc) + assert.Equal(t, test.expectID, result.ID()) + assert.Equal(t, test.expectName, result.Name()) + }) + } +} diff --git a/src/internal/m365/service/sharepoint/restore_test.go b/src/internal/m365/service/sharepoint/restore_test.go new file mode 100644 index 000000000..8b7ac93d7 --- /dev/null +++ b/src/internal/m365/service/sharepoint/restore_test.go @@ -0,0 +1,84 @@ +package sharepoint + +import ( + "testing" + + "github.com/alcionai/clues" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/common/dttm" + "github.com/alcionai/corso/src/internal/common/idname" + "github.com/alcionai/corso/src/internal/tester" + "github.com/alcionai/corso/src/pkg/control" + "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/services/m365/api" +) + +type SharepointRestoreUnitSuite struct { + tester.Suite +} + +func TestSharepointRestoreUnitSuite(t *testing.T) { + suite.Run(t, &SharepointRestoreUnitSuite{Suite: tester.NewUnitSuite(t)}) +} + +func (suite *SharepointRestoreUnitSuite) TestGetRestoreResource() { + var ( + id = "id" + name = "name" + cfgWithPR = control.DefaultRestoreConfig(dttm.HumanReadable) + ) + + cfgWithPR.ProtectedResource = id + + table := []struct { + name string + cfg control.RestoreConfig + orig idname.Provider + cache map[string]string + expectErr assert.ErrorAssertionFunc + expectProvider assert.ValueAssertionFunc + expectID string + expectName string + }{ + { + name: "use original", + cfg: control.DefaultRestoreConfig(dttm.HumanReadable), + orig: idname.NewProvider("oid", "oname"), + expectErr: assert.NoError, + expectID: "oid", + expectName: "oname", + }, + { + name: "use new", + cfg: cfgWithPR, + orig: idname.NewProvider("oid", "oname"), + cache: map[string]string{id: name}, + expectErr: assert.NoError, + expectID: id, + expectName: name, + }, + } + for _, test := range table { + suite.Run(test.name, func() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + svc, result, err := GetRestoreResource( + ctx, + api.Client{}, + test.cfg, + idname.NewCache(test.cache), + test.orig) + test.expectErr(t, err, clues.ToCore(err)) + require.NotNil(t, result) + assert.Equal(t, path.SharePointService, svc) + assert.Equal(t, test.expectID, result.ID()) + assert.Equal(t, test.expectName, result.Name()) + }) + } +} diff --git a/src/internal/operations/restore_test.go b/src/internal/operations/restore_test.go index 6655ad2c6..1c3752572 100644 --- a/src/internal/operations/restore_test.go +++ b/src/internal/operations/restore_test.go @@ -10,8 +10,6 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/alcionai/corso/src/internal/common/dttm" - "github.com/alcionai/corso/src/internal/common/idname" "github.com/alcionai/corso/src/internal/data" "github.com/alcionai/corso/src/internal/events" evmock "github.com/alcionai/corso/src/internal/events/mock" @@ -136,75 +134,6 @@ func (suite *RestoreOpUnitSuite) TestRestoreOperation_PersistResults() { } } -func (suite *RestoreOpUnitSuite) TestChooseRestoreResource() { - var ( - id = "id" - name = "name" - cfgWithPR = control.DefaultRestoreConfig(dttm.HumanReadable) - ) - - cfgWithPR.ProtectedResource = "cfgid" - - table := []struct { - name string - cfg control.RestoreConfig - ctrl *mock.Controller - orig idname.Provider - expectErr assert.ErrorAssertionFunc - expectProvider assert.ValueAssertionFunc - expectID string - expectName string - }{ - { - name: "use original", - cfg: control.DefaultRestoreConfig(dttm.HumanReadable), - ctrl: &mock.Controller{ - ProtectedResourceID: id, - ProtectedResourceName: name, - }, - orig: idname.NewProvider("oid", "oname"), - expectErr: assert.NoError, - expectID: "oid", - expectName: "oname", - }, - { - name: "look up resource with iface", - cfg: cfgWithPR, - ctrl: &mock.Controller{ - ProtectedResourceID: id, - ProtectedResourceName: name, - }, - orig: idname.NewProvider("oid", "oname"), - expectErr: assert.NoError, - expectID: id, - expectName: name, - }, - { - name: "error looking up protected resource", - cfg: cfgWithPR, - ctrl: &mock.Controller{ - ProtectedResourceErr: assert.AnError, - }, - orig: idname.NewProvider("oid", "oname"), - expectErr: assert.Error, - }, - } - for _, test := range table { - suite.Run(test.name, func() { - t := suite.T() - - ctx, flush := tester.NewContext(t) - defer flush() - - svc, result, err := chooseRestoreResource(ctx, test.ctrl, test.cfg, test.orig) - test.expectErr(t, err, clues.ToCore(err)) - require.NotNil(t, result) - assert.Equal(t, test.expectID, result.ID()) - assert.Equal(t, test.expectName, result.Name()) - }) - } -} - // --------------------------------------------------------------------------- // integration // ---------------------------------------------------------------------------