Skip to content

Commit ec95d55

Browse files
authored
Merge pull request #35 from hellofresh/feature/gh-actions-pipeline
PT-6767 Added GitHub Actions pipeline
2 parents 4b57adb + 999830f commit ec95d55

File tree

16 files changed

+367
-121
lines changed

16 files changed

+367
-121
lines changed

.github/workflows/testing.yml

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Testing
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
release:
9+
types:
10+
- created
11+
12+
jobs:
13+
lint:
14+
name: Lint
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Set up Go
18+
uses: actions/setup-go@v2-beta
19+
with:
20+
go-version: ^1.14
21+
- name: Check out code
22+
uses: actions/checkout@v2
23+
- name: Check code quality
24+
run: |
25+
go install -i golang.org/x/lint/golint
26+
make code-quality
27+
28+
test:
29+
name: Test
30+
runs-on: ubuntu-latest
31+
needs: [lint]
32+
33+
services:
34+
postgres:
35+
image: postgres:9.6-alpine
36+
ports:
37+
- "5432"
38+
env:
39+
POSTGRES_USER: test
40+
POSTGRES_PASSWORD: test
41+
POSTGRES_DB: test
42+
options: >-
43+
--health-cmd pg_isready
44+
--health-interval 10s
45+
--health-timeout 5s
46+
--health-retries 5
47+
48+
rabbit:
49+
image: rabbitmq:3.6-management-alpine
50+
ports:
51+
- "5672"
52+
- "15672"
53+
54+
redis:
55+
image: redis:3.2-alpine
56+
ports:
57+
- "6379"
58+
options: >-
59+
--health-cmd "redis-cli ping"
60+
--health-interval 10s
61+
--health-timeout 5s
62+
--health-retries 5
63+
64+
mongo:
65+
image: mongo:3
66+
ports:
67+
- "27017"
68+
69+
mysql:
70+
image: mysql:5.7
71+
ports:
72+
- "3306"
73+
env:
74+
MYSQL_ROOT_PASSWORD: test
75+
MYSQL_DATABASE: test
76+
MYSQL_USER: test
77+
MYSQL_PASSWORD: test
78+
options: >-
79+
--health-cmd "mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD"
80+
--health-interval 10s
81+
--health-timeout 5s
82+
--health-retries 5
83+
84+
http:
85+
image: pierreprinetti/apimock:latest
86+
ports:
87+
- "8080"
88+
env:
89+
HOST: ":8080"
90+
91+
steps:
92+
- name: Set up Go
93+
uses: actions/setup-go@v2-beta
94+
with:
95+
go-version: ^1.14
96+
- name: Check out code
97+
uses: actions/checkout@v2
98+
- name: Run tests
99+
if: success()
100+
run: go test -cover ./... -coverprofile=coverage.txt -covermode=atomic
101+
env:
102+
HEALTH_GO_PG_DSN: postgres://test:test@localhost:${{ job.services.postgres.ports[5432] }}/test?sslmode=disable
103+
HEALTH_GO_MQ_DSN: amqp://guest:guest@localhost:${{ job.services.rabbit.ports[5672] }}/
104+
HEALTH_GO_MQ_URL: http://guest:guest@localhost:${{ job.services.rabbit.ports[15672] }}/
105+
HEALTH_GO_RD_DSN: redis://localhost:${{ job.services.redis.ports[6379] }}/
106+
HEALTH_GO_MG_DSN: mongodb://localhost:${{ job.services.mongo.ports[27017] }}/
107+
HEALTH_GO_MS_DSN: test:test@tcp(localhost:${{ job.services.mysql.ports[3306] }})/test?charset=utf8
108+
HEALTH_GO_HTTP_URL: http://localhost:${{ job.services.http.ports[8080] }}/status
109+
110+
- name: Upload coverage to Codecov
111+
uses: codecov/codecov-action@v1
112+
if: success()
113+
with:
114+
file: ./coverage.txt
115+
fail_ci_if_error: false

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor/
2+
coverage.txt

.travis.yml

-21
This file was deleted.

