various fixes and initial focal support
[automated-distro-installer] / fai / config / tests / Faitest.pm
1 #! /usr/bin/perl
2
3 # Subroutines for automatic tests
4 #
5 # Copyright (C) 2009 Thomas Lange, lange@informatik.uni-koeln.de
6 # Based on the first version by Sebastian Hetze, 08/2008
7
8 package FAITEST;
9
10 my $errors = 0;
11
12 use strict;
13 use Getopt::Long;
14 use Pod::Usage;
15 # - - - - - - - - - - - - - - - - - - - - - - - - - -
16 sub setup_test {
17
18 my $verbose = 0;
19 my $help = 0;
20 my $man = 0;
21 $verbose = $ENV{'debug'} if $ENV{'debug'};
22
23 my $result = GetOptions (
24 "verbose=i" => \$verbose,
25 "help" => \$help,
26 "man" => \$man,
27
28 );
29
30 pod2usage(1) if $help;
31 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
32
33 open(LOGFILE,">> $ENV{LOGDIR}/test.log") || die "Can't open test.log. $!";
34 print LOGFILE "------------ Test $0 starting ------------\n";
35 }
36
37 sub printresult {
38
39 # write test result and set next test
40 my ($nexttest) = @_;
41
42 if ($errors > 0) {
43 print STDERR "\n===> $0 FAILED with $errors errors\n";
44 print LOGFILE "\n===> $0 FAILED with $errors errors\n";
45 } else {
46 print STDERR "\n===> $0 PASSED successfully\n";
47 print LOGFILE "\n===> $0 PASSED successfully\n";
48 print LOGFILE "NEXTTEST=$nexttest\n" if $nexttest;
49 }
50 close (LOGFILE);
51 return $errors;
52 }
53
54 sub getDevByMount {
55
56 my $mount = shift;
57 my $dev = qx#mount|grep $mount|cut -d' ' -f1#;
58 chomp $dev;
59 return $dev
60 }
61
62 sub checkMdStat {
63
64 my ($device, $expected) = @_;
65 my ($value) = qx#grep -i "^$device\\b" /proc/mdstat# =~ m/$device\s*:\s*(.*)/i;
66
67 if ($value eq $expected) {
68 print LOGFILE "Check raid $device success\n";
69 return 0;
70 } else {
71 print LOGFILE "Check raid $device FAILED.\n Expect <$expected>\n Found <$value>\n";
72 $errors++;
73 return 1;
74 }
75 }
76
77 sub checkE2fsAttribute {
78
79 my ($device, $attribute, $expected) = @_;
80
81 # since attribute is a space separated list of attributes, IMO we must loop over
82 # the list. Ask Sebastian again
83 my ($value) = qx#tune2fs -l $device |grep -i "$attribute"# =~ m/$attribute:\s+(.*)/i;
84
85 if ($value eq $expected) {
86 print LOGFILE "Check $attribute for $device success\n";
87 return 0;
88 } else {
89 print LOGFILE "Check $attribute for $device FAILED.\n Expect <$expected>\n Found <$value>\n";
90
91 $errors++;
92 return 1;
93 }
94 }
95
96 1;