-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtestallwithdocker.sh
More file actions
executable file
·104 lines (82 loc) · 2.43 KB
/
testallwithdocker.sh
File metadata and controls
executable file
·104 lines (82 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
COMPOSE_FILE="docker-compose-test.yml"
MARIADB_USER=godb
MARIADB_PASSWORD=godb
export GODB_MYSQL="$MARIADB_USER:$MARIADB_PASSWORD@/godb?parseTime=true"
POSTGRESQL_USER=godb
POSTGRESQL_PASSWORD=godb
export GODB_POSTGRESQL="postgres://$POSTGRESQL_USER:$POSTGRESQL_PASSWORD@localhost/godb?sslmode=disable"
SQLSERVER_USER=sa
SQLSERVER_PASSWORD=NotSoStr0ngP@ssword
export GODB_MSSQL="Server=127.0.0.1;Database=godb;User Id=$SQLSERVER_USER;Password=$SQLSERVER_PASSWORD"
STARTLOOP_SLEEP=2
STARTLOOP_MAXITERATIONS=30
start_containers() {
docker-compose -f "$COMPOSE_FILE" up -d
}
stop_containers() {
docker-compose -f "$COMPOSE_FILE" down
}
stop_containers_and_exit() {
docker-compose -f "$COMPOSE_FILE" down
exit 1
}
wait_db() {
NAME=$1
CMDCHECK=$2
COUNT=0
until ( $CMDCHECK >& /dev/null); do
echo "$NAME is starting..."
sleep $STARTLOOP_SLEEP
COUNT=$((COUNT+1))
if (( $COUNT == $STARTLOOP_MAXITERATIONS )); then
echo "$NAME take too long time to start."
return 1
fi
done
}
setup_mariadb() {
wait_db "MariaDB" "docker exec godb_test_mariadb mysql -u$MARIADB_USER -p$MARIADB_PASSWORD -h127.0.0.1 -e exit" \
|| return 1
}
setup_postgresql() {
wait_db "PostgreSQL" "docker exec godb_test_postgresql psql -U$POSTGRESQL_USER -c \\q" \
|| return 1
}
setup_sqlserver() {
wait_db "SQLServer" "docker exec godb_test_sqlserver /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SQLSERVER_PASSWORD -q exit" \
|| return 1
docker exec -i godb_test_sqlserver /opt/mssql-tools/bin/sqlcmd -S localhost -U $SQLSERVER_USER -P NotSoStr0ngP@ssword <<-EOF
create database godb;
go
alter database godb set READ_COMMITTED_SNAPSHOT ON;
go
exit
EOF
}
# Start all containers
start_containers || stop_containers_and_exit
echo Containers are starting...
# Install dependencies (while containers are starting)
echo Fetch Go dependencies
go mod download
# Wait for and setup each DB
echo Wait until DB are ready
setup_postgresql || stop_containers_and_exit
setup_mariadb || stop_containers_and_exit
setup_sqlserver || stop_containers_and_exit
echo Containers are started, DB are ready.
# Let's test (without cache) !
go clean -testcache
go test -v ./...
testresult=$?
# Cleanup
stop_containers
# Display and return a clear test status
echo ----------
if [ $testresult -eq 0 ]; then
echo OK
else
echo FAIL
fi
exit $testresult