From 8ca1a0540023946747e92adbb3a973b6956c081b Mon Sep 17 00:00:00 2001 From: Hitesh Pattanayak <48874082+HiteshRepo@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:16:59 +0530 Subject: [PATCH] generates test lists for sanity test (#5035) generates test lists for CI sanity tests #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [x] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) #4754 #### Test Plan - [x] :muscle: Manual - [x] :zap: Unit test - [x] :green_heart: E2E --- src/cmd/factory/impl/common.go | 84 ++++++++++++++----- src/cmd/factory/impl/sharepoint.go | 70 ++++++++++++++-- .../m365/service/sharepoint/mock/list.go | 32 ++++++- 3 files changed, 159 insertions(+), 27 deletions(-) diff --git a/src/cmd/factory/impl/common.go b/src/cmd/factory/impl/common.go index dfd57ff74..605ad9346 100644 --- a/src/cmd/factory/impl/common.go +++ b/src/cmd/factory/impl/common.go @@ -18,6 +18,7 @@ import ( "github.com/alcionai/corso/src/internal/m365" exchMock "github.com/alcionai/corso/src/internal/m365/service/exchange/mock" odStub "github.com/alcionai/corso/src/internal/m365/service/onedrive/stub" + siteMock "github.com/alcionai/corso/src/internal/m365/service/sharepoint/mock" m365Stub "github.com/alcionai/corso/src/internal/m365/stub" "github.com/alcionai/corso/src/internal/operations/inject" "github.com/alcionai/corso/src/internal/tester" @@ -58,7 +59,7 @@ func generateAndRestoreItems( service path.ServiceType, cat path.CategoryType, sel selectors.Selector, - tenantID, userID, destFldr string, + tenantID, resourceID, destFldr string, howMany int, dbf dataBuilderFunc, opts control.Options, @@ -73,7 +74,7 @@ func generateAndRestoreItems( nowLegacy = dttm.FormatToLegacy(time.Now()) id = uuid.NewString() subject = "automated " + now[:16] + " - " + id[:8] - body = "automated " + cat.HumanString() + " generation for " + userID + " at " + now + " - " + id + body = "automated " + cat.HumanString() + " generation for " + resourceID + " at " + now + " - " + id ) items = append(items, item{ @@ -94,7 +95,7 @@ func generateAndRestoreItems( dataColls, err := buildCollections( service, - tenantID, userID, + tenantID, resourceID, restoreCfg, collections) if err != nil { @@ -192,29 +193,31 @@ type collection struct { func buildCollections( service path.ServiceType, - tenant, user string, + tenant, resource string, restoreCfg control.RestoreConfig, colls []collection, ) ([]data.RestoreCollection, error) { - collections := make([]data.RestoreCollection, 0, len(colls)) + var ( + collections = make([]data.RestoreCollection, 0, len(colls)) + mc data.Collection + ) for _, c := range colls { - pth, err := path.Build( - tenant, - user, - service, - c.category, - false, - c.PathElements...) - if err != nil { - return nil, err - } + switch { + case service == path.ExchangeService: + emc, err := generateExchangeMockColls(tenant, resource, c) + if err != nil { + return nil, err + } - mc := exchMock.NewCollection(pth, pth, len(c.items)) + mc = emc + case service == path.SharePointService: + smc, err := generateSharepointListsMockColls(tenant, resource, c) + if err != nil { + return nil, err + } - for i := 0; i < len(c.items); i++ { - mc.Names[i] = c.items[i].name - mc.Data[i] = c.items[i].data + mc = smc } collections = append(collections, data.NoFetchRestoreCollection{Collection: mc}) @@ -223,6 +226,49 @@ func buildCollections( return collections, nil } +func generateExchangeMockColls(tenant string, resource string, c collection) (*exchMock.DataCollection, error) { + pth, err := path.Build( + tenant, + resource, + path.ExchangeService, + c.category, + false, + c.PathElements...) + if err != nil { + return nil, err + } + + emc := exchMock.NewCollection(pth, pth, len(c.items)) + + for i := 0; i < len(c.items); i++ { + emc.Names[i] = c.items[i].name + emc.Data[i] = c.items[i].data + } + + return emc, nil +} + +func generateSharepointListsMockColls(tenant string, resource string, c collection) (*siteMock.ListCollection, error) { + pth, err := path.BuildOrPrefix( + tenant, + resource, + path.SharePointService, + c.category, + false) + if err != nil { + return nil, err + } + + smc := siteMock.NewCollection(pth, pth, len(c.items)) + + for i := 0; i < len(c.items); i++ { + smc.Names[i] = c.items[i].name + smc.Data[i] = c.items[i].data + } + + return smc, nil +} + var ( folderAName = "folder-a" folderBName = "b" diff --git a/src/cmd/factory/impl/sharepoint.go b/src/cmd/factory/impl/sharepoint.go index 7f4313538..d1a097f1b 100644 --- a/src/cmd/factory/impl/sharepoint.go +++ b/src/cmd/factory/impl/sharepoint.go @@ -7,6 +7,8 @@ import ( . "github.com/alcionai/corso/src/cli/print" "github.com/alcionai/corso/src/cli/utils" + siteMock "github.com/alcionai/corso/src/internal/m365/service/sharepoint/mock" + "github.com/alcionai/corso/src/pkg/control" "github.com/alcionai/corso/src/pkg/count" "github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/logger" @@ -14,14 +16,23 @@ import ( "github.com/alcionai/corso/src/pkg/selectors" ) -var spFilesCmd = &cobra.Command{ - Use: "files", - Short: "Generate SharePoint files", - RunE: handleSharePointLibraryFileFactory, -} +var ( + spFilesCmd = &cobra.Command{ + Use: "files", + Short: "Generate SharePoint files", + RunE: handleSharePointLibraryFileFactory, + } + + spListsCmd = &cobra.Command{ + Use: "lists", + Short: "Generate SharePoint lists", + RunE: handleSharepointListsFactory, + } +) func AddSharePointCommands(cmd *cobra.Command) { cmd.AddCommand(spFilesCmd) + cmd.AddCommand(spListsCmd) } func handleSharePointLibraryFileFactory(cmd *cobra.Command, args []string) error { @@ -70,3 +81,52 @@ func handleSharePointLibraryFileFactory(cmd *cobra.Command, args []string) error return nil } + +func handleSharepointListsFactory(cmd *cobra.Command, args []string) error { + var ( + ctx = cmd.Context() + service = path.SharePointService + category = path.ListsCategory + errs = fault.New(false) + ) + + if utils.HasNoFlagsAndShownHelp(cmd) { + return nil + } + + ctrl, _, ins, err := getControllerAndVerifyResourceOwner(ctx, Site, path.SharePointService) + if err != nil { + return Only(ctx, err) + } + + deets, err := generateAndRestoreItems( + ctx, + ctrl, + service, + category, + selectors.NewSharePointRestore([]string{ins.ID()}).Selector, + Tenant, ins.ID(), Destination, + Count, + func(id, now, subject, body string) []byte { + listBytes, err := siteMock.ListBytes(id) + if err != nil { + logger.CtxErr(ctx, err) + return nil + } + return listBytes + }, + control.DefaultOptions(), + errs, + count.New()) + if err != nil { + return Only(ctx, err) + } + + for _, e := range errs.Recovered() { + logger.CtxErr(ctx, err).Error(e.Error()) + } + + deets.PrintEntries(ctx) + + return nil +} diff --git a/src/internal/m365/service/sharepoint/mock/list.go b/src/internal/m365/service/sharepoint/mock/list.go index ee0612834..93cd2ce39 100644 --- a/src/internal/m365/service/sharepoint/mock/list.go +++ b/src/internal/m365/service/sharepoint/mock/list.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/alcionai/clues" + "github.com/google/uuid" kjson "github.com/microsoft/kiota-serialization-json-go" "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/stretchr/testify/require" @@ -23,7 +24,7 @@ var ( type ListCollection struct { fullPath path.Path - Data []*ListData + Data [][]byte Names []string } @@ -56,14 +57,39 @@ func (mlc *ListCollection) Items( go func() { defer close(res) - for _, stream := range mlc.Data { - res <- stream + for i, stream := range mlc.Data { + res <- &ListData{ + ItemID: mlc.Names[i], + Reader: io.NopCloser(bytes.NewReader(stream)), + } } }() return res } +func NewCollection( + storagePath path.Path, + locationPath path.Path, + numMessagesToReturn int, +) *ListCollection { + c := &ListCollection{ + fullPath: storagePath, + Data: [][]byte{}, + Names: []string{}, + } + + for i := 0; i < numMessagesToReturn; i++ { + // We can plug in whatever data we want here (can be an io.Reader to a test data file if needed) + name := uuid.NewString() + lb, _ := ListBytes("MockListing" + name) + c.Data = append(c.Data, lb) + c.Names = append(c.Names, name) + } + + return c +} + type ListData struct { ItemID string Reader io.ReadCloser