Skip to content

Commit

Permalink
Duplication fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrubovics committed Nov 26, 2024
1 parent bfc2c08 commit 4fb118b
Showing 1 changed file with 23 additions and 136 deletions.
159 changes: 23 additions & 136 deletions lib/oscap_tests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ sub modify_ds_ansible_files {
my ($ansible_exclusions, $ansible_exclusions_diff);

# Get rule exclusions for ansible playbook
get_test_exclusions($ansible_exclusions);
get_test_exclusions_diff($ansible_exclusions_diff);
get_test_exclusions("openqa_tests_exclusions_base", $ansible_exclusions);
get_test_exclusions("openqa_tests_exclusions_diff", $ansible_exclusions_diff);
if (@$ansible_exclusions_diff > 0) { # if found SP specific exclusion
push(@$ansible_exclusions, @$ansible_exclusions_diff);
}
Expand Down Expand Up @@ -634,8 +634,8 @@ sub modify_ds_ansible_files {
my ($bash_exclusions, $bash_exclusions_diff);

# Get rule exclusions for bash playbook
get_test_exclusions($bash_exclusions);
get_test_exclusions_diff($bash_exclusions_diff);
get_test_exclusions("openqa_tests_exclusions_base", $bash_exclusions);
get_test_exclusions("openqa_tests_exclusions_diff", $bash_exclusions_diff);
if (@$bash_exclusions_diff > 0) { # if found SP specific exclusion
push(@$bash_exclusions, @$bash_exclusions_diff);
}
Expand Down Expand Up @@ -793,6 +793,7 @@ sub get_tests_config {

sub get_test_expected_results {
# Get expected results from remote file
my $file_name = $_[0];
my $eval_match = ();
my $type = "";
my $arch = "";
Expand All @@ -811,7 +812,7 @@ sub get_test_expected_results {
my $sles_sp = (split('-', $version))[1];

my $exp_fail_list_name = $sle_version . "-exp_fail_list";
my $expected_results_file_name = "openqa_tests_expected_results_base_" . $benchmark_version . ".yaml";
my $expected_results_file_name = $file_name . "_" . $benchmark_version . ".yaml";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
my @eval_match = ();

Expand All @@ -821,7 +822,7 @@ sub get_test_expected_results {
}
# In case if expected_results are not defined for specific benchmark_version
else {
$expected_results_file_name = "openqa_tests_expected_results_base.yaml";
$expected_results_file_name = "$file_name.yaml";
$return = download_file_from_https_repo($url, $expected_results_file_name);
}
if ($return == 1) {
Expand All @@ -832,83 +833,32 @@ sub get_test_expected_results {
my $expected_results = YAML::PP::Load($data);
record_info("Looking expected results", "Looking expected results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp");

$eval_match = $expected_results->{$profile_ID}->{$type}->{$arch}->{$exp_fail_list_name};
if (defined $eval_match) {
@eval_match = @$eval_match;
record_info("Got expected results", "Got expected results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp\nBenchmark: $benchmark_version\nList of expected to fail rules:\n" . (join "\n", @eval_match));
if ($expected_results_file_name =~ /base/) {
$eval_match = $expected_results->{$profile_ID}->{$type}->{$arch}->{$exp_fail_list_name};
}
else {
record_info("No expected results", "Expected results are not defined.");
$eval_match = $expected_results->{$profile_ID}->{$type}->{$arch}->{$exp_fail_list_name}->{$sles_sp};
}
}
else {
record_info("No file for expected results", "Not able to download file with expected results.\nExpected results are not defined.");
}

$_[0] = \@eval_match;
return 1;
}

sub get_test_expected_results_diff {
# Get expected results if different from base from remote file
my $eval_match = ();
my $type = "";
my $arch = "";

if ($ansible_remediation == 1) {
$type = 'ansible';
}
else {
$type = 'bash';
}
if (is_s390x) { $arch = "s390x"; }
if (is_aarch64 or is_arm) { $arch = "aarch64"; }
if (is_ppc64le) { $arch = "ppc"; }
if (is_x86_64) { $arch = "x86_64"; }
my $version = get_var('VERSION');
my $sles_sp = (split('-', $version))[1];

my $exp_fail_list_name = $sle_version . "-exp_fail_list";
my $expected_results_file_name = "openqa_tests_expected_results_diff_" . $benchmark_version . ".yaml";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
my @eval_match = ();

my $return = download_file_from_https_repo($url, $expected_results_file_name);
if ($return == 1) {
record_info("Downloded results", "Downloded expected results for benchmark version $benchmark_version");
}
# In case if expected_results are not defined for specific benchmark_version
else {
$expected_results_file_name = "openqa_tests_expected_results_diff.yaml";
$return = download_file_from_https_repo($url, $expected_results_file_name);
}
if ($return == 1) {
uload_log_file($expected_results_file_name);
my $data = script_output("cat $expected_results_file_name", quiet => 1);

# Phrase the expected results
my $expected_results = YAML::PP::Load($data);
record_info("Looking expected diff results", "Looking expected diff results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp");

$eval_match = $expected_results->{$profile_ID}->{$type}->{$arch}->{$exp_fail_list_name}->{$sles_sp};
if (defined $eval_match) {
@eval_match = @$eval_match;
record_info("Got expected diff results", "Got expected diff results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp\nBenchmark: $benchmark_version\nList of expected to fail rules:\n" . (join "\n", @eval_match));
record_info("Got expected results", "Got expected results for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exp_fail_list_name\nService Pack: $sles_sp\nBenchmark: $benchmark_version\nList of expected to fail rules:\n" . (join "\n", @eval_match));
}
else {
record_info("No expected diff results", "Expected diff results are not defined.");
record_info("No expected results", "Expected results are not defined.");
}
}
else {
record_info("No file for expected results diff", "Not able to download file with expected results.\nExpected results are not defined.");
record_info("No file for expected results", "Not able to download file with expected results.\nExpected results are not defined.");
}

$_[0] = \@eval_match;
$_[1] = \@eval_match;
return 1;
}

sub get_test_exclusions {
# Get exclusions from remote file
my $file_name = $_[0];
my $exclusions = ();
my $found = -1;
my $type = "";
Expand All @@ -934,7 +884,7 @@ sub get_test_exclusions {
my $sles_sp = (split('-', $version))[1];

my $exclusions_list_name = $sle_version . "-exclusions_list";
my $exclusions_file_name = "openqa_tests_exclusions_base_" . $benchmark_version . ".yaml";
my $exclusions_file_name = $file_name . "_" . $benchmark_version . ".yaml";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
my @exclusions = ();

Expand All @@ -944,7 +894,7 @@ sub get_test_exclusions {
}
# In case if exclusions are not defined for specific benchmark_version
else {
$exclusions_file_name = "openqa_tests_exclusions_base.yaml";
$exclusions_file_name = "$file_name.yaml";
$return = download_file_from_https_repo($url, $exclusions_file_name);
}
if ($return == 1) {
Expand All @@ -955,75 +905,12 @@ sub get_test_exclusions {
my $exclusions_data = YAML::PP::Load($data);
record_info("Looking exclusions", "Looking exclusions for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exclusions_list_name\nService Pack: $sles_sp");

$exclusions = $exclusions_data->{$profile_ID}->{$type}->{$arch}->{$exclusions_list_name};
# If results defined
if (defined $exclusions) {
@exclusions = @$exclusions;
$found = 1;
record_info("Got exclusions", "Got exclusions for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exclusions_list_name\nService Pack: $sles_sp\nBenchmark: $benchmark_version\nList of excluded rules:\n" . (join "\n", @exclusions));
}
else {
record_info("No exclusions", "Exclusions are not defined.");
}
}
else {
record_info("No file for exclusions", "Not able to download file with exclusions.\nExclusions are not defined.");
}

$_[0] = \@exclusions;
return $found;
}
}

sub get_test_exclusions_diff {
# Get exclusions from remote file - contains exclusions for SPs if different from base
my $exclusions = ();
my $found = -1;
my $type = "";
my $arch = "";
my $return = -1;

# If set in configuration to not use excusions
if ($use_exclusions == 0) {
return $found;
}
else {
if ($ansible_remediation == 1) {
$type = 'ansible';
}
else {
$type = 'bash';
}
if (is_s390x) { $arch = "s390x"; }
if (is_aarch64 or is_arm) { $arch = "aarch64"; }
if (is_ppc64le) { $arch = "ppc"; }
if (is_x86_64) { $arch = "x86_64"; }
my $version = get_var('VERSION');
my $sles_sp = (split('-', $version))[1];

my $exclusions_list_name = $sle_version . "-exclusions_list";
my $exclusions_file_name = "openqa_tests_exclusions_diff_" . $benchmark_version . ".yaml";
my $url = "https://gitlab.suse.de/seccert-public/compliance-as-code-compiled/-/raw/main/content/";
my @exclusions = ();

$return = download_file_from_https_repo($url, $exclusions_file_name);
if ($return == 1) {
record_info("Downloded exclusions", "Downloded exclusions for benchmark version $benchmark_version");
if ($exclusions_file_name =~ /base/) {
$exclusions = $exclusions_data->{$profile_ID}->{$type}->{$arch}->{$exclusions_list_name};
}
# In case if exclusions are not defined for specific benchmark_version
else {
$exclusions_file_name = "openqa_tests_exclusions_diff.yaml";
$return = download_file_from_https_repo($url, $exclusions_file_name);
}
if ($return == 1) {
uload_log_file($exclusions_file_name);
my $data = script_output("cat $exclusions_file_name", quiet => 1);

# Phrase the expected results
my $exclusions_data = YAML::PP::Load($data);
record_info("Looking exclusions", "Looking exclusions for \nprofile_ID: $profile_ID\ntype: $type\narch: $arch\nname: $exclusions_list_name\nService Pack: $sles_sp");

$exclusions = $exclusions_data->{$profile_ID}->{$type}->{$arch}->{$exclusions_list_name}->{$sles_sp};
}
# If results defined
if (defined $exclusions) {
@exclusions = @$exclusions;
Expand All @@ -1038,7 +925,7 @@ sub get_test_exclusions_diff {
record_info("No file for exclusions", "Not able to download file with exclusions.\nExclusions are not defined.");
}

$_[0] = \@exclusions;
$_[1] = \@exclusions;
return $found;
}
}
Expand Down Expand Up @@ -1385,8 +1272,8 @@ sub oscap_evaluate {
}
else {
#Verify remediated rules
$ret_expected_results = get_test_expected_results($expected_eval_match);
$ret_expected_results_diff = get_test_expected_results_diff($expected_eval_match_diff);
$ret_expected_results = get_test_expected_results("openqa_tests_expected_results_base", $expected_eval_match);
$ret_expected_results_diff = get_test_expected_results("openqa_tests_expected_results_diff", $expected_eval_match_diff);
# Found expected results in yaml file
if ($ret_expected_results == 1) {
$n_failed_rules = @$expected_eval_match;
Expand Down

0 comments on commit 4fb118b

Please sign in to comment.