diff --git a/Makefile b/Makefile index 8987e9763..f31297ab0 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,26 @@ +UNAME := $(shell sh -c 'uname') + +ifeq ($(UNAME), Darwin) + export ADVERTISED_HOST := $(shell sh -c 'boot2docker ip') +endif +ifeq ($(UNAME), Linux) + export ADVERTISED_HOST := localhost +endif + prepare: go get -d -v -t ./... - docker-compose up -d --no-recreate + +docker-compose: + docker-compose up -d + +test: prepare docker-compose + go test -v ./... test-short: prepare - go test -short ./... + go test -v -short ./... -test: prepare - go test ./... +test-cleanup: + docker-compose kill update: go get -u -v -d -t ./... diff --git a/README.md b/README.md index f4f49376c..1ce405f15 100644 --- a/README.md +++ b/README.md @@ -181,29 +181,29 @@ func init() { ## Testing -As Telegraf collects metrics from several third-party services it becomes a difficult task to mock each service as -some of them have complicated protocols which would take some time to replicate. - -To overcome this situation we've decided to use docker containers to provide a fast and reproducible environment -to test those services which require it. For other situations (i.e: https://github.com/influxdb/telegraf/blob/master/plugins/redis/redis_test.go ) a simple mock will suffice. - -To execute Telegraf tests follow these simple steps: - -- Install docker compose following [these](https://docs.docker.com/compose/install/) instructions - - NOTE: mac users should be able to simply do `brew install boot2docker` and `brew install docker-compose` - ### Execute short tests: execute `make short-test` ### Execute long tests: -These tests requre additional docker containers, such as for kafka -Mac: -execute ``ADVERTISED_HOST=`boot2docker ip` make test`` +As Telegraf collects metrics from several third-party services it becomes a +difficult task to mock each service as some of them have complicated protocols +which would take some time to replicate. -Linux: -execute `ADVERTISED_HOST=localhost make test` +To overcome this situation we've decided to use docker containers to provide a +fast and reproducible environment to test those services which require it. +For other situations +(i.e: https://github.com/influxdb/telegraf/blob/master/plugins/redis/redis_test.go ) +a simple mock will suffice. + +To execute Telegraf tests follow these simple steps: + +- Install docker compose following [these](https://docs.docker.com/compose/install/) instructions + - NOTE: mac users should be able to simply do `brew install boot2docker` + and `brew install docker-compose` + +execute `make test` ### Unit test troubleshooting: diff --git a/circle.yml b/circle.yml index 83cbd1d4c..d505706f7 100644 --- a/circle.yml +++ b/circle.yml @@ -19,5 +19,6 @@ test: - golint . - golint testutil/... - golint cmd/... - # TODO run unit tests - # - go test -short ./... + # Run short unit tests + - go test -v -short ./... + # TODO run full unit test suite diff --git a/plugins/memcached/memcached_test.go b/plugins/memcached/memcached_test.go index 522fc7bc7..501fed1b9 100644 --- a/plugins/memcached/memcached_test.go +++ b/plugins/memcached/memcached_test.go @@ -9,6 +9,10 @@ import ( ) func TestMemcachedGeneratesMetrics(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + m := &Memcached{ Servers: []string{testutil.GetLocalHost()}, } diff --git a/plugins/mysql/mysql_test.go b/plugins/mysql/mysql_test.go index e29e2e12e..b4c29146e 100644 --- a/plugins/mysql/mysql_test.go +++ b/plugins/mysql/mysql_test.go @@ -11,6 +11,10 @@ import ( ) func TestMysqlGeneratesMetrics(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + m := &Mysql{ Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())}, } @@ -54,6 +58,10 @@ func TestMysqlGeneratesMetrics(t *testing.T) { } func TestMysqlDefaultsToLocal(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + m := &Mysql{ Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())}, } diff --git a/plugins/postgresql/postgresql_test.go b/plugins/postgresql/postgresql_test.go index f9fb2b98e..363d289f9 100644 --- a/plugins/postgresql/postgresql_test.go +++ b/plugins/postgresql/postgresql_test.go @@ -10,10 +10,15 @@ import ( ) func TestPostgresqlGeneratesMetrics(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + p := &Postgresql{ Servers: []*Server{ { - Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), + Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", + testutil.GetLocalHost()), Databases: []string{"postgres"}, }, }, @@ -55,10 +60,15 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) { } func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + p := &Postgresql{ Servers: []*Server{ { - Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), + Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", + testutil.GetLocalHost()), Databases: []string{"postgres"}, }, }, @@ -76,10 +86,15 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { } func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + p := &Postgresql{ Servers: []*Server{ { - Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), + Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", + testutil.GetLocalHost()), }, }, } diff --git a/plugins/redis/redis_test.go b/plugins/redis/redis_test.go index 3e9915393..adf38ef75 100644 --- a/plugins/redis/redis_test.go +++ b/plugins/redis/redis_test.go @@ -12,6 +12,10 @@ import ( ) func TestRedisGeneratesMetrics(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + l, err := net.Listen("tcp", ":0") require.NoError(t, err) @@ -103,6 +107,10 @@ func TestRedisGeneratesMetrics(t *testing.T) { } func TestRedisCanPullStatsFromMultipleServers(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + l, err := net.Listen("tcp", ":0") require.NoError(t, err)