Skip to content

Commit

Permalink
Stop hana test module
Browse files Browse the repository at this point in the history
  • Loading branch information
lpalovsky committed Jan 10, 2025
1 parent 1cfb40c commit 8004ba4
Show file tree
Hide file tree
Showing 15 changed files with 838 additions and 7 deletions.
74 changes: 74 additions & 0 deletions lib/hacluster.pm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ our @EXPORT = qw(
crm_wait_for_maintenance
crm_check_resource_location
generate_lun_list
show_cluster_parameter
set_cluster_parameter
show_hana_resource_name
);

=head1 SYNOPSIS
Expand Down Expand Up @@ -1460,4 +1463,75 @@ sub generate_lun_list {
$index += $num_luns;
}
}


=head2 set_cluster_parameter
set_cluster_parameter();
Manage HA cluster parameter using crm shell.
=over
=item * B<resource>: Resource containing parameter
=item * B<parameter>: Parameter name
=item * B<value>: Target parameter value
=back
=cut

sub set_cluster_parameter {
my (%args) = @_;
for my $arg ('resource', 'parameter', 'value') {
croak("Mandatory argument '$arg' missing.") unless $arg;
}
my $cmd = join(' ', 'crm', 'resource', 'param', $args{resource}, 'set', $args{parameter}, $args{value});
assert_script_run($cmd);
}

=head2 show_cluster_parameter
show_cluster_parameter();
Show cluster parameter value using CRM shell.
=over
=item * B<resource>: Resource containing parameter
=item * B<parameter>: Parameter name
=back
=cut

sub show_cluster_parameter {
my (%args) = @_;
for my $arg ('resource', 'parameter') {
croak("Mandatory argument '$arg' missing.") unless $arg;
}
my $cmd = join(' ', 'crm', 'resource', 'param', $args{resource}, 'show', $args{parameter});
return script_output($cmd);
}

=head2 show_hana_resource_name
show_hana_resource_name();
Show SAP hana resource name.
=cut

sub show_hana_resource_name {
my $cmd = join(' ', 'crm', 'configure', 'show', 'related:ocf:suse:SAPHana',
'| awk \'$1 == "primitive" && $3 == "ocf:suse:SAPHana" {print $2}\'');
my $cmd_output = script_output($cmd);
# additional check if returned HANA resource exists
assert_script_run("crm resource status $cmd_output");
return $cmd_output;
}

1;
45 changes: 45 additions & 0 deletions lib/saputils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ our @EXPORT = qw(
calculate_hana_topology
check_hana_topology
check_crm_output
get_primary_node
get_failover_node
);

=head1 SYNOPSIS
Expand Down Expand Up @@ -176,4 +178,47 @@ sub check_crm_output {
return (($resource_starting != 1) && ($failed_actions != 1) ? 1 : 0);
}
=head2 get_primary_node
get_primary_node();
Returns hostname of current primary node obtained from C<SAPHanaSR-showAttr --format=script> command output
=over
=item B<input> - return value of calculate_hana_topology
=back
=cut
sub get_primary_node {
my (%args) = @_;
croak("Argument <input> missing") unless $args{input};
my $topology = $args{input};
for my $db (keys %$topology) {
return $db if $topology->{$db}{sync_state} eq 'PRIM';
}
}
=head2 get_failover_node
get_failover_node();
Returns hostname of current failover (replica) node obtained from C<SAPHanaSR-showAttr --format=script> command output.
Returns node hostname even if it's in 'SFAIL' state.
=over
=item B<input> - return value of calculate_hana_topology
=back
=cut
sub get_failover_node {
my (%args) = @_;
croak("Argument <input> missing") unless $args{input};
my $topology = $args{input};
for my $db (keys %$topology) {
return $db if grep /$topology->{$db}{sync_state}/, ('SOK', 'SFAIL');
}
}
1;
1 change: 1 addition & 0 deletions lib/sles4sap.pm
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ our @EXPORT = qw(
get_instance_profile_path
load_ase_env
upload_ase_logs
saphostctrl_list_databases
);

=head1 SYNOPSIS
Expand Down
183 changes: 183 additions & 0 deletions lib/sles4sap/database_hana.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# SUSE's openQA tests

Check failure on line 1 in lib/sles4sap/database_hana.pm

View workflow job for this annotation

GitHub Actions / CI: Running static tests with perl v5.32

File lib/sles4sap/database_hana.pm needs tidying
#
# Copyright 2017-2024 SUSE LLC
# SPDX-License-Identifier: FSFAP
#
# Summary: Functions for SAP tests
# Maintainer: QE-SAP <[email protected]>

package sles4sap::database_hana;
use strict;
use warnings;
use testapi;
use Carp qw(croak);
use Exporter qw(import);
use saputils qw(check_crm_output get_primary_node get_failover_node calculate_hana_topology);
use sles4sap::sapcontrol;

our @EXPORT = qw(
hdb_stop
wait_for_failed_resources
wait_for_takeover
register_replica
get_node_roles
);


=head1 SYNOPSIS
Package contains functions for interacting with hana database and related actions.
=cut

