-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathcfg_files_utils.pm
109 lines (89 loc) · 2.98 KB
/
cfg_files_utils.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# SUSE's openQA tests
#
# Copyright 2021 SUSE LLC
# SPDX-License-Identifier: FSFAP
# Summary: Shared library for validating and manipulating
# configuration files in the SUT.
package cfg_files_utils;
use Exporter 'import';
use strict;
use warnings;
use Test::Assert ':all';
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
use testapi;
our @EXPORT = qw(
validate_cfg_file
compare_settings
);
=head2 compare_settings
compare_settings({ expected => $value_1, current => value_2 })
Method validates that the given hash references contain the
same data. In any other case, the data from both structures
are printed, accompanied by an error message.
=cut
sub compare_settings {
my ($args) = @_;
eval {
assert_deep_equals($args->{expected}, $args->{current});
record_info("Compare settings", "Settings comparison passed") unless $args->{suppress_info};
} or do {
print "$@\n";
die "The system settings deviate from expectations. \n
Expected: " . Dumper($args->{expected}) . "\nGot: " . Dumper($args->{current}) . "\n";
};
}
=head2 validate_cfg_file
validate_cfg_file($args)
Method validates content of the config file. Method allows to validate
file by the line content or by providing key-pair values for the
configuration files which use "KEY=PAIR" syntax.
Following structure is expected for the input:
configuration_files:
- path: /etc/hosts
entries:
- '10.226.154.19\tnew.entry.de h999uz'
- '127.0.0.1\tlocalhost'
- path: /etc/sysconfig/kdump
settings:
KDUMP_DUMPLEVEL: 31
KDUMP_DUMPFORMAT: lzo
=cut
sub validate_cfg_file {
my ($args) = @_;
# Accumulate errors
my $errors = '';
foreach my $cfg_file (@{$args}) {
my $path = $cfg_file->{path};
if ($cfg_file->{empty}) {
if (script_run("[ -s $path ]") == 0) {
$errors .= "No entries should be found in '$path'.\n";
}
next;
}
my $cfg_content = script_output("cat $path");
for my $setting (keys %{$cfg_file->{settings}}) {
my ($conf_line) = grep { /$setting=/ } split(/\n/, $cfg_content);
unless ($conf_line) {
$errors .= "Setting '$setting' not found in $path.\n";
next;
}
my $value = $cfg_file->{settings}->{$setting};
if ($conf_line !~ /^$setting=[",']?$value[",']?$/) {
$errors .= "Setting '$setting' with value '$value' not found in $path.\n";
}
}
foreach my $entry (@{$cfg_file->{entries}}) {
if ($cfg_content !~ /$entry/) {
$errors .= "Entry '$entry' is not found in '$path'.\n";
}
}
foreach my $not_entry (@{$cfg_file->{not_entries}}) {
if ($cfg_content =~ /$not_entry/) {
$errors .= "Unexpected entry '$not_entry' was found in '$path'.\n";
}
}
}
die "Configuration files validation failed:\n$errors" if $errors;
}
1;