File Coverage

lib/Ubic/Service/InitScriptWrapper.pm
Criterion Covered Total %
statement 33 38 86.8
branch 5 8 62.5
condition n/a
subroutine 10 11 90.9
pod 5 5 100.0
total 53 62 85.4


line stmt bran cond sub pod time code
1             package Ubic::Service::InitScriptWrapper;
2             {
3             $Ubic::Service::InitScriptWrapper::VERSION = '0.02';
4             }
5              
6             # ABSTRACT: represent any /etc/init.d/ script as ubic service
7              
8              
9 1     1   53773 use strict;
  1         3  
  1         45  
10 1     1   6 use warnings;
  1         2  
  1         41  
11              
12 1     1   964 use parent qw(Ubic::Service::Skeleton);
  1         385  
  1         6  
13 1     1   56002 use autodie qw(:all);
  1         3  
  1         12  
14 1     1   31711 use IPC::System::Simple; # force system() support for autodie
  1         3  
  1         439  
15              
16             sub new {
17 1     1 1 45662 my $class = shift;
18 1         14 my ($init) = @_;
19 1 50       25 die "Invalid parameters" unless @_ == 1;
20              
21 1 50       20 if ($init !~ m{/}) {
22 0         0 $init = "/etc/init.d/$init";
23             }
24              
25 1 50       131 unless (-e $init) {
26 0         0 die "Init script $init not found";
27             }
28              
29 1         25 return bless { init => $init } => $class;
30             }
31              
32             sub start_impl {
33 1     1 1 455 my $self = shift;
34 1         28 system("$self->{init} start >/dev/null");
35             }
36              
37             sub stop_impl {
38 1     1 1 342 my $self = shift;
39 1         26 system("$self->{init} stop >/dev/null");
40             }
41              
42             sub reload {
43 0     0 1 0 my $self = shift;
44 0         0 system("$self->{init} reload >/dev/null");
45 0         0 return 'reloaded'; # we expect system() to fail if reload is not implemented
46             }
47              
48             sub status_impl {
49 8     8 1 162372 my $self = shift;
50 1     1   7 no autodie;
  1         1  
  1         6  
51 8         614036 my $code = system("$self->{init} status >/dev/null");
52 8 100       359 if ($code == 0) {
53 4         212 return 'running';
54             }
55             else {
56 4         173 return 'not running'; # TODO - distinguish 'not running' and 'broken'
57             }
58             }
59              
60             1;
61              
62             __END__