<commit_before>use warnings;
use strict;

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

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

my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');

use_ok('Ravada');

my $FILE_CONFIG = 't/etc/ravada.conf';

my $RVD_BACK = rvd_back($test->connector, $FILE_CONFIG);
my $RVD_FRONT= rvd_front($test->connector, $FILE_CONFIG);

my %ARG_CREATE_DOM = (
      KVM => [ id_iso => 1 ]
    ,Void => [ ]
);

my @ARG_RVD = ( config => $FILE_CONFIG,  connector => $test->connector);

my @VMS = reverse keys %ARG_CREATE_DOM;
my $USER = create_user("foo","bar");

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

sub test_create_domain {
    my $vm_name = shift;

    my $ravada = Ravada->new(@ARG_RVD);
    my $vm = $ravada->search_vm($vm_name);
    ok($vm,"I can't find VM $vm_name") or return;

    my $name = new_domain_name();

    if (!$ARG_CREATE_DOM{$vm_name}) {
        diag("VM $vm_name should be defined at \%ARG_CREATE_DOM");
        return;
    }
    my @arg_create = @{$ARG_CREATE_DOM{$vm_name}};

    my $domain;
    eval { $domain = $vm->create_domain(name => $name
                    , id_owner => $USER->id
                    , @{$ARG_CREATE_DOM{$vm_name}})
    };

    ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or exit;
    ok($domain->name
        && $domain->name eq $name,"Expecting domain name '$name' , got "
        .($domain->name or '<UNDEF>')
        ." for VM $vm_name"
    );

    return $domain;
}
sub test_clone {
    my ($vm_name, $base) = @_;
                my $clone1;
                my $name_clone = new_domain_name();
#                diag("[$vm_name] Cloning from base ".$base->name." to $name_clone");
                eval { $clone1 = $base->clone(name => $name_clone, user => $USER) };
                ok(!$@,"Expecting error='', got='".($@ or '')."'");
                ok($clone1,"Expecting new cloned domain from ".$base->name) or last;

                $clone1->shutdown_now($USER) if $clone1->is_active();
                eval { $clone1->start($USER) };
                is($@,'');
                ok($clone1->is_active);

                my $clone1b = $RVD_FRONT->search_domain($name_clone);
                ok($clone1b,"Expecting new cloned domain ".$name_clone);
                $clone1->shutdown_now($USER) if $clone1->is_active;
                ok(!$clone1->is_active);
    return $clone1;
}

sub test_mess_with_bases {
    my ($vm_name, $base, $clones) = @_;
    for my $clone (@$clones) {
        $clone->shutdown(user => $USER, timeout => 1)   if $clone->is_active;
        ok($clone->id_base,"Expecting clone has id_base , got "
                .($clone->id_base or '<UNDEF>'));
        $clone->prepare_base($USER);
    }

    $base->remove_base($USER);
    is($base->is_base,0);

    for my $clone (@$clones) {
        eval { $clone->start($USER); };
        ok(!$@,"Expecting error: '' , got: ".($@ or '')) or exit;

        ok($clone->is_active);
        $clone->shutdown(user => $USER, timeout => 1)   if $clone->is_active;

        $clone->remove_base($USER);
        eval { $clone->start($USER); };
        ok(!$@,"[$vm_name] Expecting error: '' , got '".($@ or '')."'");
        ok($clone->is_active);
        $clone->shutdown(user => $USER, timeout => 1);

    }<commit_msg>[#275] testing the description in base and clone<commit_after>use warnings;
use strict;

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

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

my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');

use_ok('Ravada');

my $FILE_CONFIG = 't/etc/ravada.conf';

my $RVD_BACK = rvd_back($test->connector, $FILE_CONFIG);
my $RVD_FRONT= rvd_front($test->connector, $FILE_CONFIG);

my %ARG_CREATE_DOM = (
      KVM => [ id_iso => 1 ]
    ,Void => [ ]
);

my @ARG_RVD = ( config => $FILE_CONFIG,  connector => $test->connector);

my @VMS = reverse keys %ARG_CREATE_DOM;
my $USER = create_user("foo","bar");

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

sub test_create_domain {
    my $vm_name = shift;

    my $ravada = Ravada->new(@ARG_RVD);
    my $vm = $ravada->search_vm($vm_name);
    ok($vm,"I can't find VM $vm_name") or return;

    my $name = new_domain_name();

    if (!$ARG_CREATE_DOM{$vm_name}) {
        diag("VM $vm_name should be defined at \%ARG_CREATE_DOM");
        return;
    }
    my @arg_create = @{$ARG_CREATE_DOM{$vm_name}};

    my $domain;
    eval { $domain = $vm->create_domain(name => $name
                    , id_owner => $USER->id
                    , @{$ARG_CREATE_DOM{$vm_name}})
    };

    ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or exit;
    ok($domain->name
        && $domain->name eq $name,"Expecting domain name '$name' , got "
        .($domain->name or '<UNDEF>')
        ." for VM $vm_name"
    );

    return $domain;
}
sub test_clone {
    my ($vm_name, $base) = @_;
    my $description = "The description text $$";
    $base->description($description);
    is($base->description,$description);

                my $clone1;
                my $name_clone = new_domain_name();
#                diag("[$vm_name] Cloning from base ".$base->name." to $name_clone");
                eval { $clone1 = $base->clone(name => $name_clone, user => $USER) };
                ok(!$@,"Expecting error='', got='".($@ or '')."'");
                ok($clone1,"Expecting new cloned domain from ".$base->name) or last;

    is($clone1->description,$description);
                $clone1->shutdown_now($USER) if $clone1->is_active();
                eval { $clone1->start($USER) };
                is($@,'');
                ok($clone1->is_active);

                my $clone1b = $RVD_FRONT->search_domain($name_clone);
                ok($clone1b,"Expecting new cloned domain ".$name_clone);
                $clone1->shutdown_now($USER) if $clone1->is_active;
                ok(!$clone1->is_active);
    is($clone1b->description,$description);
    return $clone1;
}

sub test_mess_with_bases {
    my ($vm_name, $base, $clones) = @_;
    for my $clone (@$clones) {
        $clone->shutdown(user => $USER, timeout => 1)   if $clone->is_active;
        ok($clone->id_base,"Expecting clone has id_base , got "
                .($clone->id_base or '<UNDEF>'));
        $clone->prepare_base($USER);
    }

    $base->remove_base($USER);
    is($base->is_base,0);

    for my $clone (@$clones) {
        eval { $clone->start($USER); };
        ok(!$@,"Expecting error: '' , got: ".($@ or '')) or exit;

        ok($clone->is_active);
        $clone->shutdown(user => $USER, timeout => 1)   if $clone->is_active;

        $clone->remove_base($USER);
        eval { $clone->start($USER); };
        ok(!$@,"[$vm_name] Expecting error: '' , got '".($@ or '')."'");
        ok($clone->is_active);
        $clone->shutdown(user => $USER, timeout => 1);

    }
{'fraction_non_alphanumeric': 0.2094976111191545, 'fraction_numerical': 0.005936006949471551, 'mean_word_length': 2.6281512605042017, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4962075', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2296, 'n_words': 484}