From 7989cc94f18bb85789e9b5f6002973389b9e7336 Mon Sep 17 00:00:00 2001 From: Adam Eijdenberg Date: Fri, 6 Oct 2017 10:09:37 +1100 Subject: [PATCH] Add ability to skip tests that require Docker Set env variable SKIP_DOCKER=1 to skip tests that require Docker. Why? In some CI environments it is problematic to allow Docker containers to be created, this change gives the operator the choice to accept the risk of these tests failing. --- controllers/root_test.go | 2 +- helpers/testhelpers/docker/docker.go | 14 ++++++++++++-- mailer/mailer_test.go | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/controllers/root_test.go b/controllers/root_test.go index 7f16b3d8..234e17f4 100644 --- a/controllers/root_test.go +++ b/controllers/root_test.go @@ -34,7 +34,7 @@ func TestPingWithRedis(t *testing.T) { // Create a request response, request := NewTestRequest("GET", "/ping", nil) // Start up redis. - redisURI, cleanUpRedis, pauseRedis, unapuaseRedis := CreateTestRedis() + redisURI, cleanUpRedis, pauseRedis, unapuaseRedis := CreateTestRedis(t) os.Setenv("REDIS_URI", redisURI) // Remove redis when finished. defer cleanUpRedis() diff --git a/helpers/testhelpers/docker/docker.go b/helpers/testhelpers/docker/docker.go index 8a9ce9b0..c566ee23 100644 --- a/helpers/testhelpers/docker/docker.go +++ b/helpers/testhelpers/docker/docker.go @@ -7,6 +7,7 @@ import ( "net/url" "os" "strings" + "testing" dockerclient "github.com/fsouza/go-dockerclient" "github.com/garyburd/redigo/redis" @@ -45,9 +46,17 @@ func connectToDockerNetwork(pool *dockertest.Pool, return "", false } +// skipIfNoDocker will detect if Docker is unable to run, and if not, will skip the test +func skipIfNoDocker(t *testing.T) { + if os.Getenv("SKIP_DOCKER") == "1" { + t.Skip("No support for docker") + } +} + // CreateTestRedis creates a actual redis instance with docker. // Useful for unit tests. -func CreateTestRedis() (string, func(), func(), func()) { +func CreateTestRedis(t *testing.T) (string, func(), func(), func()) { + skipIfNoDocker(t) pool, err := dockertest.NewPool("") if err != nil { log.Fatalf("Could not connect to docker: %s", err) @@ -89,7 +98,8 @@ func CreateTestRedis() (string, func(), func(), func()) { // CreateTestMailCatcher creates a actual redis instance with docker. // Useful for unit tests. -func CreateTestMailCatcher() (string, string, string, func()) { +func CreateTestMailCatcher(t *testing.T) (string, string, string, func()) { + skipIfNoDocker(t) pool, err := dockertest.NewPool("") if err != nil { log.Fatalf("Could not connect to docker: %s", err) diff --git a/mailer/mailer_test.go b/mailer/mailer_test.go index a98708cb..d5002e51 100644 --- a/mailer/mailer_test.go +++ b/mailer/mailer_test.go @@ -11,7 +11,7 @@ import ( ) func TestSendEmail(t *testing.T) { - hostname, smtpPort, apiPort, cleanup := CreateTestMailCatcher() + hostname, smtpPort, apiPort, cleanup := CreateTestMailCatcher(t) // Test InitSMTPMailer with valid path for templates. settings := helpers.Settings{ BasePath: os.Getenv(helpers.BasePathEnvVar),