Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: run Docker dependent tests whenever possible #2029

Merged
merged 7 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/docstore/mongodocstore/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"gocloud.dev/internal/docstore"
"gocloud.dev/internal/docstore/driver"
"gocloud.dev/internal/docstore/drivertest"
"gocloud.dev/internal/testing/setup"
)

const (
Expand Down Expand Up @@ -136,16 +137,16 @@ func TestConformance(t *testing.T) {
}

func newTestClient(t *testing.T) *mongo.Client {
if !setup.HasDockerTestEnvironment() {
t.Skip("Skipping Mongo tests since the Mongo server is not available")
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
client, err := Dial(ctx, serverURI)
if err != nil {
t.Fatalf("dialing to %s: %v", serverURI, err)
}
if err := client.Ping(ctx, nil); err != nil {
if err == context.DeadlineExceeded {
t.Skip("could not connect to local mongoDB server (connection timed out)")
}
t.Fatalf("connecting to %s: %v", serverURI, err)
}
return client
Expand Down
2 changes: 1 addition & 1 deletion internal/testing/runchecks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fi

# start_local_deps.sh requires that Docker is installed, via Travis services,
# which are only supported on Linux.
# Without the dependencies running, tests that depend on them are skipped.
# Tests that depend on them should check the Travis environment before running.
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
echo
echo "Starting local dependencies..."
Expand Down
8 changes: 8 additions & 0 deletions internal/testing/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,11 @@ func NewGCPDialOptions(t *testing.T, recording bool, filename string) (opts []gr
}
return opts, done
}

// HasDockerTestEnvironment returns true when either:
// 1) Not on Travis.
// 2) On Travis Linux environment, where Docker is available.
func HasDockerTestEnvironment() bool {
s := os.Getenv("TRAVIS_OS_NAME")
return s == "" || s == "linux"
}
23 changes: 5 additions & 18 deletions pubsub/kafkapubsub/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
"math/rand"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/Shopify/sarama"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"gocloud.dev/internal/testing/setup"
"gocloud.dev/pubsub"
"gocloud.dev/pubsub/driver"
"gocloud.dev/pubsub/drivertest"
Expand All @@ -52,22 +52,9 @@ type harness struct {
numTopics uint32
}

var checkOnce sync.Once
var kafkaRunning bool

func localKafkaRunning() bool {
checkOnce.Do(func() {
_, err := sarama.NewClient(localBrokerAddrs, MinimalConfig())
if err == nil {
kafkaRunning = true
}
})
return kafkaRunning
}

func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
if !localKafkaRunning() {
t.Skip("No local Kafka running, see pubsub/kafkapubsub/localkafka.sh")
if !setup.HasDockerTestEnvironment() {
t.Skip("Skipping Kafka tests since the Kafka server is not available")
}
return &harness{uniqueID: rand.Int()}, nil
}
Expand Down Expand Up @@ -196,8 +183,8 @@ func (asTest) BeforeSend(as func(interface{}) bool) error {

// TestKafkaKey tests sending/receiving a message with the Kafka message key set.
func TestKafkaKey(t *testing.T) {
if !localKafkaRunning() {
t.Skip("No local Kafka running, see pubsub/kafkapubsub/localkafka.sh")
if !setup.HasDockerTestEnvironment() {
t.Skip("Skipping Kafka tests since the Kafka server is not available")
}
const (
keyName = "kafkakey"
Expand Down
10 changes: 7 additions & 3 deletions pubsub/rabbitpubsub/rabbit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/streadway/amqp"
"gocloud.dev/gcerrors"
"gocloud.dev/internal/testing/setup"
"gocloud.dev/pubsub"
"gocloud.dev/pubsub/driver"
"gocloud.dev/pubsub/drivertest"
Expand All @@ -42,13 +43,16 @@ const rabbitURL = "amqp://guest:guest@localhost:5672/"
var logOnce sync.Once

func mustDialRabbit(t testing.TB) amqpConnection {
conn, err := amqp.Dial(rabbitURL)
if err != nil {
if !setup.HasDockerTestEnvironment() {
logOnce.Do(func() {
t.Logf("using the fake because the RabbitMQ server is not up (dial error: %v)", err)
t.Log("using the fake because the RabbitMQ server is not available")
})
return newFakeConnection()
}
conn, err := amqp.Dial(rabbitURL)
if err != nil {
t.Fatal(err)
}
logOnce.Do(func() {
t.Logf("using the RabbitMQ server at %s", rabbitURL)
})
Expand Down
34 changes: 13 additions & 21 deletions runtimevar/etcdvar/etcdvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/google/go-cmp/cmp"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
"gocloud.dev/internal/testing/setup"
"gocloud.dev/runtimevar"
"gocloud.dev/runtimevar/driver"
"gocloud.dev/runtimevar/drivertest"
Expand All @@ -32,36 +33,27 @@ import (
// To run these tests against a local etcd server, first run ./localetcd.sh.
// Then wait a few seconds for the server to be ready.

var (
etcdClient *clientv3.Client
etcdError error
)

func init() {
etcdClient, etcdError = clientv3.NewFromURL("http://localhost:2379")
if etcdError == nil {
// Check to see if the local etcd is actually running.
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, etcdError = etcdClient.Put(ctx, "unused", "unused")
}
type harness struct {
client *clientv3.Client
}

type harness struct{}

func newHarness(t *testing.T) (drivertest.Harness, error) {
if etcdError != nil {
t.Skip("No local etcd server running, see runtimevar/etcdvar/localetcd.sh")
if !setup.HasDockerTestEnvironment() {
t.Skip("Skipping etcd tests since the etcd server is not available")
}
c, err := clientv3.NewFromURL("http://localhost:2379")
if err != nil {
t.Fatalf("No local etcd server running: %v; see runtimevar/etcdvar/localetcd.sh", err)
}
return &harness{}, nil
return &harness{client: c}, nil
}

func (h *harness) MakeWatcher(ctx context.Context, name string, decoder *runtimevar.Decoder) (driver.Watcher, error) {
return newWatcher(etcdClient, name, decoder, nil), nil
return newWatcher(h.client, name, decoder, nil), nil
}

func (h *harness) CreateVariable(ctx context.Context, name string, val []byte) error {
_, err := etcdClient.Put(ctx, name, string(val))
_, err := h.client.Put(ctx, name, string(val))
return err
}

Expand All @@ -70,7 +62,7 @@ func (h *harness) UpdateVariable(ctx context.Context, name string, val []byte) e
}

func (h *harness) DeleteVariable(ctx context.Context, name string) error {
_, err := etcdClient.Delete(ctx, name)
_, err := h.client.Delete(ctx, name)
return err
}

Expand Down
22 changes: 19 additions & 3 deletions secrets/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"context"
"errors"
"os"
"sync"
"testing"
"time"

"github.com/hashicorp/vault/api"
"gocloud.dev/internal/testing/setup"
"gocloud.dev/secrets"
"gocloud.dev/secrets/driver"
"gocloud.dev/secrets/drivertest"
Expand All @@ -37,6 +39,9 @@ const (
testToken = "faketoken"
)

// enableTransit checks and makes sure the Transit API is enabled only once.
var enableTransit sync.Once
shantuo marked this conversation as resolved.
Show resolved Hide resolved

type harness struct {
client *api.Client
close func()
Expand All @@ -49,6 +54,9 @@ func (h *harness) MakeDriver(ctx context.Context) (driver.Keeper, driver.Keeper,
func (h *harness) Close() {}

func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
if !setup.HasDockerTestEnvironment() {
t.Skip("Skipping Vault tests since the Vault server is not available")
}
c, err := Dial(ctx, &Config{
Token: testToken,
APIConfig: api.Config{
Expand All @@ -60,9 +68,17 @@ func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
}
c.SetClientTimeout(3 * time.Second)
// Enable the Transit Secrets Engine to use Vault as an Encryption as a Service.
if _, err := c.Logical().Write("sys/mounts/transit", map[string]interface{}{"type": "transit"}); err != nil {
t.Skip(err, "run secrets/vault/localvault.sh to start a dev vault container")
}
enableTransit.Do(func() {
s, err := c.Logical().Read("sys/mounts")
if err != nil {
t.Fatal(err, "; run secrets/vault/localvault.sh to start a dev vault container")
}
if _, ok := s.Data["transit/"]; !ok {
if _, err := c.Logical().Write("sys/mounts/transit", map[string]interface{}{"type": "transit"}); err != nil {
t.Fatal(err, "; run secrets/vault/localvault.sh to start a dev vault container")
}
}
})

return &harness{
client: c,
Expand Down