Skip to content

Commit 7ce7f19

Browse files
Merge pull request #8 from laironacosta/feature/docker-compose-tests
Feature/docker compose tests
2 parents 9c81040 + a345750 commit 7ce7f19

File tree

11 files changed

+111
-82
lines changed

11 files changed

+111
-82
lines changed

docker-compose.test.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: '3'
2+
3+
networks:
4+
kit-go-network:
5+
driver: bridge
6+
7+
services:
8+
kit_tests:
9+
build:
10+
context: .
11+
dockerfile: ./Dockerfile.test
12+
volumes:
13+
- $PWD:/go/src/github.com/laironacosta/kit-go
14+
depends_on:
15+
- db-test
16+
networks:
17+
- kit-go-network
18+
db-test:
19+
image: postgres:13.2
20+
ports:
21+
- "5432:5432"
22+
expose:
23+
- "5432"
24+
environment:
25+
POSTGRES_USER: root
26+
POSTGRES_PASSWORD: root
27+
POSTGRES_DB: testdb
28+
restart: on-failure
29+
networks:
30+
- kit-go-network
31+
redis-test:
32+
image: redis
33+
ports:
34+
- "6379:6379"
35+
expose:
36+
- "6379"
37+
restart: on-failure
38+
networks:
39+
- kit-go-network

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ go 1.16
55
require (
66
github.com/go-pg/pg/v10 v10.9.0
77
github.com/go-redis/redis/v8 v8.8.0
8+
github.com/sirupsen/logrus v1.8.1
89
github.com/stretchr/testify v1.7.0
10+
golang.org/x/sys v0.0.0-20210402192133-700132347e07 // indirect
911
)

go.sum

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7
6363
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6464
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6565
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
66+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
67+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
6668
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
69+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
6770
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
6871
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6972
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
@@ -122,14 +125,16 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
122125
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
123126
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
124127
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
128+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
125129
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
126130
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
127131
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
128132
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
129133
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
130134
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
131-
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
132135
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
136+
golang.org/x/sys v0.0.0-20210402192133-700132347e07 h1:4k6HsQjxj6hVMsI2Vf0yKlzt5lXxZsMW1q0zaq2k8zY=
137+
golang.org/x/sys v0.0.0-20210402192133-700132347e07/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
133138
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
134139
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
135140
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
run-test: test-up test-down
2+
3+
mod:
4+
# This make rule requires Go 1.11+
5+
GO111MODULE=on go mod tidy
6+
7+
# Test Rules
8+
test:
9+
docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
10+
docker-compose -f docker-compose.test.yml down --volumes
11+
12+
test-up:
13+
docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
14+
15+
test-down:
16+
docker-compose -f docker-compose.test.yml down --volumes

pgdb/example1/example1.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
54
"github.com/go-pg/pg/v10"
65
"github.com/go-pg/pg/v10/orm"
6+
log "github.com/sirupsen/logrus"
77
)
88

