| 1 | package Test::BDD::Infrastructure::Swap; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '1.005'; # VERSION | |||||||||
| 7 | # ABSTRACT: cucumber step definitions for checking swap usage | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Test::BDD::Cucumber::StepFile qw( Given When Then ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub S { Test::BDD::Cucumber::StepFile::S } | ||||||||
|
Subroutine
S: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use Test::BDD::Infrastructure::Utils qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | convert_unit convert_cmp_operator $CMP_OPERATOR_RE convert_interval); | |||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use File::Slurp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub check_swap_ok { | ||||||||
|
Subroutine
check_swap_ok: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 21 | 0 | my $path = '/proc/swaps'; | |||||||||
| 22 | 0 | ▶ | if( ! -f $path ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | fail("$path does not exist. (not on linux?)"); | |||||||||
| 24 | 0 | return; | |||||||||
| 25 | } | ||||||||||
| 26 | 0 | my @lines = read_file( $path ); | |||||||||
| 27 | 0 | shift @lines; # remove header | |||||||||
| 28 | 0 | my @swaps; | |||||||||
| 29 | |||||||||||
| 30 | 0 | foreach my $line (@lines) { | |||||||||
| 31 | 0 | my $stats = {}; | |||||||||
| 32 | 0 | @$stats{'device', 'type', 'size', 'used space', 'priority'} | |||||||||
| 33 | = split(/\s+/, $line); | ||||||||||
| 34 | 0 | $stats->{'size'} *= 1024; | |||||||||
| 35 | 0 | $stats->{'used space'} *= 1024; | |||||||||
| 36 | 0 | $stats->{'free space'} = $stats->{'size'} - $stats->{'used space'}; | |||||||||
| 37 | 0 | $stats->{'usage'} = $stats->{'used space'} / $stats->{'size'}; | |||||||||
| 38 | 0 | push( @swaps, $stats ); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | return( \@swaps ); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | Given qr/^swap is configured$/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 45 | 0 | S->{'swap'} = check_swap_ok; | |||||||||
| 46 | 1 | }; | |||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | Then qr/there must be $CMP_OPERATOR_RE (\d+) swaps? (?:spaces? )?configured/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 49 | 0 | my $op = convert_cmp_operator( $1 ); | |||||||||
| 50 | 0 | my $count = $2; | |||||||||
| 51 | 0 | cmp_ok( scalar @{S->{'swap'}}, $op, $count, "there must be $op $count swaps configure"); | |||||||||
| 52 | 1 | }; | |||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub calc_total_swap_stats { | ||||||||
|
Subroutine
calc_total_swap_stats: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 55 | 0 | my $swaps = shift; | |||||||||
| 56 | 0 | my $total = {}; | |||||||||
| 57 | 0 | @$total{'size', 'used space', 'free space'} = (0,0,0,0); | |||||||||
| 58 | 0 | foreach my $swap ( @$swaps ) { | |||||||||
| 59 | 0 | foreach my $field ('size', 'used space', 'free space') { | |||||||||
| 60 | 0 | $total->{$field} += $swap->{$field}; | |||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | 0 | $total->{'usage'} = $total->{'used space'} / $total->{'size'}; | |||||||||
| 64 | 0 | return $total; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | Then qr/the swaps? (size|used space|free space|usage) must be $CMP_OPERATOR_RE (\d+) (\S+)?/, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 68 | 0 | my $key = $1; | |||||||||
| 69 | 0 | my $op = convert_cmp_operator( $2 ); | |||||||||
| 70 | 0 | my $count = $3; | |||||||||
| 71 | 0 | ▶ | if( defined $4 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $count = convert_unit( $3, $4 ); | |||||||||
| 73 | } | ||||||||||
| 74 | 0 | my $total = calc_total_swap_stats( S->{'swap'} ); | |||||||||
| 75 | 0 | cmp_ok( $total->{$key}, $op, $count, "the filesystems $key $op $count"); | |||||||||
| 76 | 1 | }; | |||||||||
| 77 | |||||||||||
| 78 | 1 | 1; | |||||||||
| 79 | |||||||||||
| 80 | __END__ | ||||||||||