check subfolders email counts in exchange (#2900)

<!-- PR description-->

check that no of emails in all subfolders is equivalent in restores. 

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [ ]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic
words" - "closes, fixes" to auto-close the Github issue. -->
* #<issue>

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
This commit is contained in:
neha_gupta 2023-03-23 17:10:05 +05:30 committed by GitHub
parent d1ce661b8f
commit a9c4479a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}
}
}