Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redis test #15247

Merged
merged 1 commit into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions data/console/redis_cli.sh
@@ -0,0 +1,80 @@
#!/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
Comment on lines +73 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a follow up ticket to rework this in the future?, i.e: add an extra check to know the status of the replication (basically, know if the replica is in sync with the master)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

58 changes: 14 additions & 44 deletions tests/console/redis.pm
Expand Up @@ -17,15 +17,8 @@ use strict;
use warnings;
use testapi;
use utils 'zypper_call';

sub wait_serial_or_die {
my ($feedback, %args) = @_;
$args{timeout} //= 10;

my $e = wait_serial($feedback, %args);

die('Unexpected serial output') unless (defined $e);
}
use version_utils;
use registration qw(add_suseconnect_product get_addon_fullname);

sub run {
my $self = shift;
Expand All @@ -34,46 +27,21 @@ sub run {
# install redis package
zypper_call 'in redis';

# start redis server
# start redis server on port 6379 and 6380
background_script_run('redis-server');
wait_still_screen(5);

# test if redis cli is working
enter_cmd('redis-cli');
wait_serial_or_die('127.0.0.1:6379');
enter_cmd('quit');

# test some redis cli commands
validate_script_output('redis-cli ping', sub { m/PONG/ });
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)/ });

assert_script_run 'curl -O ' . data_url('console/movies.redis');
assert_script_run('redis-cli -h localhost -p 6379 < ./movies.redis');
wait_still_screen(5);

validate_script_output('redis-cli HMGET "movie:343" title', sub { m/Spider-Man/ });

# start another redis instance with a different port
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');
}

# get redis cli prompt of new instance
enter_cmd('redis-cli -p 6380');
wait_serial_or_die('127.0.0.1:6380');
enter_cmd('quit');

# make 6380 instance a replica of redis instance running on port 6379
assert_script_run('redis-cli -p 6380 replicaof localhost 6379');
wait_still_screen(5);

# test replication
validate_script_output('redis-cli info replication', sub { m/connected_slaves:1/ });
validate_script_output('redis-cli -p 6380 info replication', sub { m/role:slave/ });
validate_script_output('redis-cli -p 6380 HMGET "movie:343" title', sub { m/Spider-Man/ });
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");
}

sub post_fail_hook {
Expand All @@ -94,6 +62,8 @@ sub cleanup {
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');
}

1;