From 596a4cc37bc00cc50af375fe6976efacef89f55f Mon Sep 17 00:00:00 2001 From: Keepers <104464746+ryanfkeepers@users.noreply.github.com> Date: Tue, 31 May 2022 18:32:26 -0600 Subject: [PATCH] initial repo init integration test (#90) (#96) Implements an integration test in the repository that runs an end- to-end test of an s3 repo initialization through kopia. --- .github/workflows/ci.yml | 22 +++++++-- src/cli/repo/s3.go | 6 +-- src/pkg/repository/repository_test.go | 71 ++++++++++++++++++++++++++- src/pkg/storage/s3.go | 8 +++ 4 files changed, 100 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 501d9451d..9f4c8d081 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,10 +1,16 @@ name: CI on: push: - branches: [main] + branches: ['**'] pull_request: branches: [main] +permissions: + # required to retrieve AWS credentials + id-token: write + # required to retrieve repository + contents: read + jobs: Run-All: runs-on: ubuntu-latest @@ -32,5 +38,15 @@ jobs: - name: Run gofmt run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi - - run: echo "Running {{ github.repository }} deployment unit tests." - - run: go test ./... + - name: Configure AWS credentials from Test account + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::951767375776:role/corso-testing-role + role-session-name: integration-testing + aws-region: us-east-1 + + - run: echo "Running ${{ github.repository }} deployment unit tests at ${{ env.TIME_OF_TEST }}." + - name: Deployment Tests + env: + INTEGRATION_TESTING: true + run: go test ./... \ No newline at end of file diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index bb8c68ca9..27e4d74fb 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -113,7 +113,7 @@ func connectS3Cmd(cmd *cobra.Command, args []string) { // helper for aggregating aws connection details. func makeS3Config() storage.S3Config { - ak := os.Getenv("AWS_ACCESS_KEY_ID") + ak := os.Getenv(storage.AWS_ACCESS_KEY_ID) if len(accessKey) > 0 { ak = accessKey } @@ -122,7 +122,7 @@ func makeS3Config() storage.S3Config { Bucket: bucket, Endpoint: endpoint, Prefix: prefix, - SecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"), - SessionToken: os.Getenv("AWS_SESSION_TOKEN"), + SecretKey: os.Getenv(storage.AWS_SECRET_ACCESS_KEY), + SessionToken: os.Getenv(storage.AWS_SESSION_TOKEN), } } diff --git a/src/pkg/repository/repository_test.go b/src/pkg/repository/repository_test.go index 506c263d3..920a5234d 100644 --- a/src/pkg/repository/repository_test.go +++ b/src/pkg/repository/repository_test.go @@ -2,15 +2,22 @@ package repository_test import ( "context" + "os" "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/alcionai/corso/pkg/repository" "github.com/alcionai/corso/pkg/storage" ) +// --------------- +// unit tests +// --------------- + type RepositorySuite struct { suite.Suite } @@ -42,7 +49,7 @@ func (suite *RepositorySuite) TestInitialize() { } // repository.Connect involves end-to-end communication with kopia, therefore this only -// tests expected error cases from +// tests expected error cases func (suite *RepositorySuite) TestConnect() { table := []struct { name string @@ -64,3 +71,65 @@ func (suite *RepositorySuite) TestConnect() { }) } } + +// --------------- +// integration tests +// --------------- + +type RepositoryIntegrationSuite struct { + suite.Suite +} + +func TestRepositoryIntegrationSuite(t *testing.T) { + runIntegrationTests := os.Getenv("INTEGRATION_TESTING") + if runIntegrationTests != "true" { + t.Skip() + } + suite.Run(t, new(RepositoryIntegrationSuite)) +} + +// 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) + } +} + +func (suite *RepositoryIntegrationSuite) TestInitialize() { + ctx := context.Background() + timeOfTest := time.Now().UTC().Format("2016-01-02T15:04:05") + suite.T().Logf("TestInitialize() run at: %s", timeOfTest) + + table := []struct { + prefix string + account repository.Account + storage storage.Storage + errCheck assert.ErrorAssertionFunc + }{ + { + prefix: "init-s3-" + timeOfTest, + storage: 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), + }, + ), + errCheck: assert.NoError, + }, + } + for _, test := range table { + suite.T().Run(test.prefix, func(t *testing.T) { + _, err := repository.Initialize(ctx, test.account, test.storage) + test.errCheck(suite.T(), err) + }) + } +} diff --git a/src/pkg/storage/s3.go b/src/pkg/storage/s3.go index d47219f6c..bad47fd47 100644 --- a/src/pkg/storage/s3.go +++ b/src/pkg/storage/s3.go @@ -9,6 +9,14 @@ type S3Config struct { SessionToken string } +// envvar consts +const ( + AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID" + AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY" + AWS_SESSION_TOKEN = "AWS_SESSION_TOKEN" +) + +// config key consts const ( keyS3AccessKey = "s3_accessKey" keyS3Bucket = "s3_bucket"