Factor out helper functions for testing (#115)

Helper functions do simple validation and initialization for S3 storage
objects.
This commit is contained in:
ashmrtn 2022-06-02 16:57:13 -07:00 committed by GitHub
parent cc3306e5e0
commit 04d11503a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 21 deletions

View File

@ -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),
},
)
}

View File

@ -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,
},