Skip to content

Commit 06196db

Browse files
committed
tests: integrate ansible playbooks with test suite
1 parent d43b885 commit 06196db

File tree

13 files changed

+297
-221
lines changed

13 files changed

+297
-221
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ jobs:
2626
with:
2727
go-version: ${{ matrix.go-version }}
2828

29-
- name: Set env vars
29+
- name: Configure environment
3030
run: |
3131
echo "GOARCH=amd64" >> $GITHUB_ENV
3232
echo "DB_HOST=127.0.0.1" >> $GITHUB_ENV
3333
echo "UPPER_DB_LOG=ERROR" >> $GITHUB_ENV
3434
35-
- name: Run target
35+
- name: Run tests
3636
run: make test

Makefile

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,10 @@
1-
SHELL ?= bash
2-
CPU_CORES ?= $(shell nproc)
3-
4-
PARALLEL_FLAGS ?= --halt-on-error 2 --jobs=$(CPU_CORES) -v -u
5-
6-
TEST_FLAGS ?= -v -failfast -race -timeout 20m
7-
81
UPPER_DB_LOG ?= WARN
92

10-
export TEST_FLAGS
11-
export PARALLEL_FLAGS
123
export UPPER_DB_LOG
134

14-
test: go-test-internal test-adapters
15-
16-
benchmark: go-benchmark-internal
17-
18-
go-benchmark-%:
5+
bench:
196
go test -v -benchtime=500ms -bench=. ./$*/...
207

21-
go-test-%:
22-
go test -v ./$*/...
23-
24-
test-adapters: \
25-
test-adapter-postgresql \
26-
test-adapter-cockroachdb \
27-
test-adapter-mysql \
28-
test-adapter-mssql \
29-
test-adapter-sqlite \
30-
test-adapter-ql \
31-
test-adapter-mongo
32-
33-
test-adapter-%:
34-
($(MAKE) -C adapter/$* test-extended || exit 1)
35-
36-
test-generic:
37-
export TEST_FLAGS="-run TestGeneric"; \
38-
$(MAKE) test-adapters
8+
test:
9+
$(MAKE) -C tests
3910

40-
goimports:
41-
for FILE in $$(find -name "*.go" | grep -v vendor); do \
42-
goimports -w $$FILE; \
43-
done

tests/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
TEST_FLAGS ?=
22

3+
all: test-all
34

45
test:
56
go test \
67
-failfast \
78
-timeout 30m \
89
-race \
9-
$(TEST_FLAGS) \
10-
-v
10+
-v \
11+
$(TEST_FLAGS)
1112

1213
test-all:
1314
TEST_FLAGS="-run ." $(MAKE) test

