tests: speed up tests

This commit is contained in:
Sameer Naik
2016-01-13 12:10:55 +05:30
parent 2c8c779776
commit fc211db311

View File

@@ -21,10 +21,8 @@ psql_client() {
# Cleans up all running/stopped containers and host mounted volumes
cleanup_environment() {
container_remove_full slave1
container_remove_full slave0
container_remove_full master
container_remove_full standalone
container_remove_full default
}
# Teardown called at the end of each test
@@ -36,169 +34,169 @@ teardown() {
cleanup_environment
@test "Port 5432 exposed and accepting external connections" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# check if postgresql server is accepting connections
run container_link_and_run_command standalone pg_isready -h $APP_NAME -p 5432 -t 5
run container_link_and_run_command default pg_isready -h $APP_NAME -p 5432 -t 5
[[ "$output" =~ "accepting connections" ]]
}
@test "User postgres created with password" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# auth as POSTGRESQL_ROOT_USER user and list all databases
run psql_client standalone -U $POSTGRESQL_ROOT_USER -Axc "\l"
run psql_client default -U $POSTGRESQL_ROOT_USER -Axc "\l"
[[ "$output" =~ "Name|postgres" ]]
}
@test "User postgres is superuser" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# check if POSTGRESQL_ROOT_USER user is a superuser
run psql_client standalone -U $POSTGRESQL_ROOT_USER -Axc "SHOW is_superuser;"
run psql_client default -U $POSTGRESQL_ROOT_USER -Axc "SHOW is_superuser;"
[[ $output =~ "is_superuser|on" ]]
}
@test "Custom database created" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE
# auth as POSTGRESQL_ROOT_USER user and list all databases
run psql_client standalone -U $POSTGRESQL_ROOT_USER -Axc "\l"
run psql_client default -U $POSTGRESQL_ROOT_USER -Axc "\l"
[[ "$output" =~ "Name|$POSTGRESQL_DATABASE" ]]
}
@test "Can't create a custom user without a password" {
# create container without specifying POSTGRESQL_PASSWORD
run container_create standalone \
run container_create default \
-e POSTGRESQL_USER=$POSTGRESQL_USER
[[ "$output" =~ "you need to provide the POSTGRESQL_PASSWORD" ]]
}
@test "Can't create a custom user without database" {
# create container without specifying POSTGRESQL_DATABASE
run container_create standalone \
run container_create default \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
[[ "$output" =~ "you need to provide the POSTGRESQL_DATABASE" ]]
}
@test "Create custom user and database with password" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# auth as POSTGRESQL_ROOT_USER user and list all databases, should fail
run psql_client standalone -U $POSTGRESQL_ROOT_USER -Axc "\l"
run psql_client default -U $POSTGRESQL_ROOT_USER -Axc "\l"
[[ "$output" =~ "authentication failed for user" ]]
# auth as POSTGRESQL_USER and list all databases
run psql_client standalone -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "\l"
run psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "\l"
[[ "$output" =~ "Name|$POSTGRESQL_DATABASE" ]]
}
@test "Can't create a replication user without a password" {
# create replication user without specifying POSTGRESQL_REPLICATION_PASSWORD
run container_create standalone \
run container_create default \
-e POSTGRESQL_REPLICATION_USER=$POSTGRESQL_REPLICATION_USER
[[ "$output" =~ "you need to provide the POSTGRESQL_REPLICATION_PASSWORD" ]]
}
@test "Can create a replication user with password" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_REPLICATION_USER=$POSTGRESQL_REPLICATION_USER \
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
run psql_client standalone -U $POSTGRESQL_ROOT_USER -Axc "SELECT usename FROM pg_catalog.pg_user WHERE usename = '$POSTGRESQL_REPLICATION_USER' AND userepl = 'true';"
run psql_client default -U $POSTGRESQL_ROOT_USER -Axc "SELECT usename FROM pg_catalog.pg_user WHERE usename = '$POSTGRESQL_REPLICATION_USER' AND userepl = 'true';"
[[ "$output" =~ "usename|$POSTGRESQL_REPLICATION_USER" ]]
}
@test "User and password settings are preserved after restart" {
container_create standalone -d \
container_create default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# restart container
container_restart standalone
container_restart default
# get container logs
run container_logs standalone
run container_logs default
[[ "$output" =~ "The credentials were set on first boot." ]]
# auth as POSTGRESQL_USER and list all databases
run psql_client standalone -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "\l"
run psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "\l"
[[ "$output" =~ "Name|$POSTGRESQL_DATABASE" ]]
}
@test "All the volumes exposed" {
container_create standalone -d
container_create default -d
# get container introspection details and check if volumes are exposed
run container_inspect standalone --format {{.Mounts}}
run container_inspect default --format {{.Mounts}}
[[ "$output" =~ "$VOL_PREFIX/data" ]]
[[ "$output" =~ "$VOL_PREFIX/conf" ]]
[[ "$output" =~ "$VOL_PREFIX/logs" ]]
}
@test "Data gets generated in conf, data and logs if bind mounted in the host" {
container_create_with_host_volumes standalone -d \
container_create_with_host_volumes default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# files expected in conf volume (subset)
run container_exec standalone ls -la $VOL_PREFIX/conf/
run container_exec default ls -la $VOL_PREFIX/conf/
[[ "$output" =~ "postgresql.conf" ]]
[[ "$output" =~ "pg_hba.conf" ]]
# files expected in data volume (subset)
run container_exec standalone ls -la $VOL_PREFIX/data/
run container_exec default ls -la $VOL_PREFIX/data/
[[ "$output" =~ "PG_VERSION" ]]
[[ "$output" =~ "base" ]]
# files expected in logs volume
run container_exec standalone ls -la $VOL_PREFIX/logs/
run container_exec default ls -la $VOL_PREFIX/logs/
[[ "$output" =~ "postgresql.log" ]]
}
@test "If host mounted, password and settings are preserved after deletion" {
container_create_with_host_volumes standalone -d \
container_create_with_host_volumes default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD
# stop and remove container
container_remove standalone
container_remove default
# recreate container without specifying any env parameters
container_create_with_host_volumes standalone -d
container_create_with_host_volumes default -d
# auth as POSTGRESQL_USER and list all databases
run psql_client standalone -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "\l"
run psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "\l"
[[ "$output" =~ "Name|$POSTGRESQL_DATABASE" ]]
}
@test "Configuration changes are preserved after deletion" {
container_create_with_host_volumes standalone -d
container_create_with_host_volumes default -d
# modify postgresql.conf
container_exec standalone sed -i 's|^[#]*[ ]*log_connections[ ]*=.*|log_connections=on|' $VOL_PREFIX/conf/postgresql.conf
container_exec standalone sed -i 's|^[#]*[ ]*log_disconnections[ ]*=.*|log_disconnections=on|' $VOL_PREFIX/conf/postgresql.conf
container_exec default sed -i 's|^[#]*[ ]*log_connections[ ]*=.*|log_connections=on|' $VOL_PREFIX/conf/postgresql.conf
container_exec default sed -i 's|^[#]*[ ]*log_disconnections[ ]*=.*|log_disconnections=on|' $VOL_PREFIX/conf/postgresql.conf
# stop and remove container
container_remove standalone
container_remove default
# relaunch container with host volumes
container_create_with_host_volumes standalone -d
container_create_with_host_volumes default -d
run container_exec standalone cat $VOL_PREFIX/conf/postgresql.conf
run container_exec default cat $VOL_PREFIX/conf/postgresql.conf
[[ "$output" =~ "log_connections=on" ]]
[[ "$output" =~ "log_disconnections=on" ]]
}
@@ -228,7 +226,7 @@ cleanup_environment
}
@test "Master database is replicated on slave" {
container_create master -d \
container_create default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
@@ -237,7 +235,7 @@ cleanup_environment
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
container_create slave0 -d \
$(container_link master $CONTAINER_NAME) \
$(container_link default $CONTAINER_NAME) \
-e POSTGRESQL_MASTER_HOST=$CONTAINER_NAME \
-e POSTGRESQL_MASTER_PORT=5432 \
-e POSTGRESQL_REPLICATION_MODE=slave \
@@ -245,7 +243,7 @@ cleanup_environment
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
# create users table on master and insert a record
psql_client master -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
"CREATE TABLE users (id serial, name varchar(40) NOT NULL); \
INSERT INTO users(name) VALUES ('Marko');"
@@ -255,7 +253,7 @@ cleanup_environment
}
@test "Replication slave can fetch replication parameters from link alias \"master\"" {
container_create master -d \
container_create default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
@@ -265,11 +263,11 @@ cleanup_environment
# create replication slave0 linked to master with alias named master
container_create slave0 -d \
$(container_link master master) \
$(container_link default master) \
-e POSTGRESQL_REPLICATION_MODE=slave
# create users table on master and insert a new row
psql_client master -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
"CREATE TABLE users (id serial, name varchar(40) NOT NULL); \
INSERT INTO users(name) VALUES ('Marko');"
@@ -279,7 +277,7 @@ cleanup_environment
}
@test "Slave synchronizes with the master (delayed start)" {
container_create master -d \
container_create default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
@@ -288,13 +286,13 @@ cleanup_environment
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
# create users table on master and insert a new row
psql_client master -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
"CREATE TABLE users (id serial, name varchar(40) NOT NULL); \
INSERT INTO users(name) VALUES ('Marko');"
# start slave linked to the master
container_create slave0 -d \
$(container_link master $CONTAINER_NAME) \
$(container_link default $CONTAINER_NAME) \
-e POSTGRESQL_MASTER_HOST=$CONTAINER_NAME \
-e POSTGRESQL_MASTER_PORT=5432 \
-e POSTGRESQL_REPLICATION_MODE=slave \
@@ -308,7 +306,7 @@ cleanup_environment
@test "Replication status is preserved after deletion" {
# create master container with host mounted volumes
run container_create_with_host_volumes master -d \
run container_create_with_host_volumes default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
@@ -317,13 +315,13 @@ cleanup_environment
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
# create users table on master and insert a new row
psql_client master -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
"CREATE TABLE users (id serial, name varchar(40) NOT NULL); \
INSERT INTO users(name) VALUES ('Marko');"
# create slave0 container with host mounted volumes, should replicate the master data
container_create_with_host_volumes slave0 -d \
$(container_link master $CONTAINER_NAME) \
$(container_link default $CONTAINER_NAME) \
-e POSTGRESQL_MASTER_HOST=$CONTAINER_NAME \
-e POSTGRESQL_MASTER_PORT=5432 \
-e POSTGRESQL_REPLICATION_MODE=slave \
@@ -332,14 +330,14 @@ cleanup_environment
# stop and remove master and slave0 containers
container_remove slave0
container_remove master
container_remove default
# start master and slave0 containers with existing host volumes and no additional env arguments
container_create_with_host_volumes master -d
container_create_with_host_volumes slave0 -d $(container_link master $CONTAINER_NAME)
container_create_with_host_volumes default -d
container_create_with_host_volumes slave0 -d $(container_link default $CONTAINER_NAME)
# insert new row into the master database
psql_client master -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c "INSERT INTO users(name) VALUES ('Polo');"
psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c "INSERT INTO users(name) VALUES ('Polo');"
# verify that all previous and new data is replicated on slave0
run psql_client slave0 -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "SELECT * FROM users;"
@@ -348,7 +346,7 @@ cleanup_environment
}
@test "Replication slave can be triggered to act as the master" {
container_create master -d \
container_create default -d \
-e POSTGRESQL_USER=$POSTGRESQL_USER \
-e POSTGRESQL_PASSWORD=$POSTGRESQL_PASSWORD \
-e POSTGRESQL_DATABASE=$POSTGRESQL_DATABASE \
@@ -357,7 +355,7 @@ cleanup_environment
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
container_create slave0 -d \
$(container_link master $CONTAINER_NAME) \
$(container_link default $CONTAINER_NAME) \
-e POSTGRESQL_MASTER_HOST=$CONTAINER_NAME \
-e POSTGRESQL_MASTER_PORT=5432 \
-e POSTGRESQL_REPLICATION_MODE=slave \
@@ -365,19 +363,19 @@ cleanup_environment
-e POSTGRESQL_REPLICATION_PASSWORD=$POSTGRESQL_REPLICATION_PASSWORD
# create users table on master and insert a new row
psql_client master -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c \
"CREATE TABLE users (id serial, name varchar(40) NOT NULL); \
INSERT INTO users(name) VALUES ('Marko');"
# stop and remove master
container_remove master
container_remove default
# trigger slave to become master
container_exec slave0 touch /tmp/postgresql.trigger.5432
sleep $SLEEP_TIME
# create slave1 that configures slave0 as the master
container_create slave1 -d \
container_create default -d \
$(container_link slave0 $CONTAINER_NAME) \
-e POSTGRESQL_MASTER_HOST=$CONTAINER_NAME \
-e POSTGRESQL_MASTER_PORT=5432 \
@@ -389,7 +387,7 @@ cleanup_environment
psql_client slave0 -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -c "INSERT INTO users(name) VALUES ('Polo');"
# verify that all past and new data is replicated on slave1
run psql_client slave1 -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "SELECT * FROM users;"
run psql_client default -U $POSTGRESQL_USER $POSTGRESQL_DATABASE -Axc "SELECT * FROM users;"
[[ "$output" =~ "name|Marko" ]]
[[ "$output" =~ "name|Polo" ]]
}