S3 test prefix naming (#228)

* Update how S3 storage structs are generated

  * fix bug in printing year of date
  * use the name of the test instead of trying to pull name from runtime
  * always log the time when making the storage struct
  * don't allow user to specify prefix

* Fixup tests for new test storage API

* Update function name and comment
This commit is contained in:
ashmrtn 2022-06-23 11:55:26 -07:00 committed by GitHub
parent 0bd23ab2ab
commit 902294b70d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 56 deletions

View File

@ -43,7 +43,6 @@ func (suite *GraphConnectorIntegrationSuite) SetupSuite() {
func (suite *GraphConnectorIntegrationSuite) TestGraphConnector() { func (suite *GraphConnectorIntegrationSuite) TestGraphConnector() {
ctesting.LogTimeOfTest(suite.T()) ctesting.LogTimeOfTest(suite.T())
suite.NotNil(suite.connector) suite.NotNil(suite.connector)
} }
type DiconnectedGraphConnectorSuite struct { type DiconnectedGraphConnectorSuite struct {

View File

@ -31,8 +31,8 @@ var (
testFileData = []byte("abcdefghijklmnopqrstuvwxyz") testFileData = []byte("abcdefghijklmnopqrstuvwxyz")
) )
func openKopiaRepo(ctx context.Context, prefix string) (*KopiaWrapper, error) { func openKopiaRepo(t *testing.T, ctx context.Context) (*KopiaWrapper, error) {
storage, err := ctesting.NewS3Storage(prefix) storage, err := ctesting.NewPrefixedS3Storage(t)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -253,23 +253,23 @@ func (suite *KopiaIntegrationSuite) SetupSuite() {
func (suite *KopiaIntegrationSuite) TestCloseTwiceDoesNotCrash() { func (suite *KopiaIntegrationSuite) TestCloseTwiceDoesNotCrash() {
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(suite.T()) t := suite.T()
k, err := openKopiaRepo(ctx, "init-s3-"+timeOfTest) k, err := openKopiaRepo(t, ctx)
require.NoError(suite.T(), err) require.NoError(t, err)
assert.NoError(suite.T(), k.Close(ctx)) assert.NoError(t, k.Close(ctx))
assert.Nil(suite.T(), k.rep) assert.Nil(t, k.rep)
assert.NoError(suite.T(), k.Close(ctx)) assert.NoError(t, k.Close(ctx))
} }
func (suite *KopiaIntegrationSuite) TestBackupCollections() { func (suite *KopiaIntegrationSuite) TestBackupCollections() {
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(suite.T()) t := suite.T()
k, err := openKopiaRepo(ctx, "init-s3-"+timeOfTest) k, err := openKopiaRepo(t, ctx)
require.NoError(suite.T(), err) require.NoError(t, err)
defer func() { defer func() {
assert.NoError(suite.T(), k.Close(ctx)) assert.NoError(t, k.Close(ctx))
}() }()
collections := []connector.DataCollection{ collections := []connector.DataCollection{
@ -284,12 +284,12 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() {
} }
stats, err := k.BackupCollections(ctx, collections) stats, err := k.BackupCollections(ctx, collections)
assert.NoError(suite.T(), err) assert.NoError(t, err)
assert.Equal(suite.T(), stats.TotalFileCount, 47) assert.Equal(t, stats.TotalFileCount, 47)
assert.Equal(suite.T(), stats.TotalDirectoryCount, 5) assert.Equal(t, stats.TotalDirectoryCount, 5)
assert.Equal(suite.T(), stats.IgnoredErrorCount, 0) assert.Equal(t, stats.IgnoredErrorCount, 0)
assert.Equal(suite.T(), stats.ErrorCount, 0) assert.Equal(t, stats.ErrorCount, 0)
assert.False(suite.T(), stats.Incomplete) assert.False(t, stats.Incomplete)
} }
func setupSimpleRepo(t *testing.T, ctx context.Context, k *KopiaWrapper) manifest.ID { func setupSimpleRepo(t *testing.T, ctx context.Context, k *KopiaWrapper) manifest.ID {
@ -316,10 +316,9 @@ func setupSimpleRepo(t *testing.T, ctx context.Context, k *KopiaWrapper) manifes
func (suite *KopiaIntegrationSuite) TestBackupAndRestoreSingleItem() { func (suite *KopiaIntegrationSuite) TestBackupAndRestoreSingleItem() {
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(suite.T())
t := suite.T() t := suite.T()
k, err := openKopiaRepo(ctx, "backup-restore-single-item-"+timeOfTest) k, err := openKopiaRepo(t, ctx)
require.NoError(t, err) require.NoError(t, err)
defer func() { defer func() {
assert.NoError(t, k.Close(ctx)) assert.NoError(t, k.Close(ctx))
@ -381,9 +380,9 @@ func (suite *KopiaIntegrationSuite) TestBackupAndRestoreSingleItem_Errors() {
for _, test := range table { for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(t) ctesting.LogTimeOfTest(t)
k, err := openKopiaRepo(ctx, "backup-restore-single-item-error-"+test.name+"-"+timeOfTest) k, err := openKopiaRepo(t, ctx)
require.NoError(t, err) require.NoError(t, err)
defer func() { defer func() {
assert.NoError(t, k.Close(ctx)) assert.NoError(t, k.Close(ctx))
@ -429,9 +428,8 @@ func (suite *KopiaIntegrationSuite) TestBackupAndRestoreSingleItem_Errors2() {
for _, test := range table { for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(t)
k, err := openKopiaRepo(ctx, "backup-restore-single-item-error2-"+test.name+"-"+timeOfTest) k, err := openKopiaRepo(t, ctx)
require.NoError(t, err) require.NoError(t, err)
defer func() { defer func() {
assert.NoError(t, k.Close(ctx)) assert.NoError(t, k.Close(ctx))

View File

@ -66,8 +66,6 @@ func (suite *BackupOpIntegrationSuite) TestNewBackupOperation() {
func (suite *BackupOpIntegrationSuite) TestBackup_Run() { func (suite *BackupOpIntegrationSuite) TestBackup_Run() {
t := suite.T() t := suite.T()
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(t)
prefix := "backup-op-run-" + timeOfTest
// m365User := "lidiah@8qzvrj.onmicrosoft.com" // m365User := "lidiah@8qzvrj.onmicrosoft.com"
// not the user we want to use, but all the others are // not the user we want to use, but all the others are
@ -81,7 +79,7 @@ func (suite *BackupOpIntegrationSuite) TestBackup_Run() {
} }
// need to initialize the repository before we can test connecting to it. // need to initialize the repository before we can test connecting to it.
st, err := ctesting.NewS3Storage(prefix) st, err := ctesting.NewPrefixedS3Storage(t)
require.NoError(t, err) require.NoError(t, err)
r, err := repository.Initialize(ctx, acct, st) r, err := repository.Initialize(ctx, acct, st)

View File

@ -3,7 +3,6 @@ package testing
import ( import (
"fmt" "fmt"
"os" "os"
"runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -35,13 +34,12 @@ func RunOnAny(tests ...string) error {
// LogTimeOfTest logs the test name and the time that it was run. // LogTimeOfTest logs the test name and the time that it was run.
func LogTimeOfTest(t *testing.T) string { func LogTimeOfTest(t *testing.T) string {
now := time.Now().UTC().Format("2016-01-02T15:04:05.0000") now := time.Now().UTC().Format("2006-01-02T15:04:05.0000")
pc, _, _, ok := runtime.Caller(1) name := t.Name()
details := runtime.FuncForPC(pc) if name == "" {
if !ok || details != nil {
t.Logf("Test run at %s.", now) t.Logf("Test run at %s.", now)
return now return now
} }
t.Logf("%s() run at %s", details.Name(), now) t.Logf("%s run at %s", name, now)
return now return now
} }

View File

@ -1,6 +1,8 @@
package testing package testing
import ( import (
"testing"
"github.com/alcionai/corso/pkg/credentials" "github.com/alcionai/corso/pkg/credentials"
"github.com/alcionai/corso/pkg/storage" "github.com/alcionai/corso/pkg/storage"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -12,9 +14,11 @@ var AWSCredentialEnvs = []string{
credentials.AWSSessionToken, credentials.AWSSessionToken,
} }
// NewS3Storage 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. // variables used for integration tests that use S3. The prefix for the storage
func NewS3Storage(prefix string) (storage.Storage, error) { // path will be unique.
func NewPrefixedS3Storage(t *testing.T) (storage.Storage, error) {
now := LogTimeOfTest(t)
cfg, err := readTestConfig() cfg, err := readTestConfig()
if err != nil { if err != nil {
return storage.Storage{}, errors.Wrap(err, "configuring storage from test file") return storage.Storage{}, errors.Wrap(err, "configuring storage from test file")
@ -25,7 +29,7 @@ func NewS3Storage(prefix string) (storage.Storage, error) {
storage.S3Config{ storage.S3Config{
AWS: credentials.GetAWS(nil), AWS: credentials.GetAWS(nil),
Bucket: cfg[testCfgBucket], Bucket: cfg[testCfgBucket],
Prefix: prefix, Prefix: t.Name() + "-" + now,
}, },
storage.CommonConfig{ storage.CommonConfig{
Corso: credentials.GetCorso(), Corso: credentials.GetCorso(),

View File

@ -106,25 +106,22 @@ func (suite *RepositoryIntegrationSuite) SetupSuite() {
func (suite *RepositoryIntegrationSuite) TestInitialize() { func (suite *RepositoryIntegrationSuite) TestInitialize() {
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(suite.T())
table := []struct { table := []struct {
prefix string name string
account repository.Account account repository.Account
storage func() (storage.Storage, error) storage func(*testing.T) (storage.Storage, error)
errCheck assert.ErrorAssertionFunc errCheck assert.ErrorAssertionFunc
}{ }{
{ {
prefix: "repository-init-s3-" + timeOfTest, name: "success",
storage: func() (storage.Storage, error) { storage: ctesting.NewPrefixedS3Storage,
return ctesting.NewS3Storage("repository-init-s3-" + timeOfTest)
},
errCheck: assert.NoError, errCheck: assert.NoError,
}, },
} }
for _, test := range table { for _, test := range table {
suite.T().Run(test.prefix, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
st, err := test.storage() st, err := test.storage(t)
assert.NoError(t, err) assert.NoError(t, err)
r, err := repository.Initialize(ctx, test.account, st) r, err := repository.Initialize(ctx, test.account, st)
if err == nil { if err == nil {
@ -141,11 +138,9 @@ func (suite *RepositoryIntegrationSuite) TestInitialize() {
func (suite *RepositoryIntegrationSuite) TestConnect() { func (suite *RepositoryIntegrationSuite) TestConnect() {
t := suite.T() t := suite.T()
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(t)
prefix := "repository-conn-s3-" + timeOfTest
// need to initialize the repository before we can test connecting to it. // need to initialize the repository before we can test connecting to it.
st, err := ctesting.NewS3Storage(prefix) st, err := ctesting.NewPrefixedS3Storage(t)
require.NoError(t, err) require.NoError(t, err)
_, err = repository.Initialize(ctx, repository.Account{}, st) _, err = repository.Initialize(ctx, repository.Account{}, st)
@ -159,8 +154,6 @@ func (suite *RepositoryIntegrationSuite) TestConnect() {
func (suite *RepositoryIntegrationSuite) TestNewBackup() { func (suite *RepositoryIntegrationSuite) TestNewBackup() {
t := suite.T() t := suite.T()
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(t)
prefix := "repository-new-backup-" + timeOfTest
m365 := credentials.GetM365() m365 := credentials.GetM365()
acct := repository.Account{ acct := repository.Account{
@ -170,7 +163,7 @@ func (suite *RepositoryIntegrationSuite) TestNewBackup() {
} }
// need to initialize the repository before we can test connecting to it. // need to initialize the repository before we can test connecting to it.
st, err := ctesting.NewS3Storage(prefix) st, err := ctesting.NewPrefixedS3Storage(t)
require.NoError(t, err) require.NoError(t, err)
r, err := repository.Initialize(ctx, acct, st) r, err := repository.Initialize(ctx, acct, st)
@ -184,8 +177,6 @@ func (suite *RepositoryIntegrationSuite) TestNewBackup() {
func (suite *RepositoryIntegrationSuite) TestNewRestore() { func (suite *RepositoryIntegrationSuite) TestNewRestore() {
t := suite.T() t := suite.T()
ctx := context.Background() ctx := context.Background()
timeOfTest := ctesting.LogTimeOfTest(t)
prefix := "repository-new-restore-" + timeOfTest
m365 := credentials.GetM365() m365 := credentials.GetM365()
acct := repository.Account{ acct := repository.Account{
@ -195,7 +186,7 @@ func (suite *RepositoryIntegrationSuite) TestNewRestore() {
} }
// need to initialize the repository before we can test connecting to it. // need to initialize the repository before we can test connecting to it.
st, err := ctesting.NewS3Storage(prefix) st, err := ctesting.NewPrefixedS3Storage(t)
require.NoError(t, err) require.NoError(t, err)
r, err := repository.Initialize(ctx, acct, st) r, err := repository.Initialize(ctx, acct, st)