Advertisement
hakonhagland

Proc::Daemon test script

Jul 20th, 2015
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.54 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6.  
  7. use Test::More tests => 19;
  8.  
  9. use Cwd;
  10.  
  11. use Proc::Daemon;
  12.  
  13.  
  14. # Since a daemon will not be able to print terminal output, we
  15. # have a test daemon creating a file and another which runs the created
  16. # Perl file.
  17. # The parent process will test for the existence of the created files
  18. # and for the running daemon.
  19.  
  20.  
  21. # Try to make sure we are in the test directory
  22. my $cwd = Cwd::cwd();
  23. chdir 't' if $cwd !~ m{/t$};
  24. $cwd = Cwd::cwd();
  25. $cwd = ($cwd =~ /^(.*)$/)[0]; # untaint (needed for 03_taintmode)
  26.  
  27.  
  28. # create object
  29. my $daemon = Proc::Daemon->new(
  30.     work_dir     => $cwd,
  31.     child_STDOUT => 'output.file',
  32.     child_STDERR => 'error.file',
  33.     pid_file     => 'pid.file',
  34. );
  35.  
  36. # create a daemon
  37. umask 022;
  38. my $Kid_PID = $daemon->init; # init instead of Init is a test for the old style too!
  39.  
  40. if ( ok( $Kid_PID, "child_1 was created with PID: " . ( defined $Kid_PID ? $Kid_PID : '<undef>' ) ) || defined $Kid_PID ) {
  41.     # here goes the child
  42.     unless ( $Kid_PID ) {
  43.         # print something into 'output.file'
  44.         print 'test1';
  45.  
  46.         # print a new Perl file
  47.         open( FILE, ">$cwd/kid.pl" ) || die;
  48.         print FILE "#!/usr/bin/perl
  49.  
  50. # create an empty file to test umask
  51. open FILE, '>$cwd/umask.file';
  52. close FILE;
  53.  
  54. # stay alive forever
  55. while ( 1 ) { sleep ( 1 ) }
  56.  
  57. exit;";
  58.         close( FILE );
  59.     }
  60.     # this is only for the parent
  61.     else {
  62.         # wait max. 1 min. for the child to exit
  63.         my $r = 0;
  64.         while ( $daemon->Status( $Kid_PID ) and $r <= 60 ) { $r++; sleep( 1 ); }
  65.  
  66.         if ( ok( ! $daemon->Status( $Kid_PID ), "child_1 process did exit within $r sec." ) ) {
  67.             if ( ok( -e "$cwd/pid.file", "child_1 has created a 'pid.file'" ) ) {
  68.                 my ( $pid, undef ) = $daemon->get_pid( "$cwd/pid.file" );
  69.                 ok( $pid == $Kid_PID, "the 'pid.file' contains the right PID: $pid" );
  70.                 ok( (stat("$cwd/pid.file"))[2] == 33152, "the 'pid.file' has right permissions" );
  71.                 unlink "$cwd/pid.file";
  72.             }
  73.  
  74.             if ( ok( -e "$cwd/output.file", "child_1 has created a 'output.file'" ) ) {
  75.                 open( FILE, "<", "$cwd/output.file" );
  76.                 ok( <FILE> eq 'test1', "the content of the 'output.file' was right." );
  77.                 close FILE;
  78.                 unlink "$cwd/output.file";
  79.             }
  80.  
  81.             if ( ok( -e "$cwd/error.file", "child_1 has created a 'error.file'" ) ) {
  82.                 unlink "$cwd/error.file";
  83.             }
  84.  
  85.             if ( ok( -e "$cwd/kid.pl", "child_1 has created the 'kid.pl' file" ) ) {
  86.                 my $Kid_PID2 = $daemon->Init( {
  87.                     exec_command => "perl $cwd/kid.pl",
  88.                     # this is essentially a noop but gives us better test coverage
  89.                     setgid => (split / /, $))[0],
  90.                     setuid => $>,
  91.                 } );
  92.  
  93.                 if ( ok( $Kid_PID2, "child_2 was created with PID: " . ( defined $Kid_PID2 ? $Kid_PID2 : '<undef>' ) ) ) {
  94.                     if ( ok( -e "$cwd/pid_1.file", "child_2 created a 'pid_1.file'" ) ) {
  95.                         my ( $pid, undef ) = $daemon->get_pid( "$cwd/pid_1.file" );
  96.                         ok( $pid == $Kid_PID2, "the 'pid_1.file' contains the right PID: $pid" )
  97.                     }
  98.  
  99.                     # wait max. 1 min. for the (second) child to write all files
  100.                     $r = 0;
  101.                     while ( ! -e "$cwd/error_1.file" and $r <= 60 ) { $r++; sleep( 1 ); }
  102.  
  103.                     if ( ok( -e "$cwd/output_1.file", "child_2 created a 'output_1.file'" ) ) {
  104.                         unlink "$cwd/output_1.file";
  105.                     }
  106.  
  107.                     if ( ok( -e "$cwd/error_1.file", "child_2 created a 'error_1.file'" ) ) {
  108.                         unlink "$cwd/error_1.file";
  109.                     }
  110.  
  111.                     my $pid = $daemon->get_pid_by_proc_table_attr( 'cmndline', "perl $cwd/kid.pl", 1 );
  112.                     diag( "Proc::ProcessTable is installed and did find the right PID for 'perl $cwd/kid.pl': $pid" )
  113.                         if defined $pid and $pid == $Kid_PID2;
  114.  
  115.                     $pid = $daemon->Status( "$cwd/pid_1.file" );
  116.                     ok( $pid == $Kid_PID2, "'kid.pl' daemon is still running" );
  117.  
  118.                     my $stopped = $daemon->Kill_Daemon();
  119.                     ok( $stopped == 1, "stop daemon 'kid.pl'" );
  120.  
  121.                     $r = 0;
  122.                     while ( $pid = $daemon->Status( $Kid_PID2 ) and $r <= 60 ) {
  123.                         $r++; sleep( 1 );
  124.                     }
  125.                     ok( $pid != $Kid_PID2, "'kid.pl' daemon was stopped within $r sec." );
  126.  
  127.                     unlink "$cwd/pid_1.file";
  128.  
  129.                     ok( (stat("$cwd/umask.file"))[2] == 33188, "the 'umask.file' has right permissions" );
  130.                     unlink "$cwd/umask.file";
  131.                 }
  132.  
  133.                 unlink "$cwd/kid.pl";
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139. my $daemon2 = Proc::Daemon->new(
  140.     work_dir     => $cwd,
  141.     child_STDOUT => 'output2.file',
  142.     child_STDERR => 'error2.file',
  143.     pid_file     => 'pid2.file',
  144.     file_umask   => 022,
  145. );
  146.  
  147. my $Kid_PID2 = $daemon2->Init;
  148.  
  149. if ( $Kid_PID2 ) {
  150.     # wait max. 1 min. for the child to exit
  151.     my $r = 0;
  152.     while ( $daemon2->Status( $Kid_PID2 ) and $r <= 60 ) { $r++; sleep( 1 ); }
  153.  
  154.     ok( (stat("$cwd/pid2.file"))[2] == 33188, "the 'pid2.file' has right permissions via file_umask" );
  155.     unlink "$cwd/output2.file", "$cwd/error2.file", "$cwd/pid2.file";
  156. }
  157.  
  158. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement