Skip to content

Commit

Permalink
Merge pull request #9 from hellofresh/hotfix/tests
Browse files Browse the repository at this point in the history
Fixed Rabbit checker and added tests
  • Loading branch information
vgarvardt committed Sep 25, 2017
2 parents be5e838 + 9ac2b99 commit d22affb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions checks/mongo/check_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
2 changes: 1 addition & 1 deletion checks/postgres/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
6 changes: 6 additions & 0 deletions checks/rabbitmq/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions checks/rabbitmq/check_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
22 changes: 22 additions & 0 deletions checks/redis/check_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions docker/postgres/docker-entrypoint-initdb.d/init-test-db.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d22affb

Please sign in to comment.