summaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/test/TestUtils/VespaTest.pm
blob: 68b822dceb14ccfbbfe3e148ab9ca0210002e5fc (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package TestUtils::VespaTest;

use Test::More;
use TestUtils::OutputCapturer;
use Yahoo::Vespa::Utils;

BEGIN {
    use base 'Exporter';
    our @EXPORT = qw(
        isOutput
        matchesOutput
        setApplication
        assertRun
        assertRunMatches
        printTest
        useColors
        setLocalHost
    );
}

my $APPLICATION;

&initialize();

return 1;

sub initialize {
    Yahoo::Vespa::Utils::initializeUnitTest(
            'testhost.yahoo.com', \&mockedExitHandler);
}

sub setLocalHost {
    my ($host) = @_;
    Yahoo::Vespa::Utils::initializeUnitTest(
            $host, \&mockedExitHandler);
}

sub useColors {
    TestUtils::OutputCapturer::useColors(@_);
}

sub mockedExitHandler {
    my ($exitcode) = @_;
    die "Application exited with exitcode $exitcode.";
}

sub setApplication {
    my ($main_func) = @_;
    $APPLICATION = $main_func;
}

sub assertRun {
    my ($testname, $argstring,
        $expected_exitcode, $expected_stdout, $expected_stderr) = @_;
    my $exitcode = &run($argstring);
    is( $exitcode, $expected_exitcode, "$testname - exitcode" );
    # print OutputCapturer::getStdOut();
    isOutput($expected_stdout, $expected_stderr, $testname);
}

sub assertRunMatches {
    my ($testname, $argstring,
        $expected_exitcode, $expected_stdout, $expected_stderr) = @_;
    my $exitcode = &run($argstring);
    is( $exitcode, $expected_exitcode, "$testname - exitcode" );
    # print OutputCapturer::getStdOut();
    matchesOutput($expected_stdout, $expected_stderr, $testname);
}

sub run {
    my ($argstring) = @_;
    my @args = split(/\s+/, $argstring);
    eval {
        Yahoo::Vespa::ArgParser::initialize();
        &$APPLICATION(\@args);
    };
    my $exitcode = 0;
    if ($@) {
        if ($@ =~ /Application exited with exitcode (\d+)\./) {
            $exitcode = 1;
        } else {
            print "Unknown die signal '" . $@ . "'\n";
        }
    }
    return $exitcode;
}

sub printTest {
    print "Test: ", @_;
}