Factor out helper functions for testing (#115)
Helper functions do simple validation and initialization for S3 storage objects.
This commit is contained in:
parent
cc3306e5e0
commit
04d11503a9
45
src/internal/testing/storage.go
Normal file
45
src/internal/testing/storage.go
Normal 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),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
ctesting "github.com/alcionai/corso/internal/testing"
|
||||||
"github.com/alcionai/corso/pkg/repository"
|
"github.com/alcionai/corso/pkg/repository"
|
||||||
"github.com/alcionai/corso/pkg/storage"
|
"github.com/alcionai/corso/pkg/storage"
|
||||||
)
|
)
|
||||||
@ -98,14 +99,7 @@ func TestRepositoryIntegrationSuite(t *testing.T) {
|
|||||||
|
|
||||||
// ensure all required env values are populated
|
// ensure all required env values are populated
|
||||||
func (suite *RepositoryIntegrationSuite) SetupSuite() {
|
func (suite *RepositoryIntegrationSuite) SetupSuite() {
|
||||||
s3Envs := []string{
|
require.NoError(suite.T(), ctesting.CheckS3EnvVars())
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *RepositoryIntegrationSuite) TestInitialize() {
|
func (suite *RepositoryIntegrationSuite) TestInitialize() {
|
||||||
@ -122,19 +116,7 @@ func (suite *RepositoryIntegrationSuite) TestInitialize() {
|
|||||||
{
|
{
|
||||||
prefix: "init-s3-" + timeOfTest,
|
prefix: "init-s3-" + timeOfTest,
|
||||||
storage: func() (storage.Storage, error) {
|
storage: func() (storage.Storage, error) {
|
||||||
return storage.NewStorage(
|
return ctesting.NewS3Storage("init-s3-" + timeOfTest)
|
||||||
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),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
errCheck: assert.NoError,
|
errCheck: assert.NoError,
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user