From a9c4479a7b1685ef6b6f36d1f37abaaedfc66c0e Mon Sep 17 00:00:00 2001 From: neha_gupta Date: Thu, 23 Mar 2023 17:10:05 +0530 Subject: [PATCH] check subfolders email counts in exchange (#2900) check that no of emails in all subfolders is equivalent in restores. #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [ ] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * # #### Test Plan - [ ] :muscle: Manual --- src/cmd/sanity_test/sanity_tests.go | 142 +++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 13 deletions(-) diff --git a/src/cmd/sanity_test/sanity_tests.go b/src/cmd/sanity_test/sanity_tests.go index 88ec297e8..506c00ca3 100644 --- a/src/cmd/sanity_test/sanity_tests.go +++ b/src/cmd/sanity_test/sanity_tests.go @@ -46,13 +46,21 @@ func main() { } } -func checkEmailRestoration(client *msgraphsdk.GraphServiceClient, testUser, folderName string, startTime time.Time) { +// checkEmailRestoration verifies that the emails count in restored folder is equivalent to +// emails in actual m365 account +func checkEmailRestoration( + client *msgraphsdk.GraphServiceClient, + testUser, + folderName string, + startTime time.Time, +) { var ( messageCount = make(map[string]int32) restoreFolder models.MailFolderable ) - builder := client.UsersById(testUser).MailFolders() + user := client.UsersById(testUser) + builder := user.MailFolders() for { result, err := builder.Get(context.Background(), nil) @@ -64,7 +72,10 @@ func checkEmailRestoration(client *msgraphsdk.GraphServiceClient, testUser, fold res := result.GetValue() for _, r := range res { - name := *r.GetDisplayName() + name, ok := ptr.ValOK(r.GetDisplayName()) + if !ok { + continue + } var rStartTime time.Time @@ -82,7 +93,9 @@ func checkEmailRestoration(client *msgraphsdk.GraphServiceClient, testUser, fold continue } - messageCount[*r.GetDisplayName()] = *r.GetTotalItemCount() + getAllSubFolder(client, testUser, r, name, messageCount) + + messageCount[name], _ = ptr.ValOK(r.GetTotalItemCount()) } link, ok := ptr.ValOK(result.GetOdataNextLink()) @@ -93,8 +106,13 @@ func checkEmailRestoration(client *msgraphsdk.GraphServiceClient, testUser, fold builder = users.NewItemMailFoldersRequestBuilder(link, client.GetAdapter()) } - user := client.UsersById(testUser) - folder := user.MailFoldersById(*restoreFolder.GetId()) + folderID, ok := ptr.ValOK(restoreFolder.GetId()) + if !ok { + fmt.Printf("can't find ID of restore folder") + os.Exit(1) + } + + folder := user.MailFoldersById(folderID) childFolder, err := folder.ChildFolders().Get(context.Background(), nil) if err != nil { @@ -103,15 +121,113 @@ func checkEmailRestoration(client *msgraphsdk.GraphServiceClient, testUser, fold } for _, restore := range childFolder.GetValue() { - if messageCount[*restore.GetDisplayName()] != *restore.GetTotalItemCount() { - fmt.Println("Restore was not succesfull for: ", - *restore.GetDisplayName(), - "Folder count: ", - messageCount[*restore.GetDisplayName()], - "Restore count: ", - *restore.GetTotalItemCount()) + restoreDisplayName, ok := ptr.ValOK(restore.GetDisplayName()) + if !ok { + continue + } + + restoreItemCount, _ := ptr.ValOK(restore.GetTotalItemCount()) + + if messageCount[restoreDisplayName] != restoreItemCount { + fmt.Println("Restore was not succesfull for: ", restoreDisplayName, + "Folder count: ", messageCount[restoreDisplayName], + "Restore count: ", restoreItemCount) os.Exit(1) } + + checkAllSubFolder(client, testUser, restore, restoreDisplayName, messageCount) + } +} + +// getAllSubFolder will recursively check for all subfolders and get the corresponding +// email count. +func getAllSubFolder( + client *msgraphsdk.GraphServiceClient, + testUser string, + r models.MailFolderable, + parentFolder string, + messageCount map[string]int32, +) { + folderID, ok := ptr.ValOK(r.GetId()) + + if !ok { + fmt.Println("unable to get sub folder ID") + return + } + + user := client.UsersById(testUser) + folder := user.MailFoldersById(folderID) + + childFolder, err := folder.ChildFolders().Get(context.Background(), nil) + if err != nil { + fmt.Printf("Error getting the drive: %v\n", err) + os.Exit(1) + } + + for _, child := range childFolder.GetValue() { + childDisplayName, _ := ptr.ValOK(child.GetDisplayName()) + + fullFolderName := parentFolder + "/" + childDisplayName + + messageCount[fullFolderName], _ = ptr.ValOK(child.GetTotalItemCount()) + + childFolderCount, _ := ptr.ValOK(child.GetChildFolderCount()) + + // recursively check for subfolders + if childFolderCount > 0 { + parentFolder := fullFolderName + + getAllSubFolder(client, testUser, child, parentFolder, messageCount) + } + } +} + +// checkAllSubFolder will recursively traverse inside the restore folder and +// verify that data matched in all subfolders +func checkAllSubFolder( + client *msgraphsdk.GraphServiceClient, + testUser string, + r models.MailFolderable, + parentFolder string, + messageCount map[string]int32, +) { + folderID, ok := ptr.ValOK(r.GetId()) + + if !ok { + fmt.Println("unable to get sub folder ID") + return + } + + user := client.UsersById(testUser) + folder := user.MailFoldersById(folderID) + + childFolder, err := folder.ChildFolders().Get(context.Background(), nil) + if err != nil { + fmt.Printf("Error getting the drive: %v\n", err) + os.Exit(1) + } + + for _, child := range childFolder.GetValue() { + childDisplayName, _ := ptr.ValOK(child.GetDisplayName()) + + fullFolderName := parentFolder + "/" + childDisplayName + + childTotalCount, _ := ptr.ValOK(child.GetTotalItemCount()) + + if messageCount[fullFolderName] != childTotalCount { + fmt.Println("Restore was not succesfull for: ", fullFolderName, + "Folder count: ", messageCount[fullFolderName], + "Restore count: ", childTotalCount) + os.Exit(1) + } + + childFolderCount, _ := ptr.ValOK(child.GetChildFolderCount()) + + if childFolderCount > 0 { + parentFolder := fullFolderName + + checkAllSubFolder(client, testUser, child, parentFolder, messageCount) + } } }