-
Notifications
You must be signed in to change notification settings - Fork 405
/
helpers.sh
98 lines (83 loc) · 2.27 KB
/
helpers.sh
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
#!/bin/bash
set -exo pipefail
function get_host {
case ${DOCKER_LOCATION:-localhost} in
localhost)
echo "localhost"
;;
docker_ip)
if [[ ! -z ${DOCKER_HOST} ]]
then
DOCKER_IP=`echo ${DOCKER_HOST} | awk -F/ '{print $3}'| awk -F: '{print $1}'`
fi
echo ${DOCKER_IP}
;;
container_ip)
echo "$(docker inspect -f '{{.NetworkSettings.IPAddress}}' ${1})"
;;
esac
}
function get_port {
local NAME=$1
local PORT_START=${FN_TEST_PORT_RANGE_START:-33000}
local SERVICE_LIST=(
"fn_basic_tests_mysql"
"fn_basic_tests_postgres"
"fn_api_tests_mysql"
"fn_api_tests_postgres"
"fn_system_tests_mysql"
"fn_system_tests_postgres"
)
local IDX=0
while [ ${IDX} -lt ${#SERVICE_LIST[@]} ]
do
if [ ${SERVICE_LIST[$IDX]} = "${NAME}" ]; then
echo $((${PORT_START}+${IDX}))
return
fi
IDX=$(($IDX+1))
done
echo "Invalid context/component: ${NAME} not in service list"
exit -1
}
function spawn_sqlite3 {
local CONTEXT=$1
touch /tmp/${CONTEXT}_sqllite3.db
echo "sqlite3:///tmp/${CONTEXT}_sqllite3.db"
}
function spawn_mysql {
local CONTEXT=$1
local PORT=$(get_port ${CONTEXT}_mysql)
local HOST=$(get_host ${CONTEXT}_mysql)
local ID=$(docker run --name ${CONTEXT}_mysql \
-p ${PORT}:3306 \
-e MYSQL_DATABASE=funcs \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.22)
echo "mysql://root:root@tcp(${HOST}:${PORT})/funcs"
}
function spawn_postgres {
local CONTEXT=$1
local PORT=$(get_port ${CONTEXT}_postgres)
local HOST=$(get_host ${CONTEXT}_postgres)
local ID=$(docker run --name ${CONTEXT}_postgres \
-e "POSTGRES_DB=funcs" \
-e "POSTGRES_PASSWORD=root" \
-p ${PORT}:5432 \
-d postgres:9.3-alpine)
echo "postgres://postgres:root@${HOST}:${PORT}/funcs?sslmode=disable"
}
function docker_pull_postgres {
docker pull postgres:9.3-alpine
}
function docker_pull_mysql {
docker pull mysql:5.7.22
}
function remove_containers {
local CONTEXT=$1
for i in mysql postgres
do
docker rm -fv ${CONTEXT}_${i} 2>/dev/null || true
done
rm -f /tmp/${CONTEXT}_sqllite3.db
}