Commit d390bf21 authored by fv3rdugo's avatar fv3rdugo
Browse files

wip(renewMAC): test

Issue #1201
parent 46f02700
......@@ -1610,7 +1610,7 @@ sub _xml_modify_network {
my $self = shift;
my $doc = shift;
my $network = shift;
warn $network;
my ($type, $source );
if (ref($network) =~ /^Ravada/) {
($type, $source) = ($network->type , $network->source);
......@@ -1996,49 +1996,48 @@ sub _xml_modify_mac {
my $self = shift;
my $doc = shift or confess "Missing XML doc";
my (@if_mac) = $doc->findnodes('/domain/devices/interface/mac')
my ($if_mac) = $doc->findnodes('/domain/devices/interface/mac')
or exit;
for my $if_mac (@if_mac) {
my $mac = $if_mac->getAttribute('address');
my $mac = $if_mac->getAttribute('address');
my @macparts0 = split/:/,$mac;
my @macparts0 = split/:/,$mac;
my @old_macs;
my @old_macs;
for my $dom ($self->vm->list_all_domains) {
my $doc = $XML->load_xml(string => $dom->get_xml_description()) or die "ERROR: $!\n";
for my $dom ($self->vm->list_all_domains) {
my $doc = $XML->load_xml(string => $dom->get_xml_description()) or die "ERROR: $!\n";
for my $nic ( $doc->findnodes('/domain/devices/interface/mac')) {
my $nic_mac = $nic->getAttribute('address');
push @old_macs,($nic_mac);
}
for my $nic ( $doc->findnodes('/domain/devices/interface/mac')) {
my $nic_mac = $nic->getAttribute('address');
push @old_macs,($nic_mac);
}
}
warn $if_mac;
my $new_mac;
my @tried;
for ( 1 .. 1000 ) {
for my $cont ( 1 .. 1000 ) {
my @macparts = @macparts0;
my $pos = int(rand(scalar(@macparts)-3))+3;
my $num =sprintf "%02X", rand(0xff);
die "Missing num " if !defined $num;
$macparts[$pos] = $num;
$new_mac = lc(join(":",@macparts));
warn $new_mac;
push @tried,($new_mac);
last if !grep /^$new_mac$/i,@old_macs && $self->_unique_mac($new_mac);
push @old_macs,($new_mac);
}
my $new_mac;
my @tried;
for ( 1 .. 1000 ) {
for my $cont ( 1 .. 1000 ) {
my @macparts = @macparts0;
my $pos = int(rand(scalar(@macparts)-3))+3;
my $num =sprintf "%02X", rand(0xff);
die "Missing num " if !defined $num;
$macparts[$pos] = $num;
$new_mac = lc(join(":",@macparts));
push @tried,($new_mac);
last if !grep /^$new_mac$/i,@old_macs && $self->_unique_mac($new_mac);
push @old_macs,($new_mac);
}
if ( $self->_unique_mac($new_mac) ) {
$if_mac->setAttribute(address => $new_mac);
return;
}
if ( $self->_unique_mac($new_mac) ) {
$if_mac->setAttribute(address => $new_mac);
return;
}
die "I can't find a new unique mac '$new_mac'\n".Dumper(\@tried);
}
die "I can't find a new unique mac '$new_mac'\n".Dumper(\@tried);
}
......
......@@ -56,7 +56,7 @@ sub test_req_prepare_base{
my $domain0 = $RAVADA->search_domain($name);
ok(!$domain0->is_base,"Domain $name should not be base");
my $req = Ravada::Request->prepare_base(id_domain => $domain0->id, uid => user_admin->id);
$RAVADA->_process_all_requests_dont_fork();
......@@ -85,20 +85,21 @@ sub test_add_nic {
wait_request();
is($req->error,"");
is($req->status,"done");
#Read xml
sub read_mac{
my $domain = shift;
my $xml = XML::LibXML->load_xml(string => $domain->get_xml_base());
my @mac;
my @mac;
my (@if_mac) = $xml->findnodes('/domain/devices/interface/mac');
for my $if_mac (@if_mac) {
my $mac = $if_mac->getAttribute('address');
warn $mac;
push @mac, $mac;
}
return(@mac);
}
#Prepare base
test_req_prepare_base($domain->name);
......@@ -115,14 +116,14 @@ sub test_add_nic {
wait_request();
is($req2->error,"");
is($req2->status,"done");
my $domain_clon = $RAVADA->search_domain($name);
my @mac = read_mac($domain);
my @mac2 = read_mac($domain_clon);
isnt($mac[0],$mac2[0]);
isnt($mac[1],$mac2[1]);
isnt($mac[0],$mac2[0], "1st MAC from 1st NIC cloned are the same");
isnt($mac[1],$mac2[1], "2nd MAC from 2nd NIC cloned are the same");
}
......@@ -149,6 +150,6 @@ SKIP: {
test_add_nic($vm_name);
}
#clean();
clean();
done_testing();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment