Mark more unit tests as 'integration' tests when they rely on external services/docker

This commit is contained in:
Cameron Sparr 2015-08-04 14:52:44 -06:00
parent aa0adc98f9
commit 27132b48db
7 changed files with 75 additions and 25 deletions

View File

@ -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: prepare:
go get -d -v -t ./... 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 test-short: prepare
go test -short ./... go test -v -short ./...
test: prepare test-cleanup:
go test ./... docker-compose kill
update: update:
go get -u -v -d -t ./... go get -u -v -d -t ./...

View File

@ -181,29 +181,29 @@ func init() {
## Testing ## 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 short tests:
execute `make short-test` execute `make short-test`
### Execute long tests: ### Execute long tests:
These tests requre additional docker containers, such as for kafka
Mac: As Telegraf collects metrics from several third-party services it becomes a
execute ``ADVERTISED_HOST=`boot2docker ip` make test`` difficult task to mock each service as some of them have complicated protocols
which would take some time to replicate.
Linux: To overcome this situation we've decided to use docker containers to provide a
execute `ADVERTISED_HOST=localhost make test` 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: ### Unit test troubleshooting:

View File

@ -19,5 +19,6 @@ test:
- golint . - golint .
- golint testutil/... - golint testutil/...
- golint cmd/... - golint cmd/...
# TODO run unit tests # Run short unit tests
# - go test -short ./... - go test -v -short ./...
# TODO run full unit test suite

View File

@ -9,6 +9,10 @@ import (
) )
func TestMemcachedGeneratesMetrics(t *testing.T) { func TestMemcachedGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
m := &Memcached{ m := &Memcached{
Servers: []string{testutil.GetLocalHost()}, Servers: []string{testutil.GetLocalHost()},
} }

View File

@ -11,6 +11,10 @@ import (
) )
func TestMysqlGeneratesMetrics(t *testing.T) { func TestMysqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
m := &Mysql{ m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())}, Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
} }
@ -54,6 +58,10 @@ func TestMysqlGeneratesMetrics(t *testing.T) {
} }
func TestMysqlDefaultsToLocal(t *testing.T) { func TestMysqlDefaultsToLocal(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
m := &Mysql{ m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())}, Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
} }

View File

@ -10,10 +10,15 @@ import (
) )
func TestPostgresqlGeneratesMetrics(t *testing.T) { func TestPostgresqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ 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"}, Databases: []string{"postgres"},
}, },
}, },
@ -55,10 +60,15 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
} }
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) { func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ 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"}, Databases: []string{"postgres"},
}, },
}, },
@ -76,10 +86,15 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
} }
func TestPostgresqlDefaultsToAllDatabases(t *testing.T) { func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{ p := &Postgresql{
Servers: []*Server{ Servers: []*Server{
{ {
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()), Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
}, },
}, },
} }

View File

@ -12,6 +12,10 @@ import (
) )
func TestRedisGeneratesMetrics(t *testing.T) { func TestRedisGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
l, err := net.Listen("tcp", ":0") l, err := net.Listen("tcp", ":0")
require.NoError(t, err) require.NoError(t, err)
@ -103,6 +107,10 @@ func TestRedisGeneratesMetrics(t *testing.T) {
} }
func TestRedisCanPullStatsFromMultipleServers(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") l, err := net.Listen("tcp", ":0")
require.NoError(t, err) require.NoError(t, err)