Remove unused code and tests (#3995)
Getting all folders was originally for the purge script. We've now transitioned to a powershell-based purge script making this code unused. Tests have coverage for a little more beyond just getting all folders but other code should either have tests for similar functionality or doesn't need it. --- #### 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 - [x] 🐛 Bugfix - [ ] 🗺️ Documentation - [ ] 🤖 Supportability/Tests - [ ] 💻 CI/Deployment - [ ] 🧹 Tech Debt/Cleanup #### Test Plan - [x] 💪 Manual - [ ] ⚡ Unit test - [ ] 💚 E2E
This commit is contained in:
parent
3b73b61c90
commit
95197872c5
@ -2,13 +2,10 @@ package onedrive
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/alcionai/clues"
|
|
||||||
"github.com/microsoftgraph/msgraph-sdk-go/models"
|
"github.com/microsoftgraph/msgraph-sdk-go/models"
|
||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/common/ptr"
|
|
||||||
"github.com/alcionai/corso/src/internal/m365/graph"
|
"github.com/alcionai/corso/src/internal/m365/graph"
|
||||||
"github.com/alcionai/corso/src/pkg/fault"
|
"github.com/alcionai/corso/src/pkg/fault"
|
||||||
"github.com/alcionai/corso/src/pkg/logger"
|
"github.com/alcionai/corso/src/pkg/logger"
|
||||||
@ -148,105 +145,3 @@ func newItem(name string, folder bool) *models.DriveItem {
|
|||||||
|
|
||||||
return itemToCreate
|
return itemToCreate
|
||||||
}
|
}
|
||||||
|
|
||||||
type Displayable struct {
|
|
||||||
models.DriveItemable
|
|
||||||
}
|
|
||||||
|
|
||||||
func (op *Displayable) GetDisplayName() *string {
|
|
||||||
return op.GetName()
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAllFolders returns all folders in all drives for the given user. If a
|
|
||||||
// prefix is given, returns all folders with that prefix, regardless of if they
|
|
||||||
// are a subfolder or top-level folder in the hierarchy.
|
|
||||||
func GetAllFolders(
|
|
||||||
ctx context.Context,
|
|
||||||
bh BackupHandler,
|
|
||||||
pager api.DrivePager,
|
|
||||||
prefix string,
|
|
||||||
errs *fault.Bus,
|
|
||||||
) ([]*Displayable, error) {
|
|
||||||
ds, err := api.GetAllDrives(ctx, pager, true, maxDrivesRetries)
|
|
||||||
if err != nil {
|
|
||||||
return nil, clues.Wrap(err, "getting OneDrive folders")
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
folders = map[string]*Displayable{}
|
|
||||||
el = errs.Local()
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, drive := range ds {
|
|
||||||
if el.Failure() != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
id = ptr.Val(drive.GetId())
|
|
||||||
name = ptr.Val(drive.GetName())
|
|
||||||
)
|
|
||||||
|
|
||||||
ictx := clues.Add(ctx, "drive_id", id, "drive_name", clues.Hide(name))
|
|
||||||
collector := func(
|
|
||||||
_ context.Context,
|
|
||||||
_, _ string,
|
|
||||||
items []models.DriveItemable,
|
|
||||||
_ map[string]string,
|
|
||||||
_ map[string]string,
|
|
||||||
_ map[string]struct{},
|
|
||||||
_ map[string]map[string]string,
|
|
||||||
_ bool,
|
|
||||||
_ *fault.Bus,
|
|
||||||
) error {
|
|
||||||
for _, item := range items {
|
|
||||||
// Skip the root item.
|
|
||||||
if item.GetRoot() != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only selecting folders right now, not packages.
|
|
||||||
if item.GetFolder() == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
itemID := ptr.Val(item.GetId())
|
|
||||||
if len(itemID) == 0 {
|
|
||||||
logger.Ctx(ctx).Info("folder missing ID")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasPrefix(ptr.Val(item.GetName()), prefix) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the item instead of the folder because the item has more
|
|
||||||
// functionality.
|
|
||||||
folders[itemID] = &Displayable{item}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, _, err = collectItems(
|
|
||||||
ictx,
|
|
||||||
bh.NewItemPager(id, "", nil),
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
collector,
|
|
||||||
map[string]string{},
|
|
||||||
"",
|
|
||||||
errs)
|
|
||||||
if err != nil {
|
|
||||||
el.AddRecoverable(ctx, clues.Wrap(err, "enumerating items in drive"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res := make([]*Displayable, 0, len(folders))
|
|
||||||
|
|
||||||
for _, f := range folders {
|
|
||||||
res = append(res, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, el.Failure()
|
|
||||||
}
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package onedrive
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/alcionai/clues"
|
"github.com/alcionai/clues"
|
||||||
@ -13,17 +12,13 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/internal/common/dttm"
|
|
||||||
"github.com/alcionai/corso/src/internal/common/prefixmatcher"
|
"github.com/alcionai/corso/src/internal/common/prefixmatcher"
|
||||||
"github.com/alcionai/corso/src/internal/common/ptr"
|
|
||||||
"github.com/alcionai/corso/src/internal/m365/graph"
|
"github.com/alcionai/corso/src/internal/m365/graph"
|
||||||
"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/pkg/account"
|
"github.com/alcionai/corso/src/pkg/account"
|
||||||
"github.com/alcionai/corso/src/pkg/control"
|
"github.com/alcionai/corso/src/pkg/control"
|
||||||
"github.com/alcionai/corso/src/pkg/fault"
|
"github.com/alcionai/corso/src/pkg/fault"
|
||||||
"github.com/alcionai/corso/src/pkg/logger"
|
|
||||||
"github.com/alcionai/corso/src/pkg/path"
|
|
||||||
"github.com/alcionai/corso/src/pkg/selectors"
|
"github.com/alcionai/corso/src/pkg/selectors"
|
||||||
"github.com/alcionai/corso/src/pkg/services/m365/api"
|
"github.com/alcionai/corso/src/pkg/services/m365/api"
|
||||||
"github.com/alcionai/corso/src/pkg/services/m365/api/mock"
|
"github.com/alcionai/corso/src/pkg/services/m365/api/mock"
|
||||||
@ -317,113 +312,6 @@ func (suite *OneDriveIntgSuite) SetupSuite() {
|
|||||||
require.NoError(t, err, clues.ToCore(err))
|
require.NoError(t, err, clues.ToCore(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *OneDriveIntgSuite) TestCreateGetDeleteFolder() {
|
|
||||||
t := suite.T()
|
|
||||||
|
|
||||||
ctx, flush := tester.NewContext(t)
|
|
||||||
defer flush()
|
|
||||||
|
|
||||||
var (
|
|
||||||
folderIDs = []string{}
|
|
||||||
folderName1 = "Corso_Folder_Test_" + dttm.FormatNow(dttm.SafeForTesting)
|
|
||||||
folderElements = []string{folderName1}
|
|
||||||
)
|
|
||||||
|
|
||||||
pager := suite.ac.Drives().NewUserDrivePager(suite.userID, nil)
|
|
||||||
|
|
||||||
drives, err := api.GetAllDrives(ctx, pager, true, maxDrivesRetries)
|
|
||||||
require.NoError(t, err, clues.ToCore(err))
|
|
||||||
require.NotEmpty(t, drives)
|
|
||||||
|
|
||||||
// TODO: Verify the intended drive
|
|
||||||
driveID := ptr.Val(drives[0].GetId())
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
for _, id := range folderIDs {
|
|
||||||
ictx := clues.Add(ctx, "folder_id", id)
|
|
||||||
|
|
||||||
// deletes require unique http clients
|
|
||||||
// https://github.com/alcionai/corso/issues/2707
|
|
||||||
err := suite.ac.Drives().DeleteItem(ictx, driveID, id)
|
|
||||||
if err != nil {
|
|
||||||
logger.CtxErr(ictx, err).Errorw("deleting folder")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
rootFolder, err := suite.ac.Drives().GetRootFolder(ctx, driveID)
|
|
||||||
require.NoError(t, err, clues.ToCore(err))
|
|
||||||
|
|
||||||
restoreDir := path.Builder{}.Append(folderElements...)
|
|
||||||
drivePath := path.DrivePath{
|
|
||||||
DriveID: driveID,
|
|
||||||
Root: "root:",
|
|
||||||
Folders: folderElements,
|
|
||||||
}
|
|
||||||
|
|
||||||
caches := NewRestoreCaches(nil)
|
|
||||||
caches.DriveIDToDriveInfo.Store(driveID, driveInfo{rootFolderID: ptr.Val(rootFolder.GetId())})
|
|
||||||
|
|
||||||
rh := NewRestoreHandler(suite.ac)
|
|
||||||
|
|
||||||
folderID, err := createRestoreFolders(ctx, rh, &drivePath, restoreDir, caches)
|
|
||||||
require.NoError(t, err, clues.ToCore(err))
|
|
||||||
|
|
||||||
folderIDs = append(folderIDs, folderID)
|
|
||||||
|
|
||||||
folderName2 := "Corso_Folder_Test_" + dttm.FormatNow(dttm.SafeForTesting)
|
|
||||||
restoreDir = restoreDir.Append(folderName2)
|
|
||||||
|
|
||||||
folderID, err = createRestoreFolders(ctx, rh, &drivePath, restoreDir, caches)
|
|
||||||
require.NoError(t, err, clues.ToCore(err))
|
|
||||||
|
|
||||||
folderIDs = append(folderIDs, folderID)
|
|
||||||
|
|
||||||
table := []struct {
|
|
||||||
name string
|
|
||||||
prefix string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "NoPrefix",
|
|
||||||
prefix: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Prefix",
|
|
||||||
prefix: "Corso_Folder_Test",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range table {
|
|
||||||
suite.Run(test.name, func() {
|
|
||||||
t := suite.T()
|
|
||||||
bh := itemBackupHandler{
|
|
||||||
suite.ac.Drives(),
|
|
||||||
(&selectors.OneDriveBackup{}).Folders(selectors.Any())[0],
|
|
||||||
}
|
|
||||||
pager := suite.ac.Drives().NewUserDrivePager(suite.userID, nil)
|
|
||||||
|
|
||||||
ctx, flush := tester.NewContext(t)
|
|
||||||
defer flush()
|
|
||||||
|
|
||||||
allFolders, err := GetAllFolders(ctx, bh, pager, test.prefix, fault.New(true))
|
|
||||||
require.NoError(t, err, clues.ToCore(err))
|
|
||||||
|
|
||||||
foundFolderIDs := []string{}
|
|
||||||
|
|
||||||
for _, f := range allFolders {
|
|
||||||
|
|
||||||
if ptr.Val(f.GetName()) == folderName1 || ptr.Val(f.GetName()) == folderName2 {
|
|
||||||
foundFolderIDs = append(foundFolderIDs, ptr.Val(f.GetId()))
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.True(t, strings.HasPrefix(ptr.Val(f.GetName()), test.prefix), "folder prefix")
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.ElementsMatch(t, folderIDs, foundFolderIDs)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *OneDriveIntgSuite) TestOneDriveNewCollections() {
|
func (suite *OneDriveIntgSuite) TestOneDriveNewCollections() {
|
||||||
creds, err := tconfig.NewM365Account(suite.T()).M365Config()
|
creds, err := tconfig.NewM365Account(suite.T()).M365Config()
|
||||||
require.NoError(suite.T(), err, clues.ToCore(err))
|
require.NoError(suite.T(), err, clues.ToCore(err))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user