Makefile

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
OK_COLOR=\033[32;01m
22
NO_COLOR=\033[0m
33

4-
all: deps lint test
5-
6-
lint:
7-
@echo "$(OK_COLOR)==> Linting... $(NO_COLOR)"
8-
@go vet ./...
4+
all: deps test
95

106
deps:
117
@echo "$(OK_COLOR)==> Installing dependencies $(NO_COLOR)"
8+
@go install -i golang.org/x/lint/golint
129
@go mod vendor
1310

14-
test:
11+
code-quality: lint format vet
12+
13+
test: code-quality
1514
@echo "$(OK_COLOR)==> Running tests against container deps $(NO_COLOR)"
1615
@docker-compose up -d
1716
@sleep 3 && \
@@ -22,6 +21,18 @@ test:
2221
HEALTH_GO_MG_DSN="mongodb://`docker-compose port mongo 27017`/" \
2322
HEALTH_GO_MS_DSN="test:test@tcp(`docker-compose port mysql 3306`)/test?charset=utf8" \
2423
HEALTH_GO_HTTP_URL="http://`docker-compose port http 8080`/status" \
25-
go test -cover ./...
24+
go test -cover ./... -coverprofile=coverage.txt -covermode=atomic
25+
26+
lint:
27+
@echo "$(OK_COLOR)==> Checking code style with 'golint' tool$(NO_COLOR)"
28+
@go list ./... | xargs -n 1 golint -set_exit_status
29+
30+
format:
31+
@echo "$(OK_COLOR)==> Checking code formating with 'gofmt' tool$(NO_COLOR)"
32+
@gofmt -l -s checks | grep ".*\.go"; if [ "$$?" = "0" ]; then exit 1; fi
33+
34+
vet:
35+
@echo "$(OK_COLOR)==> Checking code correctness with 'go vet' tool$(NO_COLOR)"
36+
@go vet ./...
2637

27-
.PHONY: all deps test lint
38+
.PHONY: all deps test code-quality lint format vet

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# health-go
2-
[![Build Status](https://travis-ci.com/hellofresh/health-go.svg?branch=master)](https://travis-ci.com/hellofresh/health-go)
32
[![Go Report Card](https://goreportcard.com/badge/github.com/hellofresh/health-go)](https://goreportcard.com/report/github.com/hellofresh/health-go)
43
[![Go Doc](https://godoc.org/github.com/hellofresh/health-go?status.svg)](https://godoc.org/github.com/hellofresh/health-go)
4+
[![Coverage Status](https://codecov.io/gh/hellofresh/health-go/branch/master/graph/badge.svg)](https://codecov.io/gh/hellofresh/health-go)
55

66
* Exposes an HTTP handler that retrieves health status of the application
77
* Implements some generic checkers for the following services:

checks/mongo/check.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@ import (
99
"go.mongodb.org/mongo-driver/mongo/readpref"
1010
)
1111

12+
const (
13+
defaultTimeoutConnect = 5 * time.Second
14+
defaultTimeoutDisconnect = 5 * time.Second
15+
defaultTimeoutPing = 5 * time.Second
16+
)
17+
1218
// Config is the MongoDB checker configuration settings container.
1319
type Config struct {
1420
// DSN is the MongoDB instance connection DSN. Required.
1521
DSN string
1622
// LogFunc is the callback function for errors logging during check.
1723
// If not set logging is skipped.
1824
LogFunc func(err error, details string, extra ...interface{})
25+
26+
// TimeoutConnect defines timeout for establishing mongo connection, if not set - default value is used
27+
TimeoutConnect time.Duration
28+
// TimeoutDisconnect defines timeout for closing connection, if not set - default value is used
29+
TimeoutDisconnect time.Duration
30+
// TimeoutDisconnect defines timeout for making ping request, if not set - default value is used
31+
TimeoutPing time.Duration
1932
}
2033

2134
// New creates new MongoDB health check that verifies the following:
@@ -26,6 +39,18 @@ func New(config Config) func() error {
2639
config.LogFunc = func(err error, details string, extra ...interface{}) {}
2740
}
2841

42+
if config.TimeoutConnect == 0 {
43+
config.TimeoutConnect = defaultTimeoutConnect
44+
}
45+
46+
if config.TimeoutDisconnect == 0 {
47+
config.TimeoutDisconnect = defaultTimeoutDisconnect
48+
}
49+
50+
if config.TimeoutPing == 0 {
51+
config.TimeoutPing = defaultTimeoutPing
52+
}
53+
2954
return func() error {
3055
var ctx context.Context
3156
var cancel context.CancelFunc
@@ -36,7 +61,7 @@ func New(config Config) func() error {
3661
return err
3762
}
3863

39-
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
64+
ctx, cancel = context.WithTimeout(context.Background(), config.TimeoutConnect)
4065
defer cancel()
4166
err = client.Connect(ctx)
4267

@@ -46,14 +71,14 @@ func New(config Config) func() error {
4671
}
4772

4873
defer func() {
49-
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
74+
ctx, cancel = context.WithTimeout(context.Background(), config.TimeoutDisconnect)
5075
defer cancel()
5176
if err := client.Disconnect(ctx); err != nil {
5277
config.LogFunc(err, "MongoDB health check failed on closing connection")
5378
}
5479
}()
5580

56-
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
81+
ctx, cancel = context.WithTimeout(context.Background(), config.TimeoutPing)
5782
defer cancel()
5883
err = client.Ping(ctx, readpref.Primary())
5984

checks/mysql/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package mysql
33
import (
44
"database/sql"
55

6-
_ "github.com/go-sql-driver/mysql"
6+
_ "github.com/go-sql-driver/mysql" // import mysql driver
77
)
88

99
// Config is the MySQL checker configuration settings container.

checks/mysql/check_test.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@ package mysql
33
import (
44
"database/sql"
55
"os"
6+
"sync"
67
"testing"
78
"time"
89

10+
_ "github.com/go-sql-driver/mysql"
911
"github.com/stretchr/testify/assert"
1012
"github.com/stretchr/testify/require"
1113
)
1214

1315
const mysqlDSNEnv = "HEALTH_GO_MS_DSN"
1416

15-
func getDSN(t *testing.T) string {
16-
if mysqlDSN, ok := os.LookupEnv(mysqlDSNEnv); ok {
17-
return mysqlDSN
18-
}
19-
20-
t.Fatalf("required env variable missing: %s", mysqlDSNEnv)
21-
return ""
22-
}
23-
2417
func TestNew(t *testing.T) {
18+
initDB(t)
19+
2520
check := New(Config{
2621
DSN: getDSN(t),
2722
})
@@ -31,6 +26,8 @@ func TestNew(t *testing.T) {
3126
}
3227

3328
func TestEnsureConnectionIsClosed(t *testing.T) {
29+
initDB(t)
30+
3431
mysqlDSN := getDSN(t)
3532

3633
db, err := sql.Open("mysql", mysqlDSN)
@@ -66,3 +63,38 @@ func TestEnsureConnectionIsClosed(t *testing.T) {
6663

6764
assert.Equal(t, initialConnections, currentConnections)
6865
}
66+
67+
func getDSN(t *testing.T) string {
68+
t.Helper()
69+
70+
mysqlDSN, ok := os.LookupEnv(mysqlDSNEnv)
71+
require.True(t, ok)
72+
73+
return mysqlDSN
74+
}
75+
76+
var dbInit sync.Once
77+
78+
func initDB(t *testing.T) {
79+
t.Helper()
80+
81+
dbInit.Do(func() {
82+
db, err := sql.Open("mysql", getDSN(t))
83+
require.NoError(t, err)
84+
85+
defer func() {
86+
err := db.Close()
87+
assert.NoError(t, err)
88+
}()
89+
90+
_, err = db.Exec(`
91+
CREATE TABLE IF NOT EXISTS test (
92+
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
93+
secret VARCHAR(256) NOT NULL,
94+
extra VARCHAR(256) NOT NULL,
95+
redirect_uri VARCHAR(256) NOT NULL
96+
);
97+
`)
98+
require.NoError(t, err)
99+
})
100+
}

checks/postgres/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package postgres
33
import (
44
"database/sql"
55

6-
_ "github.com/lib/pq"
6+
_ "github.com/lib/pq" // import pg driver
77
)
88

99
// Config is the PostgreSQL checker configuration settings container.

0 commit comments

Comments
 (0)