From 854635ac242adb410fecaf96bd6188d8c8d3bceb Mon Sep 17 00:00:00 2001 From: Keepers Date: Thu, 18 Aug 2022 18:24:32 -0600 Subject: [PATCH] allow tests to set a specific kopia config dir (#607) --- src/cli/backup/exchange_integration_test.go | 2 -- src/cli/restore/exchange_integration_test.go | 7 ++--- src/internal/kopia/conn.go | 32 +++++++++++++++----- src/internal/kopia/conn_test.go | 17 +++++++++-- src/internal/kopia/model_store_test.go | 5 +-- src/internal/kopia/wrapper_test.go | 5 +-- src/internal/tester/storage.go | 6 +++- src/pkg/storage/common.go | 6 ++++ 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/cli/backup/exchange_integration_test.go b/src/cli/backup/exchange_integration_test.go index e601f0fd7..eef45b56f 100644 --- a/src/cli/backup/exchange_integration_test.go +++ b/src/cli/backup/exchange_integration_test.go @@ -167,8 +167,6 @@ func (suite *PreparedBackupExchangeIntegrationSuite) SetupSuite() { control.NewOptions(false)) require.NoError(t, suite.backupOp.Run(ctx)) require.NoError(t, err) - - time.Sleep(3 * time.Second) } func (suite *PreparedBackupExchangeIntegrationSuite) TestExchangeListCmd() { diff --git a/src/cli/restore/exchange_integration_test.go b/src/cli/restore/exchange_integration_test.go index 4d6cbf653..eefc776d4 100644 --- a/src/cli/restore/exchange_integration_test.go +++ b/src/cli/restore/exchange_integration_test.go @@ -2,7 +2,6 @@ package restore_test import ( "testing" - "time" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -45,11 +44,11 @@ func (suite *RestoreExchangeIntegrationSuite) SetupSuite() { t := suite.T() _, err := tester.GetRequiredEnvSls( tester.AWSStorageCredEnvs, - tester.M365AcctCredEnvs) + tester.M365AcctCredEnvs, + ) require.NoError(t, err) // aggregate required details - suite.acct = tester.NewM365Account(t) suite.st = tester.NewPrefixedS3Storage(t) @@ -80,8 +79,6 @@ func (suite *RestoreExchangeIntegrationSuite) SetupSuite() { control.NewOptions(false)) require.NoError(t, suite.backupOp.Run(ctx)) require.NoError(t, err) - - time.Sleep(3 * time.Second) } func (suite *RestoreExchangeIntegrationSuite) TestExchangeRestoreCmd() { diff --git a/src/internal/kopia/conn.go b/src/internal/kopia/conn.go index cffc33a0e..887cd01ed 100644 --- a/src/internal/kopia/conn.go +++ b/src/internal/kopia/conn.go @@ -2,11 +2,13 @@ package kopia import ( "context" + "path/filepath" "sync" "github.com/kopia/kopia/repo" "github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/compression" + "github.com/kopia/kopia/repo/content" "github.com/kopia/kopia/snapshot" "github.com/kopia/kopia/snapshot/policy" "github.com/pkg/errors" @@ -16,8 +18,9 @@ import ( ) const ( - defaultKopiaConfigFilePath = "/tmp/repository.config" - defaultCompressor = "s2-default" + defaultKopiaConfigDir = "/tmp/" + defaultKopiaConfigFile = "repository.config" + defaultCompressor = "s2-default" ) var ( @@ -73,7 +76,7 @@ func (w *conn) Initialize(ctx context.Context) error { return w.commonConnect( ctx, - defaultKopiaConfigFilePath, + cfg.KopiaCfgDir, bst, cfg.CorsoPassword, defaultCompressor, @@ -94,7 +97,7 @@ func (w *conn) Connect(ctx context.Context) error { return w.commonConnect( ctx, - defaultKopiaConfigFilePath, + cfg.KopiaCfgDir, bst, cfg.CorsoPassword, defaultCompressor, @@ -103,22 +106,35 @@ func (w *conn) Connect(ctx context.Context) error { func (w *conn) commonConnect( ctx context.Context, - configPath string, + configDir string, bst blob.Storage, password, compressor string, ) error { + var opts *repo.ConnectOptions + if len(configDir) > 0 { + opts = &repo.ConnectOptions{ + CachingOptions: content.CachingOptions{ + CacheDirectory: configDir, + }, + } + } else { + configDir = defaultKopiaConfigDir + } + + cfgFile := filepath.Join(configDir, defaultKopiaConfigFile) + // todo - issue #75: nil here should be storage.ConnectOptions() if err := repo.Connect( ctx, - configPath, + cfgFile, bst, password, - nil, + opts, ); err != nil { return errors.Wrap(err, errConnect.Error()) } - if err := w.open(ctx, configPath, password); err != nil { + if err := w.open(ctx, cfgFile, password); err != nil { return err } diff --git a/src/internal/kopia/conn_test.go b/src/internal/kopia/conn_test.go index c2f8e5cb2..6ec13789a 100644 --- a/src/internal/kopia/conn_test.go +++ b/src/internal/kopia/conn_test.go @@ -15,9 +15,9 @@ import ( //revive:disable:context-as-argument func openKopiaRepo(t *testing.T, ctx context.Context) (*conn, error) { - storage := tester.NewPrefixedS3Storage(t) + st := tester.NewPrefixedS3Storage(t) - k := NewConn(storage) + k := NewConn(st) if err := k.Initialize(ctx); err != nil { return nil, err } @@ -215,3 +215,16 @@ func (suite *WrapperIntegrationSuite) TestCompressorSetOnInitAndConnect() { assert.Equal(t, defaultCompressor, string(p.CompressionPolicy.CompressorName)) } + +func (suite *WrapperIntegrationSuite) TestInitAndConnWithTempDirectory() { + ctx := context.Background() + t := suite.T() + + k, err := openKopiaRepo(t, ctx) + require.NoError(t, err) + require.NoError(t, k.Close(ctx)) + + // Re-open with Connect. + require.NoError(t, k.Connect(ctx)) + assert.NoError(t, k.Close(ctx)) +} diff --git a/src/internal/kopia/model_store_test.go b/src/internal/kopia/model_store_test.go index a743b7bc9..f4e43216c 100644 --- a/src/internal/kopia/model_store_test.go +++ b/src/internal/kopia/model_store_test.go @@ -51,8 +51,9 @@ func (suite *ModelStoreUnitSuite) TestCloseWithoutInitDoesNotPanic() { // --------------- type ModelStoreIntegrationSuite struct { suite.Suite - ctx context.Context - m *ModelStore + ctx context.Context + m *ModelStore + cfgDir string } func TestModelStoreIntegrationSuite(t *testing.T) { diff --git a/src/internal/kopia/wrapper_test.go b/src/internal/kopia/wrapper_test.go index ba329a938..5266034ef 100644 --- a/src/internal/kopia/wrapper_test.go +++ b/src/internal/kopia/wrapper_test.go @@ -452,9 +452,10 @@ func (suite *KopiaIntegrationSuite) SetupSuite() { } func (suite *KopiaIntegrationSuite) SetupTest() { + t := suite.T() suite.ctx = context.Background() - c, err := openKopiaRepo(suite.T(), suite.ctx) - require.NoError(suite.T(), err) + c, err := openKopiaRepo(t, suite.ctx) + require.NoError(t, err) suite.w = &Wrapper{c} } diff --git a/src/internal/tester/storage.go b/src/internal/tester/storage.go index a945fe5ed..5756f1449 100644 --- a/src/internal/tester/storage.go +++ b/src/internal/tester/storage.go @@ -18,6 +18,9 @@ var AWSStorageCredEnvs = []string{ // NewPrefixedS3Storage returns a storage.Storage object initialized with environment // variables used for integration tests that use S3. The prefix for the storage // path will be unique. +// Uses t.TempDir() to generate a unique config storage and caching directory for this +// test. Suites that need to identify this value can retrieve it again from the common +// configs. func NewPrefixedS3Storage(t *testing.T) storage.Storage { now := LogTimeOfTest(t) cfg, err := readTestConfig() @@ -31,7 +34,8 @@ func NewPrefixedS3Storage(t *testing.T) storage.Storage { Prefix: t.Name() + "-" + now, }, storage.CommonConfig{ - Corso: credentials.GetCorso(), + Corso: credentials.GetCorso(), + KopiaCfgDir: t.TempDir(), }, ) require.NoError(t, err, "creating storage") diff --git a/src/pkg/storage/common.go b/src/pkg/storage/common.go index 0edc0e46e..d2106de1d 100644 --- a/src/pkg/storage/common.go +++ b/src/pkg/storage/common.go @@ -8,11 +8,14 @@ import ( type CommonConfig struct { credentials.Corso // requires: CorsoPassword + + KopiaCfgDir string } // config key consts const ( keyCommonCorsoPassword = "common_corsoPassword" + keyCommonKopiaCfgDir = "common_kopiaCfgDir" ) // StringConfig transforms a commonConfig struct into a plain @@ -21,6 +24,7 @@ const ( func (c CommonConfig) StringConfig() (map[string]string, error) { cfg := map[string]string{ keyCommonCorsoPassword: c.CorsoPassword, + keyCommonKopiaCfgDir: c.KopiaCfgDir, } return cfg, c.validate() } @@ -30,6 +34,7 @@ func (s Storage) CommonConfig() (CommonConfig, error) { c := CommonConfig{} if len(s.Config) > 0 { c.CorsoPassword = orEmptyString(s.Config[keyCommonCorsoPassword]) + c.KopiaCfgDir = orEmptyString(s.Config[keyCommonKopiaCfgDir]) } return c, c.validate() } @@ -39,5 +44,6 @@ func (c CommonConfig) validate() error { if len(c.CorsoPassword) == 0 { return errors.Wrap(errMissingRequired, credentials.CorsoPassword) } + // kopiaCfgFilePath is not required return nil }