72_add_nic.t 3.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use warnings;
use strict;

use Data::Dumper;
use Test::More;

use lib 't/lib';
use Test::Ravada;
use XML::LibXML;


use_ok('Ravada');
use_ok('Ravada::Request');

init();
my @VMS = vm_names();
my $USER = create_user("foo","bar", 1);
our $TIMEOUT_SHUTDOWN = 10;

#TODO use config file for DIR_XML
our $DIR_XML = "etc/xml";
$DIR_XML = "/var/lib/ravada/xml/" if $0 =~ m{^/usr/sbin};

our $XML = XML::LibXML->new();
my $RAVADA;
my $BACKEND = 'KVM';

################################################################
sub test_create_domain {
    my $vm_name = shift;
    my $vm = rvd_back->search_vm($vm_name);
    ok($vm,"I can't find VM $vm_name") or return;

    my $name = new_domain_name();

    my $domain;
37
    $domain = $vm->create_domain(name => $name
38
39
                    , id_owner => $USER->id
                    , disk => 1024 * 1024
40
41
42
43
                    , arg_create_dom($vm_name));

	my $req = Ravada::Request->add_hardware(name => 'network', id_domain => $domain->id, uid => user_admin->id);
	wait_request();
44

45
	ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or return;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    ok($domain->name
        && $domain->name eq $name,"Expecting domain name '$name' , got "
        .($domain->name or '<UNDEF>')
        ." for VM $vm_name"
    );

    return $domain;
}

sub test_req_prepare_base{
    my $name = shift;

    my $domain0 = $RAVADA->search_domain($name);
    ok(!$domain0->is_base,"Domain $name should not be base");
fv3rdugo's avatar
fv3rdugo committed
60

61
62
	my $req = Ravada::Request->prepare_base(id_domain => $domain0->id, uid => user_admin->id);
    wait_request();
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    ok($req->status('done'),"Request should be done, it is".$req->status);
    ok(!$req->error(),"Request error ".$req->error);

    my $domain = $RAVADA->search_domain($name);
    ok($domain->is_base,"Domain $name should be base");
    ok(scalar $domain->list_files_base," Domain $name should have files_base, got ".
        scalar $domain->list_files_base);

    $domain->is_public(1);
}


sub test_add_nic {
    my $vm_name = shift;

#    diag("Testing add description $vm_name");
80
    my $vm = rvd_back->search_vm($vm_name);
81
    my $domain = test_create_domain($vm_name);
fv3rdugo's avatar
fv3rdugo committed
82

83
84
85
86
    #Read xml
    sub read_mac{
        my $domain = shift;
        my $xml = XML::LibXML->load_xml(string => $domain->get_xml_base());
fv3rdugo's avatar
fv3rdugo committed
87
        my @mac;
88
89
90
        my (@if_mac) = $xml->findnodes('/domain/devices/interface/mac');
        for my $if_mac (@if_mac) {
            my $mac = $if_mac->getAttribute('address');
91
            diag "\n$mac";
92
93
94
95
            push @mac, $mac;
        }
        return(@mac);
    }
fv3rdugo's avatar
fv3rdugo committed
96

97
    #Prepare base
98
	test_req_prepare_base($domain->name);
99

100
	#Clone domain
101
102
103
104
105
106
107
108
109
110
111
112
    my $name = new_domain_name();
    my $domain_father = $domain;
    diag("requesting create domain $name, cloned from ".$domain_father->name);
    my $req2 = Ravada::Request->create_domain(
        name => $name
        ,id_base => $domain_father->id
       ,id_owner => user_admin->id
        ,vm => $BACKEND
    );
    wait_request();
    is($req2->error,"");
    is($req2->status,"done");
fv3rdugo's avatar
fv3rdugo committed
113

114
    my $domain_clon = $RAVADA->search_domain($name);
fv3rdugo's avatar
fv3rdugo committed
115

116
117
    my @mac = read_mac($domain);
    my @mac2 = read_mac($domain_clon);
fv3rdugo's avatar
fv3rdugo committed
118
119
120
    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");

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
}


#######################################################

#######################################################

clean();
eval { $RAVADA = rvd_back() };
ok($RAVADA,"I can't launch a new Ravada");# or exit;

my $vm_name = 'KVM';
my $vm = rvd_back->search_vm($vm_name);

SKIP: {

    my $msg = "SKIPPED: No virtual managers found";
    if ($vm && $vm_name =~ /kvm/i && $>) {
        $msg = "SKIPPED: Test must run as root";
        $vm = undef;
    }
    skip($msg,10)   if !$vm;

    test_add_nic($vm_name);
}

fv3rdugo's avatar
fv3rdugo committed
147
clean();
148
149

done_testing();