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.
This commit is contained in:
Keepers 2022-06-06 13:17:40 -06:00 committed by GitHub
parent ecb894fc17
commit 44392ab8b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 122 additions and 47 deletions

View File

@ -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 ./...

View File

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

View File

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

View File

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

View File

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

View File

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