added remove and test change_ip
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 28 Aug 2009 18:51:43 +0000 (18:51 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 28 Aug 2009 18:51:43 +0000 (18:51 +0000)
lib/PXElator/client.pm
lib/PXElator/t/client.t

index 45fadf7..a55a6fa 100644 (file)
@@ -111,7 +111,7 @@ sub save_ip_mac {
        my $mac_path = mac_path($mac);
        unlink $mac_path if -l $mac_path;       # XXX audit?
        symlink ip_path($ip), $mac_path;
-       write_file ip_path($ip,'mac'), $mac;
+       write_file( ip_path($ip,'mac'), $mac );
 }
 
 sub ip_from_mac($) {
@@ -144,10 +144,12 @@ sub mac_from_ip($) {
 
 sub change_ip($$) {
        my ($old, $new) = @_;
-       my $mac = mac_from_ip($old);
+       return if $old eq $new;
+       my $mac = mac_from_ip($old) || die "no mac for $old";
        rename ip_path($old), ip_path($new);
        unlink mac_path($mac);
        symlink ip_path($new), mac_path($mac);
+       return $new;
 }
 
 sub all_ips {
@@ -159,4 +161,13 @@ sub all_ips {
        } glob("$server::conf/ip/*") 
 }
 
+sub remove {
+       my $ip = shift;
+       unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
+       if ( my $mac = mac_from_ip $ip ) {
+               unlink "$server::conf/mac/$mac";
+       }
+       rmdir "$server::conf/ip/$ip";
+}
+
 1;
index 142f109..0bc7322 100755 (executable)
@@ -4,7 +4,7 @@ use warnings;
 use strict;
 use autodie;
 
-use Test::More tests => 16;
+use Test::More tests => 19;
 use Data::Dump qw/dump/;
 use English;
 
@@ -23,6 +23,8 @@ cmp_ok( client::conf( $host => 'test' ), 'eq', 'value', 'value' );
 
 ok( ! client::conf( $host => 'non-existing' ), 'conf non-existing' );
 
+ok( client::remove( $host ), "remove $host" );
+
 ok( my $ip = client::next_ip( $mac ), 'next_ip' );
 diag $ip;
 
@@ -40,6 +42,9 @@ diag dump $conf;
 ok( my @ips = client::all_ips(), 'all_ips' );
 diag dump @ips;
 
-diag "cleanup";
-ok( unlink( $_ ), "unlink $_" ) foreach ( glob("$dir/ip/$host/*"), "$dir/mac/$mac" );
-ok( rmdir "$dir/ip/$host", 'rmdir' );
+ok( my $new_ip = client::change_ip( $ip, $host ), 'change_ip' );
+ok( ! client::change_ip( $new_ip, $new_ip ), 'change_ip without change' );
+ok( my $old_ip = client::change_ip( $new_ip, $ip ), 'change_ip back' );
+cmp_ok( $old_ip, 'eq', $ip, 'ip back' );
+
+ok( client::remove( $ip ), "remove $ip" );