=head2 sudo
sudo($activate);
Return string 'sudo ' (space included) if there is any 'true' equivalent passed as an argument, otherwise return empty string.
This is to be used to prepend 'sudo' to any command under a condition.
Example: script_run(sudo($args{as_root}) . 'whoami');
=over
=item * B<$activate>: Any value which is an equivalent to 'true' makes functkion return 'sudo'. Default: false
=back
=cut

sub sudo {
return ('sudo ') if @_;
}

=head2 hdb_stop
hdb_stop(instance_id=>'00', [switch_user=>'sidadm']);
Stop hana database using C<HDB stop> command. Function expects to be executed as sidadm, however you can use B<switch_user>
to execute command using C<sudo su -> as a different user. The user needs to have correct permissions for performing
requested action.
Function waits till all DB processes are stopped.
=over
=item * B<instance_id>: Database instance ID. Mandatory.
=item * B<switch_user>: Execute command as specified user with help of C<sudo su ->. Default: undef
=item * B<command>: HDB command to trigger. Default: stop
=back
=cut

sub hdb_stop {
my (%args) = @_;
my $stop_timeout = 600;
$args{command} //= 'stop';
croak("Command '$args{command}' is not supported.") unless grep(/$args{command}/, ('kill', 'stop'));

$args{command} = 'kill -x' if( $args{command} eq 'kill');
my $sudo_su = $args{switch_user} ? "sudo su - $args{switch_user} -c" : '';
my $cmd = join(' ', $sudo_su, '"', 'HDB', $args{command}, '"');
record_info('HDB stop', "Executing '$cmd' on " . script_output('hostname'));
assert_script_run($cmd, timeout => $stop_timeout);
sapcontrol_process_check(instance_id => $args{instance_id}, expected_state => 'stopped', wait_for_state => 'yes', timeout => $stop_timeout);
record_info('DB stopped');
}

=head2 wait_for_failed_resources
wait_for_failed_resources();
Wait until 'crm_mon' starts showing failed resources. This can be used as first indicator of a started failover.
=cut

sub wait_for_failed_resources {
my $timeout = 300;
my $start_time = time;
while (check_crm_output(input => script_output('crm_mon -R -r -n -N -1', quiet => 1))) {
sleep 30;
die("Cluster did not register any failed resource within $timeout sec") if (time - $timeout > $start_time);
}
record_info('CRM info', "Cluster registered failed resources\n" . script_output('crm_mon -R -r -n -N -1', quiet => 1));
}

=head2 wait_for_takeover
wait_for_takeover(target_node=>'expeliarmus');
Waits until B<target_node> performs takeover and reaches 'PRIM' state.
=over
=item * B<target_node>: Node hostname which is expected to take over.
=back
=cut

sub wait_for_takeover {
my (%args) = @_;
my $timeout = 300;
my $start_time = time;
my $topology;
my $takeover_ok;
until ($takeover_ok) {
die("Node '$args{target_node}' did not take over within $timeout sec") if (time - $timeout > $start_time);
$topology = calculate_hana_topology(input => script_output('SAPHanaSR-showAttr --format=script'));
$takeover_ok = 1 if (get_primary_node(input => $topology) eq $args{target_node});
sleep 30;
}
}

=head2 register_replica
register_replica(target_hostname=>'Dumbledore', instance_id=>'00');
Executes
=over
=item * B<target_hostname>: Hostname of the node that should be registered as replica
=item * B<instance_id>: Instance ID
=item * B<switch_user>: Execute command as specified user with help of C<sudo su ->. Default: undef
=back
=cut

sub register_replica {
my (%args) = @_;
croak('Argument "$replica_hostname" missing') unless $args{target_hostname};
my $topology = calculate_hana_topology(input => script_output('SAPHanaSR-showAttr --format=script'));
my $primary_hostname = get_primary_node(input => $topology);
croak("Primary node '$primary_hostname' not found in 'SAPHanaSR-showAttr' output") unless $primary_hostname;
croak("Replica node '$args{target_hostname}' not found in 'SAPHanaSR-showAttr' output") unless
$topology->{$args{target_hostname}};

my $cmd = join(' ',
'hdbnsutil',
'-sr_register',
"--remoteHost=$primary_hostname",
"--remoteInstance=$args{instance_id}",
"--replicationMode=$topology->{$args{target_hostname}}{srmode}",
"--operationMode=$topology->{$args{target_hostname}}{op_mode}",
"--name=$topology->{$args{target_hostname}}{site}",
'--online');
$cmd = join(' ', 'sudo', 'su', '-', $args{switch_user}, '-c', '"', $cmd, '"') if $args{switch_user};
assert_script_run($cmd);
}



sub get_node_roles {
my $topology = calculate_hana_topology(input => script_output('SAPHanaSR-showAttr --format=script'));
my $primary_db = get_primary_node(input => $topology);
my $replica_db = get_failover_node(input => $topology);
return(($primary_db, $replica_db));
}
Loading

0 comments on commit 8004ba4

Please sign in to comment.