From 04d11503a9a3fe7a5fa932756c2d591d5e66c111 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Thu, 2 Jun 2022 16:57:13 -0700 Subject: [PATCH] Factor out helper functions for testing (#115) Helper functions do simple validation and initialization for S3 storage objects. --- src/internal/testing/storage.go | 45 +++++++++++++++++++++++++++ src/pkg/repository/repository_test.go | 24 ++------------ 2 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/internal/testing/storage.go diff --git a/src/internal/testing/storage.go b/src/internal/testing/storage.go new file mode 100644 index 000000000..0d62849de --- /dev/null +++ b/src/internal/testing/storage.go @@ -0,0 +1,45 @@ +package testing + +import ( + "os" + + "github.com/pkg/errors" + + "github.com/alcionai/corso/pkg/storage" +) + +// CheckS3EnvVars returns as error if any of the environment variables required for +// integration tests using S3 is empty. It does not check the validity of the +// variables with S3. +func CheckS3EnvVars() error { + s3Envs := []string{ + storage.AWS_ACCESS_KEY_ID, + storage.AWS_SECRET_ACCESS_KEY, + storage.AWS_SESSION_TOKEN, + } + for _, env := range s3Envs { + if os.Getenv(env) == "" { + return errors.Errorf("env var [%s] must be populated for integration testing", env) + } + } + + return nil +} + +// NewS3Storage returns a storage.Storage object initialized with environment +// variables used for integration tests that use S3. +func NewS3Storage(prefix string) (storage.Storage, error) { + return storage.NewStorage( + storage.ProviderS3, + storage.S3Config{ + AccessKey: os.Getenv(storage.AWS_ACCESS_KEY_ID), + Bucket: "test-corso-repo-init", + Prefix: prefix, + SecretKey: os.Getenv(storage.AWS_SECRET_ACCESS_KEY), + SessionToken: os.Getenv(storage.AWS_SESSION_TOKEN), + }, + storage.CommonConfig{ + CorsoPassword: os.Getenv(storage.CORSO_PASSWORD), + }, + ) +} diff --git a/src/pkg/repository/repository_test.go b/src/pkg/repository/repository_test.go index 974d00998..ebfdce3ea 100644 --- a/src/pkg/repository/repository_test.go +++ b/src/pkg/repository/repository_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + ctesting "github.com/alcionai/corso/internal/testing" "github.com/alcionai/corso/pkg/repository" "github.com/alcionai/corso/pkg/storage" ) @@ -98,14 +99,7 @@ func TestRepositoryIntegrationSuite(t *testing.T) { // ensure all required env values are populated func (suite *RepositoryIntegrationSuite) SetupSuite() { - s3Envs := []string{ - storage.AWS_ACCESS_KEY_ID, - storage.AWS_SECRET_ACCESS_KEY, - storage.AWS_SESSION_TOKEN, - } - for _, env := range s3Envs { - require.NotZerof(suite.T(), os.Getenv(env), "env var [%s] must be populated for integration testing", env) - } + require.NoError(suite.T(), ctesting.CheckS3EnvVars()) } func (suite *RepositoryIntegrationSuite) TestInitialize() { @@ -122,19 +116,7 @@ func (suite *RepositoryIntegrationSuite) TestInitialize() { { prefix: "init-s3-" + timeOfTest, storage: func() (storage.Storage, error) { - return storage.NewStorage( - storage.ProviderS3, - storage.S3Config{ - AccessKey: os.Getenv(storage.AWS_ACCESS_KEY_ID), - Bucket: "test-corso-repo-init", - Prefix: "init-s3-" + timeOfTest, - SecretKey: os.Getenv(storage.AWS_SECRET_ACCESS_KEY), - SessionToken: os.Getenv(storage.AWS_SESSION_TOKEN), - }, - storage.CommonConfig{ - CorsoPassword: os.Getenv(storage.CORSO_PASSWORD), - }, - ) + return ctesting.NewS3Storage("init-s3-" + timeOfTest) }, errCheck: assert.NoError, },