Commit 50fed357 authored by Fernando Verdugo's avatar Fernando Verdugo
Browse files

fix(renewMAC): renew MAC all nics

- testing 2 nics

Issue #1201
parent d390bf21
...@@ -1608,10 +1608,10 @@ sub _xml_modify_memory { ...@@ -1608,10 +1608,10 @@ sub _xml_modify_memory {
sub _xml_modify_network { sub _xml_modify_network {
my $self = shift; my $self = shift;
my $doc = shift; my $doc = shift;
my $network = shift; my $network = shift;
warn $network;
my ($type, $source ); my ($type, $source );
if (ref($network) =~ /^Ravada/) { if (ref($network) =~ /^Ravada/) {
($type, $source) = ($network->type , $network->source); ($type, $source) = ($network->type , $network->source);
} else { } else {
...@@ -1993,53 +1993,52 @@ sub _new_uuid { ...@@ -1993,53 +1993,52 @@ sub _new_uuid {
} }
sub _xml_modify_mac { sub _xml_modify_mac {
my $self = shift; my $self = shift;
my $doc = shift or confess "Missing XML doc"; my $doc = shift or confess "Missing XML doc";
my @old_macs;
my ($if_mac) = $doc->findnodes('/domain/devices/interface/mac') for my $dom ($self->vm->list_all_domains) {
or exit; my $doc = $XML->load_xml(string => $dom->get_xml_description()) or die "ERROR: $!\n";
my $mac = $if_mac->getAttribute('address');
my @macparts0 = split/:/,$mac;
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 $nic ( $doc->findnodes('/domain/devices/interface/mac')) { for my $nic ( $doc->findnodes('/domain/devices/interface/mac')) {
my $nic_mac = $nic->getAttribute('address'); my $nic_mac = $nic->getAttribute('address');
push @old_macs,($nic_mac); 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);
}
if ( $self->_unique_mac($new_mac) ) { for my $if_mac ($doc->findnodes('/domain/devices/interface/mac') ) {
$if_mac->setAttribute(address => $new_mac); my $mac = $if_mac->getAttribute('address');
return;
}
}
die "I can't find a new unique mac '$new_mac'\n".Dumper(\@tried);
} my @macparts0 = split/:/,$mac;
my $new_mac;
my @tried;
my $foundit;
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);
}
if ( $self->_unique_mac($new_mac) ) {
$if_mac->setAttribute(address => $new_mac);
$foundit = 1;
last;
}
}
die "I can't find a new unique mac '$new_mac'\n".Dumper(\@tried) if !$foundit;
}
}
=pod =pod
......
...@@ -28,20 +28,21 @@ my $BACKEND = 'KVM'; ...@@ -28,20 +28,21 @@ my $BACKEND = 'KVM';
################################################################ ################################################################
sub test_create_domain { sub test_create_domain {
my $vm_name = shift; my $vm_name = shift;
my $vm = rvd_back->search_vm($vm_name); my $vm = rvd_back->search_vm($vm_name);
ok($vm,"I can't find VM $vm_name") or return; ok($vm,"I can't find VM $vm_name") or return;
my $name = new_domain_name(); my $name = new_domain_name();
my $domain; my $domain;
eval { $domain = $vm->create_domain(name => $name $domain = $vm->create_domain(name => $name
, id_owner => $USER->id , id_owner => $USER->id
, disk => 1024 * 1024 , disk => 1024 * 1024
, arg_create_dom($vm_name)) , arg_create_dom($vm_name));
};
my $req = Ravada::Request->add_hardware(name => 'network', id_domain => $domain->id, uid => user_admin->id);
wait_request();
ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or return; ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or return;
ok($domain->name ok($domain->name
&& $domain->name eq $name,"Expecting domain name '$name' , got " && $domain->name eq $name,"Expecting domain name '$name' , got "
.($domain->name or '<UNDEF>') .($domain->name or '<UNDEF>')
...@@ -57,8 +58,8 @@ sub test_req_prepare_base{ ...@@ -57,8 +58,8 @@ sub test_req_prepare_base{
my $domain0 = $RAVADA->search_domain($name); my $domain0 = $RAVADA->search_domain($name);
ok(!$domain0->is_base,"Domain $name should not be base"); ok(!$domain0->is_base,"Domain $name should not be base");
my $req = Ravada::Request->prepare_base(id_domain => $domain0->id, uid => user_admin->id); my $req = Ravada::Request->prepare_base(id_domain => $domain0->id, uid => user_admin->id);
$RAVADA->_process_all_requests_dont_fork(); wait_request();
ok($req->status('done'),"Request should be done, it is".$req->status); ok($req->status('done'),"Request should be done, it is".$req->status);
ok(!$req->error(),"Request error ".$req->error); ok(!$req->error(),"Request error ".$req->error);
...@@ -76,15 +77,8 @@ sub test_add_nic { ...@@ -76,15 +77,8 @@ sub test_add_nic {
my $vm_name = shift; my $vm_name = shift;
# diag("Testing add description $vm_name"); # diag("Testing add description $vm_name");
my $vm =rvd_back->search_vm($vm_name); my $vm = rvd_back->search_vm($vm_name);
my $domain = test_create_domain($vm_name); my $domain = test_create_domain($vm_name);
my $req = Ravada::Request->add_hardware(name => 'network',
, id_domain => $domain->id
, uid => $USER->id
);
wait_request();
is($req->error,"");
is($req->status,"done");
#Read xml #Read xml
sub read_mac{ sub read_mac{
...@@ -94,16 +88,16 @@ sub test_add_nic { ...@@ -94,16 +88,16 @@ sub test_add_nic {
my (@if_mac) = $xml->findnodes('/domain/devices/interface/mac'); my (@if_mac) = $xml->findnodes('/domain/devices/interface/mac');
for my $if_mac (@if_mac) { for my $if_mac (@if_mac) {
my $mac = $if_mac->getAttribute('address'); my $mac = $if_mac->getAttribute('address');
warn $mac; diag "\n$mac";
push @mac, $mac; push @mac, $mac;
} }
return(@mac); return(@mac);
} }
#Prepare base #Prepare base
test_req_prepare_base($domain->name); test_req_prepare_base($domain->name);
#Clone domain #Clone domain
my $name = new_domain_name(); my $name = new_domain_name();
my $domain_father = $domain; my $domain_father = $domain;
diag("requesting create domain $name, cloned from ".$domain_father->name); diag("requesting create domain $name, cloned from ".$domain_father->name);
......
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