File Coverage

blib/lib/StorageDisplay/Collect/CMD/Replay.pm
Criterion Covered Total %
statement 64 75 85.3
branch 9 14 64.2
condition n/a
subroutine 14 14 100.0
pod 0 4 0.0
total 87 107 81.3


line stmt bran cond sub pod time code
1             #
2             # This file is part of StorageDisplay
3             #
4             # This software is copyright (c) 2014-2023 by Vincent Danjean.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 7     7   53 use strict;
  7         13  
  7         293  
10 7     7   34 use warnings;
  7         14  
  7         448  
11 7     7   87 use 5.14.0;
  7         24  
12              
13             # in order to load the StorageDisplay::Collect::CMD module
14 7     7   35 use StorageDisplay::Collect;
  7         11  
  7         452  
15              
16             package StorageDisplay::Collect::CMD::Replay;
17             # ABSTRACT: Use cached command output to collect data
18             our $VERSION = '2.06'; # VERSION
19              
20              
21 7     7   36 use parent -norequire => "StorageDisplay::Collect::CMD";
  7         100  
  7         71  
22 7     7   368 use Scalar::Util 'blessed';
  7         15  
  7         467  
23 7     7   37 use Data::Dumper;
  7         10  
  7         384  
24 7     7   15260 use Data::Compare;
  7         99807  
  7         80  
25              
26             sub new {
27 7     7 0 21 my $class = shift;
28 7         40 my %args = ( @_ );
29 7 50       40 if (not exists($args{'replay-data'})) {
30 0         0 die 'replay-data argument required';
31             }
32 7         100 my $self = $class->SUPER::new(@_);
33 7         79 $self->{'_attr_replay_data'} = $args{'replay-data'};
34 7         28 $self->{'_attr_replay_data_nextid'}=0;
35 7         27 return $self;
36             }
37              
38             sub _replay {
39 451     451   843 my $self = shift;
40 451         714 my $args = shift;
41 451         764 my $ignore_keys = shift;
42 451         769 my $msgerr = shift;
43              
44 451         1360 my $entry = $self->{'_attr_replay_data'}->[$self->{'_attr_replay_data_nextid'}++];
45 451 50       1670 if (not defined($entry)) {
46 0         0 print STDERR "E: no record for $msgerr\n";
47 0         0 die "No records anymore\n";
48             }
49 451         687 foreach my $k (keys %{$args}) {
  451         1594  
50 895 50       2461 if (not exists($entry->{$k})) {
51 0         0 print STDERR "E: no record for $msgerr\n";
52 0         0 die "Missing '$k' in record:\n".Data::Dumper->Dump([$entry], ['record'])."\n";
53             }
54             }
55 451 50       2438 if (! Compare($entry, $args, { ignore_hash_keys => $ignore_keys })) {
56 0         0 print STDERR "E: record for different arguments\n";
57 0         0 foreach my $k (@{$ignore_keys}) {
  0         0  
58 0         0 delete($entry->{$k});
59             }
60 0         0 die "Bad record:\n".
61             Data::Dumper->Dump([$args, $entry], ['requested', 'recorded'])."\n";
62             }
63 451         153540 return $entry;
64             }
65              
66             sub _replay_cmd {
67 444     444   869 my $self = shift;
68 444         1447 my $args = { @_ };
69             my $cmd = $self->_replay(
70             $args,
71             ['stdout', 'root'],
72 444         1339 "command ".$self->cmd2str(@{$args->{'cmd'}}),
  444         1797  
73             );
74 444         2690 my $cmdrequested = $self->cmd2str(@{$args->{'cmd'}});
  444         2043  
75 444 50       1558 if ($args->{'root'} != $cmd->{'root'}) {
76 0         0 print STDERR "W: Root mode different for $cmdrequested\n";
77             }
78 444 100       26828 print STDERR "Replaying".($cmd->{'root'}?' (as root)':'')
79             .": ", $cmdrequested, "\n";
80 444         1163 my @infos = @{$cmd->{'stdout'}};
  444         14145  
81 444         15649 my $infos = join("\n", @infos);
82 444 100       1231 if (scalar(@infos)) {
83             # will add final endline
84 416         803 $infos .= "\n";
85             }
86 444         4936 open(my $fh, "<", \$infos);
87 444         10616 return $fh;
88             }
89              
90             sub open_cmd_pipe {
91 112     112 0 199 my $self = shift;
92 112         489 return $self->_replay_cmd(
93             'root' => 0,
94             'cmd' => [ @_ ],
95             );
96             }
97              
98             sub open_cmd_pipe_root {
99 332     332 0 576 my $self = shift;
100 332         1452 return $self->_replay_cmd(
101             'root' => 1,
102             'cmd' => [ @_ ],
103             );
104             }
105              
106             sub has_file {
107 7     7 0 18 my $self = shift;
108 7         18 my $filename = shift;
109 7         49 my $fileaccess = $self->_replay(
110             {
111             'filename' => $filename,
112             },
113             [ 'value' ],
114             "file access check to '$filename'");
115 7         49 return $fileaccess->{'value'};
116             }
117              
118             1;
119              
120             __END__
121              
122             =pod
123              
124             =encoding UTF-8
125              
126             =head1 NAME
127              
128             StorageDisplay::Collect::CMD::Replay - Use cached command output to collect data
129              
130             =head1 VERSION
131              
132             version 2.06
133              
134             This module is mainly useful for debug or test only. It allows
135             one to replace real data collect on machine by the recorded
136             output of all commands.
137              
138             =head1 AUTHOR
139              
140             Vincent Danjean <Vincent.Danjean@ens-lyon.org>
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2014-2023 by Vincent Danjean.
145              
146             This is free software; you can redistribute it and/or modify it under
147             the same terms as the Perl 5 programming language system itself.
148              
149             =cut