99
type User struct {
@@ -25,9 +25,9 @@ func main() {
2525

2626
func exampleDBModel() {
2727
db := pg.Connect(&pg.Options{
28-
User: "postgres",
29-
Password: "postgres",
30-
Database: "pg-db-go",
28+
User: "root",
29+
Password: "root",
30+
Database: "db-test",
3131
})
3232
defer db.Close()
3333

@@ -89,9 +89,9 @@ func exampleDBModel() {
8989
panic(err)
9090
}
9191

92-
fmt.Printf("%+v \n", user)
93-
fmt.Printf("%+v \n", users)
94-
fmt.Printf("%+v \n", book)
92+
log.Infof("%+v \n", user)
93+
log.Infof("%+v \n", users)
94+
log.Infof("%+v \n", book)
9595

9696
}
9797

pgdb/example2/example2.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4-
"fmt"
54
"github.com/Lairon/db-go/pgdb"
65
"github.com/go-pg/pg/v10"
76
"github.com/go-pg/pg/v10/orm"
7+
log "github.com/sirupsen/logrus"
88
)
99

1010
type User struct {
@@ -34,9 +34,9 @@ func main() {
3434
func NewPgClient() *PgClient {
3535
return &PgClient{
3636
db: pgdb.NewPgDB(&pg.Options{
37-
User: "postgres",
38-
Password: "postgres",
39-
Database: "pg-db-go",
37+
User: "root",
38+
Password: "root",
39+
Database: "db-test",
4040
}),
4141
}
4242
}
@@ -102,9 +102,9 @@ func (c *PgClient) createSchemeInsertSelect() {
102102
panic(err)
103103
}
104104

105-
fmt.Printf("%+v \n", user)
106-
fmt.Printf("%+v \n", users)
107-
fmt.Printf("%+v \n", book)
105+
log.Infof("%+v \n", user)
106+
log.Infof("%+v \n", users)
107+
log.Infof("%+v \n", book)
108108

109109
}
110110

pgdb/pg_db.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package pgdb
22

33
import (
44
"github.com/go-pg/pg/v10"
5-
"log"
5+
log "github.com/sirupsen/logrus"
66
"sync"
7+
"time"
78
)
89

910
var (
@@ -20,15 +21,20 @@ func NewPgDB(pgOptions *pg.Options) *pg.DB {
2021
}
2122

2223
func Connect(pgOptions *pg.Options) *pg.DB {
24+
log.Infoln("Connecting to postgres database...")
2325
db := pg.Connect(pgOptions)
2426

2527
var n int
26-
_, err := db.QueryOne(pg.Scan(&n), "SELECT 1")
27-
if err != nil {
28-
log.Panicf("Postgres connection error %+v\n", err)
28+
if _, err := db.QueryOne(pg.Scan(&n), "SELECT 1"); err != nil {
29+
log.Errorln(err)
30+
time.Sleep(2 * time.Second)
31+
32+
db = pg.Connect(pgOptions)
33+
if _, err := db.QueryOne(pg.Scan(&n), "SELECT 1"); err != nil {
34+
log.Panicf("Postgres connection error %+v\n", err)
35+
}
2936
}
3037

31-
log.Println("Successfully connected to ->", db.Options().Addr)
32-
38+
log.Infoln("Connection to postgres verified and successfully connected...")
3339
return db
3440
}

pgdb/pg_db_test.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
11
package pgdb
22

33
import (
4-
"fmt"
54
"github.com/go-pg/pg/v10"
65
"github.com/stretchr/testify/assert"
76
"testing"
87
)
98

109
var pgOptions = &pg.Options{
11-
Addr: "localhost:5432",
12-
User: "postgres",
13-
Password: "postgres",
14-
Database: "pg-db-go",
10+
Addr: "db-test:5432",
11+
User: "root",
12+
Password: "root",
13+
Database: "testdb",
1514
}
1615

1716
func TestNewPgDB_WhenOptionsOk_ThenCreateClient(t *testing.T) {
1817
got := NewPgDB(pgOptions)
19-
got.Close()
18+
defer got.Close()
2019

2120
assert.NotNil(t, got)
2221
assert.Equal(t, pgOptions.Addr, got.Options().Addr)
2322
assert.Equal(t, pgOptions.User, got.Options().User)
2423
assert.Equal(t, pgOptions.Password, got.Options().Password)
2524
assert.Equal(t, pgOptions.Database, got.Options().Database)
2625
}
27-
28-
func TestNewPgDB_WhenOptionsNil_ThenPanic(t *testing.T) {
29-
defer func() {
30-
if r := recover(); r != nil {
31-
assert.Equal(t, fmt.Sprint("runtime error: invalid memory address or nil pointer dereference"), fmt.Sprint(r))
32-
}
33-
}()
34-
NewPgDB(nil)
35-
}
36-
37-
func TestNewPgDB_WhenOptionsContainBadAddr_ThenPanic(t *testing.T) {
38-
defer func() {
39-
if r := recover(); r != nil {
40-
assert.Equal(t, fmt.Sprintln("Postgres connection error dial tcp: lookup bad: no such host"), fmt.Sprint(r))
41-
}
42-
}()
43-
NewPgDB(&pg.Options{
44-
Addr: "bad:6379",
45-
User: "postgres",
46-
Password: "postgres",
47-
Database: "pg-db-go",
48-
})
49-
}

redisdb/example1/example1.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package main
22

33
import (
44
"context"
5-
"fmt"
65
"github.com/Lairon/db-go/redisdb"
76
"github.com/go-redis/redis/v8"
8-
"log"
7+
log "github.com/sirupsen/logrus"
98
"time"
109
)
1110

@@ -25,13 +24,13 @@ func main() {
2524
client.SetKey(ctx, key, value, 10*time.Second)
2625
val := client.GetKey(ctx, key)
2726

28-
fmt.Printf("Key: %s, Value: %s", key, val)
27+
log.Infof("Key: %s, Value: %s", key, val)
2928
}
3029

3130
func NewRedisClient() *RedisClient {
3231
return &RedisClient{
3332
db: redisdb.NewRedisDB(&redis.Options{
34-
Addr: "localhost:6379",
33+
Addr: "redis-test:6379",
3534
Password: "", // no password set
3635
DB: 0, // use default DB
3736
}),

redisdb/redis_db.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package redisdb
33
import (
44
"context"
55
"github.com/go-redis/redis/v8"
6-
"log"
6+
log "github.com/sirupsen/logrus"
77
"sync"
8+
"time"
89
)
910

1011
var (
@@ -21,12 +22,20 @@ func NewRedisDB(redisOptions *redis.Options) *redis.Client {
2122
}
2223

2324
func Connect(redisOptions *redis.Options) *redis.Client {
25+
log.Infoln("Connecting to redis database...")
26+
ctx := context.Background()
2427
db := redis.NewClient(redisOptions)
2528

26-
if err := db.Ping(context.Background()).Err(); err != nil {
27-
log.Panicf("Redis connection error %+v\n", err)
29+
if err := db.Ping(ctx).Err(); err != nil {
30+
log.Errorln(err)
31+
time.Sleep(2 * time.Second)
32+
33+
db := redis.NewClient(redisOptions)
34+
if err := db.Ping(ctx).Err(); err != nil {
35+
log.Panicf("Redis connection error %+v\n", err)
36+
}
2837
}
2938

30-
log.Println("Redis successfully connected to ->", db.Options().Addr)
39+
log.Infoln("Redis successfully connected...")
3140
return db
3241
}

0 commit comments

Comments
 (0)