allow tests to set a specific kopia config dir (#607)

This commit is contained in:
Keepers 2022-08-18 18:24:32 -06:00 committed by GitHub
parent c3c361282d
commit 854635ac24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 22 deletions

View File

@ -167,8 +167,6 @@ func (suite *PreparedBackupExchangeIntegrationSuite) SetupSuite() {
control.NewOptions(false)) control.NewOptions(false))
require.NoError(t, suite.backupOp.Run(ctx)) require.NoError(t, suite.backupOp.Run(ctx))
require.NoError(t, err) require.NoError(t, err)
time.Sleep(3 * time.Second)
} }
func (suite *PreparedBackupExchangeIntegrationSuite) TestExchangeListCmd() { func (suite *PreparedBackupExchangeIntegrationSuite) TestExchangeListCmd() {

View File

@ -2,7 +2,6 @@ package restore_test
import ( import (
"testing" "testing"
"time"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -45,11 +44,11 @@ func (suite *RestoreExchangeIntegrationSuite) SetupSuite() {
t := suite.T() t := suite.T()
_, err := tester.GetRequiredEnvSls( _, err := tester.GetRequiredEnvSls(
tester.AWSStorageCredEnvs, tester.AWSStorageCredEnvs,
tester.M365AcctCredEnvs) tester.M365AcctCredEnvs,
)
require.NoError(t, err) require.NoError(t, err)
// aggregate required details // aggregate required details
suite.acct = tester.NewM365Account(t) suite.acct = tester.NewM365Account(t)
suite.st = tester.NewPrefixedS3Storage(t) suite.st = tester.NewPrefixedS3Storage(t)
@ -80,8 +79,6 @@ func (suite *RestoreExchangeIntegrationSuite) SetupSuite() {
control.NewOptions(false)) control.NewOptions(false))
require.NoError(t, suite.backupOp.Run(ctx)) require.NoError(t, suite.backupOp.Run(ctx))
require.NoError(t, err) require.NoError(t, err)
time.Sleep(3 * time.Second)
} }
func (suite *RestoreExchangeIntegrationSuite) TestExchangeRestoreCmd() { func (suite *RestoreExchangeIntegrationSuite) TestExchangeRestoreCmd() {

View File

@ -2,11 +2,13 @@ package kopia
import ( import (
"context" "context"
"path/filepath"
"sync" "sync"
"github.com/kopia/kopia/repo" "github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/compression" "github.com/kopia/kopia/repo/compression"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/snapshot" "github.com/kopia/kopia/snapshot"
"github.com/kopia/kopia/snapshot/policy" "github.com/kopia/kopia/snapshot/policy"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -16,8 +18,9 @@ import (
) )
const ( const (
defaultKopiaConfigFilePath = "/tmp/repository.config" defaultKopiaConfigDir = "/tmp/"
defaultCompressor = "s2-default" defaultKopiaConfigFile = "repository.config"
defaultCompressor = "s2-default"
) )
var ( var (
@ -73,7 +76,7 @@ func (w *conn) Initialize(ctx context.Context) error {
return w.commonConnect( return w.commonConnect(
ctx, ctx,
defaultKopiaConfigFilePath, cfg.KopiaCfgDir,
bst, bst,
cfg.CorsoPassword, cfg.CorsoPassword,
defaultCompressor, defaultCompressor,
@ -94,7 +97,7 @@ func (w *conn) Connect(ctx context.Context) error {
return w.commonConnect( return w.commonConnect(
ctx, ctx,
defaultKopiaConfigFilePath, cfg.KopiaCfgDir,
bst, bst,
cfg.CorsoPassword, cfg.CorsoPassword,
defaultCompressor, defaultCompressor,
@ -103,22 +106,35 @@ func (w *conn) Connect(ctx context.Context) error {
func (w *conn) commonConnect( func (w *conn) commonConnect(
ctx context.Context, ctx context.Context,
configPath string, configDir string,
bst blob.Storage, bst blob.Storage,
password, compressor string, password, compressor string,
) error { ) 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() // todo - issue #75: nil here should be storage.ConnectOptions()
if err := repo.Connect( if err := repo.Connect(
ctx, ctx,
configPath, cfgFile,
bst, bst,
password, password,
nil, opts,
); err != nil { ); err != nil {
return errors.Wrap(err, errConnect.Error()) 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 return err
} }

View File

@ -15,9 +15,9 @@ import (
//revive:disable:context-as-argument //revive:disable:context-as-argument
func openKopiaRepo(t *testing.T, ctx context.Context) (*conn, error) { 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 { if err := k.Initialize(ctx); err != nil {
return nil, err return nil, err
} }
@ -215,3 +215,16 @@ func (suite *WrapperIntegrationSuite) TestCompressorSetOnInitAndConnect() {
assert.Equal(t, defaultCompressor, string(p.CompressionPolicy.CompressorName)) 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))
}

View File

@ -51,8 +51,9 @@ func (suite *ModelStoreUnitSuite) TestCloseWithoutInitDoesNotPanic() {
// --------------- // ---------------
type ModelStoreIntegrationSuite struct { type ModelStoreIntegrationSuite struct {
suite.Suite suite.Suite
ctx context.Context ctx context.Context
m *ModelStore m *ModelStore
cfgDir string
} }
func TestModelStoreIntegrationSuite(t *testing.T) { func TestModelStoreIntegrationSuite(t *testing.T) {

View File

@ -452,9 +452,10 @@ func (suite *KopiaIntegrationSuite) SetupSuite() {
} }
func (suite *KopiaIntegrationSuite) SetupTest() { func (suite *KopiaIntegrationSuite) SetupTest() {
t := suite.T()
suite.ctx = context.Background() suite.ctx = context.Background()
c, err := openKopiaRepo(suite.T(), suite.ctx) c, err := openKopiaRepo(t, suite.ctx)
require.NoError(suite.T(), err) require.NoError(t, err)
suite.w = &Wrapper{c} suite.w = &Wrapper{c}
} }

View File

@ -18,6 +18,9 @@ var AWSStorageCredEnvs = []string{
// NewPrefixedS3Storage returns a storage.Storage object initialized with environment // NewPrefixedS3Storage returns a storage.Storage object initialized with environment
// variables used for integration tests that use S3. The prefix for the storage // variables used for integration tests that use S3. The prefix for the storage
// path will be unique. // 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 { func NewPrefixedS3Storage(t *testing.T) storage.Storage {
now := LogTimeOfTest(t) now := LogTimeOfTest(t)
cfg, err := readTestConfig() cfg, err := readTestConfig()
@ -31,7 +34,8 @@ func NewPrefixedS3Storage(t *testing.T) storage.Storage {
Prefix: t.Name() + "-" + now, Prefix: t.Name() + "-" + now,
}, },
storage.CommonConfig{ storage.CommonConfig{
Corso: credentials.GetCorso(), Corso: credentials.GetCorso(),
KopiaCfgDir: t.TempDir(),
}, },
) )
require.NoError(t, err, "creating storage") require.NoError(t, err, "creating storage")

View File

@ -8,11 +8,14 @@ import (
type CommonConfig struct { type CommonConfig struct {
credentials.Corso // requires: CorsoPassword credentials.Corso // requires: CorsoPassword
KopiaCfgDir string
} }
// config key consts // config key consts
const ( const (
keyCommonCorsoPassword = "common_corsoPassword" keyCommonCorsoPassword = "common_corsoPassword"
keyCommonKopiaCfgDir = "common_kopiaCfgDir"
) )
// StringConfig transforms a commonConfig struct into a plain // StringConfig transforms a commonConfig struct into a plain
@ -21,6 +24,7 @@ const (
func (c CommonConfig) StringConfig() (map[string]string, error) { func (c CommonConfig) StringConfig() (map[string]string, error) {
cfg := map[string]string{ cfg := map[string]string{
keyCommonCorsoPassword: c.CorsoPassword, keyCommonCorsoPassword: c.CorsoPassword,
keyCommonKopiaCfgDir: c.KopiaCfgDir,
} }
return cfg, c.validate() return cfg, c.validate()
} }
@ -30,6 +34,7 @@ func (s Storage) CommonConfig() (CommonConfig, error) {
c := CommonConfig{} c := CommonConfig{}
if len(s.Config) > 0 { if len(s.Config) > 0 {
c.CorsoPassword = orEmptyString(s.Config[keyCommonCorsoPassword]) c.CorsoPassword = orEmptyString(s.Config[keyCommonCorsoPassword])
c.KopiaCfgDir = orEmptyString(s.Config[keyCommonKopiaCfgDir])
} }
return c, c.validate() return c, c.validate()
} }
@ -39,5 +44,6 @@ func (c CommonConfig) validate() error {
if len(c.CorsoPassword) == 0 { if len(c.CorsoPassword) == 0 {
return errors.Wrap(errMissingRequired, credentials.CorsoPassword) return errors.Wrap(errMissingRequired, credentials.CorsoPassword)
} }
// kopiaCfgFilePath is not required
return nil return nil
} }