Skip to content

Commit 9ac2b99

Browse files
committed
Fixed Rabbit checker and added tests
1 parent be5e838 commit 9ac2b99

File tree

8 files changed

+123
-2
lines changed

8 files changed

+123
-2
lines changed

Makefile

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ lint:
66
test:
77
@go test -v -cover ./...
88

9-
.PHONY: all test lint
9+
checks:
10+
@docker-compose up -d
11+
@sleep 3
12+
@echo "Running checks tests against container deps" && \
13+
HEALTH_GO_PG_DSN="postgres://test:test@`docker-compose port postgres 5432`/test?sslmode=disable" \
14+
HEALTH_GO_MQ_DSN="amqp://guest:guest@`docker-compose port rabbit 5672`/" \
15+
HEALTH_GO_RD_DSN="redis://`docker-compose port redis 6379`/" \
16+
HEALTH_GO_MG_DSN="`docker-compose port mongo 27017`/" \
17+
go test -v -cover ./...
18+
19+
.PHONY: all test lint checks

checks/mongo/check_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mongo
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
const mgDSNEnv = "HEALTH_GO_MG_DSN"
9+
10+
func TestNew(t *testing.T) {
11+
if os.Getenv(mgDSNEnv) == "" {
12+
t.SkipNow()
13+
}
14+
15+
check := New(Config{
16+
DSN: os.Getenv(mgDSNEnv),
17+
})
18+
19+
if err := check(); err != nil {
20+
t.Fatalf("MongoDB check failed: %s", err.Error())
21+
}
22+
}

checks/postgres/check_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestNew(t *testing.T) {
1515

1616
check := New(Config{
1717
DSN: os.Getenv(pgDSNEnv),
18-
Table: "client",
18+
Table: "test",
1919
IDColumn: "id",
2020
InsertColumnsFunc: func() map[string]interface{} {
2121
return map[string]interface{}{

checks/rabbitmq/check.go

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Config struct {
3737
// - connection establishing
3838
// - getting channel from the connection
3939
// - declaring topic exchange
40+
// - declaring queue
4041
// - binding a queue to the exchange with the defined routing key
4142
// - publishing a message to the exchange with the defined routing key
4243
// - consuming published message
@@ -85,6 +86,11 @@ func New(config Config) func() error {
8586
return err
8687
}
8788

89+
if _, err := ch.QueueDeclare(config.Queue, false, false, false, false, nil); err != nil {
90+
config.LogFunc(err, "RabbitMQ health check failed during declaring queue")
91+
return err
92+
}
93+
8894
if err := ch.QueueBind(config.Queue, config.RoutingKey, config.Exchange, false, nil); err != nil {
8995
config.LogFunc(err, "RabbitMQ health check failed during binding")
9096
return err

checks/rabbitmq/check_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package rabbitmq
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
const mqDSNEnv = "HEALTH_GO_MQ_DSN"
9+
10+
func TestNew(t *testing.T) {
11+
if os.Getenv(mqDSNEnv) == "" {
12+
t.SkipNow()
13+
}
14+
15+
check := New(Config{
16+
DSN: os.Getenv(mqDSNEnv),
17+
})
18+
19+
if err := check(); err != nil {
20+
t.Fatalf("RabbitMQ check failed: %s", err.Error())
21+
}
22+
}

checks/redis/check_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package redis
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
const rdDSNEnv = "HEALTH_GO_RD_DSN"
9+
10+
func TestNew(t *testing.T) {
11+
if os.Getenv(rdDSNEnv) == "" {
12+
t.SkipNow()
13+
}
14+
15+
check := New(Config{
16+
DSN: os.Getenv(rdDSNEnv),
17+
})
18+
19+
if err := check(); err != nil {
20+
t.Fatalf("Redis check failed: %s", err.Error())
21+
}
22+
}

docker-compose.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
services:
3+
4+
postgres:
5+
image: postgres:9.5-alpine
6+
ports:
7+
- "5432"
8+
volumes:
9+
- ./docker/postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
10+
environment:
11+
POSTGRES_USER: test
12+
POSTGRES_PASSWORD: test
13+
POSTGRES_DB: test
14+
15+
rabbit:
16+
image: rabbitmq:3.6-management-alpine
17+
ports:
18+
- "5672"
19+
20+
redis:
21+
image: redis:3.2-alpine
22+
ports:
23+
- "6379"
24+
25+
mongo:
26+
image: mongo:3
27+
ports:
28+
- "27017"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
5+
CREATE TABLE IF NOT EXISTS test (
6+
id TEXT NOT NULL PRIMARY KEY,
7+
secret TEXT NOT NULL,
8+
extra TEXT NOT NULL,
9+
redirect_uri TEXT NOT NULL
10+
);
11+
EOSQL

0 commit comments

Comments
 (0)