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

Redis7 conflict resolution #20954

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 109 additions & 40 deletions tests/console/redis.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,134 @@ use serial_terminal 'select_serial_terminal';
use utils qw(zypper_call script_retry validate_script_output_retry);
use registration qw(add_suseconnect_product get_addon_fullname);

sub run {
my $self = shift;
select_serial_terminal;
my @redis_versions = ("redis", "redis7");
my %ROLES = (
MASTER => 'MASTER',
REPLICA => 'REPLICA',
);
my $base_port = 6379;
my %PORTS = map { $ROLES{$_} => $base_port++ } keys %ROLES;
my %REDIS_CLI_CMD = map {
$_ => "redis-cli -p " . $PORTS{$ROLES{$_}}
} keys %ROLES;
my %logfile_locations = map {
my $version = $_;
$version => {
map {
$ROLES{$_} => "/var/log/redis/redis-server_" . $version . "_${\lc($ROLES{$_})}" . ".log"
} keys %ROLES
}
} @redis_versions;
my $killall_redis_server_cmd = "killall redis-server";
my $remove_test_db_file_cmd = "rm -f movies.redis";

# install redis package
zypper_call 'in redis';
assert_script_run('redis-server --version');

# start redis server on port 6379 and test that it works
assert_script_run('redis-server --daemonize yes --logfile /var/log/redis/redis-server_6379.log');
script_retry('redis-cli ping', delay => 5, retry => 12);
validate_script_output_retry('redis-cli ping', sub { m/PONG/ }, delay => 5, retry => 12);
sub test_ping {
my (%args) = @_;
$args{target} //= $ROLES{MASTER};
record_info("Test ping to target: " . $args{target});
die("Invalid target: " . $args{target}) unless exists $REDIS_CLI_CMD{$args{target}};
my $cmd = $REDIS_CLI_CMD{$args{target}};
script_retry("$cmd ping", delay => 5, retry => 12);
validate_script_output_retry("$cmd ping", sub { m/PONG/ }, delay => 5, retry => 12);
}

# 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/bar/ });
sub test_crud {
record_info("Test Create Read Update Delete");
validate_script_output($REDIS_CLI_CMD{$ROLES{MASTER}} . " set foo bar", sub { m/OK/ });
validate_script_output($REDIS_CLI_CMD{$ROLES{MASTER}} . " get foo", sub { m/bar/ });
validate_script_output($REDIS_CLI_CMD{$ROLES{MASTER}} . " pfselftest", sub { m/OK/ });
validate_script_output($REDIS_CLI_CMD{$ROLES{MASTER}} . " flushdb", sub { m/OK/ });
validate_script_output($REDIS_CLI_CMD{$ROLES{MASTER}} . " get foo", sub { !m/bar/ });
}

sub load_test_db_and_validate {
record_info("Load test database and validate data");
assert_script_run 'curl -O ' . data_url('console/movies.redis');
assert_script_run('redis-cli -h localhost -p 6379 < ./movies.redis');
assert_script_run($REDIS_CLI_CMD{$ROLES{MASTER}} . " < ./movies.redis");
validate_script_output($REDIS_CLI_CMD{$ROLES{MASTER}} . " HMGET \"movie:343\" title", sub { m/Spider-Man/ });
}

sub verify_replication_status {
record_info("Verify replication status");
validate_script_output_retry($REDIS_CLI_CMD{$ROLES{MASTER}} . " info replication", sub { m/connected_slaves:1/ }, delay => 5, retry => 12);
validate_script_output($REDIS_CLI_CMD{$ROLES{REPLICA}} . " info replication", sub { m/role:slave/ });
validate_script_output_retry($REDIS_CLI_CMD{$ROLES{REPLICA}} . " info replication", sub { m/master_link_status:up/ }, delay => 5, retry => 12);
}

sub configure_and_test_master {
record_info("Configure and test master");
test_ping(target => $ROLES{MASTER});
test_crud();
load_test_db_and_validate();
}

validate_script_output('redis-cli HMGET "movie:343" title', sub { m/Spider-Man/ });
sub configure_and_test_replica {
record_info("Configure and test replica");
test_ping(target => $ROLES{REPLICA});
assert_script_run($REDIS_CLI_CMD{$ROLES{REPLICA}} . " replicaof localhost " . $PORTS{$ROLES{MASTER}});
verify_replication_status();
validate_script_output($REDIS_CLI_CMD{$ROLES{REPLICA}} . " HMGET \"movie:343\" title", sub { m/Spider-Man/ });
}

# start redis server on port 6380 and test that it works
assert_script_run('redis-server --daemonize yes --port 6380 --logfile /var/log/redis/redis-server_6380.log');
validate_script_output_retry('redis-cli -p 6380 ping', sub { m/PONG/ }, delay => 5, retry => 12);
sub cleanup_redis {
record_info("Cleanup after testing");
foreach my $role (values %ROLES) {
assert_script_run($REDIS_CLI_CMD{$role} . " flushall");
}
assert_script_run($killall_redis_server_cmd);
assert_script_run($remove_test_db_file_cmd);
assert_script_run("find / -type f -name 'dump.rdb' -print -exec rm -f {} + || true");
}

# make 6380 instance a replica of redis instance running on port 6379
assert_script_run('redis-cli -p 6380 replicaof localhost 6379');
sub upload_redis_logs {
my (%args) = @_;
$args{redis_version} //= $redis_versions[0];
record_info("Upload logs for " . $args{redis_version});
foreach my $role (values %ROLES) {
my $logfile = $logfile_locations{$args{redis_version}}{$role};
upload_logs($logfile) if -e $logfile;
}
}

# test master knows about the slave and vice versa
volodymyrkatkalov marked this conversation as resolved.
Show resolved Hide resolved
validate_script_output_retry('redis-cli info replication', sub { m/connected_slaves:1/ }, delay => 5, retry => 12);
validate_script_output('redis-cli -p 6380 info replication', sub { m/role:slave/ });
sub test_redis {
my (%args) = @_;
$args{redis_version} //= $redis_versions[0];
zypper_call('in --force-resolution --solver-focus Update ' . $args{redis_version});
record_info("Testing " . $args{redis_version});
foreach my $role (values %ROLES) {
my $port = $PORTS{$role};
my $logfile = $logfile_locations{$args{redis_version}}{$role};
my $redis_server_cmd = "redis-server --daemonize yes --port $port --logfile $logfile";
assert_script_run($redis_server_cmd);
}
configure_and_test_master();
configure_and_test_replica();
cleanup_redis();
upload_redis_logs(redis_version => $args{redis_version});
}

# test that the synchronization finished and the data are reachable from slave
validate_script_output_retry('redis-cli info replication', sub { m/state=online/ }, delay => 5, retry => 12);
validate_script_output('redis-cli -p 6380 HMGET "movie:343" title', sub { m/Spider-Man/ });
sub run {
my $self = shift;
select_serial_terminal;
foreach my $redis_version (@redis_versions) {
test_redis(redis_version => $redis_version);
}
}

sub post_fail_hook {
my $self = shift;
$self->cleanup();
script_run($killall_redis_server_cmd);
script_run($remove_test_db_file_cmd);
foreach my $redis_version (@redis_versions) {
upload_redis_logs(redis_version => $redis_version);
}
$self->SUPER::post_fail_hook;
}

sub post_run_hook {
my $self = shift;
$self->cleanup();
zypper_call('rm -u ' . $redis_versions[-1]);
$self->SUPER::post_run_hook;
}

sub cleanup {
upload_logs('/var/log/redis/redis-server_6379.log');
volodymyrkatkalov marked this conversation as resolved.
Show resolved Hide resolved
upload_logs('/var/log/redis/redis-server_6380.log');
assert_script_run('redis-cli -h localhost flushall');
assert_script_run('killall redis-server');
assert_script_run('rm -f movies.redis');
}

1;
Loading