pf-tools/pf-tools: 2 new changesets
parmelan-guest at users.alioth.debian.org
parmelan-guest at users.alioth.debian.org
Sat Dec 18 19:10:13 UTC 2010
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/70c16666dc13
changeset: 1122:70c16666dc13
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Fri Dec 03 23:56:57 2010 +0100
description:
make_dhcpd_conf_file(), __build_dhcpd_conf() and mk_dhcp: use a template
details: http://hg.debian.org/hg/pf-tools/pf-tools/rev/ecbac7c44d67
changeset: 1123:ecbac7c44d67
user: Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date: Sun Dec 12 19:40:42 2010 +0100
description:
Fix tests for get_site_config()
diffstat:
6 files changed, 16 insertions(+), 9 deletions(-)
lib/PFTools/Structqueries.pm | 4 +++-
lib/PFTools/Utils.pm | 2 +-
sbin/mk_dhcp | 8 ++++----
t/13.conf.t | 2 --
t/20.files.dhcpd.conf.header | 1 -
t/20.files.dhcpd.conf.tpl | 8 ++++++++
diffs (363 lines):
diff -r 42db44ed0bcc -r ecbac7c44d67 lib/PFTools/Structqueries.pm
--- a/lib/PFTools/Structqueries.pm Fri Dec 03 16:22:54 2010 +0100
+++ b/lib/PFTools/Structqueries.pm Sun Dec 12 19:40:42 2010 +0100
@@ -327,7 +327,13 @@
croak q{ERROR: Invalid global_config};
}
- return $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
+ my $site_ref = $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
+
+ if ( not $site_ref ) {
+ croak qq{ERROR: Unknown site '$site_name'};
+ }
+
+ return $site_ref;
}
=head2 get_vlan_config( $vlan_name, $global_config, $site_name )
diff -r 42db44ed0bcc -r ecbac7c44d67 lib/PFTools/Utils.pm
--- a/lib/PFTools/Utils.pm Fri Dec 03 16:22:54 2010 +0100
+++ b/lib/PFTools/Utils.pm Sun Dec 12 19:40:42 2010 +0100
@@ -1,6 +1,7 @@
package PFTools::Utils;
-# FIXME: tests and documentation for all functions (at least the public ones)
+# FIXME use check_* as much as possible
+# FIXME tests and documentation for all functions (at least the public ones)
#
# Copyright (C) 2010 Christophe Caillet <quadchris at free.fr>
@@ -628,8 +629,7 @@
=item I<site_name> the site name
-=item I<header_filename> the name of a file whose content will be used as a
-header for the generated content (FIXME use a template instead)
+=item I<template_filename> the name of file containing the template to use
=item I<global_config> a reference to the global configuration hash
@@ -1756,8 +1756,7 @@
=over
-=item I<header_filename> (optional) the name of a file whose content will be
-used as a header for the generated content (FIXME use a template instead)
+=item I<template_filename> the name of file containing the template to use
=item I<site_name> the site name
@@ -1769,26 +1768,21 @@
sub __build_dhcpd_conf {
my ($args_ref) = @_;
- check_mandatory_args_type( $args_ref, q{}, qw( site_name ) );
+ check_mandatory_args_type(
+ $args_ref, q{},
+ qw( site_name template_filename )
+ );
check_mandatory_args_type( $args_ref, q{HASH}, qw( global_config ) );
- check_optional_args_type( $args_ref, q{}, qw( header_filename ) );
+ my $site_ref = get_site_config(
+ $args_ref->{'site_name'},
+ $args_ref->{'global_config'},
+ );
- my $site_ref = get_site_config( $args_ref->{'site_name'},
- $args_ref->{'global_config'} );
-
- my $headers_ref
- = $args_ref->{'header_filename'}
- ? __read_file_in_array( $args_ref->{'header_filename'}, 1 )
- : [];
-
- my @hosts = ();
+ my @hosts = ();
my @subnets = ();
foreach my $vlan ( keys %{$site_ref} ) {
- push @subnets,
- qq/subnet $site_ref->{$vlan}->{'subnet'} netmask $site_ref->{$vlan}->{'netmask'} {/,
- q/}/,
- q{};
+ push @subnets, $site_ref->{$vlan};
foreach my $hostclass ( keys %{ $site_ref->{$vlan} } ) {
next
@@ -1798,18 +1792,25 @@
my $host_ref = $site_ref->{$vlan}->{$hostclass};
foreach my $hostname ( keys %{$host_ref} ) {
- push @hosts, qq/host $hostname {/;
-
- push @hosts,
- map {qq{\t$_}} # 1 tab indentation
- @{ $host_ref->{$hostname} };
-
- push @hosts, q/}/, q{};
+ push @hosts, {
+ hostname => $hostname,
+ definitions => $host_ref->{$hostname},
+ };
}
}
}
- return [ @{$headers_ref}, @subnets, @hosts, q{} ];
+ my $template_filename = $args_ref->{'template_filename'};
+ my $vars_ref = {
+ hosts => \@hosts,
+ subnets => \@subnets,
+ };
+ my $lines_ref = [
+ split qr{ \n }xms,
+ __read_and_process_template( $template_filename, $vars_ref ), -1
+ ];
+
+ return $lines_ref;
}
=head2 __get_kpkg_from_kernel( $pxefilename, $deploymode )
diff -r 42db44ed0bcc -r ecbac7c44d67 sbin/mk_dhcp
--- a/sbin/mk_dhcp Fri Dec 03 16:22:54 2010 +0100
+++ b/sbin/mk_dhcp Sun Dec 12 19:40:42 2010 +0100
@@ -35,26 +35,19 @@
# Vars
my @options_specs = (
- 'help',
- 'header|H=s',
+ 'config|c=s',
+ 'help|h',
+ 'output|o=s',
'site|s=s',
'store=s',
- 'config|c=s',
- 'output|o=s',
+ 'template|t=s',
);
#########################
# Default options value
my $options = {
- 'help' => 0,
- 'site' => '',
- 'store' => '',
- 'config' => '',
'output' => '-',
};
-
-my $PF_CONFIG = {};
-my $GLOBAL_STRUCT = {};
my $program = basename $PROGRAM_NAME;
@@ -65,12 +58,12 @@
print STDERR << "# ENDHELP";
Usage: $program [options]
- --help : print help and exit
- -H --header : header file for dhcp configuration
- -s --site : site on which hostname is defined
- --store : file where global structure datas are in storable format (optional)
- -c --config : pf-tools config file (optional)
- -o --output : output file
+ --help : print this message and exit
+ -t --template : template file
+ -s --site : site on which hostname is defined
+ --store : file where global structure datas are in storable format (optional)
+ -c --config : pf-tools config file (optional)
+ -o --output : output file
# ENDHELP
}
@@ -86,26 +79,22 @@
exit 0;
}
-( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS(
+my ( $pf_config, $global_config ) = Init_TOOLS(
q{},
$options->{'config'},
$options->{'store'}
);
-$options->{'site'} ||= $PF_CONFIG->{'location'}->{'site'};
+$options->{'site'} ||= $pf_config->{'location'}->{'site'};
unless ( $options->{'site'} ) {
- die "A site MUST BE defined for building DNS zone forward";
-}
-
-unless ( get_site_config( $options->{'site'}, $GLOBAL_STRUCT ) ) {
- die "Unknown site $options->{'site'}";
+ die q{ERROR: No site specified and no default site in configuration};
}
my $args_ref = {
- header_filename => $options->{'header'},
- site_name => $options->{'site'},
- global_config => $GLOBAL_STRUCT,
- filename => $options->{'output'},
+ site_name => $options->{'site'},
+ template_filename => $options->{'template'},
+ global_config => $global_config,
+ filename => $options->{'output'},
};
make_dhcpd_conf_file($args_ref);
diff -r 42db44ed0bcc -r ecbac7c44d67 t/13.conf.t
--- a/t/13.conf.t Fri Dec 03 16:22:54 2010 +0100
+++ b/t/13.conf.t Sun Dec 12 19:40:42 2010 +0100
@@ -1715,11 +1715,9 @@
qr{ \A ERROR: [ ] Invalid [ ] global_config }xms
=> q{Dies if non-hashref global_config};
-lives_ok { $result = get_site_config( q{site_name}, $global_config ) }
- => q{Lives ok for an unknown hostname};
-
-is $result, undef
- => q{And returns an undefined value};
+throws_ok { get_site_config( q{site_name}, $global_config ) }
+qr{ \A ERROR: [ ] Unknown [ ] site [ ] }xms
+ => q{Dies if unknown site_name};
$result = get_site_config( q{cbv4}, $global_config );
diff -r 42db44ed0bcc -r ecbac7c44d67 t/20.files.dhcpd.conf.header
--- a/t/20.files.dhcpd.conf.header Fri Dec 03 16:22:54 2010 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-This is a multi-line
-header test
-
diff -r 42db44ed0bcc -r ecbac7c44d67 t/20.files.dhcpd.conf.tpl
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/t/20.files.dhcpd.conf.tpl Sun Dec 12 19:40:42 2010 +0100
@@ -0,0 +1,16 @@
+# Generated from a sample dhcpd.conf template
+
+[% FOREACH subnet IN subnets -%]
+subnet [% subnet.subnet %] netmask [% subnet.netmask %] {
+}
+
+[% END -%]
+
+[%- FOREACH host IN hosts -%]
+host [% host.hostname %] {
+[% FOREACH definition IN host.definitions -%]
+ [% definition %]
+[% END -%]
+}
+
+[% END -%]
diff -r 42db44ed0bcc -r ecbac7c44d67 t/20.files.t
--- a/t/20.files.t Fri Dec 03 16:22:54 2010 +0100
+++ b/t/20.files.t Sun Dec 12 19:40:42 2010 +0100
@@ -1176,7 +1176,7 @@
=> q{Correctly fixes /etc/hosts file for host cbv4-rdeploy01 site cbv4'}
or note explain $result;
-ok unlink $output_filename
+ok unlink($output_filename)
=> q{Removed test output file};
ok copy($input_filename, $output_filename)
@@ -1192,17 +1192,32 @@
=> q{Correctly fixes /etc/hosts file for host cbv4-rdeploy01 site cbv4, even if input and output are the same file'}
or note explain $result;
-ok unlink $output_filename
+ok unlink($output_filename)
=> q{Removed test output file};
########################################################################
note('Testing PFTools::Utils::__build_dhcpd_conf');
can_ok( 'PFTools::Utils', qw( PFTools::Utils::__build_dhcpd_conf ) );
+my $template_filename = q{t/20.files.dhcpd.conf.tpl};
$output_filename = q{/tmp/test.dhcpd.conf};
+$site_name = q{foo-site};
+$args_ref = {
+ site_name => $site_name,
+ template_filename => $template_filename,
+ global_config => $global_config,
+};
+
+throws_ok { PFTools::Utils::__build_dhcpd_conf($args_ref); }
+qr{ \A ERROR: [ ] Unknown [ ] site [ ] }xms
+ => q{Dies if unknown site_name};
+
$site_name = q{cbv4-pfds};
+$args_ref->{'site_name'} = $site_name;
$expected_result = [ split qr{ \n }xms, <<"EOT", -1 ];
+# Generated from a sample dhcpd.conf template
+
subnet 10.1.0.0 netmask 255.255.0.0 {
}
@@ -1222,48 +1237,44 @@
EOT
-$args_ref = {
- site_name => $site_name,
- global_config => $global_config,
-};
$result = PFTools::Utils::__build_dhcpd_conf($args_ref);
is_deeply $result, $expected_result
- => q{Mk_dhcp returns the correct content for site cbv4}
+ => q{Returns the correct content for site cbv4}
or note explain $result;
-my $header_filename = q{t/20.files.dhcpd.conf.header};
-$args_ref->{'header_filename'} = $header_filename;
-my $headers_ref = PFTools::Utils::__read_file_in_array($header_filename, 1);
-$expected_result = [ @{$headers_ref}, @{$expected_result} ];
-
$result = PFTools::Utils::__build_dhcpd_conf($args_ref);
-
is_deeply $result, $expected_result
- => q{Mk_dhcp returns the correct content for site cbv4 with header file}
+ => q{Template OK}
or note explain $result;
########################################################################
note('Testing PFTools::Utils::make_dhcpd_conf_file');
can_ok( 'PFTools::Utils', qw( make_dhcpd_conf_file ) );
-my $output_filename = q{t/20.files.dhcpd.conf.output};
+$output_filename = q{t/20.files.dhcpd.conf.output};
$args_ref = {
- header_filename => $header_filename,
- site_name => $site_name,
- global_config => $global_config,
- filename => $output_filename,
+ site_name => q{bar_site},
+ template_filename => $template_filename,
+ global_config => $global_config,
+ filename => $output_filename,
};
+
+throws_ok { make_dhcpd_conf_file($args_ref); }
+qr{ \A ERROR: [ ] Unknown [ ] site [ ] }xms
+ => q{Dies if unknown site_name};
+
+$args_ref->{'site_name'} = $site_name;
ok make_dhcpd_conf_file($args_ref)
=> q{Returns a true value};
$result = PFTools::Utils::__read_file_in_array($output_filename, 1);
is_deeply $result, $expected_result
- => qq{Correctly creates dhcpd.conf file for site $site_name and header file $header_filename}
+ => qq{Correctly creates dhcpd.conf file for site $site_name}
or note explain $result;
-ok unlink $output_filename
+ok unlink($output_filename)
=> q{Removed test output file};
More information about the pf-tools-commits
mailing list