-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathtest.sh
160 lines (134 loc) · 5.11 KB
/
test.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bats
CONTAINER_NAME=bitnami-mongodb-test
IMAGE_NAME=${IMAGE_NAME:-bitnami/mongodb}
SLEEP_TIME=5
MONGODB_ROOT_USER=root
MONGODB_DATABASE=test_database
MONGODB_USER=test_user
MONGODB_PASSWORD=test_password
VOL_PREFIX=/bitnami/mongodb
HOST_VOL_PREFIX=${HOST_VOL_PREFIX:-/tmp/bitnami/$CONTAINER_NAME}
cleanup_running_containers() {
if [ "$(docker ps -a | grep $CONTAINER_NAME)" ]; then
docker rm -fv $CONTAINER_NAME
fi
}
setup() {
cleanup_running_containers
mkdir -p $HOST_VOL_PREFIX
}
teardown() {
cleanup_running_containers
}
cleanup_volumes_content() {
docker run --rm\
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
-v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs\
$IMAGE_NAME rm -rf $VOL_PREFIX/data/ $VOL_PREFIX/logs/ $VOL_PREFIX/conf/
}
create_container(){
docker run --name $CONTAINER_NAME "$@" $IMAGE_NAME
sleep $SLEEP_TIME
}
# $1 is the command
mongo_client(){
docker run --rm --link $CONTAINER_NAME:$CONTAINER_NAME $IMAGE_NAME mongo --host $CONTAINER_NAME "$@"
}
create_full_container(){
docker run -d --name $CONTAINER_NAME\
-e MONGODB_USER=$MONGODB_USER\
-e MONGODB_DATABASE=$MONGODB_DATABASE\
-e MONGODB_PASSWORD=$MONGODB_PASSWORD $IMAGE_NAME
sleep $SLEEP_TIME
}
create_full_container_mounted(){
docker run -d --name $CONTAINER_NAME\
-e MONGODB_USER=$MONGODB_USER\
-e MONGODB_DATABASE=$MONGODB_DATABASE\
-e MONGODB_PASSWORD=$MONGODB_PASSWORD\
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
-v $HOST_VOL_PREFIX/logs:$VOL_PREFIX/logs\
$IMAGE_NAME
sleep $SLEEP_TIME
}
@test "Port 27017 exposed and accepting external connections" {
create_container -d
run mongo_client admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "Root user created with password" {
create_container -d -e MONGODB_PASSWORD=$MONGODB_PASSWORD
# Can not login as root
run mongo_client -u $MONGODB_ROOT_USER admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ "login failed" ]]
run mongo_client -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "Root user has access to admin database" {
create_container -d -e MONGODB_PASSWORD=$MONGODB_PASSWORD
run mongo_client -u $MONGODB_ROOT_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "Can't create root user without password" {
run create_container -e MONGODB_USER=$MONGODB_ROOT_USER
[[ "$output" =~ "you need to provide the MONGODB_PASSWORD" ]]
}
@test "Can't create a custom user without password" {
run create_container -e MONGODB_USER=$MONGODB_USER
[[ "$output" =~ "you need to provide the MONGODB_PASSWORD" ]]
}
@test "Can't create a custom user without database" {
run create_container -e MONGODB_USER=$MONGODB_USER -e MONGODB_PASSWORD=$MONGODB_PASSWORD
[[ "$output" =~ "you need to provide the MONGODB_DATABASE" ]]
}
@test "Create custom user and database with password" {
create_full_container
# Cannot login without password
run mongo_client -u $MONGODB_USER $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ "login failed" ]]
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "Custom user can't access admin database" {
create_full_container
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD admin --eval "printjson(db.adminCommand('listDatabases'))"
[[ "$output" =~ 'login failed' ]]
}
@test "User and password settings are preserved after restart" {
create_full_container
docker stop $CONTAINER_NAME
docker start $CONTAINER_NAME
sleep $SLEEP_TIME
run docker logs $CONTAINER_NAME
[[ "$output" =~ "The credentials were set on first boot." ]]
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
}
@test "If host mounted, password and settings are preserved after deletion" {
cleanup_volumes_content
create_full_container_mounted
docker rm -fv $CONTAINER_NAME
run docker run -d --name $CONTAINER_NAME\
-v $HOST_VOL_PREFIX/data:$VOL_PREFIX/data\
-v $HOST_VOL_PREFIX/conf:$VOL_PREFIX/conf\
$IMAGE_NAME
sleep $SLEEP_TIME
run mongo_client -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE --eval "printjson(db.adminCommand('listCollections'))"
[[ "$output" =~ '"ok" : 1' ]]
cleanup_volumes_content
}
@test "All the volumes exposed" {
create_container -d
run docker inspect $CONTAINER_NAME
[[ "$output" =~ "$VOL_PREFIX/data" ]]
[[ "$output" =~ "$VOL_PREFIX/conf" ]]
[[ "$output" =~ "$VOL_PREFIX/logs" ]]
}
@test "Data gets generated in conf and data if bind mounted in the host" {
create_full_container_mounted
run docker run -v $HOST_VOL_PREFIX:$HOST_VOL_PREFIX --rm $IMAGE_NAME ls -l $HOST_VOL_PREFIX/conf/mongodb.conf $HOST_VOL_PREFIX/logs/mongodb.log
[ $status = 0 ]
cleanup_volumes_content
}