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 2 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"
}
24 changes: 14 additions & 10 deletions pubsub/kafkapubsub/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"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 @@ -55,19 +56,19 @@ type harness struct {
var checkOnce sync.Once
var kafkaRunning bool

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

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")
}
if err := localKafkaRunning(); err != nil {
t.Fatalf("No local Kafka running: %v, see pubsub/kafkapubsub/localkafka.sh", err)
}
return &harness{uniqueID: rand.Int()}, nil
}
Expand Down Expand Up @@ -196,8 +197,11 @@ 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")
}
if err := localKafkaRunning(); err != nil {
shantuo marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("No local Kafka running: %v, see pubsub/kafkapubsub/localkafka.sh", err)
}
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
20 changes: 13 additions & 7 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 @@ -38,20 +39,25 @@ var (
)

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")
if setup.HasDockerTestEnvironment() {
etcdClient, etcdError = clientv3.NewFromURL("http://localhost:2379")
if etcdError == nil {
shantuo marked this conversation as resolved.
Show resolved Hide resolved
// 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{}

func newHarness(t *testing.T) (drivertest.Harness, error) {
if !setup.HasDockerTestEnvironment() {
t.Skip("Skipping etcd tests since the etcd server is not available")
}
if etcdError != nil {
t.Skip("No local etcd server running, see runtimevar/etcdvar/localetcd.sh")
t.Fatal("No local etcd server running, see runtimevar/etcdvar/localetcd.sh", etcdError)
}
return &harness{}, nil
}
Expand Down
21 changes: 18 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,8 @@ const (
testToken = "faketoken"
)

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 +53,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 +67,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