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

Run short unit tests as part of the telegraf circleci process #91

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 18 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
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
go test -short ./...
go test -v -short ./...

test: prepare
go test ./...
test-cleanup:
docker-compose kill

update:
go get -u -v -d -t ./...
Expand Down
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,30 +181,30 @@ 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 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 `make short-test`

### Execute long tests:
These tests requre additional docker containers, such as for kafka

Mac:
execute ``ADVERTISED_HOST=`boot2docker ip` make test``
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.

Linux:
execute `ADVERTISED_HOST=localhost make test`
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:

Try killing your docker containers via `docker-compose kill` and then re-running
Try cleaning up your test environment by executing `make test-cleanup` and
re-running
5 changes: 3 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ test:
- golint .
- golint testutil/...
- golint cmd/...
# TODO run unit tests
# - go test -short ./...
# Run short unit tests
- make test-short
# TODO run full unit test suite
4 changes: 4 additions & 0 deletions plugins/memcached/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
)

func TestMemcachedGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

m := &Memcached{
Servers: []string{testutil.GetLocalHost()},
}
Expand Down
8 changes: 8 additions & 0 deletions plugins/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
)

func TestMysqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
}
Expand Down Expand Up @@ -54,6 +58,10 @@ func TestMysqlGeneratesMetrics(t *testing.T) {
}

func TestMysqlDefaultsToLocal(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

m := &Mysql{
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
}
Expand Down
21 changes: 18 additions & 3 deletions plugins/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import (
)

func TestPostgresqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

p := &Postgresql{
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"},
},
},
Expand Down Expand Up @@ -55,10 +60,15 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
}

func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

p := &Postgresql{
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"},
},
},
Expand All @@ -76,10 +86,15 @@ func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
}

func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

p := &Postgresql{
Servers: []*Server{
{
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable", testutil.GetLocalHost()),
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
testutil.GetLocalHost()),
},
},
}
Expand Down
8 changes: 8 additions & 0 deletions plugins/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
)

func TestRedisGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

l, err := net.Listen("tcp", ":0")
require.NoError(t, err)

Expand Down Expand Up @@ -103,6 +107,10 @@ func TestRedisGeneratesMetrics(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")
require.NoError(t, err)

Expand Down
6 changes: 6 additions & 0 deletions plugins/system/ps/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cpu

import (
"fmt"
"os"
"runtime"
"testing"
"time"
Expand Down Expand Up @@ -94,5 +95,10 @@ func TestCPUPercent(t *testing.T) {
}

func TestCPUPercentPerCpu(t *testing.T) {
// Skip Per-CPU tests when running from a Circle CI container,
// see: https://github.com/golang/go/issues/11609
if os.Getenv("CIRCLE_BUILD_NUM") != "" {
t.Skip("Detected that we are in a circleci container, skipping Per-CPU tests")
}
testCPUPercent(t, true)
}