pf-tools/pf-tools: 2 new changesets

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Fri Dec 3 15:11:39 UTC 2010


details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/16bfe8c890ee
changeset: 1119:16bfe8c890ee
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 15:45:48 2010 +0100
description:
New function: get_site_config() + tests

details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/45db1b085905
changeset: 1120:45db1b085905
user:      Thomas Parmelan <tom+pf-tools at ankh.fr.EU.org>
date:      Fri Dec 03 16:10:19 2010 +0100
description:
make_dhcpd_conf_file(), and use it in mk_dhcp

diffstat:

3 files changed, 33 insertions(+), 6 deletions(-)
sbin/mk_dhcp |    7 ++-----
t/13.conf.t  |   31 +++++++++++++++++++++++++++++++
t/20.files.t |    1 -

diffs (395 lines):

diff -r d262ba34f2c5 -r 45db1b085905 lib/PFTools/Structqueries.pm
--- a/lib/PFTools/Structqueries.pm	Fri Dec 03 10:41:57 2010 +0100
+++ b/lib/PFTools/Structqueries.pm	Fri Dec 03 16:10:19 2010 +0100
@@ -45,6 +45,7 @@
     get_mode_from_host_ref
     get_pkgtype_from_hostname
     get_host_config
+    get_site_config
     get_vlan_config
     is_private_vlan
     resolve_hostname_from_global_config
@@ -305,6 +306,30 @@
     return $host_config;
 }
 
+=head2 get_site_config( $site_name, $global_config )
+
+This function returns a reference to the site configuration hash for
+I<$site_name>.
+
+=cut
+
+sub get_site_config {
+    my ( $site_name, $global_config ) = @_;
+
+    if ( not $site_name or ref $site_name ) {
+        croak q{ERROR: Invalid site_name};
+    }
+
+    if (ref $global_config ne 'HASH'
+        or not exists $global_config->{'DHCP'}->{'BY_SITE'}
+        )
+    {
+        croak q{ERROR: Invalid global_config};
+    }
+
+    return $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
+}
+
 =head2 get_vlan_config( $vlan_name, $global_config, $site_name )
 
 This function returns a reference to the vlan config hash for I<$vlan_name>.
diff -r d262ba34f2c5 -r 45db1b085905 lib/PFTools/Utils.pm
--- a/lib/PFTools/Utils.pm	Fri Dec 03 10:41:57 2010 +0100
+++ b/lib/PFTools/Utils.pm	Fri Dec 03 16:10:19 2010 +0100
@@ -52,7 +52,6 @@
     Init_TOOLS
 
     Do_update_from_GLOBAL
-    Mk_dhcp
     Change_kopt_for_hostname
 
     Search_and_replace
@@ -60,6 +59,7 @@
 
 our @EXPORT_OK = qw(
     fix_etc_hosts
+    make_dhcpd_conf_file
     make_interfaces_file
     make_preseed_file
     make_pxe_boot_and_preseed_files
@@ -615,6 +615,38 @@
     }
 
     __make_file( { %{$arguments_ref}, file_type => q{interfaces} } );
+
+    return 1;
+}
+
+=head2 make_dhcpd_conf_file($arg_ref)
+
+This function writes the I<dhcpd.conf> configuration file for a given site. It
+takes the following named arguments in %{$arg_ref}:
+
+=over
+
+=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<global_config> a reference to the global configuration hash
+
+=item I<filename> the output file name
+
+=back
+
+=cut
+
+sub make_dhcpd_conf_file {
+    my ($arguments_ref) = @_;
+
+    unless ( ref $arguments_ref eq 'HASH' ) {
+        croak q{ERROR: Invalid $arguments_ref};
+    }
+
+    __make_file( { %{$arguments_ref}, file_type => q{dhcpd.conf} } );
 
     return 1;
 }
@@ -1724,13 +1756,12 @@
 
 =over
 
-=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<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<site_ref> a reference to the site configuration hash (that would be 
-C<$global_config->{'DHCP'}->{'BY_SITE'}->{$site}> for the current
-implementation) (FIXME use I<site_name> instead)
+=item I<site_name> the site name
 
+=item I<global_config> a reference to the global configuration hash
 =back
 
 =cut
