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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#-*- mode: cperl -*-#
use Test::More;
use blib;
chdir 't' if -d 't';
require './setup.pl';
unless( have_crontab() ) {
plan skip_all => "no crontab available";
}
plan tests => 41;
use_ok('Config::Crontab');
my $ct;
my $crontabf = ".tmp_crontab.$$";
my $crontabd = <<'_CRONTAB_';
MAILTO=scott
## logs nightly
#30 4 * * * /home/scott/bin/weblog.pl -v -s daily >> ~/tmp/logs/weblog.log 2>&1
## logs weekly
#35 4 * * 1 /home/scott/bin/weblog.pl -v -s weekly >> ~/tmp/logs/weblog.log 2>&1
## run a backup
20 2 * * 5 /usr/bin/tar -zcvf .backup/`$HOME/bin/dateish`.tar.gz ~/per
40 2 * * 5 /usr/bin/scp $HOME/.backup/`$HOME/bin/dateish`.tar.gz mx:~/backup/tub
## fetch ufo
13 9 * * 1-5 env DISPLAY=tub:0 ~/bin/fetch_image
## check versions
#MAILTO=phil
#10 5 * * 1-5 $HOME/fetch_version -q
## start spamd
@reboot /usr/local/bin/spamd -c -d -p 1783
_CRONTAB_
my $crontabd2 = <<'_CRONTAB_';
MAILTO=scott
## fetch ufo
13 9 * * 1-5 env DISPLAY=tub:0 ~/bin/fetch_image
## logs nightly
#30 4 * * * /home/scott/bin/weblog.pl -v -s daily >> ~/tmp/logs/weblog.log 2>&1
## run a backup
20 2 * * 5 /usr/bin/tar -zcvf .backup/`$HOME/bin/dateish`.tar.gz ~/per
40 2 * * 5 /usr/bin/scp $HOME/.backup/`$HOME/bin/dateish`.tar.gz mx:~/backup/tub
## logs weekly
#35 4 * * 1 /home/scott/bin/weblog.pl -v -s weekly >> ~/tmp/logs/weblog.log 2>&1
## start spamd
@reboot /usr/local/bin/spamd -c -d -p 1783
## check versions
#MAILTO=phil
#10 5 * * 1-5 $HOME/fetch_version -q
_CRONTAB_
my $crontabd3 = <<'_CRONTAB_';
MAILTO=scott
## fetch ufo
13 9 * * 1-5 env DISPLAY=tub:0 ~/bin/fetch_image
## logs nightly
#30 4 * * * /home/scott/bin/weblog.pl -v -s daily >> ~/tmp/logs/weblog.log 2>&1
## logs weekly
#35 4 * * 1 /home/scott/bin/weblog.pl -v -s weekly >> ~/tmp/logs/weblog.log 2>&1
## start spamd
@reboot /usr/local/bin/spamd -c -d -p 1783
## check versions
#MAILTO=phil
#10 5 * * 1-5 $HOME/fetch_version -q
## run a backup
20 2 * * 5 /usr/bin/tar -zcvf .backup/`$HOME/bin/dateish`.tar.gz ~/per
40 2 * * 5 /usr/bin/scp $HOME/.backup/`$HOME/bin/dateish`.tar.gz mx:~/backup/tub
_CRONTAB_
## write a crontab file
open FILE, ">$crontabf"
or die "Couldn't open $crontabf: $!\n";
print FILE $crontabd;
close FILE;
## read our crontab
$ct = new Config::Crontab( -file => $crontabf );
ok( $ct->read, "read" );
is( $ct->dump, $crontabd, "dump" );
## rearrange the furniture
ok( $ct->up($ct->block($ct->select( data_re => 'start spamd' ))), "up" );
ok( $ct->down($ct->block($ct->select( data_re => 'logs weekly' ))), "down" );
ok( $ct->first($ct->block($ct->select( data_re => 'fetch ufo' ))), "first" );
ok( $ct->down($ct->block($ct->select( data_re => 'fetch ufo' ))), "down" );
is( $ct->dump, $crontabd2, "dump clean" );
ok( $ct->write, "write" );
undef $ct;
## read our file again
$ct = new Config::Crontab;
$ct->file($crontabf);
$ct->read;
is( $ct->dump, $crontabd2, "dump clean" );
## more furniture adjustments
ok( $ct->last($ct->block($ct->select( data_re => 'run a backup' ))), "last" );
is( $ct->dump, $crontabd3, "dump clean" );
## write to a different file
ok( $ct->file(".foo_" . $crontabf), "new file" );
ok( $ct->write, "write" );
undef $ct;
## test a pipe with 'new'
$ct = new Config::Crontab( -file => "cat $crontabf|" );
$ct->read;
is( $ct->dump, $crontabd2, "dump clean" );
undef $ct;
## test a pipe with 'new'
$ct = new Config::Crontab( -file => "perl -ne 'print' $crontabf|" );
$ct->read;
is( $ct->dump, $crontabd2, "dump clean" );
undef $ct;
## test a pipe with 'file'
$ct = new Config::Crontab;
$ct->file("perl -ne 'print' $crontabf|");
$ct->read;
is( $ct->dump, $crontabd2, "dump clean" );
undef $ct;
## test a pipe with 'file'
$ct = new Config::Crontab;
$ct->read( -file => "perl -ne 'print' $crontabf|" );
is( $ct->dump, $crontabd2, "dump clean" );
undef $ct;
##
$ct = new Config::Crontab( -file => ".foo_" . $crontabf );
ok( $ct->read, "read" );
is( $ct->dump, $crontabd3, "dump clean" );
## make sure nothing bad happened to this file
ok( $ct->file($crontabf) );
ok( $ct->read );
is( $ct->dump, $crontabd2, "dump clean" );
## make all the comments last
$ct = new Config::Crontab( -file => $crontabf );
ok( $ct->read, "read" );
for my $block ( $ct->blocks ) {
$block->last($block->select( type => 'comment' ));
}
undef $ct;
## do mode tests
## line tests turned out to be "shoot the arrow, whatever it hits, call the target"
$ct = new Config::Crontab( -file => $crontabf,
-mode => 'line' );
is( $ct->mode, 'line', "line mode" );
ok( $ct->read, "read" );
is( $ct->blocks, $ct->select, "blocks selected" );
my $crontabd4 = $crontabd2; $crontabd4 =~ s/(\S)\n(\S)/$1\n\n$2/sg;
is( $ct->dump, $crontabd4, "dump clean" );
undef $ct;
## file tests
$ct = new Config::Crontab( -file => $crontabf,
-mode => 'file' );
is( $ct->mode, 'file', "file mode" );
ok( $ct->read, "read" );
is( $ct->dump, $crontabd2, "dump clean" );
undef $ct;
## do squeeze tests
my $crontabd5 = <<_CRONTAB_;
## comment 1
5 8 * * * /bin/command1
## for best results
## squeeze from end of tube
5 10 * * * /bin/command2
## comment 3
MAILTO=joe
0 0 * * Fri /bin/backup
_CRONTAB_
open FILE, ">$crontabf"
or die "Could not open $crontabf: $!\n";
print FILE $crontabd5;
close FILE;
## squeeze: FIXME: squeeze(0) doesn't work properly
$ct = new Config::Crontab( -file => $crontabf,
-squeeze => 0 );
ok( $ct->squeeze(1) ); ## squeeze
ok( $ct->read );
is( $ct->dump, <<_CRONTAB_, "squeeze dump clean" );
## comment 1
5 8 * * * /bin/command1
## for best results
## squeeze from end of tube
5 10 * * * /bin/command2
## comment 3
MAILTO=joe
0 0 * * Fri /bin/backup
_CRONTAB_
undef $ct;
## test reading/writing from a nonexistent file: strict should take effect here
$ct = new Config::Crontab( -file => ".tmp_foo.$$" );
ok( $block = new Config::Crontab::Block, "new block" );
ok( $block->last( new Config::Crontab::Event( -data => '5 10 15 20 Mon /bin/monday' ) ), "last" );
ok( $ct->last($block), "last again" );
ok( $ct->write, "write" );
undef $ct;
$ct = new Config::Crontab( -file => ".tmp_foo.$$" );
is( $ct->dump, <<_DUMP_, "strict good" );
5 10 15 20 Mon /bin/monday
_DUMP_
undef $ct;
unlink ".tmp_foo.$$";
eval { $ct = new Config::Crontab( -file => ".tmp_foo.$$", -strict => 1 ) };
ok( $@, "strict error" );
undef $ct;
eval { $ct = new Config::Crontab( -file => ".tmp_foo.$$", -strict => 0 ) };
ok( !$@, "unstrict clean" );
## we don't test non-squeeze mode because it doesn't really work
END {
unlink $crontabf;
unlink ".foo_$crontabf";
unlink ".tmp_foo.$$";
}
## this is a test for trying to write bogus data into the crontab
#perl -Mblib -MConfig::Crontab -e '$c=new Config::Crontab; $c->read; $c->last(new Config::Crontab::Block(-data => "## testing\n5 * * * Buz /bin/date")); $c->write or die "Bad write: $@\n"'
|