From d545b197ea2e309461146de0964916d52d6c7fdf Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 01:46:43 -0300 Subject: [PATCH 1/8] Add docker containers to test services. This commit initializes the needed services which are not mocked so tests can be executed in any environment with docker. Some default modifications (i.e: connection strings) were also made to current tests to accomodate them for this setup. A docker-compose.yml file is provided with all the necessary parameters for this services to be initialized. Future services can be added easily by extending this configuration file In addition a makefile has been introduced to simplify command execution --- Makefile | 11 +++++++++++ README.md | 13 +++++++++++++ docker-compose.yml | 16 ++++++++++++++++ .../kafka_consumer_integration_test.go | 3 +++ plugins/mysql/mysql_test.go | 6 ++++-- plugins/postgresql/postgresql_test.go | 6 +++--- plugins/redis/redis_test.go | 8 ++++---- 7 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 Makefile create mode 100644 docker-compose.yml diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..12b3aeac7 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +prepare: + go get -d -v -t ./... + docker-compose up -d + +test: prepare + go test -short ./... + +update: + go get -u -v -d -t ./... + +.PHONY: test diff --git a/README.md b/README.md index e7e95455d..c3373f4b8 100644 --- a/README.md +++ b/README.md @@ -175,3 +175,16 @@ 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 this simple steps: + +- Install docker compose following [these](https://docs.docker.com/compose/install/) instructions +- execute `make test` + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..d54a25f75 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +mysql: + image: mysql + ports: + - "3306:3306" + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: true + +memcached: + image: memcached + ports: + - "11211:11211" + +postgres: + image: postgres + ports: + - "5432:5432" diff --git a/plugins/kafka_consumer/kafka_consumer_integration_test.go b/plugins/kafka_consumer/kafka_consumer_integration_test.go index 1541cb127..2a82a2374 100644 --- a/plugins/kafka_consumer/kafka_consumer_integration_test.go +++ b/plugins/kafka_consumer/kafka_consumer_integration_test.go @@ -14,6 +14,9 @@ import ( ) func TestReadsMetricsFromKafka(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } var zkPeers, brokerPeers []string if len(os.Getenv("ZOOKEEPER_PEERS")) == 0 { diff --git a/plugins/mysql/mysql_test.go b/plugins/mysql/mysql_test.go index 76cb28b0d..f09c0307e 100644 --- a/plugins/mysql/mysql_test.go +++ b/plugins/mysql/mysql_test.go @@ -11,7 +11,7 @@ import ( func TestMysqlGeneratesMetrics(t *testing.T) { m := &Mysql{ - Servers: []string{""}, + Servers: []string{"root@tcp(localhost:3306)/"}, } var acc testutil.Accumulator @@ -53,7 +53,9 @@ func TestMysqlGeneratesMetrics(t *testing.T) { } func TestMysqlDefaultsToLocal(t *testing.T) { - m := &Mysql{} + m := &Mysql{ + Servers: []string{"root@tcp(localhost:3306)/"}, + } var acc testutil.Accumulator diff --git a/plugins/postgresql/postgresql_test.go b/plugins/postgresql/postgresql_test.go index 91776d619..775087700 100644 --- a/plugins/postgresql/postgresql_test.go +++ b/plugins/postgresql/postgresql_test.go @@ -12,7 +12,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "sslmode=disable", + Address: "host=localhost user=postgres sslmode=disable", Databases: []string{"postgres"}, }, }, @@ -57,7 +57,7 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "sslmode=disable", + Address: "host=localhost user=postgres sslmode=disable", Databases: []string{"postgres"}, }, }, @@ -78,7 +78,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "sslmode=disable", + Address: "host=localhost user=postgres sslmode=disable", }, }, } diff --git a/plugins/redis/redis_test.go b/plugins/redis/redis_test.go index 317fde783..7bd412e15 100644 --- a/plugins/redis/redis_test.go +++ b/plugins/redis/redis_test.go @@ -83,7 +83,7 @@ func TestRedisGeneratesMetrics(t *testing.T) { } for _, c := range checkInt { - assert.NoError(t, acc.ValidateValue(c.name, c.value)) + assert.True(t, acc.CheckValue(c.name, c.value)) } checkFloat := []struct { @@ -98,7 +98,7 @@ func TestRedisGeneratesMetrics(t *testing.T) { } for _, c := range checkFloat { - assert.NoError(t, acc.ValidateValue(c.name, c.value)) + assert.True(t, acc.CheckValue(c.name, c.value)) } } @@ -174,7 +174,7 @@ func TestRedisCanPullStatsFromMultipleServers(t *testing.T) { } for _, c := range checkInt { - assert.NoError(t, acc.ValidateValue(c.name, c.value)) + assert.True(t, acc.CheckValue(c.name, c.value)) } checkFloat := []struct { @@ -189,7 +189,7 @@ func TestRedisCanPullStatsFromMultipleServers(t *testing.T) { } for _, c := range checkFloat { - assert.NoError(t, acc.ValidateValue(c.name, c.value)) + assert.True(t, acc.CheckValue(c.name, c.value)) } } From 0db55007abc2a7d2b8acba98f1b7bdb8735f711a Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 01:51:13 -0300 Subject: [PATCH 2/8] Add cirleci script --- circle.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 circle.yml diff --git a/circle.yml b/circle.yml new file mode 100644 index 000000000..0c76101b9 --- /dev/null +++ b/circle.yml @@ -0,0 +1,15 @@ +machine: + python: + version: 2.7.3 + services: + - docker + +dependencies: + pre: + - pip install -U docker-compose==1.2.0 + - docker-compose pull + + +test: + override: + - make test From ae385b336d081b84f5d4731b3e29a3e108db81a8 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 02:20:25 -0300 Subject: [PATCH 3/8] Remove unnecessary circleci configuration as we're using default provided services Update test users to use circleci default services --- circle.yml | 14 +------------- docker-compose.yml | 4 +++- plugins/postgresql/postgresql_test.go | 6 +++--- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/circle.yml b/circle.yml index 0c76101b9..c5cfc7323 100644 --- a/circle.yml +++ b/circle.yml @@ -1,15 +1,3 @@ -machine: - python: - version: 2.7.3 - services: - - docker - -dependencies: - pre: - - pip install -U docker-compose==1.2.0 - - docker-compose pull - - test: override: - - make test + - go test -short ./... diff --git a/docker-compose.yml b/docker-compose.yml index d54a25f75..5945f06ab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ mysql: ports: - "3306:3306" environment: - MYSQL_ALLOW_EMPTY_PASSWORD: true + MYSQL_ALLOW_EMPTY_PASSWORD: yes memcached: image: memcached @@ -14,3 +14,5 @@ postgres: image: postgres ports: - "5432:5432" + environment: + POSTGRES_USER: ubuntu diff --git a/plugins/postgresql/postgresql_test.go b/plugins/postgresql/postgresql_test.go index 775087700..ab17601b1 100644 --- a/plugins/postgresql/postgresql_test.go +++ b/plugins/postgresql/postgresql_test.go @@ -12,7 +12,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=postgres sslmode=disable", + Address: "host=localhost user=ubuntu sslmode=disable", Databases: []string{"postgres"}, }, }, @@ -57,7 +57,7 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=postgres sslmode=disable", + Address: "host=localhost user=ubuntu sslmode=disable", Databases: []string{"postgres"}, }, }, @@ -78,7 +78,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=postgres sslmode=disable", + Address: "host=localhost user=ubuntu sslmode=disable", }, }, } From 63552282d70eb1e802812132ffce4c8bf4041d58 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 03:38:08 -0300 Subject: [PATCH 4/8] Remove circle ci implementation due to Golang bug. I've tried to set-up circleci but I came across a Golang issue that was preventing CPU tests to PASS. Instead of ignoring those tests I've submitted an issue to Go (https://github.com/golang/go/issues/11609) hoping that this gets fixed soon. --- circle.yml | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 circle.yml diff --git a/circle.yml b/circle.yml deleted file mode 100644 index c5cfc7323..000000000 --- a/circle.yml +++ /dev/null @@ -1,3 +0,0 @@ -test: - override: - - go test -short ./... From 4471e2bdbbe42c6baf887c27300d2d26206648bc Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 03:46:53 -0300 Subject: [PATCH 5/8] Use postgres default configuration --- docker-compose.yml | 2 -- plugins/postgresql/postgresql_test.go | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5945f06ab..f2c1b2b54 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,5 +14,3 @@ postgres: image: postgres ports: - "5432:5432" - environment: - POSTGRES_USER: ubuntu diff --git a/plugins/postgresql/postgresql_test.go b/plugins/postgresql/postgresql_test.go index ab17601b1..775087700 100644 --- a/plugins/postgresql/postgresql_test.go +++ b/plugins/postgresql/postgresql_test.go @@ -12,7 +12,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=ubuntu sslmode=disable", + Address: "host=localhost user=postgres sslmode=disable", Databases: []string{"postgres"}, }, }, @@ -57,7 +57,7 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=ubuntu sslmode=disable", + Address: "host=localhost user=postgres sslmode=disable", Databases: []string{"postgres"}, }, }, @@ -78,7 +78,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=ubuntu sslmode=disable", + Address: "host=localhost user=postgres sslmode=disable", }, }, } From aa86c16838acc707fcad583701677def8a6ef704 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 21:17:44 -0300 Subject: [PATCH 6/8] Add --no-recreate option to prepare target --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 12b3aeac7..cefa9ac44 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ prepare: go get -d -v -t ./... - docker-compose up -d + docker-compose up -d --no-recreate test: prepare go test -short ./... From d2810ddc95b5400ef26a733d5fbe4a0a1f0acaea Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 22:18:31 -0300 Subject: [PATCH 7/8] Add DOCKER_HOST support for tests This allows to run tests in environments where DOCKER_HOST is used. This is extremely helpful when using boot2docker to run docker --- plugins/memcached/memcached_test.go | 2 +- plugins/mysql/mysql_test.go | 5 +++-- plugins/postgresql/postgresql_test.go | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/memcached/memcached_test.go b/plugins/memcached/memcached_test.go index 08e696fb7..522fc7bc7 100644 --- a/plugins/memcached/memcached_test.go +++ b/plugins/memcached/memcached_test.go @@ -10,7 +10,7 @@ import ( func TestMemcachedGeneratesMetrics(t *testing.T) { m := &Memcached{ - Servers: []string{"localhost"}, + Servers: []string{testutil.GetLocalHost()}, } var acc testutil.Accumulator diff --git a/plugins/mysql/mysql_test.go b/plugins/mysql/mysql_test.go index f09c0307e..e29e2e12e 100644 --- a/plugins/mysql/mysql_test.go +++ b/plugins/mysql/mysql_test.go @@ -1,6 +1,7 @@ package mysql import ( + "fmt" "strings" "testing" @@ -11,7 +12,7 @@ import ( func TestMysqlGeneratesMetrics(t *testing.T) { m := &Mysql{ - Servers: []string{"root@tcp(localhost:3306)/"}, + Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())}, } var acc testutil.Accumulator @@ -54,7 +55,7 @@ func TestMysqlGeneratesMetrics(t *testing.T) { func TestMysqlDefaultsToLocal(t *testing.T) { m := &Mysql{ - Servers: []string{"root@tcp(localhost:3306)/"}, + Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())}, } var acc testutil.Accumulator diff --git a/plugins/postgresql/postgresql_test.go b/plugins/postgresql/postgresql_test.go index 775087700..f9fb2b98e 100644 --- a/plugins/postgresql/postgresql_test.go +++ b/plugins/postgresql/postgresql_test.go @@ -1,6 +1,7 @@ package postgresql import ( + "fmt" "testing" "github.com/influxdb/telegraf/testutil" @@ -12,7 +13,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=postgres sslmode=disable", + Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), Databases: []string{"postgres"}, }, }, @@ -57,7 +58,7 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=postgres sslmode=disable", + Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), Databases: []string{"postgres"}, }, }, @@ -78,7 +79,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { p := &Postgresql{ Servers: []*Server{ { - Address: "host=localhost user=postgres sslmode=disable", + Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), }, }, } From ef335d9fd736923fef0365cab7814be15eb338cd Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 6 Jul 2015 22:21:46 -0300 Subject: [PATCH 8/8] Add missing files --- testutil/testutil.go | 12 ++++++++++++ testutil/testutil_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 testutil/testutil.go create mode 100644 testutil/testutil_test.go diff --git a/testutil/testutil.go b/testutil/testutil.go new file mode 100644 index 000000000..71c3ce0c2 --- /dev/null +++ b/testutil/testutil.go @@ -0,0 +1,12 @@ +package testutil + +import "os" + +var localhost = "localhost" + +func GetLocalHost() string { + if dockerHostVar := os.Getenv("DOCKER_HOST"); dockerHostVar != "" { + return dockerHostVar + } + return localhost +} diff --git a/testutil/testutil_test.go b/testutil/testutil_test.go new file mode 100644 index 000000000..39f548c0f --- /dev/null +++ b/testutil/testutil_test.go @@ -0,0 +1,26 @@ +package testutil + +import ( + "os" + "testing" +) + +func TestDockerHost(t *testing.T) { + + os.Unsetenv("DOCKER_HOST") + + host := GetLocalHost() + + if host != localhost { + t.Fatalf("Host should be localhost when DOCKER_HOST is not set. Current value [%s]", host) + } + + os.Setenv("DOCKER_HOST", "1.1.1.1") + + host = GetLocalHost() + + if host != "1.1.1.1" { + t.Fatalf("Host should take DOCKER_HOST value when set. Current value is [%s] and DOCKER_HOST is [%s]", host, os.Getenv("DOCKER_HOST")) + } + +}