tests/adapter_config_test.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package db_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"math/rand"
7+
"net"
8+
"os"
9+
"strconv"
10+
"time"
11+
12+
"github.com/stretchr/testify/suite"
13+
"github.com/upper/db/v4"
14+
15+
"github.com/upper/db/v4/adapter/cockroachdb"
16+
"github.com/upper/db/v4/adapter/mongo"
17+
"github.com/upper/db/v4/adapter/mssql"
18+
"github.com/upper/db/v4/adapter/mysql"
19+
"github.com/upper/db/v4/adapter/postgresql"
20+
21+
cockroachdbtest "github.com/upper/db/v4/tests/cockroachdb"
22+
mongotest "github.com/upper/db/v4/tests/mongo"
23+
mssqltest "github.com/upper/db/v4/tests/mssql"
24+
mysqltest "github.com/upper/db/v4/tests/mysql"
25+
postgresqltest "github.com/upper/db/v4/tests/postgresql"
26+
)
27+
28+
const (
29+
defaultUsername = "upper_db_user"
30+
defaultPassword = "upp3r//S3cr37"
31+
defaultDatabase = "upper_db"
32+
defaultHost = "127.0.0.1"
33+
defaultTimeZone = "Canada/Eastern"
34+
)
35+
36+
const (
37+
minPort = 40000
38+
maxPort = 60000
39+
)
40+
41+
const TimeZone = defaultTimeZone
42+
43+
var defaultTimeLocation, _ = time.LoadLocation(TimeZone)
44+
45+
type Helper interface {
46+
Session() db.Session
47+
48+
Adapter() string
49+
50+
SetUp() error
51+
TearDown() error
52+
}
53+
54+
type Suite struct {
55+
suite.Suite
56+
57+
Helper
58+
}
59+
60+
func (s *Suite) AfterTest(suiteName, testName string) {
61+
err := s.TearDown()
62+
s.Require().NoError(err)
63+
}
64+
65+
func (s *Suite) BeforeTest(suiteName, testName string) {
66+
err := s.SetUp()
67+
s.Require().NoError(err)
68+
}
69+
70+
func init() {
71+
defaultMap := map[string]string{
72+
"DB_USERNAME": defaultUsername,
73+
"DB_PASSWORD": defaultPassword,
74+
"DB_NAME": defaultDatabase,
75+
"DB_HOST": defaultHost,
76+
"DB_TIMEZONE": defaultTimeZone,
77+
}
78+
79+
for k, v := range defaultMap {
80+
if os.Getenv(k) == "" {
81+
os.Setenv(k, v)
82+
}
83+
}
84+
}
85+
86+
func getRandomPort() int {
87+
for i := 0; i < 5; i++ {
88+
port := minPort + rand.Intn(maxPort-minPort)
89+
90+
li, err := net.Listen("tcp", defaultHost+":"+strconv.Itoa(port))
91+
if err == nil {
92+
li.Close()
93+
return port
94+
}
95+
}
96+
97+
log.Fatalf("could not find a free port")
98+
99+
return 0
100+
}
101+
102+
func newPostgreSQLTestHelper() func() (Helper, string, int) {
103+
return func() (Helper, string, int) {
104+
host, port := os.Getenv("DB_HOST"), getRandomPort()
105+
106+
connURL := postgresql.ConnectionURL{
107+
Database: os.Getenv("DB_NAME"),
108+
User: os.Getenv("DB_USERNAME"),
109+
Password: os.Getenv("DB_PASSWORD"),
110+
Host: host + ":" + fmt.Sprintf("%d", port),
111+
Options: map[string]string{
112+
"timezone": os.Getenv("DB_TIMEZONE"),
113+
},
114+
}
115+
116+
helper := postgresqltest.NewHelper(connURL)
117+
return helper, host, port
118+
}
119+
}
120+
121+
func newMySQLTestHelper() func() (Helper, string, int) {
122+
return func() (Helper, string, int) {
123+
host, port := os.Getenv("DB_HOST"), getRandomPort()
124+
125+
connURL := mysql.ConnectionURL{
126+
Database: os.Getenv("DB_NAME"),
127+
User: os.Getenv("DB_USERNAME"),
128+
Password: os.Getenv("DB_PASSWORD"),
129+
Host: host + ":" + fmt.Sprintf("%d", port),
130+
Options: map[string]string{
131+
"parseTime": "true",
132+
"time_zone": fmt.Sprintf(`'%s'`, TimeZone),
133+
"loc": TimeZone,
134+
},
135+
}
136+
137+
helper := mysqltest.NewHelper(connURL)
138+
return helper, host, port
139+
}
140+
}
141+
142+
func newCockroachDBTestHelper() func() (Helper, string, int) {
143+
return func() (Helper, string, int) {
144+
host, port := os.Getenv("DB_HOST"), getRandomPort()
145+
146+
connURL := cockroachdb.ConnectionURL{
147+
Database: os.Getenv("DB_NAME"),
148+
User: os.Getenv("DB_USERNAME"),
149+
Password: os.Getenv("DB_PASSWORD"),
150+
Host: host + ":" + fmt.Sprintf("%d", port),
151+
Options: map[string]string{
152+
"timezone": os.Getenv("DB_TIMEZONE"),
153+
},
154+
}
155+
156+
helper := cockroachdbtest.NewHelper(connURL)
157+
return helper, host, port
158+
}
159+
}
160+
161+
func newMSSQLTestHelper() func() (Helper, string, int) {
162+
return func() (Helper, string, int) {
163+
host, port := os.Getenv("DB_HOST"), getRandomPort()
164+
165+
connURL := mssql.ConnectionURL{
166+
Database: os.Getenv("DB_NAME"),
167+
User: os.Getenv("DB_USERNAME"),
168+
Password: os.Getenv("DB_PASSWORD"),
169+
Host: host + ":" + fmt.Sprintf("%d", port),
170+
Options: map[string]string{},
171+
}
172+
173+
helper := mssqltest.NewHelper(connURL)
174+
return helper, host, port
175+
}
176+
}
177+
178+
func newMongoDBTestHelper() func() (Helper, string, int) {
179+
return func() (Helper, string, int) {
180+
host, port := os.Getenv("DB_HOST"), getRandomPort()
181+
182+
connURL := mongo.ConnectionURL{
183+
Database: os.Getenv("DB_NAME"),
184+
User: os.Getenv("DB_USERNAME"),
185+
Password: os.Getenv("DB_PASSWORD"),
186+
Host: host + ":" + fmt.Sprintf("%d", port),
187+
Options: map[string]string{},
188+
}
189+
190+
helper := mongotest.NewHelper(connURL)
191+
return helper, host, port
192+
}
193+
}

