From fdd2401f7b26c9d5e43daf0a779b17a2b1d59d54 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 4 Aug 2015 08:58:32 -0600 Subject: [PATCH] Adding Kafka docker container and utilizing it in unit tests --- Makefile | 5 ++++- README.md | 22 ++++++++++++++++--- circle.yml | 4 ++++ docker-compose.yml | 11 ++++++++++ .../kafka_consumer_integration_test.go | 15 ++----------- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index cefa9ac44..8987e9763 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,12 @@ prepare: go get -d -v -t ./... docker-compose up -d --no-recreate -test: prepare +test-short: prepare go test -short ./... +test: prepare + go test ./... + update: go get -u -v -d -t ./... diff --git a/README.md b/README.md index 4a607556e..f4f49376c 100644 --- a/README.md +++ b/README.md @@ -185,10 +185,26 @@ As Telegraf collects metrics from several third-party services it becomes a diff 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 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: +To execute Telegraf tests follow these simple steps: - Install docker compose following [these](https://docs.docker.com/compose/install/) instructions -- execute `make test` + - 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`` + +Linux: +execute `ADVERTISED_HOST=localhost make test` + +### Unit test troubleshooting: + +Try killing your docker containers via `docker-compose kill` and then re-running diff --git a/circle.yml b/circle.yml index ccbfae779..83cbd1d4c 100644 --- a/circle.yml +++ b/circle.yml @@ -11,9 +11,13 @@ test: - go vet ./... # Verify that all files are properly go formatted - "[ `git ls-files | grep '.go$' | xargs gofmt -l 2>&1 | wc -l` -eq 0 ]" + # Only docker-compose up kafka, the other services are already running + # see: https://circleci.com/docs/environment#databases + # - docker-compose up -d kafka override: # Enforce that testutil, cmd, and main directory are fully linted - golint . - golint testutil/... - golint cmd/... # TODO run unit tests + # - go test -short ./... diff --git a/docker-compose.yml b/docker-compose.yml index f2c1b2b54..c51a0235b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,3 +14,14 @@ postgres: image: postgres ports: - "5432:5432" + +# advertised host env variable must be set at runtime, ie, +# ADVERTISED_HOST=`boot2docker ip` docker-compose up -d +kafka: + image: spotify/kafka + ports: + - "2181:2181" + - "9092:9092" + environment: + ADVERTISED_HOST: + ADVERTISED_PORT: 9092 diff --git a/plugins/kafka_consumer/kafka_consumer_integration_test.go b/plugins/kafka_consumer/kafka_consumer_integration_test.go index 2a82a2374..325318014 100644 --- a/plugins/kafka_consumer/kafka_consumer_integration_test.go +++ b/plugins/kafka_consumer/kafka_consumer_integration_test.go @@ -2,8 +2,6 @@ package kafka_consumer import ( "fmt" - "os" - "strings" "testing" "time" @@ -19,17 +17,8 @@ func TestReadsMetricsFromKafka(t *testing.T) { } var zkPeers, brokerPeers []string - if len(os.Getenv("ZOOKEEPER_PEERS")) == 0 { - zkPeers = []string{"localhost:2181"} - } else { - zkPeers = strings.Split(os.Getenv("ZOOKEEPER_PEERS"), ",") - } - - if len(os.Getenv("KAFKA_PEERS")) == 0 { - brokerPeers = []string{"localhost:9092"} - } else { - brokerPeers = strings.Split(os.Getenv("KAFKA_PEERS"), ",") - } + zkPeers = []string{testutil.GetLocalHost() + ":2181"} + brokerPeers = []string{testutil.GetLocalHost() + ":9092"} k := &Kafka{ ConsumerGroupName: "telegraf_test_consumers",