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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
# vim: sw=4 : ts=4 : et
use warnings;
use strict;
use Test::More tests => 18;
use Test::LongString;
use POSIX ();
use File::Temp qw( tempdir );
use Data::Dumper;
use File::Slurp;
use Munin::Node::SpoolReader;
use Munin::Node::SpoolWriter;
### new
{
my $dir = POSIX::getcwd();
my $writer = new_ok('Munin::Node::SpoolReader' => [
spooldir => $dir,
], 'spooldir provided to constructor');
is($writer->{spooldir}, $dir, 'spooldir key is set');
isa_ok($writer->{spooldirhandle}, 'GLOB', 'spooldirhandle is a glob');
}
{
eval { Munin::Node::SpoolReader->new(fnord => 'blort') };
like($@, qr/./, 'Dies if no spooldir provided');
}
### fetch
{
my $dir = tempdir(CLEANUP => 1);
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
my $reader = Munin::Node::SpoolReader->new(spooldir => $dir);
# write some data
$writer->write(1234567890, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'system.value 1',
]);
$writer->write(1234567900, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'system.value 2',
]);
$writer->write(1234567910, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'system.value 3',
]);
is_string($reader->fetch(1234567899), <<EOS, 'Fetched data since the write');
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567900:2
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567910:3
EOS
is_string($reader->fetch(1234567900), <<EOS, 'Start timestamp is not inclusive');
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567910:3
EOS
is_string($reader->fetch(1), <<EOS, 'Timestamp predates all result: all results are returned');
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567890:1
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567900:2
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567910:3
EOS
is_string($reader->fetch(1234567999), '', 'Timestamp postdates the last result: empty string');
}
{
my $dir = tempdir(CLEANUP => 1);
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
my $reader = Munin::Node::SpoolReader->new(spooldir => $dir);
# write some data
$writer->write(1234567890, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'',
'system.value 1',
'',
]);
is_string($reader->fetch(1), <<EOS, 'Blank lines are ignored');
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567890:1
EOS
}
{
my $dir = tempdir(CLEANUP => 1);
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
my $reader = Munin::Node::SpoolReader->new(spooldir => $dir);
# write results for two different plugins
$writer->write(1234567890, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'system.value 3',
]);
$writer->write(1234567910, 'blort', [
'graph_title Memory usage',
'slab.label slab',
'slab.value 123',
]);
ok(my $fetched = $reader->fetch(1234567800), 'Several services to fetch');
my $f1 = <<EOT;
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567890:3
EOT
my $f2 = <<EOT;
multigraph blort
graph_title Memory usage
slab.label slab
slab.value 1234567910:123
EOT
like($fetched, qr(\A$f1$f2|$f2$f1\Z)m, 'Got results for both services, in either order, and nothing else');
is($reader->fetch(1234567900), <<EOS, 'Several plugins to fetch, but only one is recent enough');
multigraph blort
graph_title Memory usage
slab.label slab
slab.value 1234567910:123
EOS
}
{
my $dir = tempdir(CLEANUP => 1);
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
my $reader = Munin::Node::SpoolReader->new(spooldir => $dir);
# write two sets of results, with a slightly different config
$writer->write(1234567890, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'system.value 3',
]);
$writer->write(1234567990, 'fnord-foo', [
'graph_title CPU usage!', # this line has changed
'system.label system',
'system.value 4',
]);
ok(my $fetched = $reader->fetch(1234567800), 'Several sets of results to fetch');
my $f1 = <<EOT;
multigraph fnord_foo
graph_title CPU usage
system.label system
system.value 1234567890:3
EOT
my $f2 = <<EOT;
multigraph fnord_foo
graph_title CPU usage!
system.label system
system.value 1234567990:4
EOT
like($fetched, qr(\A$f1$f2|$f2$f1\Z)m, 'Got results for both services, in either order, and nothing else');
}
### _get_spooled_plugins
### list
{
my $dir = tempdir(CLEANUP => 1);
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
my $reader = Munin::Node::SpoolReader->new(spooldir => $dir);
is_deeply([ sort $reader->_get_spooled_plugins ], [], 'No spooled plugins to list');
is($reader->list, "\n", 'No spooled plugins to list');
# write "results" for several plugins
$writer->write(1234567890, 'fnord-foo', [
'graph_title CPU usage',
'system.label system',
'system.value 1',
]);
$writer->write(1234567890, 'floop', [
'graph_title Memory usage',
'system.label system',
'system.value 3.14',
]);
$writer->write(1234567890, 'blort', [
'graph_title Flux capacitance',
'system.label system',
'system.value -4',
]);
$writer->write(1334567890, 'blort', [
'graph_title Flux capacitance',
'system.label system',
'system.value -4',
]);
open my $cruft, '>', "$dir/cruft" or die "Unable to create cruft file: $!";
print $cruft "rubbish\n";
close $cruft;
is_deeply([ sort $reader->_get_spooled_plugins ], [ sort qw( fnord_foo floop blort ) ], 'Retrieved list of spooled plugins');
is($reader->list, "blort floop fnord_foo\n", 'Retrieved stringified list of spooled plugins');
}
|