From 0b65b2020034d2194b312cb9232c1bfb2e5700c8 Mon Sep 17 00:00:00 2001 From: Keepers Date: Mon, 10 Oct 2022 15:14:10 -0600 Subject: [PATCH] attach logger to test ctx, use flusher (#1083) ## Description Replace all cases of context.Background() and context.TODO() with the testing ctx, which seeds a logging instance into the context. The seed is less important than the flush action, since errant log.Ctx(ctx) calls will automatically use the log singleton. But in order for the logs to show up within the test, they need to get flushed. See my comments for focus on non-chaff changes. ## Type of change - [x] :robot: Test ## Issue(s) * #902 ## Test Plan - [x] :muscle: Manual - [x] :zap: Unit test --- src/cli/backup/exchange_integration_test.go | 40 +++++++++++--- src/cli/backup/exchange_test.go | 11 ++-- src/cli/cli_test.go | 4 -- src/cli/print/print_test.go | 7 ++- src/cli/repo/s3_integration_test.go | 23 +++++--- src/cli/restore/exchange_integration_test.go | 20 ++++--- src/internal/common/buckets_test.go | 2 - .../exchange/exchange_service_test.go | 31 +++++++---- .../connector/exchange/iterators_test.go | 5 +- .../exchange/mail_folder_cache_test.go | 20 ++++--- .../exchange/service_functions_test.go | 17 ++++-- .../graph_connector_disconnected_test.go | 15 ++++-- .../connector/graph_connector_test.go | 52 +++++++++++++------ .../connector/onedrive/collections_test.go | 8 ++- src/internal/connector/onedrive/item_test.go | 14 +++-- src/internal/connector/support/status_test.go | 29 +++++++---- src/internal/kopia/conn_test.go | 43 +++++++++++---- src/internal/kopia/model_store_test.go | 28 +++++++--- src/internal/kopia/wrapper_test.go | 35 +++++++++---- src/internal/operations/backup_test.go | 18 +++++-- src/internal/operations/restore_test.go | 25 ++++++--- src/internal/tester/cli.go | 12 +++-- src/pkg/logger/logger.go | 11 ++-- src/pkg/repository/repository_load_test.go | 29 ++++------- src/pkg/repository/repository_test.go | 26 +++++++--- src/pkg/selectors/exchange_test.go | 7 ++- src/pkg/selectors/onedrive_test.go | 7 ++- src/pkg/selectors/scopes_test.go | 7 ++- src/pkg/selectors/selectors_reduce_test.go | 6 ++- src/pkg/services/m365/m365_test.go | 6 ++- src/pkg/store/backup_test.go | 17 +++--- 31 files changed, 393 insertions(+), 182 deletions(-) diff --git a/src/cli/backup/exchange_integration_test.go b/src/cli/backup/exchange_integration_test.go index eb85c8a75..2a101ca15 100644 --- a/src/cli/backup/exchange_integration_test.go +++ b/src/cli/backup/exchange_integration_test.go @@ -61,6 +61,10 @@ func TestBackupExchangeIntegrationSuite(t *testing.T) { func (suite *BackupExchangeIntegrationSuite) SetupSuite() { t := suite.T() + ctx, flush := tester.NewContext() + + defer flush() + _, err := tester.GetRequiredEnvSls( tester.AWSStorageCredEnvs, tester.M365AcctCredEnvs) @@ -82,7 +86,7 @@ func (suite *BackupExchangeIntegrationSuite) SetupSuite() { suite.vpr, suite.cfgFP, err = tester.MakeTempTestConfigClone(t, force) require.NoError(t, err) - ctx := config.SetViper(tester.NewContext(), suite.vpr) + ctx = config.SetViper(ctx, suite.vpr) suite.m365UserID = tester.M365UserID(t) // init the repo first @@ -97,7 +101,9 @@ func (suite *BackupExchangeIntegrationSuite) TestExchangeBackupCmd() { recorder.Reset() suite.T().Run(set.String(), func(t *testing.T) { - ctx := config.SetViper(tester.NewContext(), suite.vpr) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + defer flush() cmd := tester.StubRootCmd( "backup", "create", "exchange", @@ -171,7 +177,11 @@ func (suite *PreparedBackupExchangeIntegrationSuite) SetupSuite() { suite.vpr, suite.cfgFP, err = tester.MakeTempTestConfigClone(t, force) require.NoError(t, err) - ctx := config.SetViper(tester.NewContext(), suite.vpr) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + + defer flush() + suite.m365UserID = tester.M365UserID(t) // init the repo first @@ -220,7 +230,9 @@ func (suite *PreparedBackupExchangeIntegrationSuite) TestExchangeListCmd() { recorder.Reset() suite.T().Run(set.String(), func(t *testing.T) { - ctx := config.SetViper(tester.NewContext(), suite.vpr) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + defer flush() cmd := tester.StubRootCmd( "backup", "list", "exchange", @@ -248,7 +260,10 @@ func (suite *PreparedBackupExchangeIntegrationSuite) TestExchangeDetailsCmd() { recorder.Reset() suite.T().Run(set.String(), func(t *testing.T) { - ctx := config.SetViper(tester.NewContext(), suite.vpr) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + defer flush() + bID := suite.backupOps[set].Results.BackupID // fetch the details from the repo first @@ -342,7 +357,10 @@ func (suite *BackupDeleteExchangeIntegrationSuite) SetupSuite() { suite.vpr, suite.cfgFP, err = tester.MakeTempTestConfigClone(t, force) require.NoError(t, err) - ctx := config.SetViper(tester.NewContext(), suite.vpr) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + + defer flush() // init the repo first suite.repo, err = repository.Initialize(ctx, suite.acct, suite.st, control.Options{}) @@ -360,8 +378,11 @@ func (suite *BackupDeleteExchangeIntegrationSuite) SetupSuite() { } func (suite *BackupDeleteExchangeIntegrationSuite) TestExchangeBackupDeleteCmd() { - ctx := config.SetViper(tester.NewContext(), suite.vpr) t := suite.T() + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + + defer flush() cmd := tester.StubRootCmd( "backup", "delete", "exchange", @@ -383,8 +404,11 @@ func (suite *BackupDeleteExchangeIntegrationSuite) TestExchangeBackupDeleteCmd() } func (suite *BackupDeleteExchangeIntegrationSuite) TestExchangeBackupDeleteCmd_UnknownID() { - ctx := config.SetViper(tester.NewContext(), suite.vpr) t := suite.T() + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + + defer flush() cmd := tester.StubRootCmd( "backup", "delete", "exchange", diff --git a/src/cli/backup/exchange_test.go b/src/cli/backup/exchange_test.go index 92ee37b84..3b5ee6464 100644 --- a/src/cli/backup/exchange_test.go +++ b/src/cli/backup/exchange_test.go @@ -1,7 +1,6 @@ package backup import ( - "context" "testing" "github.com/spf13/cobra" @@ -215,7 +214,8 @@ func (suite *ExchangeSuite) TestExchangeBackupCreateSelectors() { } func (suite *ExchangeSuite) TestExchangeBackupDetailsSelectors() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() for _, test := range testdata.ExchangeOptionDetailLookups { suite.T().Run(test.Name, func(t *testing.T) { @@ -234,9 +234,11 @@ func (suite *ExchangeSuite) TestExchangeBackupDetailsSelectors() { func (suite *ExchangeSuite) TestExchangeBackupDetailsSelectorsBadBackupID() { t := suite.T() - ctx := context.Background() + ctx, flush := tester.NewContext() backupGetter := &testdata.MockBackupGetter{} + defer flush() + output, err := runDetailsExchangeCmd( ctx, backupGetter, @@ -250,7 +252,8 @@ func (suite *ExchangeSuite) TestExchangeBackupDetailsSelectorsBadBackupID() { // TODO(ashmrtn): Uncomment these when the CLI validates flag input values. //func (suite *ExchangeSuite) TestExchangeBackupDetailsSelectorsBadFormats() { -// ctx := context.Background() +// ctx, flush := tester.NewContext() +// defer flush() // // for _, test := range testdata.BadExchangeOptionsFormats { // suite.T().Run(test.Name, func(t *testing.T) { diff --git a/src/cli/cli_test.go b/src/cli/cli_test.go index 1c7ddbec0..dc7340fc0 100644 --- a/src/cli/cli_test.go +++ b/src/cli/cli_test.go @@ -10,10 +10,6 @@ import ( "github.com/alcionai/corso/src/cli" ) -type CliSuite struct { - suite.Suite -} - type CLISuite struct { suite.Suite } diff --git a/src/cli/print/print_test.go b/src/cli/print/print_test.go index a2efccd8a..0b7370c07 100644 --- a/src/cli/print/print_test.go +++ b/src/cli/print/print_test.go @@ -2,13 +2,12 @@ package print import ( "bytes" + "context" "testing" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" - - "github.com/alcionai/corso/src/internal/tester" ) type PrintUnitSuite struct { @@ -20,10 +19,10 @@ func TestPrintUnitSuite(t *testing.T) { } func (suite *PrintUnitSuite) TestOnly() { - ctx := tester.NewContext() t := suite.T() c := &cobra.Command{} - ctx = SetRootCmd(ctx, c) + // cannot use tester.NewContext() here: circular imports + ctx := SetRootCmd(context.Background(), c) assert.NoError(t, Only(ctx, nil)) assert.True(t, c.SilenceUsage) } diff --git a/src/cli/repo/s3_integration_test.go b/src/cli/repo/s3_integration_test.go index 2022fbd1c..ac45c6960 100644 --- a/src/cli/repo/s3_integration_test.go +++ b/src/cli/repo/s3_integration_test.go @@ -55,7 +55,8 @@ func (suite *S3IntegrationSuite) TestInitS3Cmd() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := tester.NewContext() + ctx, flush := tester.NewContext() + defer flush() st := tester.NewPrefixedS3Storage(t) cfg, err := st.S3Config() @@ -80,8 +81,10 @@ func (suite *S3IntegrationSuite) TestInitS3Cmd() { } func (suite *S3IntegrationSuite) TestInitMultipleTimes() { - ctx := tester.NewContext() t := suite.T() + ctx, flush := tester.NewContext() + + defer flush() st := tester.NewPrefixedS3Storage(t) cfg, err := st.S3Config() @@ -108,8 +111,10 @@ func (suite *S3IntegrationSuite) TestInitMultipleTimes() { } func (suite *S3IntegrationSuite) TestInitS3Cmd_missingBucket() { - ctx := tester.NewContext() t := suite.T() + ctx, flush := tester.NewContext() + + defer flush() st := tester.NewPrefixedS3Storage(t) cfg, err := st.S3Config() @@ -147,7 +152,9 @@ func (suite *S3IntegrationSuite) TestConnectS3Cmd() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := tester.NewContext() + ctx, flush := tester.NewContext() + defer flush() + st := tester.NewPrefixedS3Storage(t) cfg, err := st.S3Config() require.NoError(t, err) @@ -182,8 +189,10 @@ func (suite *S3IntegrationSuite) TestConnectS3Cmd() { } func (suite *S3IntegrationSuite) TestConnectS3Cmd_BadBucket() { - ctx := tester.NewContext() t := suite.T() + ctx, flush := tester.NewContext() + + defer flush() st := tester.NewPrefixedS3Storage(t) cfg, err := st.S3Config() @@ -206,8 +215,10 @@ func (suite *S3IntegrationSuite) TestConnectS3Cmd_BadBucket() { } func (suite *S3IntegrationSuite) TestConnectS3Cmd_BadPrefix() { - ctx := tester.NewContext() t := suite.T() + ctx, flush := tester.NewContext() + + defer flush() st := tester.NewPrefixedS3Storage(t) cfg, err := st.S3Config() diff --git a/src/cli/restore/exchange_integration_test.go b/src/cli/restore/exchange_integration_test.go index f1ea5a630..bf7dc161e 100644 --- a/src/cli/restore/exchange_integration_test.go +++ b/src/cli/restore/exchange_integration_test.go @@ -14,7 +14,6 @@ import ( "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/control" - "github.com/alcionai/corso/src/pkg/logger" "github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/repository" "github.com/alcionai/corso/src/pkg/selectors" @@ -59,6 +58,10 @@ func TestRestoreExchangeIntegrationSuite(t *testing.T) { func (suite *RestoreExchangeIntegrationSuite) SetupSuite() { t := suite.T() + + ctx, flush := tester.NewContext() + defer flush() + _, err := tester.GetRequiredEnvSls( tester.AWSStorageCredEnvs, tester.M365AcctCredEnvs, @@ -80,7 +83,6 @@ func (suite *RestoreExchangeIntegrationSuite) SetupSuite() { suite.vpr, suite.cfgFP, err = tester.MakeTempTestConfigClone(t, force) require.NoError(t, err) - ctx := config.SetViper(tester.NewContext(), suite.vpr) suite.m365UserID = tester.M365UserID(t) // init the repo first @@ -125,9 +127,10 @@ func (suite *RestoreExchangeIntegrationSuite) SetupSuite() { func (suite *RestoreExchangeIntegrationSuite) TestExchangeRestoreCmd() { for _, set := range backupDataSets { suite.T().Run(set.String(), func(t *testing.T) { - ctx := config.SetViper(tester.NewContext(), suite.vpr) - ctx, _ = logger.SeedLevel(ctx, logger.Development) - defer logger.Flush(ctx) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + + defer flush() cmd := tester.StubRootCmd( "restore", "exchange", @@ -148,9 +151,10 @@ func (suite *RestoreExchangeIntegrationSuite) TestExchangeRestoreCmd_badTimeFlag } suite.T().Run(set.String(), func(t *testing.T) { - ctx := config.SetViper(tester.NewContext(), suite.vpr) - ctx, _ = logger.SeedLevel(ctx, logger.Development) - defer logger.Flush(ctx) + ctx, flush := tester.NewContext() + ctx = config.SetViper(ctx, suite.vpr) + + defer flush() var timeFilter string switch set { diff --git a/src/internal/common/buckets_test.go b/src/internal/common/buckets_test.go index dbc14d704..b44149deb 100644 --- a/src/internal/common/buckets_test.go +++ b/src/internal/common/buckets_test.go @@ -1,7 +1,6 @@ package common_test import ( - "context" "testing" "github.com/stretchr/testify/assert" @@ -12,7 +11,6 @@ import ( type CommonBucketsSuite struct { suite.Suite - ctx context.Context } func TestCommonBucketsSuite(t *testing.T) { diff --git a/src/internal/connector/exchange/exchange_service_test.go b/src/internal/connector/exchange/exchange_service_test.go index 8298d6a25..ca5c29991 100644 --- a/src/internal/connector/exchange/exchange_service_test.go +++ b/src/internal/connector/exchange/exchange_service_test.go @@ -260,7 +260,9 @@ func (suite *ExchangeServiceSuite) TestSetupExchangeCollection() { // TestGraphQueryFunctions verifies if Query functions APIs // through Microsoft Graph are functional func (suite *ExchangeServiceSuite) TestGraphQueryFunctions() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + userID := tester.M365UserID(suite.T()) tests := []struct { name string @@ -309,7 +311,10 @@ func (suite *ExchangeServiceSuite) TestGraphQueryFunctions() { // at the top level of the file tree func (suite *ExchangeServiceSuite) TestGetContainerID() { userID := tester.M365UserID(suite.T()) - ctx := context.Background() + ctx, flush := tester.NewContext() + + defer flush() + tests := []struct { name string containerName string @@ -370,14 +375,16 @@ func (suite *ExchangeServiceSuite) TestGetContainerID() { //========================== // Restore Functions -//====================== +//========================== // TestRestoreContact ensures contact object can be created, placed into // the Corso Folder. The function handles test clean-up. func (suite *ExchangeServiceSuite) TestRestoreContact() { + ctx, flush := tester.NewContext() + defer flush() + var ( t = suite.T() - ctx = context.Background() userID = tester.M365UserID(t) now = time.Now() folderName = "TestRestoreContact: " + common.FormatSimpleDateTime(now) @@ -394,7 +401,7 @@ func (suite *ExchangeServiceSuite) TestRestoreContact() { assert.NoError(t, err) }() - info, err := RestoreExchangeContact(context.Background(), + info, err := RestoreExchangeContact(ctx, mockconnector.GetMockContactBytes("Corso TestContact"), suite.es, control.Copy, @@ -407,9 +414,11 @@ func (suite *ExchangeServiceSuite) TestRestoreContact() { // TestRestoreEvent verifies that event object is able to created // and sent into the test account of the Corso user in the newly created Corso Calendar func (suite *ExchangeServiceSuite) TestRestoreEvent() { + ctx, flush := tester.NewContext() + defer flush() + var ( t = suite.T() - ctx = context.Background() userID = tester.M365UserID(t) name = "TestRestoreEvent: " + common.FormatSimpleDateTime(time.Now()) ) @@ -425,7 +434,7 @@ func (suite *ExchangeServiceSuite) TestRestoreEvent() { assert.NoError(t, err) }() - info, err := RestoreExchangeEvent(context.Background(), + info, err := RestoreExchangeEvent(ctx, mockconnector.GetMockEventWithAttendeesBytes(name), suite.es, control.Copy, @@ -438,7 +447,9 @@ func (suite *ExchangeServiceSuite) TestRestoreEvent() { // TestGetRestoreContainer checks the ability to Create a "container" for the // GraphConnector's Restore Workflow based on OptionIdentifier. func (suite *ExchangeServiceSuite) TestGetRestoreContainer() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + dest := tester.DefaultTestRestoreDestination() tests := []struct { name string @@ -498,7 +509,9 @@ func (suite *ExchangeServiceSuite) TestGetRestoreContainer() { // TestRestoreExchangeObject verifies path.Category usage for restored objects func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() userID := tester.M365UserID(t) now := time.Now() diff --git a/src/internal/connector/exchange/iterators_test.go b/src/internal/connector/exchange/iterators_test.go index 770f2fbec..706aa1a27 100644 --- a/src/internal/connector/exchange/iterators_test.go +++ b/src/internal/connector/exchange/iterators_test.go @@ -1,7 +1,6 @@ package exchange import ( - "context" "testing" absser "github.com/microsoft/kiota-abstractions-go/serialization" @@ -71,8 +70,10 @@ func loadService(t *testing.T) *exchangeService { // TestIterativeFunctions verifies that GraphQuery to Iterate // functions are valid for current versioning of msgraph-go-sdk func (suite *ExchangeIteratorSuite) TestIterativeFunctions() { + ctx, flush := tester.NewContext() + defer flush() + var ( - ctx = context.Background() t = suite.T() mailScope, contactScope, eventScope []selectors.ExchangeScope userID = tester.M365UserID(t) diff --git a/src/internal/connector/exchange/mail_folder_cache_test.go b/src/internal/connector/exchange/mail_folder_cache_test.go index c3b0671e5..db3b9efd2 100644 --- a/src/internal/connector/exchange/mail_folder_cache_test.go +++ b/src/internal/connector/exchange/mail_folder_cache_test.go @@ -1,7 +1,6 @@ package exchange import ( - "context" stdpath "path" "strings" "testing" @@ -220,7 +219,8 @@ func TestConfiguredMailFolderCacheUnitSuite(t *testing.T) { } func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderNoPathsCached() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() for _, c := range suite.allContainers { suite.T().Run(*c.GetDisplayName(), func(t *testing.T) { @@ -233,8 +233,10 @@ func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderNoPathsCa } func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderCachesPaths() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() c := suite.allContainers[len(suite.allContainers)-1] p, err := suite.mc.IDToPath(ctx, c.id) @@ -251,8 +253,10 @@ func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderCachesPat } func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderErrorsParentNotFound() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() last := suite.allContainers[len(suite.allContainers)-1] almostLast := suite.allContainers[len(suite.allContainers)-2] @@ -263,8 +267,10 @@ func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderErrorsPar } func (suite *ConfiguredMailFolderCacheUnitSuite) TestLookupCachedFolderErrorsNotFound() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() _, err := suite.mc.IDToPath(ctx, "foo") assert.Error(t, err) @@ -305,6 +311,9 @@ func TestMailFolderCacheIntegrationSuite(t *testing.T) { } func (suite *MailFolderCacheIntegrationSuite) TestDeltaFetch() { + ctx, flush := tester.NewContext() + defer flush() + tests := []struct { name string root string @@ -318,7 +327,6 @@ func (suite *MailFolderCacheIntegrationSuite) TestDeltaFetch() { root: topFolderID, }, } - ctx := context.Background() userID := tester.M365UserID(suite.T()) for _, test := range tests { diff --git a/src/internal/connector/exchange/service_functions_test.go b/src/internal/connector/exchange/service_functions_test.go index 9711aaf7c..ade9fde44 100644 --- a/src/internal/connector/exchange/service_functions_test.go +++ b/src/internal/connector/exchange/service_functions_test.go @@ -1,7 +1,6 @@ package exchange import ( - "context" "testing" "github.com/stretchr/testify/assert" @@ -34,8 +33,10 @@ func (suite *ServiceFunctionsIntegrationSuite) SetupSuite() { } func (suite *ServiceFunctionsIntegrationSuite) TestGetAllCalendars() { + ctx, flush := tester.NewContext() + defer flush() + gs := loadService(suite.T()) - ctx := context.Background() table := []struct { name, contains, user string @@ -79,9 +80,11 @@ func (suite *ServiceFunctionsIntegrationSuite) TestGetAllCalendars() { } func (suite *ServiceFunctionsIntegrationSuite) TestGetAllContactFolders() { + ctx, flush := tester.NewContext() + defer flush() + gs := loadService(suite.T()) user := tester.M365UserID(suite.T()) - ctx := context.Background() table := []struct { name, contains, user string @@ -125,8 +128,10 @@ func (suite *ServiceFunctionsIntegrationSuite) TestGetAllContactFolders() { } func (suite *ServiceFunctionsIntegrationSuite) TestGetAllMailFolders() { + ctx, flush := tester.NewContext() + defer flush() + gs := loadService(suite.T()) - ctx := context.Background() table := []struct { name, contains, user string @@ -170,7 +175,9 @@ func (suite *ServiceFunctionsIntegrationSuite) TestGetAllMailFolders() { } func (suite *ServiceFunctionsIntegrationSuite) TestCollectContainers() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + failFast := false containerCount := 1 t := suite.T() diff --git a/src/internal/connector/graph_connector_disconnected_test.go b/src/internal/connector/graph_connector_disconnected_test.go index 6a68143e9..0caca3457 100644 --- a/src/internal/connector/graph_connector_disconnected_test.go +++ b/src/internal/connector/graph_connector_disconnected_test.go @@ -1,7 +1,6 @@ package connector import ( - "context" "sync" "testing" @@ -30,7 +29,9 @@ func TestDisconnectedGraphSuite(t *testing.T) { } func (suite *DisconnectedGraphConnectorSuite) TestBadConnection() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + table := []struct { name string acct func(t *testing.T) account.Account @@ -88,8 +89,11 @@ func (suite *DisconnectedGraphConnectorSuite) TestBuild() { } func statusTestTask(gc *GraphConnector, objects, success, folder int) { + ctx, flush := tester.NewContext() + defer flush() + status := support.CreateStatus( - context.Background(), + ctx, support.Restore, folder, support.CollectionMetrics{ Objects: objects, @@ -181,7 +185,10 @@ func (suite *DisconnectedGraphConnectorSuite) TestGraphConnector_ErrorChecking() func (suite *DisconnectedGraphConnectorSuite) TestRestoreFailsBadService() { t := suite.T() - ctx := context.Background() + + ctx, flush := tester.NewContext() + defer flush() + gc := GraphConnector{wg: &sync.WaitGroup{}} sel := selectors.Selector{ Service: selectors.ServiceUnknown, diff --git a/src/internal/connector/graph_connector_test.go b/src/internal/connector/graph_connector_test.go index 7a9b3bd28..8a4e1aebc 100644 --- a/src/internal/connector/graph_connector_test.go +++ b/src/internal/connector/graph_connector_test.go @@ -45,7 +45,9 @@ func TestGraphConnectorIntegrationSuite(t *testing.T) { } func (suite *GraphConnectorIntegrationSuite) SetupSuite() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + _, err := tester.GetRequiredEnvVars(tester.M365AcctCredEnvs...) require.NoError(suite.T(), err) suite.connector = loadConnector(ctx, suite.T()) @@ -61,7 +63,10 @@ func (suite *GraphConnectorIntegrationSuite) TestSetTenantUsers() { Users: make(map[string]string, 0), credentials: suite.connector.credentials, } - ctx := context.Background() + + ctx, flush := tester.NewContext() + defer flush() + service, err := newConnector.createService(false) require.NoError(suite.T(), err) @@ -80,7 +85,9 @@ func (suite *GraphConnectorIntegrationSuite) TestSetTenantUsers() { // - contacts // - events func (suite *GraphConnectorIntegrationSuite) TestExchangeDataCollection() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + connector := loadConnector(ctx, suite.T()) tests := []struct { name string @@ -137,12 +144,14 @@ func (suite *GraphConnectorIntegrationSuite) TestExchangeDataCollection() { // test account can be successfully downloaded into bytes and restored into // M365 mail objects func (suite *GraphConnectorIntegrationSuite) TestMailSerializationRegression() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() connector := loadConnector(ctx, t) sel := selectors.NewExchangeBackup() sel.Include(sel.MailFolders([]string{suite.user}, []string{exchange.DefaultMailFolder})) - collection, err := connector.createCollections(context.Background(), sel.Scopes()[0]) + collection, err := connector.createCollections(ctx, sel.Scopes()[0]) require.NoError(t, err) for _, edc := range collection { @@ -170,7 +179,10 @@ func (suite *GraphConnectorIntegrationSuite) TestMailSerializationRegression() { // and to store contact within Collection. Downloaded contacts are run through // a regression test to ensure that downloaded items can be uploaded. func (suite *GraphConnectorIntegrationSuite) TestContactSerializationRegression() { - connector := loadConnector(context.Background(), suite.T()) + ctx, flush := tester.NewContext() + defer flush() + + connector := loadConnector(ctx, suite.T()) tests := []struct { name string @@ -181,7 +193,7 @@ func (suite *GraphConnectorIntegrationSuite) TestContactSerializationRegression( getCollection: func(t *testing.T) []*exchange.Collection { sel := selectors.NewExchangeBackup() sel.Include(sel.ContactFolders([]string{suite.user}, []string{exchange.DefaultContactFolder})) - collections, err := connector.createCollections(context.Background(), sel.Scopes()[0]) + collections, err := connector.createCollections(ctx, sel.Scopes()[0]) require.NoError(t, err) return collections @@ -219,7 +231,10 @@ func (suite *GraphConnectorIntegrationSuite) TestContactSerializationRegression( // TestEventsSerializationRegression ensures functionality of createCollections // to be able to successfully query, download and restore event objects func (suite *GraphConnectorIntegrationSuite) TestEventsSerializationRegression() { - connector := loadConnector(context.Background(), suite.T()) + ctx, flush := tester.NewContext() + defer flush() + + connector := loadConnector(ctx, suite.T()) tests := []struct { name, expected string @@ -231,7 +246,7 @@ func (suite *GraphConnectorIntegrationSuite) TestEventsSerializationRegression() getCollection: func(t *testing.T) []*exchange.Collection { sel := selectors.NewExchangeBackup() sel.Include(sel.EventCalendars([]string{suite.user}, []string{exchange.DefaultCalendar})) - collections, err := connector.createCollections(context.Background(), sel.Scopes()[0]) + collections, err := connector.createCollections(ctx, sel.Scopes()[0]) require.NoError(t, err) return collections @@ -243,7 +258,7 @@ func (suite *GraphConnectorIntegrationSuite) TestEventsSerializationRegression() getCollection: func(t *testing.T) []*exchange.Collection { sel := selectors.NewExchangeBackup() sel.Include(sel.EventCalendars([]string{suite.user}, []string{"Birthdays"})) - collections, err := connector.createCollections(context.Background(), sel.Scopes()[0]) + collections, err := connector.createCollections(ctx, sel.Scopes()[0]) require.NoError(t, err) return collections @@ -283,7 +298,9 @@ func (suite *GraphConnectorIntegrationSuite) TestEventsSerializationRegression() // The final test insures that more than a 75% of the user collections are // returned. If an error was experienced, the test will fail overall func (suite *GraphConnectorIntegrationSuite) TestAccessOfInboxAllUsers() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() connector := loadConnector(ctx, t) sel := selectors.NewExchangeBackup() @@ -293,7 +310,7 @@ func (suite *GraphConnectorIntegrationSuite) TestAccessOfInboxAllUsers() { for _, scope := range scopes { users := scope.Get(selectors.ExchangeUser) standard := (len(users) / 4) * 3 - collections, err := connector.createCollections(context.Background(), scope) + collections, err := connector.createCollections(ctx, scope) require.NoError(t, err) suite.Greater(len(collections), standard) } @@ -342,7 +359,8 @@ func (suite *GraphConnectorIntegrationSuite) TestEmptyCollections() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() deets, err := suite.connector.RestoreDataCollections(ctx, test.sel, dest, test.col) require.NoError(t, err) @@ -645,7 +663,9 @@ func (suite *GraphConnectorIntegrationSuite) TestRestoreAndBackup() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + // Get a dest per test so they're independent. dest := tester.DefaultTestRestoreDestination() @@ -845,7 +865,9 @@ func (suite *GraphConnectorIntegrationSuite) TestMultiFolderBackupDifferentNames for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + restoreSel := getSelectorWith(test.service) dests := make([]control.RestoreDestination, 0, len(test.collections)) allItems := 0 diff --git a/src/internal/connector/onedrive/collections_test.go b/src/internal/connector/onedrive/collections_test.go index 91a2c0ce8..6b3cbc312 100644 --- a/src/internal/connector/onedrive/collections_test.go +++ b/src/internal/connector/onedrive/collections_test.go @@ -1,7 +1,6 @@ package onedrive import ( - "context" "testing" msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" @@ -9,6 +8,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/tester" ) func expectedPathAsSlice(t *testing.T, tenant, user string, rest ...string) []string { @@ -126,8 +127,11 @@ func (suite *OneDriveCollectionsSuite) TestUpdateCollections() { for _, tt := range tests { suite.T().Run(tt.testCase, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + c := NewCollections(tenant, user, &MockGraphService{}, nil) - err := c.updateCollections(context.Background(), "driveID", tt.items) + err := c.updateCollections(ctx, "driveID", tt.items) tt.expect(t, err) assert.Equal(t, len(tt.expectedCollectionPaths), len(c.collectionMap)) assert.Equal(t, tt.expectedItemCount, c.numItems) diff --git a/src/internal/connector/onedrive/item_test.go b/src/internal/connector/onedrive/item_test.go index 19845634f..0cac044b2 100644 --- a/src/internal/connector/onedrive/item_test.go +++ b/src/internal/connector/onedrive/item_test.go @@ -48,6 +48,9 @@ func TestItemIntegrationSuite(t *testing.T) { } func (suite *ItemIntegrationSuite) SetupSuite() { + ctx, flush := tester.NewContext() + defer flush() + _, err := tester.GetRequiredEnvVars(tester.M365AcctCredEnvs...) require.NoError(suite.T(), err) @@ -63,7 +66,7 @@ func (suite *ItemIntegrationSuite) SetupSuite() { suite.user = tester.M365UserID(suite.T()) - drives, err := drives(context.TODO(), suite, suite.user) + drives, err := drives(ctx, suite, suite.user) require.NoError(suite.T(), err) // Test Requirement 1: Need a drive require.Greaterf(suite.T(), len(drives), 0, "user %s does not have a drive", suite.user) @@ -78,7 +81,8 @@ func (suite *ItemIntegrationSuite) SetupSuite() { // 2) It assumes the drive has a file it can use to test `driveItemReader` // The test checks these in below func (suite *ItemIntegrationSuite) TestItemReader() { - ctx := context.TODO() + ctx, flush := tester.NewContext() + defer flush() var driveItemID string // This item collector tries to find "a" drive item that is a file to test the reader function @@ -122,7 +126,8 @@ func (suite *ItemIntegrationSuite) TestItemReader() { // It creates a new `testfolder_ item and writes data to it func (suite *ItemIntegrationSuite) TestItemWriter() { - ctx := context.TODO() + ctx, flush := tester.NewContext() + defer flush() root, err := suite.Client().DrivesById(suite.driveID).Root().Get(ctx, nil) require.NoError(suite.T(), err) @@ -175,7 +180,8 @@ func mockDataReader(size int64) (io.Reader, int64) { } func (suite *ItemIntegrationSuite) TestDriveGetFolder() { - ctx := context.TODO() + ctx, flush := tester.NewContext() + defer flush() root, err := suite.Client().DrivesById(suite.driveID).Root().Get(ctx, nil) require.NoError(suite.T(), err) diff --git a/src/internal/connector/support/status_test.go b/src/internal/connector/support/status_test.go index 5c1fdcc89..35d17d698 100644 --- a/src/internal/connector/support/status_test.go +++ b/src/internal/connector/support/status_test.go @@ -1,13 +1,14 @@ package support import ( - "context" "errors" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + + "github.com/alcionai/corso/src/internal/tester" ) type GCStatusTestSuite struct { @@ -51,8 +52,11 @@ func (suite *GCStatusTestSuite) TestCreateStatus() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + result := CreateStatus( - context.Background(), + ctx, test.params.operationType, test.params.folders, CollectionMetrics{test.params.objects, test.params.success, 0}, @@ -69,8 +73,11 @@ func (suite *GCStatusTestSuite) TestCreateStatus_InvalidStatus() { params := statusParams{Backup, 9, 3, 13, errors.New("invalidcl")} require.Panics(t, func() { + ctx, flush := tester.NewContext() + defer flush() + CreateStatus( - context.Background(), + ctx, params.operationType, params.folders, CollectionMetrics{ @@ -85,7 +92,9 @@ func (suite *GCStatusTestSuite) TestCreateStatus_InvalidStatus() { } func (suite *GCStatusTestSuite) TestMergeStatus() { - simpleContext := context.Background() + ctx, flush := tester.NewContext() + defer flush() + table := []struct { name string one ConnectorOperationStatus @@ -95,7 +104,7 @@ func (suite *GCStatusTestSuite) TestMergeStatus() { }{ { name: "Test: Status + unknown", - one: *CreateStatus(simpleContext, Backup, 1, CollectionMetrics{1, 1, 0}, nil, ""), + one: *CreateStatus(ctx, Backup, 1, CollectionMetrics{1, 1, 0}, nil, ""), two: ConnectorOperationStatus{}, expected: statusParams{Backup, 1, 1, 1, nil}, isIncomplete: assert.False, @@ -103,22 +112,22 @@ func (suite *GCStatusTestSuite) TestMergeStatus() { { name: "Test: unknown + Status", one: ConnectorOperationStatus{}, - two: *CreateStatus(simpleContext, Backup, 1, CollectionMetrics{1, 1, 0}, nil, ""), + two: *CreateStatus(ctx, Backup, 1, CollectionMetrics{1, 1, 0}, nil, ""), expected: statusParams{Backup, 1, 1, 1, nil}, isIncomplete: assert.False, }, { name: "Test: Successful + Successful", - one: *CreateStatus(simpleContext, Backup, 1, CollectionMetrics{1, 1, 0}, nil, ""), - two: *CreateStatus(simpleContext, Backup, 3, CollectionMetrics{3, 3, 0}, nil, ""), + one: *CreateStatus(ctx, Backup, 1, CollectionMetrics{1, 1, 0}, nil, ""), + two: *CreateStatus(ctx, Backup, 3, CollectionMetrics{3, 3, 0}, nil, ""), expected: statusParams{Backup, 4, 4, 4, nil}, isIncomplete: assert.False, }, { name: "Test: Successful + Unsuccessful", - one: *CreateStatus(simpleContext, Backup, 13, CollectionMetrics{17, 17, 0}, nil, ""), + one: *CreateStatus(ctx, Backup, 13, CollectionMetrics{17, 17, 0}, nil, ""), two: *CreateStatus( - simpleContext, + ctx, Backup, 8, CollectionMetrics{ diff --git a/src/internal/kopia/conn_test.go b/src/internal/kopia/conn_test.go index 137c58ac3..f6ee1c2a3 100644 --- a/src/internal/kopia/conn_test.go +++ b/src/internal/kopia/conn_test.go @@ -41,7 +41,9 @@ func TestWrapperUnitSuite(t *testing.T) { } func (suite *WrapperUnitSuite) TestCloseWithoutOpenDoesNotCrash() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + k := conn{} assert.NotPanics(suite.T(), func() { @@ -73,8 +75,10 @@ func (suite *WrapperIntegrationSuite) SetupSuite() { } func (suite *WrapperIntegrationSuite) TestRepoExistsError() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() st := tester.NewPrefixedS3Storage(t) k := NewConn(st) @@ -88,8 +92,10 @@ func (suite *WrapperIntegrationSuite) TestRepoExistsError() { } func (suite *WrapperIntegrationSuite) TestBadProviderErrors() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() st := tester.NewPrefixedS3Storage(t) st.Provider = storage.ProviderUnknown @@ -99,8 +105,10 @@ func (suite *WrapperIntegrationSuite) TestBadProviderErrors() { } func (suite *WrapperIntegrationSuite) TestConnectWithoutInitErrors() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() st := tester.NewPrefixedS3Storage(t) k := NewConn(st) @@ -108,7 +116,9 @@ func (suite *WrapperIntegrationSuite) TestConnectWithoutInitErrors() { } func (suite *WrapperIntegrationSuite) TestCloseTwiceDoesNotCrash() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() k, err := openKopiaRepo(t, ctx) @@ -119,7 +129,9 @@ func (suite *WrapperIntegrationSuite) TestCloseTwiceDoesNotCrash() { } func (suite *WrapperIntegrationSuite) TestCloseAfterWrap() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() k, err := openKopiaRepo(t, ctx) @@ -150,7 +162,9 @@ func (suite *WrapperIntegrationSuite) TestOpenAfterClose() { } func (suite *WrapperIntegrationSuite) TestBadCompressorType() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() k, err := openKopiaRepo(t, ctx) @@ -164,7 +178,9 @@ func (suite *WrapperIntegrationSuite) TestBadCompressorType() { } func (suite *WrapperIntegrationSuite) TestGetPolicyOrDefault_GetsDefault() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() k, err := openKopiaRepo(t, ctx) @@ -187,7 +203,9 @@ func (suite *WrapperIntegrationSuite) TestGetPolicyOrDefault_GetsDefault() { } func (suite *WrapperIntegrationSuite) TestSetCompressor() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() compressor := "pgzip" @@ -280,7 +298,8 @@ func (suite *WrapperIntegrationSuite) TestConfigDefaultsSetOnInitAndConnect() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() k, err := openKopiaRepo(t, ctx) require.NoError(t, err) @@ -309,7 +328,9 @@ func (suite *WrapperIntegrationSuite) TestConfigDefaultsSetOnInitAndConnect() { } func (suite *WrapperIntegrationSuite) TestInitAndConnWithTempDirectory() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() k, err := openKopiaRepo(t, ctx) diff --git a/src/internal/kopia/model_store_test.go b/src/internal/kopia/model_store_test.go index 2a7a04055..e7bfce68d 100644 --- a/src/internal/kopia/model_store_test.go +++ b/src/internal/kopia/model_store_test.go @@ -45,8 +45,11 @@ func TestModelStoreUnitSuite(t *testing.T) { func (suite *ModelStoreUnitSuite) TestCloseWithoutInitDoesNotPanic() { assert.NotPanics(suite.T(), func() { + ctx, flush := tester.NewContext() + defer flush() + m := &ModelStore{} - m.Close(context.Background()) + m.Close(ctx) }) } @@ -55,8 +58,9 @@ func (suite *ModelStoreUnitSuite) TestCloseWithoutInitDoesNotPanic() { // --------------- type ModelStoreIntegrationSuite struct { suite.Suite - ctx context.Context - m *ModelStore + ctx context.Context + m *ModelStore + flush func() } func TestModelStoreIntegrationSuite(t *testing.T) { @@ -76,11 +80,12 @@ func (suite *ModelStoreIntegrationSuite) SetupSuite() { } func (suite *ModelStoreIntegrationSuite) SetupTest() { - suite.ctx = context.Background() + suite.ctx, suite.flush = tester.NewContext() suite.m = getModelStore(suite.T(), suite.ctx) } func (suite *ModelStoreIntegrationSuite) TearDownTest() { + defer suite.flush() assert.NoError(suite.T(), suite.m.Close(suite.ctx)) } @@ -584,7 +589,9 @@ func (suite *ModelStoreIntegrationSuite) TestPutUpdate() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + theModelType := model.BackupOpSchema m := getModelStore(t, ctx) @@ -657,7 +664,8 @@ func (suite *ModelStoreIntegrationSuite) TestPutUpdate_FailsNotMatchingPrev() { for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() m := getModelStore(t, ctx) defer func() { @@ -726,7 +734,9 @@ func (suite *ModelStoreRegressionSuite) SetupSuite() { // Tests that if we get an error or crash while in the middle of an Update no // results will be visible to higher layers. func (suite *ModelStoreRegressionSuite) TestFailDuringWriteSessionHasNoVisibleEffect() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() m := getModelStore(t, ctx) @@ -823,7 +833,9 @@ func reconnectToModelStore( // Ensures there's no shared configuration state between different instances of // the ModelStore (and consequently the underlying kopia instances). func (suite *ModelStoreRegressionSuite) TestMultipleConfigs() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() numEntries := 10 deets := details.DetailsModel{ diff --git a/src/internal/kopia/wrapper_test.go b/src/internal/kopia/wrapper_test.go index cd01f0333..7932d3ed5 100644 --- a/src/internal/kopia/wrapper_test.go +++ b/src/internal/kopia/wrapper_test.go @@ -22,6 +22,7 @@ import ( "github.com/alcionai/corso/src/internal/data" "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup/details" + "github.com/alcionai/corso/src/pkg/logger" "github.com/alcionai/corso/src/pkg/path" ) @@ -477,16 +478,21 @@ func TestKopiaUnitSuite(t *testing.T) { func (suite *KopiaUnitSuite) TestCloseWithoutInitDoesNotPanic() { assert.NotPanics(suite.T(), func() { + ctx, flush := tester.NewContext() + defer flush() + w := &Wrapper{} - w.Close(context.Background()) + w.Close(ctx) }) } func (suite *KopiaUnitSuite) TestBuildDirectoryTree() { tester.LogTimeOfTest(suite.T()) + ctx, flush := tester.NewContext() + + defer flush() t := suite.T() - ctx := context.Background() tenant := "a-tenant" user1 := testUser user1Encoded := encodeAsPath(user1) @@ -569,7 +575,9 @@ func (suite *KopiaUnitSuite) TestBuildDirectoryTree() { } func (suite *KopiaUnitSuite) TestBuildDirectoryTree_MixedDirectory() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + subdir := "subfolder" p2, err := suite.testPath.Append(subdir, false) @@ -714,7 +722,8 @@ func (suite *KopiaUnitSuite) TestBuildDirectoryTree_Fails() { } for _, test := range table { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() suite.T().Run(test.name, func(t *testing.T) { _, err := inflateDirTree(ctx, test.layout, nil) @@ -728,8 +737,9 @@ func (suite *KopiaUnitSuite) TestBuildDirectoryTree_Fails() { // --------------- type KopiaIntegrationSuite struct { suite.Suite - w *Wrapper - ctx context.Context + w *Wrapper + ctx context.Context + flush func() testPath1 path.Path testPath2 path.Path @@ -773,7 +783,7 @@ func (suite *KopiaIntegrationSuite) SetupSuite() { func (suite *KopiaIntegrationSuite) SetupTest() { t := suite.T() - suite.ctx = context.Background() + suite.ctx, suite.flush = tester.NewContext() c, err := openKopiaRepo(t, suite.ctx) require.NoError(t, err) @@ -782,6 +792,7 @@ func (suite *KopiaIntegrationSuite) SetupTest() { } func (suite *KopiaIntegrationSuite) TearDownTest() { + defer suite.flush() assert.NoError(suite.T(), suite.w.Close(suite.ctx)) } @@ -812,7 +823,9 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() { func (suite *KopiaIntegrationSuite) TestRestoreAfterCompressionChange() { t := suite.T() - ctx := context.Background() + ctx, flush := tester.NewContext() + + defer flush() k, err := openKopiaRepo(t, ctx) require.NoError(t, err) @@ -930,7 +943,8 @@ func (suite *KopiaIntegrationSuite) TestBackupCollectionsHandlesNoCollections() for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() s, d, err := suite.w.BackupCollections(ctx, test.collections) require.NoError(t, err) @@ -1051,7 +1065,7 @@ func (suite *KopiaSimpleRepoIntegrationSuite) SetupTest() { t := suite.T() expectedDirs := 6 expectedFiles := len(suite.filesByPath) - suite.ctx = context.Background() + suite.ctx, _ = logger.SeedLevel(context.Background(), logger.Development) c, err := openKopiaRepo(t, suite.ctx) require.NoError(t, err) @@ -1090,6 +1104,7 @@ func (suite *KopiaSimpleRepoIntegrationSuite) SetupTest() { func (suite *KopiaSimpleRepoIntegrationSuite) TearDownTest() { assert.NoError(suite.T(), suite.w.Close(suite.ctx)) + logger.Flush(suite.ctx) } type i64counter struct { diff --git a/src/internal/operations/backup_test.go b/src/internal/operations/backup_test.go index 843500ab3..addb906fd 100644 --- a/src/internal/operations/backup_test.go +++ b/src/internal/operations/backup_test.go @@ -1,7 +1,6 @@ package operations import ( - "context" "testing" "time" @@ -34,8 +33,10 @@ func TestBackupOpSuite(t *testing.T) { } func (suite *BackupOpSuite) TestBackupOperation_PersistResults() { + ctx, flush := tester.NewContext() + defer flush() + var ( - ctx = context.Background() kw = &kopia.Wrapper{} sw = &store.Wrapper{} acct = account.Account{} @@ -155,8 +156,11 @@ func (suite *BackupOpIntegrationSuite) TestNewBackupOperation() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + _, err := NewBackupOperation( - context.Background(), + ctx, test.opts, test.kw, test.sw, @@ -171,8 +175,10 @@ func (suite *BackupOpIntegrationSuite) TestNewBackupOperation() { // TestBackup_Run ensures that Integration Testing works // for the following scopes: Contacts, Events, and Mail func (suite *BackupOpIntegrationSuite) TestBackup_Run() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() m365UserID := tester.M365UserID(t) acct := tester.NewM365Account(t) @@ -262,8 +268,10 @@ func (suite *BackupOpIntegrationSuite) TestBackup_Run() { } func (suite *BackupOpIntegrationSuite) TestBackupOneDrive_Run() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() m365UserID := tester.M365UserID(t) acct := tester.NewM365Account(t) diff --git a/src/internal/operations/restore_test.go b/src/internal/operations/restore_test.go index e0fd73913..177e342b1 100644 --- a/src/internal/operations/restore_test.go +++ b/src/internal/operations/restore_test.go @@ -37,8 +37,10 @@ func TestRestoreOpSuite(t *testing.T) { } func (suite *RestoreOpSuite) TestRestoreOperation_PersistResults() { + ctx, flush := tester.NewContext() + defer flush() + var ( - ctx = context.Background() kw = &kopia.Wrapper{} sw = &store.Wrapper{} acct = account.Account{} @@ -142,11 +144,13 @@ func TestRestoreOpIntegrationSuite(t *testing.T) { } func (suite *RestoreOpIntegrationSuite) SetupSuite() { + ctx, flush := tester.NewContext() + defer flush() + _, err := tester.GetRequiredEnvVars(tester.M365AcctCredEnvs...) require.NoError(suite.T(), err) t := suite.T() - ctx := context.Background() m365UserID := tester.M365UserID(t) acct := tester.NewM365Account(t) @@ -194,7 +198,9 @@ func (suite *RestoreOpIntegrationSuite) SetupSuite() { } func (suite *RestoreOpIntegrationSuite) TearDownSuite() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + if suite.ms != nil { suite.ms.Close(ctx) } @@ -229,8 +235,11 @@ func (suite *RestoreOpIntegrationSuite) TestNewRestoreOperation() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + _, err := NewRestoreOperation( - context.Background(), + ctx, test.opts, test.kw, test.sw, @@ -245,8 +254,10 @@ func (suite *RestoreOpIntegrationSuite) TestNewRestoreOperation() { } func (suite *RestoreOpIntegrationSuite) TestRestore_Run() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() rsel := selectors.NewExchangeRestore() rsel.Include(rsel.Users([]string{tester.M365UserID(t)})) @@ -285,8 +296,10 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run() { } func (suite *RestoreOpIntegrationSuite) TestRestore_Run_ErrorNoResults() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() rsel := selectors.NewExchangeRestore() rsel.Include(rsel.Users(selectors.None())) diff --git a/src/internal/tester/cli.go b/src/internal/tester/cli.go index 49c94a402..beeda5946 100644 --- a/src/internal/tester/cli.go +++ b/src/internal/tester/cli.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/alcionai/corso/src/internal/common" + "github.com/alcionai/corso/src/pkg/logger" ) // StubRootCmd builds a stub cobra command to be used as @@ -31,7 +32,12 @@ func StubRootCmd(args ...string) *cobra.Command { return c } -func NewContext() context.Context { - type stub struct{} - return context.WithValue(context.Background(), stub{}, stub{}) +func NewContext() (context.Context, func()) { + ctx, _ := logger.SeedLevel(context.Background(), logger.Development) + return ctx, func() { logger.Flush(ctx) } +} + +func WithContext(ctx context.Context) (context.Context, func()) { + ctx, _ = logger.SeedLevel(ctx, logger.Development) + return ctx, func() { logger.Flush(ctx) } } diff --git a/src/pkg/logger/logger.go b/src/pkg/logger/logger.go index ebe57a9a4..12bed6971 100644 --- a/src/pkg/logger/logger.go +++ b/src/pkg/logger/logger.go @@ -153,10 +153,15 @@ func Seed(ctx context.Context) (ctxOut context.Context, zsl *zap.SugaredLogger) // SeedLevel embeds a logger into the context with the given log-level. func SeedLevel(ctx context.Context, level logLevel) (context.Context, *zap.SugaredLogger) { - _, zsl := genLogger(level) - ctxWV := context.WithValue(ctx, ctxKey, zsl) + l := ctx.Value(ctxKey) + if l == nil { + zsl := singleton(level) + ctxWV := context.WithValue(ctx, ctxKey, zsl) - return ctxWV, zsl + return ctxWV, zsl + } + + return ctx, l.(*zap.SugaredLogger) } // Ctx retrieves the logger embedded in the context. diff --git a/src/pkg/repository/repository_load_test.go b/src/pkg/repository/repository_load_test.go index b49be6fe3..815de5136 100644 --- a/src/pkg/repository/repository_load_test.go +++ b/src/pkg/repository/repository_load_test.go @@ -16,7 +16,6 @@ import ( "github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/control" - "github.com/alcionai/corso/src/pkg/logger" "github.com/alcionai/corso/src/pkg/repository" "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/storage" @@ -31,7 +30,9 @@ func initM365Repo(t *testing.T) ( ) require.NoError(t, err) - ctx := tester.NewContext() + ctx, flush := tester.NewContext() + defer flush() + st := tester.NewPrefixedS3Storage(t) ac := tester.NewM365Account(t) opts := control.Options{ @@ -217,18 +218,12 @@ func (suite *RepositoryLoadTestExchangeSuite) TeardownSuite() { suite.repo.Close(suite.ctx) } -func (suite *RepositoryLoadTestExchangeSuite) SetupTest() { - suite.ctx, _ = logger.SeedLevel(context.Background(), logger.Development) -} - -func (suite *RepositoryLoadTestExchangeSuite) TeardownTest() { - logger.Flush(suite.ctx) -} - func (suite *RepositoryLoadTestExchangeSuite) TestExchange() { + ctx, flush := tester.NewContext() + defer flush() + var ( t = suite.T() - ctx = context.Background() r = suite.repo service = "exchange" ) @@ -291,18 +286,12 @@ func (suite *RepositoryLoadTestOneDriveSuite) TeardownSuite() { suite.repo.Close(suite.ctx) } -func (suite *RepositoryLoadTestOneDriveSuite) SetupTest() { - suite.ctx, _ = logger.SeedLevel(context.Background(), logger.Development) -} - -func (suite *RepositoryLoadTestOneDriveSuite) TeardownTest() { - logger.Flush(suite.ctx) -} - func (suite *RepositoryLoadTestOneDriveSuite) TestOneDrive() { + ctx, flush := tester.NewContext() + defer flush() + var ( t = suite.T() - ctx = context.Background() r = suite.repo service = "one_drive" ) diff --git a/src/pkg/repository/repository_test.go b/src/pkg/repository/repository_test.go index c0a1a7244..b7c8cdf2b 100644 --- a/src/pkg/repository/repository_test.go +++ b/src/pkg/repository/repository_test.go @@ -1,7 +1,6 @@ package repository_test import ( - "context" "testing" "github.com/stretchr/testify/assert" @@ -46,9 +45,12 @@ func (suite *RepositorySuite) TestInitialize() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + st, err := test.storage() assert.NoError(t, err) - _, err = repository.Initialize(context.Background(), test.account, st, control.Options{}) + _, err = repository.Initialize(ctx, test.account, st, control.Options{}) test.errCheck(t, err, "") }) } @@ -74,9 +76,12 @@ func (suite *RepositorySuite) TestConnect() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + st, err := test.storage() assert.NoError(t, err) - _, err = repository.Connect(context.Background(), test.account, st, control.Options{}) + _, err = repository.Connect(ctx, test.account, st, control.Options{}) test.errCheck(t, err) }) } @@ -110,7 +115,8 @@ func (suite *RepositoryIntegrationSuite) SetupSuite() { } func (suite *RepositoryIntegrationSuite) TestInitialize() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() table := []struct { name string @@ -140,8 +146,10 @@ func (suite *RepositoryIntegrationSuite) TestInitialize() { } func (suite *RepositoryIntegrationSuite) TestConnect() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() // need to initialize the repository before we can test connecting to it. st := tester.NewPrefixedS3Storage(t) @@ -155,8 +163,10 @@ func (suite *RepositoryIntegrationSuite) TestConnect() { } func (suite *RepositoryIntegrationSuite) TestNewBackup() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() acct := tester.NewM365Account(t) @@ -172,8 +182,10 @@ func (suite *RepositoryIntegrationSuite) TestNewBackup() { } func (suite *RepositoryIntegrationSuite) TestNewRestore() { + ctx, flush := tester.NewContext() + defer flush() + t := suite.T() - ctx := context.Background() acct := tester.NewM365Account(t) dest := tester.DefaultTestRestoreDestination() diff --git a/src/pkg/selectors/exchange_test.go b/src/pkg/selectors/exchange_test.go index 6fccbd5a9..0524e1d84 100644 --- a/src/pkg/selectors/exchange_test.go +++ b/src/pkg/selectors/exchange_test.go @@ -1,7 +1,6 @@ package selectors import ( - "context" "testing" "time" @@ -10,6 +9,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/alcionai/corso/src/internal/common" + "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/filters" "github.com/alcionai/corso/src/pkg/path" @@ -1129,8 +1129,11 @@ func (suite *ExchangeSelectorSuite) TestExchangeRestore_Reduce() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + sel := test.makeSelector() - results := sel.Reduce(context.Background(), test.deets) + results := sel.Reduce(ctx, test.deets) paths := results.Paths() assert.Equal(t, test.expect, paths) }) diff --git a/src/pkg/selectors/onedrive_test.go b/src/pkg/selectors/onedrive_test.go index 439bf1d79..e8a18f79b 100644 --- a/src/pkg/selectors/onedrive_test.go +++ b/src/pkg/selectors/onedrive_test.go @@ -1,7 +1,6 @@ package selectors import ( - "context" "testing" "time" @@ -10,6 +9,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/alcionai/corso/src/internal/common" + "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/path" ) @@ -262,8 +262,11 @@ func (suite *OneDriveSelectorSuite) TestOneDriveRestore_Reduce() { } for _, test := range table { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + sel := test.makeSelector() - results := sel.Reduce(context.Background(), test.deets) + results := sel.Reduce(ctx, test.deets) paths := results.Paths() assert.Equal(t, test.expect, paths) }) diff --git a/src/pkg/selectors/scopes_test.go b/src/pkg/selectors/scopes_test.go index 5ffc44986..ce1b55c14 100644 --- a/src/pkg/selectors/scopes_test.go +++ b/src/pkg/selectors/scopes_test.go @@ -1,13 +1,13 @@ package selectors import ( - "context" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/filters" "github.com/alcionai/corso/src/pkg/path" @@ -246,9 +246,12 @@ func (suite *SelectorScopesSuite) TestReduce() { for _, test := range reduceTestTable { suite.T().Run(test.name, func(t *testing.T) { + ctx, flush := tester.NewContext() + defer flush() + ds := deets() result := reduce[mockScope]( - context.Background(), + ctx, &ds, test.sel().Selector, dataCats) diff --git a/src/pkg/selectors/selectors_reduce_test.go b/src/pkg/selectors/selectors_reduce_test.go index 971b6b258..7aae1bc32 100644 --- a/src/pkg/selectors/selectors_reduce_test.go +++ b/src/pkg/selectors/selectors_reduce_test.go @@ -1,7 +1,6 @@ package selectors_test import ( - "context" "testing" "time" @@ -9,6 +8,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/alcionai/corso/src/internal/common" + "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/selectors/testdata" @@ -23,7 +23,9 @@ func TestSelectorReduceSuite(t *testing.T) { } func (suite *SelectorReduceSuite) TestReduce() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() + allDetails := testdata.GetDetailsSet() table := []struct { name string diff --git a/src/pkg/services/m365/m365_test.go b/src/pkg/services/m365/m365_test.go index 3a7398841..16b0c04f8 100644 --- a/src/pkg/services/m365/m365_test.go +++ b/src/pkg/services/m365/m365_test.go @@ -1,7 +1,6 @@ package m365 import ( - "context" "testing" "github.com/stretchr/testify/require" @@ -31,9 +30,12 @@ func (suite *M365IntegrationSuite) SetupSuite() { } func (suite *M365IntegrationSuite) TestUsers() { + ctx, flush := tester.NewContext() + defer flush() + acct := tester.NewM365Account(suite.T()) - users, err := Users(context.Background(), acct) + users, err := Users(ctx, acct) require.NoError(suite.T(), err) require.NotNil(suite.T(), users) diff --git a/src/pkg/store/backup_test.go b/src/pkg/store/backup_test.go index 8bf10e44f..abb2ce9f7 100644 --- a/src/pkg/store/backup_test.go +++ b/src/pkg/store/backup_test.go @@ -1,7 +1,6 @@ package store_test import ( - "context" "testing" "time" @@ -11,6 +10,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/alcionai/corso/src/internal/model" + "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/store" @@ -51,7 +51,8 @@ func TestStoreBackupUnitSuite(t *testing.T) { } func (suite *StoreBackupUnitSuite) TestGetBackup() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() table := []struct { name string @@ -83,7 +84,8 @@ func (suite *StoreBackupUnitSuite) TestGetBackup() { } func (suite *StoreBackupUnitSuite) TestGetBackups() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() table := []struct { name string @@ -116,7 +118,8 @@ func (suite *StoreBackupUnitSuite) TestGetBackups() { } func (suite *StoreBackupUnitSuite) TestDeleteBackup() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() table := []struct { name string @@ -144,7 +147,8 @@ func (suite *StoreBackupUnitSuite) TestDeleteBackup() { } func (suite *StoreBackupUnitSuite) TestGetDetails() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() table := []struct { name string @@ -176,7 +180,8 @@ func (suite *StoreBackupUnitSuite) TestGetDetails() { } func (suite *StoreBackupUnitSuite) TestGetDetailsFromBackupID() { - ctx := context.Background() + ctx, flush := tester.NewContext() + defer flush() table := []struct { name string