From 656564c1e50b527d0bfcd5745b4b6cdb07893a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Dost=C3=A1l?= Date: Tue, 13 Sep 2022 08:34:33 +0200 Subject: [PATCH] Enhance the redis test to know the status of the replication --- data/console/redis_cli.sh | 80 --------------------------------------- tests/console/redis.pm | 58 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 102 deletions(-) delete mode 100755 data/console/redis_cli.sh diff --git a/data/console/redis_cli.sh b/data/console/redis_cli.sh deleted file mode 100755 index f67e59d795b6..000000000000 --- a/data/console/redis_cli.sh +++ /dev/null @@ -1,80 +0,0 @@ -#!/bin/bash - -function connect() { - port=$1 - a=$(redis-cli -p $port ping) # Connects to the redis server at the address 127.0.0.1 with $port. - if [ "$a" = "PONG" ] - then - echo 'redis-server is running' - else - echo 'Failed: redis-server is not started' - exit 1 - fi -} - -function exec_redis() { - cmd=$1 - expec_res=$2 - res=$($cmd) - if [ "$res" = "$expec_res" ] - then - echo "$cmd: $expec_res" - else - echo 'Failed: redis-cli command '"$cmd"' execution' - exit 1 - fi -} - -## ==== Prepare environment ================================================= ## -set -o pipefail -connect 6379 -# Execute redis-cli commands -exec_redis 'redis-cli set foo bar' 'OK' -exec_redis 'redis-cli get foo' 'bar' -exec_redis 'redis-cli pfselftest' 'OK' -exec_redis 'redis-cli flushdb' 'OK' -out=$(redis-cli -p 6379 get foo) -if [ "$out" = "" ] || [ $out = "(nil)" ] -then - echo "redis-cli -p 6379 get foo: $out" -else - echo 'Failed: redis-cli command 'redis-cli get foo' failed to execute' - exit 1 -fi - -# Load a test db from the data directory -out=$(redis-cli -h localhost -p 6379 < ./movies.redis) -exec_redis 'redis-cli HMGET movie:343 title' 'Spider-Man' - -# Connect to the new instance of redis-server with port 6380 and make 6380 instance a -# replica of redis instance running on port 6379 and verify the result -connect 6380 -exec_redis 'redis-cli -p 6380 replicaof 127.0.0.1 6379' 'OK' -sleep 10 - -out=$(redis-cli info replication) - -if [[ "$out" =~ "connected_slaves:1" ]] -then - echo "redis-cli info replication on master" -else - echo "Failed: redis-cli info replication on master, slave took longer than 10 seconds to show up" - exit 1 -fi -out=$(redis-cli -p 6380 info replication) - -if [[ "$out" =~ "role:slave" ]] -then - echo "Role:slave in redis-cli on replica" -else - echo "Failed: redis-cli info replication on replica" - exit 1 -fi -out=$(redis-cli -p 6380 HMGET "movie:343" title) -if [ "$out" = "Spider-Man" ] -then - echo "redis-cli replicaof done" -else - echo "Failed: redis-cli replication on replica " - exit 1 -fi diff --git a/tests/console/redis.pm b/tests/console/redis.pm index 5ca6fae53eb6..05a82033fa5a 100644 --- a/tests/console/redis.pm +++ b/tests/console/redis.pm @@ -16,8 +16,7 @@ use base 'consoletest'; use strict; use warnings; use testapi; -use utils 'zypper_call'; -use version_utils; +use utils qw(zypper_call script_retry validate_script_output_retry); use registration qw(add_suseconnect_product get_addon_fullname); sub run { @@ -26,22 +25,40 @@ sub run { # install redis package zypper_call 'in redis'; + assert_script_run('redis-server --version'); + + + # start redis server on port 6379 and test that it works + background_script_run('redis-server --daemonize yes'); + script_retry('redis-cli ping'); + validate_script_output_retry('redis-cli ping', sub { m/PONG/ }); + + # test some redis cli commands + validate_script_output('redis-cli set foo bar', sub { m/OK/ }); + validate_script_output('redis-cli get foo', sub { m/bar/ }); + validate_script_output('redis-cli pfselftest', sub { m/OK/ }); + validate_script_output('redis-cli flushdb', sub { m/OK/ }); + validate_script_output('redis-cli get foo', sub { m/(nil)/ }); - # start redis server on port 6379 and 6380 - background_script_run('redis-server'); - wait_still_screen(5); - background_script_run('redis-server --port 6380'); - wait_still_screen(5); assert_script_run 'curl -O ' . data_url('console/movies.redis'); - run_script('redis_cli.sh'); -} + assert_script_run('redis-cli -h localhost -p 6379 < ./movies.redis'); + + validate_script_output('redis-cli HMGET "movie:343" title', sub { m/Spider-Man/ }); + + # start redis server on port 6380 and test that it works + background_script_run('redis-server --daemonize yes --port 6380'); + validate_script_output_retry('redis-cli -p 6380 ping', sub { m/PONG/ }); + + # make 6380 instance a replica of redis instance running on port 6379 + assert_script_run('redis-cli -p 6380 replicaof localhost 6379'); + + # test master knows about the slave and vice versa + validate_script_output_retry('redis-cli info replication', sub { m/connected_slaves:1/ }); + validate_script_output('redis-cli -p 6380 info replication', sub { m/role:slave/ }); -sub run_script { - my $script = shift; - record_info($script, "Running shell script: $script"); - assert_script_run("curl " . data_url("console/$script") . " -o '$script'"); - assert_script_run("chmod a+rx '$script'"); - assert_script_run("./$script"); + # test that the synchronization finished and the data are reachable from slave + validate_script_output_retry('redis-cli info replication', sub { m/state=online/ }); + validate_script_output('redis-cli -p 6380 HMGET "movie:343" title', sub { m/Spider-Man/ }); } sub post_fail_hook { @@ -57,13 +74,10 @@ sub post_run_hook { } sub cleanup { - script_run('redis-cli -h localhost flushall'); - wait_still_screen(5); - script_run('killall redis-server'); - wait_still_screen(5); - script_run('rm -f movies.redis'); - wait_still_screen(5); - script_run('rm -f redis_cli.sh'); + upload_logs('/var/log/redis'); + assert_script_run('redis-cli -h localhost flushall'); + assert_script_run('killall redis-server'); + assert_script_run('rm -f movies.redis'); } 1;