diff --git a/Makefile b/Makefile index fa1873e..8696ad3 100644 --- a/Makefile +++ b/Makefile @@ -6,4 +6,14 @@ lint: test: @go test -v -cover ./... -.PHONY: all test lint +checks: + @docker-compose up -d + @sleep 3 + @echo "Running checks tests against container deps" && \ + HEALTH_GO_PG_DSN="postgres://test:test@`docker-compose port postgres 5432`/test?sslmode=disable" \ + HEALTH_GO_MQ_DSN="amqp://guest:guest@`docker-compose port rabbit 5672`/" \ + HEALTH_GO_RD_DSN="redis://`docker-compose port redis 6379`/" \ + HEALTH_GO_MG_DSN="`docker-compose port mongo 27017`/" \ + go test -v -cover ./... + +.PHONY: all test lint checks diff --git a/checks/mongo/check_test.go b/checks/mongo/check_test.go new file mode 100644 index 0000000..84a12db --- /dev/null +++ b/checks/mongo/check_test.go @@ -0,0 +1,22 @@ +package mongo + +import ( + "os" + "testing" +) + +const mgDSNEnv = "HEALTH_GO_MG_DSN" + +func TestNew(t *testing.T) { + if os.Getenv(mgDSNEnv) == "" { + t.SkipNow() + } + + check := New(Config{ + DSN: os.Getenv(mgDSNEnv), + }) + + if err := check(); err != nil { + t.Fatalf("MongoDB check failed: %s", err.Error()) + } +} diff --git a/checks/postgres/check_test.go b/checks/postgres/check_test.go index a54f256..659497d 100644 --- a/checks/postgres/check_test.go +++ b/checks/postgres/check_test.go @@ -15,7 +15,7 @@ func TestNew(t *testing.T) { check := New(Config{ DSN: os.Getenv(pgDSNEnv), - Table: "client", + Table: "test", IDColumn: "id", InsertColumnsFunc: func() map[string]interface{} { return map[string]interface{}{ diff --git a/checks/rabbitmq/check.go b/checks/rabbitmq/check.go index 09403f3..99e2ab1 100644 --- a/checks/rabbitmq/check.go +++ b/checks/rabbitmq/check.go @@ -37,6 +37,7 @@ type Config struct { // - connection establishing // - getting channel from the connection // - declaring topic exchange +// - declaring queue // - binding a queue to the exchange with the defined routing key // - publishing a message to the exchange with the defined routing key // - consuming published message @@ -85,6 +86,11 @@ func New(config Config) func() error { return err } + if _, err := ch.QueueDeclare(config.Queue, false, false, false, false, nil); err != nil { + config.LogFunc(err, "RabbitMQ health check failed during declaring queue") + return err + } + if err := ch.QueueBind(config.Queue, config.RoutingKey, config.Exchange, false, nil); err != nil { config.LogFunc(err, "RabbitMQ health check failed during binding") return err diff --git a/checks/rabbitmq/check_test.go b/checks/rabbitmq/check_test.go new file mode 100644 index 0000000..8101942 --- /dev/null +++ b/checks/rabbitmq/check_test.go @@ -0,0 +1,22 @@ +package rabbitmq + +import ( + "os" + "testing" +) + +const mqDSNEnv = "HEALTH_GO_MQ_DSN" + +func TestNew(t *testing.T) { + if os.Getenv(mqDSNEnv) == "" { + t.SkipNow() + } + + check := New(Config{ + DSN: os.Getenv(mqDSNEnv), + }) + + if err := check(); err != nil { + t.Fatalf("RabbitMQ check failed: %s", err.Error()) + } +} diff --git a/checks/redis/check_test.go b/checks/redis/check_test.go new file mode 100644 index 0000000..39d662d --- /dev/null +++ b/checks/redis/check_test.go @@ -0,0 +1,22 @@ +package redis + +import ( + "os" + "testing" +) + +const rdDSNEnv = "HEALTH_GO_RD_DSN" + +func TestNew(t *testing.T) { + if os.Getenv(rdDSNEnv) == "" { + t.SkipNow() + } + + check := New(Config{ + DSN: os.Getenv(rdDSNEnv), + }) + + if err := check(); err != nil { + t.Fatalf("Redis check failed: %s", err.Error()) + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d268053 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +version: '3' +services: + + postgres: + image: postgres:9.5-alpine + ports: + - "5432" + volumes: + - ./docker/postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + environment: + POSTGRES_USER: test + POSTGRES_PASSWORD: test + POSTGRES_DB: test + + rabbit: + image: rabbitmq:3.6-management-alpine + ports: + - "5672" + + redis: + image: redis:3.2-alpine + ports: + - "6379" + + mongo: + image: mongo:3 + ports: + - "27017" diff --git a/docker/postgres/docker-entrypoint-initdb.d/init-test-db.sh b/docker/postgres/docker-entrypoint-initdb.d/init-test-db.sh new file mode 100644 index 0000000..1ff8583 --- /dev/null +++ b/docker/postgres/docker-entrypoint-initdb.d/init-test-db.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL +CREATE TABLE IF NOT EXISTS test ( + id TEXT NOT NULL PRIMARY KEY, + secret TEXT NOT NULL, + extra TEXT NOT NULL, + redirect_uri TEXT NOT NULL +); +EOSQL