@@ -1738,10 +1769,13 @@
 sub __build_dhcpd_conf {
     my ($args_ref) = @_;
 
-    check_mandatory_args_type( $args_ref, q{HASH}, qw( site_ref ) );
-    my $site_ref = $args_ref->{'site_ref'};
+    check_mandatory_args_type( $args_ref, q{}, qw( site_name ) );
+    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 $headers_ref
         = $args_ref->{'header_filename'}
@@ -2101,22 +2135,23 @@
 is different from the original content. STDOUT is also supported (if
 I<filename> is '-').
 
-The path to I<filename> is automagically created via make_path() if needed.
+The path to I<filename> is automagically created with make_path() if needed.
 
 =cut
 
+my %build_content_for = (
+    q{dhcpd.conf}   => \&__build_dhcpd_conf,
+    etc_hosts       => \&__fix_etc_hosts,
+    interfaces      => \&__build_interfaces,
+    preseed         => \&__build_preseed,
+    pxe_boot        => \&__build_pxe_boot,
+    q{resolv.conf}  => \&__build_resolv_conf,
+    q{sources.list} => \&__build_sources_list,
+    zone            => \&__build_zone,
+);
+
 sub __make_file {
     my ($arguments_ref) = @_;
-
-    my %build_content_for = (
-        etc_hosts       => \&__fix_etc_hosts,
-        interfaces      => \&__build_interfaces,
-        preseed         => \&__build_preseed,
-        pxe_boot        => \&__build_pxe_boot,
-        q{resolv.conf}  => \&__build_resolv_conf,
-        q{sources.list} => \&__build_sources_list,
-        zone            => \&__build_zone,
-    );
 
     # NOTE: all other parameters are properly checked by
     # __build_FOO() => no need to check them here
diff -r d262ba34f2c5 -r 45db1b085905 sbin/mk_dhcp
--- a/sbin/mk_dhcp	Fri Dec 03 10:41:57 2010 +0100
+++ b/sbin/mk_dhcp	Fri Dec 03 16:10:19 2010 +0100
@@ -45,15 +45,15 @@
 #########################
 # Default options value
 my $options = {
-    'help'      => 0,
-    'site'      => '',
-    'store'     => '',
-    'config'    => '',
-    'output'    => '-',
+    'help'   => 0,
+    'site'   => '',
+    'store'  => '',
+    'config' => '',
+    'output' => '-',
 };
 
-my $PF_CONFIG         = {};
-my $GLOBAL_STRUCT     = {};
+my $PF_CONFIG     = {};
+my $GLOBAL_STRUCT = {};
 
 my $program = basename $PROGRAM_NAME;
 
@@ -77,11 +77,10 @@
 ############################################
 ### MAIN
 
-GetOptions( 
-    $options, @options_specs
-) or die "Didn't grok options (see --help).\n";
+GetOptions( $options, @options_specs )
+    or die "Didn't grok options (see --help).\n";
 
-if ($options->{'help'}) {
+if ( $options->{'help'} ) {
     Do_help();
     exit 0;
 }
@@ -92,27 +91,22 @@
     $options->{'store'}
 );
 
-unless( $options->{'site'} ) {
-    unless( $PF_CONFIG->{'location'}->{'site'} ) {
-        die "A site MUST BE defined for building DNS zone forward";
-    }
-    $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( $GLOBAL_STRUCT->{'DHCP'}->{'BY_SITE'}->{$options->{'site'}} ) {
+unless ( get_site_config( $options->{'site'}, $GLOBAL_STRUCT ) ) {
     die "Unknown site $options->{'site'}";
 }
 
-my $DHCP = Mk_dhcp( 
-    $options->{'header'},
-    $GLOBAL_STRUCT->{'DHCP'}->{'BY_SITE'}->{$options->{'site'}}
-);
+my $args_ref = {
+    header_filename => $options->{'header'},
+    site_name       => $options->{'site'},
+    global_config   => $GLOBAL_STRUCT,
+    filename        => $options->{'output'},
+};
 
-my $output_fh = IO::File->new ( '>' . $options->{'output'} )
-    or die "Unable to open destination $options->{'output'}: $OS_ERROR";
-$output_fh->print ( join "\n", @{$DHCP} )
-    or die "Unable to write to destination $options->{'output'}: $OS_ERROR";
-$output_fh->close()
-    or die "Unable to close destination $options->{'output'}: $OS_ERROR";
+make_dhcpd_conf_file($args_ref);
 
 exit 0;
diff -r d262ba34f2c5 -r 45db1b085905 t/13.conf.t
--- a/t/13.conf.t	Fri Dec 03 10:41:57 2010 +0100
+++ b/t/13.conf.t	Fri Dec 03 16:10:19 2010 +0100
@@ -1686,6 +1686,69 @@
     or note explain $result;
 
 
+########################################################################
+note('Testing PFTools::Structqueries::get_site_config');
+can_ok( 'PFTools::Structqueries', qw( get_site_config ) );
+
+# http://rt.perl.org/rt3/Public/Bug/Display.html?id=52610
+SKIP: {
+    skip q{Perl bug #52610}, 2; # only if Test::Exception < 0.31 ?
+
+throws_ok { get_site_config() }
+qr{ \A ERROR: [ ] Invalid [ ] site_name }xms
+    => 'Dies if no site_name';
+
+throws_ok { get_site_config( q{} ) }
+qr{ \A ERROR: [ ] Invalid [ ] site_name }xms
+    => q{Dies if empty site_name};
+}
+
+throws_ok { get_site_config( {} ) }
+qr{ \A ERROR: [ ] Invalid [ ] site_name }xms
+    => q{Dies if non-scalar site_name};
+
+throws_ok { get_site_config( q{site_name} ) }
+qr{ \A ERROR: [ ] Invalid [ ] global_config }xms
+    => 'Dies if no global_config';
+
+throws_ok { get_site_config( q{site_name}, q{global_config} ) }
+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};
+
+$result = get_site_config( q{cbv4}, $global_config );
+
+$expected_result = {
+    'vlan-systeme' => {
+        'netmask'      => '255.255.0.0',
+        'subnet'       => '10.1.0.0',
+        'cbv4-rdeploy' => {
+            'cbv4-rdeploy01' => [
+                'hardware ethernet 00:1e:c9:ff:42:0b;',
+                'fixed-address 10.1.167.1;',
+                'filename pxelinux.0;',
+                'option domain-name-servers nsprivate.private;'
+            ],
+            'cbv4-rdeploy00' => [
+                'hardware ethernet 00:1e:c9:ff:08:e4;',
+                'fixed-address 10.1.167.0;',
+                'filename pxelinux.0;',
+                'option domain-name-servers nsprivate.private;'
+            ],
+        },
+    },
+};
+
+is_deeply $result, $expected_result
+    => q{Returns the correct site_ref for cbv4};
+
+
+
 #TODO: {
 #    local $TODO = 'Depends on other, still failing, tests';
 #} # END TODO
diff -r d262ba34f2c5 -r 45db1b085905 t/20.files.t
--- a/t/20.files.t	Fri Dec 03 10:41:57 2010 +0100
+++ b/t/20.files.t	Fri Dec 03 16:10:19 2010 +0100
@@ -1199,10 +1199,8 @@
 note('Testing PFTools::Utils::__build_dhcpd_conf');
 can_ok( 'PFTools::Utils', qw( PFTools::Utils::__build_dhcpd_conf ) );
 
-my $header_filename = q{}; # q{FOOBAR};
 $output_filename = q{/tmp/test.dhcpd.conf};
 $site_name = q{cbv4-pfds};
-my $site_ref = $global_config->{'DHCP'}->{'BY_SITE'}->{$site_name};
 
 $expected_result = [ split qr{ \n }xms, <<"EOT", -1 ];
 subnet 10.1.0.0 netmask 255.255.0.0 {
@@ -1225,8 +1223,8 @@
 EOT
 
 $args_ref = {
-    header_filename => $header_filename,
-    site_ref => $site_ref,
+    site_name       => $site_name,
+    global_config   => $global_config,
 };
 $result = PFTools::Utils::__build_dhcpd_conf($args_ref);
 
@@ -1234,10 +1232,10 @@
     => q{Mk_dhcp returns the correct content for site cbv4}
     or note explain $result;
 
-$header_filename = q{t/20.files.dhcpd.conf.header};
+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} ];
-$args_ref->{'header_filename'} = $header_filename;
 
 $result = PFTools::Utils::__build_dhcpd_conf($args_ref);
 
@@ -1245,12 +1243,27 @@
     => q{Mk_dhcp returns the correct content for site cbv4 with header file}
     or note explain $result;
 
-#$args_ref = {
-#    site_name => $site_name,
-#    header_filename => $header_filename,
-#    global_config => $global_config,
-#};
-#
-#$result = PFTools::Utils::__build_dhcpd_conf($args_ref);
+########################################################################
+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};
 
+$args_ref = {
+    header_filename => $header_filename,
+    site_name       => $site_name,
+    global_config   => $global_config,
+    filename        => $output_filename,
+};
+
+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}
+    or note explain $result;
+
+ok unlink $output_filename
+    => q{Removed test output file};
+



More information about the pf-tools-commits mailing list