From 44392ab8b25424ce1f5ce36bbd69934b883525b8 Mon Sep 17 00:00:00 2001 From: Keepers <104464746+ryanfkeepers@users.noreply.github.com> Date: Mon, 6 Jun 2022 13:17:40 -0600 Subject: [PATCH] adds env-based integration test controls (#128) * adds env-based integration test controls Some integration tests were running locally without being told to do so via env variables. This adds some controllers for validating if and when those tests should run, and for ensuring the env variables necessary to run the test are available. * testing helper comments, func name change * add testing timeOf log, skip gc ci tests * corrected the test skip in GC_test * update kopia_test with helpers * rename /testing files to *_test.go This will ensure these files do not compile into the release binary. * Revert "rename /testing files to *_test.go" This reverts commit 04fd2046cc6d11bf3e3eb98d98a3e84b1bba6366. --- .github/workflows/ci.yml | 2 +- .../connector/graph_connector_test.go | 74 +++++++++++-------- src/internal/kopia/kopia_test.go | 13 +--- src/internal/testing/envvars.go | 21 ++++++ src/internal/testing/integration_runners.go | 46 ++++++++++++ src/pkg/repository/repository_test.go | 13 ++-- 6 files changed, 122 insertions(+), 47 deletions(-) create mode 100644 src/internal/testing/envvars.go create mode 100644 src/internal/testing/integration_runners.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d947f9ad..c53a8e063 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,6 +48,6 @@ jobs: - run: echo "Running ${{ github.repository }} deployment unit tests at ${{ env.TIME_OF_TEST }}." - name: Deployment Tests env: - INTEGRATION_TESTING: true + CORSO_CI_TESTS: true CORSO_PASSWORD: ${{ secrets.INTEGRATION_TEST_CORSO_PASSWORD }} run: go test ./... \ No newline at end of file diff --git a/src/internal/connector/graph_connector_test.go b/src/internal/connector/graph_connector_test.go index 2a1367f29..d5d79bbfb 100644 --- a/src/internal/connector/graph_connector_test.go +++ b/src/internal/connector/graph_connector_test.go @@ -1,44 +1,63 @@ package connector_test import ( - "os" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" graph "github.com/alcionai/corso/internal/connector" + ctesting "github.com/alcionai/corso/internal/testing" ) -type GraphConnectorTestSuite struct { +type GraphConnectorIntegrationSuite struct { suite.Suite connector *graph.GraphConnector } -type DiconnectedGraphConnectorTestSuite struct { +func TestGraphConnectorSuite(t *testing.T) { + if err := ctesting.RunOnAny( + ctesting.CORSO_CI_TESTS, + ctesting.CORSO_GRAPH_CONNECTOR_TESTS, + ); err != nil { + t.Skip(err) + } + if err := ctesting.RunOnAny( + "this-is-fake-it-forces-a-skip-until-we-fix-ci-details-here(rkeepers)", + ); err != nil { + t.Skip(err) + } + suite.Run(t, new(GraphConnectorIntegrationSuite)) +} + +func (suite *GraphConnectorIntegrationSuite) SetupSuite() { + evs, err := ctesting.GetRequiredEnvVars("TENANT_ID", "CLIENT_ID", "CLIENT_SECRET") + if err != nil { + suite.T().Fatal(err) + } + suite.connector, err = graph.NewGraphConnector( + evs["TENANT_ID"], + evs["CLIENT_ID"], + evs["CLIENT_SECRET"]) + suite.NoError(err) +} + +func (suite *GraphConnectorIntegrationSuite) TestGraphConnector() { + ctesting.LogTimeOfTest(suite.T()) + suite.NotNil(suite.connector) +} + +// -------------------- + +type DiconnectedGraphConnectorSuite struct { suite.Suite } -func (suite *GraphConnectorTestSuite) SetupSuite() { - tenant := os.Getenv("TENANT_ID") - client := os.Getenv("CLIENT_ID") - secret := os.Getenv("CLIENT_SECRET") - if os.Getenv("CI") == "" { - var err error - suite.connector, err = graph.NewGraphConnector(tenant, client, secret) - assert.Nil(suite.T(), err) - } -} - -func TestGraphConnectorSuite(t *testing.T) { - suite.Run(t, new(GraphConnectorTestSuite)) -} - func TestDisconnectedGraphSuite(t *testing.T) { - suite.Run(t, new(DiconnectedGraphConnectorTestSuite)) + suite.Run(t, new(DiconnectedGraphConnectorSuite)) } -func (suite *DiconnectedGraphConnectorTestSuite) TestBadConnection() { +func (suite *DiconnectedGraphConnectorSuite) TestBadConnection() { table := []struct { name string params []string @@ -53,15 +72,10 @@ func (suite *DiconnectedGraphConnectorTestSuite) TestBadConnection() { }, } for _, test := range table { - gc, err := graph.NewGraphConnector(test.params[0], test.params[1], test.params[2]) - assert.Nil(suite.T(), gc, test.name+" failed") - assert.NotNil(suite.T(), err, test.name+"failed") + suite.T().Run(test.name, func(t *testing.T) { + gc, err := graph.NewGraphConnector(test.params[0], test.params[1], test.params[2]) + assert.Nil(t, gc, test.name+" failed") + assert.NotNil(t, err, test.name+"failed") + }) } } - -func (suite *GraphConnectorTestSuite) TestGraphConnector() { - if os.Getenv("INTEGRATION_TESTING") != "" { - suite.T().Skip("Environmental Variables not set") - } - suite.True(suite.connector != nil) -} diff --git a/src/internal/kopia/kopia_test.go b/src/internal/kopia/kopia_test.go index 5c87dfa86..2f05ec9e1 100644 --- a/src/internal/kopia/kopia_test.go +++ b/src/internal/kopia/kopia_test.go @@ -2,9 +2,7 @@ package kopia import ( "context" - "os" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -36,8 +34,7 @@ type KopiaUnitSuite struct { func (suite *KopiaUnitSuite) TestCloseWithoutOpenDoesNotCrash() { ctx := context.Background() - timeOfTest := time.Now().UTC().Format("2016-01-02T15:04:05") - suite.T().Logf("TestCloseWithoutOpenDoesNotCrash() run at %s", timeOfTest) + ctesting.LogTimeOfTest(suite.T()) k := KopiaWrapper{} assert.NotPanics(suite.T(), func() { @@ -53,9 +50,8 @@ type KopiaIntegrationSuite struct { } func TestKopiaIntegrationSuite(t *testing.T) { - runIntegrationTests := os.Getenv("INTEGRATION_TESTING") - if runIntegrationTests != "true" { - t.Skip() + if err := ctesting.RunOnAny(ctesting.CORSO_CI_TESTS); err != nil { + t.Skip(err) } suite.Run(t, new(KopiaIntegrationSuite)) @@ -67,8 +63,7 @@ func (suite *KopiaIntegrationSuite) SetupSuite() { func (suite *KopiaIntegrationSuite) TestCloseTwiceDoesNotCrash() { ctx := context.Background() - timeOfTest := time.Now().UTC().Format("2016-01-02T15:04:05") - suite.T().Logf("TestCloseTwiceDoesNotCrash() run at %s", timeOfTest) + timeOfTest := ctesting.LogTimeOfTest(suite.T()) k, err := openKopiaRepo(ctx, "init-s3-"+timeOfTest) assert.NoError(suite.T(), err) diff --git a/src/internal/testing/envvars.go b/src/internal/testing/envvars.go new file mode 100644 index 000000000..84bcd1083 --- /dev/null +++ b/src/internal/testing/envvars.go @@ -0,0 +1,21 @@ +package testing + +import ( + "errors" + "os" +) + +// GetRequiredEnvVars retrieves the provided env vars from the os. +// Retrieved values are populated into the resulting map. +// If any of the env values are zero length, returns an error. +func GetRequiredEnvVars(evs ...string) (map[string]string, error) { + vals := map[string]string{} + for _, ev := range evs { + ge := os.Getenv(ev) + if len(ge) == 0 { + return nil, errors.New(ev + " env var required for test suite") + } + vals[ev] = ge + } + return vals, nil +} diff --git a/src/internal/testing/integration_runners.go b/src/internal/testing/integration_runners.go new file mode 100644 index 000000000..0b7d8e31b --- /dev/null +++ b/src/internal/testing/integration_runners.go @@ -0,0 +1,46 @@ +package testing + +import ( + "fmt" + "os" + "runtime" + "strings" + "testing" + "time" +) + +const ( + CORSO_CI_TESTS = "CORSO_CI_TESTS" + CORSO_GRAPH_CONNECTOR_TESTS = "CORSO_GRAPH_CONNECTOR_TESTS" + CORSO_REPOSITORY_TESTS = "CORSO_REPOSITORY_TESTS" +) + +// RunOnAny takes in a list of env variable names and returns +// an error if all of them are zero valued. Implication being: +// if any of those env vars are truthy, you should run the +// subsequent tests. +func RunOnAny(tests ...string) error { + var l int + for _, test := range tests { + l += len(os.Getenv(test)) + } + if l == 0 { + return fmt.Errorf( + "%s env vars are not flagged for testing", + strings.Join(tests, ", ")) + } + return nil +} + +// LogTimeOfTest logs the test name and the time that it was run. +func LogTimeOfTest(t *testing.T) string { + now := time.Now().UTC().Format("2016-01-02T15:04:05") + pc, _, _, ok := runtime.Caller(1) + details := runtime.FuncForPC(pc) + if !ok || details != nil { + t.Logf("Test run at %s.", now) + return now + } + t.Logf("%s() run at %s", details.Name(), now) + return now +} diff --git a/src/pkg/repository/repository_test.go b/src/pkg/repository/repository_test.go index b69200d80..e90bb8bce 100644 --- a/src/pkg/repository/repository_test.go +++ b/src/pkg/repository/repository_test.go @@ -2,9 +2,7 @@ package repository_test import ( "context" - "os" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -90,9 +88,11 @@ type RepositoryIntegrationSuite struct { } func TestRepositoryIntegrationSuite(t *testing.T) { - runIntegrationTests := os.Getenv("INTEGRATION_TESTING") - if runIntegrationTests != "true" { - t.Skip() + if err := ctesting.RunOnAny( + ctesting.CORSO_CI_TESTS, + ctesting.CORSO_REPOSITORY_TESTS, + ); err != nil { + t.Skip(err) } suite.Run(t, new(RepositoryIntegrationSuite)) } @@ -104,8 +104,7 @@ func (suite *RepositoryIntegrationSuite) SetupSuite() { 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) + timeOfTest := ctesting.LogTimeOfTest(suite.T()) table := []struct { prefix string