forked from os-autoinst/os-autoinst-distri-opensuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_utils_auxiliary.t
57 lines (46 loc) · 2.9 KB
/
05_utils_auxiliary.t
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use strict;
use warnings;
use Test::More;
use Test::Warnings;
use Test::MockModule;
use Test::Exception;
use testapi;
use utils;
## Test auxiliary routines.
# Add additional unit tests for auxiliary subroutines (e.g. util.pm) here.
subtest 'script_retry' => sub {
# Override script_run
my $testapi = Test::MockModule->new('utils');
# script_run runs the commands on the local machine as bash
$testapi->redefine("script_run", sub { return system("bash -c '$_[0]'"); });
is script_retry('true', retry => 2, delay => 0, timeout => 1), 0, "script_retry(true)";
is script_retry('echo Hello', retry => 2, delay => 0, timeout => 1), 0, "script_retry(echo)";
isnt script_retry('false', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry(false)";
dies_ok { script_retry('false', retry => 2, delay => 0, timeout => 1) } 'script_retry(false) is expected to die';
isnt script_retry('! true', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry('! true')";
isnt script_retry('!true', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry('!true')";
is script_retry('!false', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry('!false')";
is script_retry('! false', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry('! false')";
is script_retry('! false', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry('! false')";
# This fails on first run but succeeds on second run. Test if we are actually retrying
is script_retry('rm -f test', retry => 1, delay => 0, timeout => 1), 0, 'removing test file';
is script_retry('bash -c "if [[ -f test ]]; then exit 0; else touch test; exit 1; fi"', retry => 2, delay => 0, timeout => 1, die => 0), 0, "script_retry - OK on second try";
# Note: This is the only test that waits for one second. Disable if time is crucial.
dies_ok { script_retry('sleep 10', retry => 1, delay => 0, timeout => 1) } 'script_retry(sleep) is expected to die';
};
subtest 'validate_script_output_retry' => sub {
my $module = Test::MockModule->new('testapi');
my $basetest = Test::MockModule->new('basetest');
$basetest->noop('record_resultfile');
$autotest::current_test = new basetest;
$module->mock('script_output', sub { 'foo' });
lives_ok { validate_script_output_retry('echo foo', qr/foo/, retry => 2) } 'Do not throw exception';
throws_ok { validate_script_output_retry('echo foo', qr/bar/, retry => 2, delay => 0) } qr/validate output/, 'Exception thrown';
my @results;
$module->mock('script_output', sub { shift @results });
@results = qw(1 2 3 foo);
lives_ok { validate_script_output_retry('echo foo', qr/foo/, retry => 4, delay => 0) } 'Success on 4th retry';
@results = qw(1 2 3 foo);
throws_ok { validate_script_output_retry('echo foo', qr/foo/, retry => 3, delay => 0) } qr/validate output/, 'Not enough retries';
};
done_testing;