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

Container services #49

Merged
merged 8 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prepare:
go get -d -v -t ./...
docker-compose up -d --no-recreate

test: prepare
go test -short ./...

update:
go get -u -v -d -t ./...

.PHONY: test
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mysql:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: yes

memcached:
image: memcached
ports:
- "11211:11211"

postgres:
image: postgres
ports:
- "5432:5432"
3 changes: 3 additions & 0 deletions plugins/kafka_consumer/kafka_consumer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion plugins/memcached/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestMemcachedGeneratesMetrics(t *testing.T) {
m := &Memcached{
Servers: []string{"localhost"},
Servers: []string{testutil.GetLocalHost()},
}

var acc testutil.Accumulator
Expand Down
7 changes: 5 additions & 2 deletions plugins/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mysql

import (
"fmt"
"strings"
"testing"

Expand All @@ -11,7 +12,7 @@ import (

func TestMysqlGeneratesMetrics(t *testing.T) {
m := &Mysql{
Servers: []string{""},
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -53,7 +54,9 @@ func TestMysqlGeneratesMetrics(t *testing.T) {
}

func TestMysqlDefaultsToLocal(t *testing.T) {
m := &Mysql{}
m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
}

var acc testutil.Accumulator

Expand Down
7 changes: 4 additions & 3 deletions plugins/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgresql

import (
"fmt"
"testing"

"github.com/influxdb/telegraf/testutil"
Expand All @@ -12,7 +13,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
p := &Postgresql{
Servers: []*Server{
{
Address: "sslmode=disable",
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the changes below it mean that the tests won't run as the current user any longer, which breaks the non-docker test setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanphx AFAIK there's no non-docker test setup in telegraf. The reason why this PR exists is because if you need to run telegraf tests as today you need to install almost all the services (which are not mocked) in your local machine and expect them to be set-up with the proper/default? (is there really a proper?) configuration.

This PR provides a docker based deterministic way to set-up all the services and run tests against them, so changing the default DB test user (BTW, I've used the default in the official postgres docker image) seems trivial as long as the tests run in a deterministic way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, I had to change the user because the postgres docker image uses it to initialize the DB configuration. If I wanted to use some other user (like the default/session one) then I had to make a custom docker image which I didn't want to do.

Databases: []string{"postgres"},
},
},
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
p := &Postgresql{
Servers: []*Server{
{
Address: "sslmode=disable",
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()),
Databases: []string{"postgres"},
},
},
Expand All @@ -78,7 +79,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
p := &Postgresql{
Servers: []*Server{
{
Address: "sslmode=disable",
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()),
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
}

Expand Down
12 changes: 12 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}

}