Further enhancements to the config module

Continue work to move all the config code out of the main module into a sub module.  Not yet used by the main module.
This commit is contained in:
Duncan Ferguson 2011-07-21 08:23:49 +01:00
parent 81a79902b6
commit b51644512d
6 changed files with 142 additions and 4 deletions

View file

@ -30,6 +30,7 @@ my $build = Module::Build->new(
'Test::Pod' => 0,
'Test::Trap' => 0,
'Readonly' => 0,
'File::Which' => 0,
},
add_to_cleanup => ['App-ClusterSSH-*'],
create_makefile_pl => 'traditional',

View file

@ -9,6 +9,7 @@ use Carp;
use base qw/ App::ClusterSSH::Base /;
use App::ClusterSSH::Host;
use App::ClusterSSH::Config;
use POSIX ":sys_wait_h";
use Pod::Usage;
@ -50,12 +51,19 @@ sub new {
my $self = $class->SUPER::new(%args);
$self->{config} = App::ClusterSSH::Config->new();
# catch and reap any zombies
$SIG{CHLD} = \&REAPER;
return $self;
}
sub config {
my ($self) = @_;
return $self->{config};
}
sub REAPER {
my $kid;
do {
@ -2316,6 +2324,8 @@ the code until this time.
=item close_inactive_sessions
=item config
=item create_menubar
=item create_windows

View file

@ -11,7 +11,7 @@ use Carp;
use base qw/ App::ClusterSSH::Base /;
my %clusters;
my @app_specific = ( qw/ title comms method ssh rsh telnet ccon / );
my @app_specific = (qw/ title comms method ssh rsh telnet ccon /);
my %default_config = (
terminal => "xterm",
terminal_args => "",
@ -167,6 +167,64 @@ sub parse_config_file {
$self->validate_args(%read_config);
}
# could use File::Which for some of this but we also search a few other places
# just in case $PATH isnt set up right
sub find_binary {
my ( $self, $binary ) = @_;
if ( !$binary ) {
croak(
App::ClusterSSH::Exception::Config->throw(
error => $self->loc('argument not provided'),
),
);
}
$self->debug( 2, "Looking for $binary" );
my $path;
if ( !-x $binary || substr( $binary, 0, 1 ) ne '/' ) {
foreach (
split( /:/, $ENV{PATH} ), qw!
/bin
/sbin
/usr/sbin
/usr/bin
/usr/local/bin
/usr/local/sbin
/opt/local/bin
/opt/local/sbin
!
)
{
$self->debug( 3, "Looking in $_" );
if ( -f $_ . '/' . $binary && -x $_ . '/' . $binary ) {
$path = $_ . '/' . $binary;
$self->debug( 2, "Found at $path" );
last;
}
}
}
else {
$self->debug( 2, "Already configured OK" );
$path = $binary;
}
if ( !$path || !-f $path || !-x $path ) {
croak(
App::ClusterSSH::Exception::Config->throw(
error => $self->loc(
'"[_1]" binary not found - please amend $PATH or the cssh config file',
$binary
),
),
);
}
chomp($path);
return $path;
}
#use overload (
# q{""} => sub {
# my ($self) = @_;
@ -197,7 +255,7 @@ Object representing application configuration
Create a new configuration object.
=item $config->parse_config_file('<filename');
=item $config->parse_config_file('<filename>');
Read in configuration from given filename
@ -205,6 +263,11 @@ Read in configuration from given filename
Validate and apply all configuration loaded at this point
=item $path = $self->find_binary('<name>');
Locate the binary <name> and return the full path. Doesn't just search
$PATH in case the environment isn't set up correctly
=back
=head1 AUTHOR

View file

@ -7,4 +7,4 @@ BEGIN {
use_ok( 'App::ClusterSSH' );
}
diag( "Testing App::ClusterSSH $App::ClusterSSH::VERSION, Perl $], $^X" );
note( "Testing App::ClusterSSH $App::ClusterSSH::VERSION, Perl $], $^X" );

View file

@ -6,6 +6,7 @@ use lib "$Bin/../lib";
use Test::More;
use Test::Trap;
use File::Which qw(which);
use Readonly;
@ -154,7 +155,11 @@ trap {
};
is( $trap->leaveby, 'die', 'died ok' );
isa_ok( $trap->die, 'App::ClusterSSH::Exception::Config' );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->die,
'Unknown configuration parameters: missing,rubbish',
'die message correct'
);
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
@ -176,4 +181,42 @@ is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
}
note('find_binary tests');
my $path;
$config = App::ClusterSSH::Config->new();
trap {
$path = $config->find_binary();
};
is( $trap->leaveby, 'die', 'died ok' );
isa_ok( $trap->die, 'App::ClusterSSH::Exception::Config' );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->die, 'argument not provided', 'die message correct' );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
trap {
$path = $config->find_binary('missing');
};
is( $trap->leaveby, 'die', 'died ok' );
isa_ok( $trap->die, 'App::ClusterSSH::Exception::Config' );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->die, '"missing" binary not found - please amend $PATH or the cssh config file', 'die message correct' );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
trap {
$path = $config->find_binary('ls');
};
is( $trap->leaveby, 'return', 'returned ok' );
isa_ok( $config, "App::ClusterSSH::Config" );
isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
is($path, which('ls'), 'Found correct path to "ls"');
done_testing();

21
t/80clusterssh.t Normal file
View file

@ -0,0 +1,21 @@
use strict;
use warnings;
use FindBin qw($Bin $Script);
use lib "$Bin/../lib";
use Test::More;
use Test::Trap;
use File::Which qw(which);
use Readonly;
BEGIN { use_ok("App::ClusterSSH") }
my $app;
$app = App::ClusterSSH->new();
isa_ok( $app, 'App::ClusterSSH' );
isa_ok( $app->config, 'App::ClusterSSH::Config' );
done_testing();