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($) {
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 {
} 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;
use strict;
use autodie;
-use Test::More tests => 16;
+use Test::More tests => 19;
use Data::Dump qw/dump/;
use English;
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;
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" );