tests/ansible/ansible.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[defaults]
22
inventory = ./inventory.yml
3+
strategy = free
4+
gather_facts = False

tests/ansible/inventory.yml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,34 @@ all:
77
db_password: upp3r//S3cr37
88
db_name: upper_db
99
container_bind_ip: "127.0.0.1"
10+
container_bind_port: "{{ lookup('env', 'CONTAINER_BIND_PORT') }}"
1011
container_name_prefix: "upper_db_"
1112
health_check_retries: 10
1213
health_check_delay: 3
13-
postgresql_base_port: 5432
14-
mysql_base_port: 3306
15-
cockroachdb_base_port: 26257
16-
mssql_base_port: 1433
17-
mongodb_base_port: 27017
1814
hosts:
1915
postgresql-17:
2016
postgresql_version: "17"
21-
container_bind_port: "{{ postgresql_base_port }}"
2217
postgresql-16:
2318
postgresql_version: "16"
24-
container_bind_port: "{{ postgresql_base_port + 1 }}"
2519
postgresql-15:
2620
postgresql_version: "15"
27-
container_bind_port: "{{ postgresql_base_port + 2 }}"
21+
mysql-latest:
22+
mysql_version: "latest"
2823
mysql-lts:
2924
mysql_version: "lts"
30-
container_bind_port: "{{ mysql_base_port }}"
3125
mysql-5:
3226
mysql_version: "5"
33-
container_bind_port: "{{ mysql_base_port + 1 }}"
3427
cockroachdb-v23:
3528
cockroachdb_version: "v23.2.6"
36-
container_bind_port: "{{ cockroachdb_base_port }}"
3729
cockroachdb-v22:
3830
cockroachdb_version: "v22.2.19"
39-
container_bind_port: "{{ cockroachdb_base_port + 1 }}"
4031
cockroachdb-v21:
4132
cockroachdb_version: "v21.2.17"
42-
container_bind_port: "{{ cockroachdb_base_port + 2 }}"
4333
mssql-2022:
4434
mssql_version: "2022-latest"
45-
container_bind_port: "{{ mssql_base_port }}"
4635
mssql-2019:
4736
mssql_version: "2019-latest"
48-
container_bind_port: "{{ mssql_base_port + 1 }}"
4937
mongodb-8:
5038
mongodb_version: "8"
51-
container_bind_port: "{{ mongodb_base_port }}"
5239
mongodb-7:
5340
mongodb_version: "7"
54-
container_bind_port: "{{ mongodb_base_port + 1 }}"

tests/ansible/playbook.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
- hosts: "all"
2-
strategy: free
3-
become: true
41
- hosts: "postgresql-*"
52
roles:
63
- role: postgresql

tests/ansible/roles/cockroachdb/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set_fact:
33
container_name: "{{ container_name_prefix }}cockroachdb_{{ cockroachdb_version }}"
44
container_image: "{{ docker_public_registry }}cockroachdb/cockroach:{{ cockroachdb_version }}"
5+
container_bind_port: "{{ 26257 if container_bind_port == '' else container_bind_port }}"
56
tags:
67
- up
78
- down

tests/ansible/roles/mongodb/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set_fact:
33
container_name: "{{ container_name_prefix }}mongo_{{ mongodb_version }}"
44
container_image: "{{ docker_public_registry }}mongo:{{ mongodb_version }}"
5+
container_bind_port: "{{ 27017 if container_bind_port == '' else container_bind_port }}"
56
tags:
67
- up
78
- down

tests/ansible/roles/mssql/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set_fact:
33
container_name: "{{ container_name_prefix }}mssql_{{ mssql_version }}"
44
container_image: "mcr.microsoft.com/mssql/server:{{ mssql_version }}"
5+
container_bind_port: "{{ 1433 if container_bind_port == '' else container_bind_port }}"
56
tags:
67
- up
78
- down

0 commit comments

Comments
 (0)