Skip to content

Commit

Permalink
Add docker containers to test services.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marcosnils committed Jul 6, 2015
1 parent 1d8e647 commit d545b19
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
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

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: true

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
6 changes: 4 additions & 2 deletions plugins/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestMysqlGeneratesMetrics(t *testing.T) {
m := &Mysql{
Servers: []string{""},
Servers: []string{"root@tcp(localhost:3306)/"},
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions plugins/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
Expand Down Expand Up @@ -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"},
},
},
Expand All @@ -78,7 +78,7 @@ func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
p := &Postgresql{
Servers: []*Server{
{
Address: "sslmode=disable",
Address: "host=localhost user=postgres sslmode=disable",
},
},
}
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

0 comments on commit d545b19

Please sign in to comment.