<commit_before>use warnings;
use strict;

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

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

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

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

init($test->connector, $FILE_CONFIG);

my $USER = create_user('foo','bar');
my $TIMEOUT_SHUTDOWN = 10;

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


sub test_create_domain_xml {
    my $vm_name = shift;
    my $file_xml = shift;

    my $name = new_domain_name();

    die "Missing '$file_xml'" if !-e $file_xml;
    my $vm = rvd_back->search_vm($vm_name);

    my $device_disk = $vm->create_volume(
        name => $name
        ,xml => "etc/xml/dsl-volume.xml");
    ok($device_disk,"Expecting a device disk") or return;
    ok(-e $device_disk);


    my $xml = $vm->_define_xml($name, $file_xml);
    my @nodes;
    for my $device ( qw(image jpeg zlib playback streaming)) {
        my $path = "/domain/devices/graphics/$device";
        my ($node) = $xml->findnodes($path);
        is($node,undef,"Expecting no node $path");
        push @nodes, ( $node ) if $node;
    }
    return if @nodes;
    Ravada::VM::KVM::_xml_modify_disk($xml,[$device_disk]);
    my $dom;
    eval { $dom = $vm->vm->define_domain($xml) };
    ok(!$@,"Expecting error='' , got '".($@ or '')."'") or return
    ok($dom,"Expecting a VM defined from $file_xml") or return;

    my $domain = Ravada::Domain::KVM->new(domain => $dom, _vm => $vm);
    $domain->_insert_db(name=> $name, id_owner => $USER->id);

    return $domain;
}

sub test_drivers_type {
    my $vm_name = shift;
    my $file_xml = shift;
    my $type = shift;

    my $domain = test_create_domain_xml($vm_name, $file_xml) or return;

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

    my @drivers = $domain->drivers();
    ok(scalar @drivers,"Expecting defined drivers");
    isa_ok(\@drivers,'ARRAY');

    my $driver_type = $domain->drivers($type);

    my $value = $driver_type->get_value();
    is($value,undef,"Expecting no value for $type");

    $value = $domain->get_driver($type);
    is($value,undef,"Expecting no value for $type");

    my @options = $driver_type->get_options();
    isa_ok(\@options,'ARRAY');
    ok(scalar @options > 1,"Expecting more than 1 options , got ".scalar(@options));

    for my $option (@options) {
        _domain_shutdown($domain);

        diag("Setting $type $option->{value}");

        die "No value for driver ".Dumper($option)  if !$option->{value};
        eval { $domain->set_driver($type => $option->{value}) };
        ok(!$@,"Expecting no error, got : ".($@ or ''));
        is($domain->get_driver($type), $option->{value}) or next;

        _domain_shutdown($domain),
        is($domain->get_driver($type), $option->{value}) or next;

        {
            my $domain2 = $vm->search_domain($domain->name);
            my $value2 = $domain2->get_driver($type);
            is($value2 , $option->{value});
        }
        $domain->start($USER)   if !$domain->is_active;

        {
            my $domain2 = $vm->search_domain($domain->name);
            my $value2 = $domain2->get_driver($type);
            is($value2 , $option->{value});

        }

    }
}
<commit_msg>Remove the domain after test<commit_after>use warnings;
use strict;

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

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

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

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

init($test->connector, $FILE_CONFIG);

my $USER = create_user('foo','bar');
my $TIMEOUT_SHUTDOWN = 10;

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


sub test_create_domain_xml {
    my $vm_name = shift;
    my $file_xml = shift;

    my $name = new_domain_name();

    die "Missing '$file_xml'" if !-e $file_xml;
    my $vm = rvd_back->search_vm($vm_name);

    my $device_disk = $vm->create_volume(
        name => $name
        ,xml => "etc/xml/dsl-volume.xml");
    ok($device_disk,"Expecting a device disk") or return;
    ok(-e $device_disk);


    my $xml = $vm->_define_xml($name, $file_xml);
    my @nodes;
    for my $device ( qw(image jpeg zlib playback streaming)) {
        my $path = "/domain/devices/graphics/$device";
        my ($node) = $xml->findnodes($path);
        is($node,undef,"Expecting no node $path");
        push @nodes, ( $node ) if $node;
    }
    return if @nodes;
    Ravada::VM::KVM::_xml_modify_disk($xml,[$device_disk]);
    my $dom;
    eval { $dom = $vm->vm->define_domain($xml) };
    ok(!$@,"Expecting error='' , got '".($@ or '')."'") or return
    ok($dom,"Expecting a VM defined from $file_xml") or return;

    my $domain = Ravada::Domain::KVM->new(domain => $dom, _vm => $vm);
    $domain->_insert_db(name=> $name, id_owner => $USER->id);

    return $domain;
}

sub test_drivers_type {
    my $vm_name = shift;
    my $file_xml = shift;
    my $type = shift;

    my $domain = test_create_domain_xml($vm_name, $file_xml) or return;

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

    my @drivers = $domain->drivers();
    ok(scalar @drivers,"Expecting defined drivers");
    isa_ok(\@drivers,'ARRAY');

    my $driver_type = $domain->drivers($type);

    my $value = $driver_type->get_value();
    is($value,undef,"Expecting no value for $type");

    $value = $domain->get_driver($type);
    is($value,undef,"Expecting no value for $type");

    my @options = $driver_type->get_options();
    isa_ok(\@options,'ARRAY');
    ok(scalar @options > 1,"Expecting more than 1 options , got ".scalar(@options));

    for my $option (@options) {
        _domain_shutdown($domain);

        diag("Setting $type $option->{value}");

        die "No value for driver ".Dumper($option)  if !$option->{value};
        eval { $domain->set_driver($type => $option->{value}) };
        ok(!$@,"Expecting no error, got : ".($@ or ''));
        is($domain->get_driver($type), $option->{value}) or next;

        _domain_shutdown($domain),
        is($domain->get_driver($type), $option->{value}) or next;

        {
            my $domain2 = $vm->search_domain($domain->name);
            my $value2 = $domain2->get_driver($type);
            is($value2 , $option->{value});
        }
        $domain->start($USER)   if !$domain->is_active;

        {
            my $domain2 = $vm->search_domain($domain->name);
            my $value2 = $domain2->get_driver($type);
            is($value2 , $option->{value});

        }

    }
    $domain->remove($USER);
}

{'fraction_non_alphanumeric': 0.20134741999693767, 'fraction_numerical': 0.003981013627315878, 'mean_word_length': 3.1525746980292433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 36}, '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': '5786311', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2197, 'n_words': 489}