File: 07-commands.t

package info (click to toggle)
os-autoinst 4.5.1527308405.8b586d5-4.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,688 kB
  • sloc: perl: 10,424; cpp: 1,527; python: 217; makefile: 211; sh: 71; xml: 11
file content (89 lines) | stat: -rwxr-xr-x 2,256 bytes parent folder | download
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
#!/usr/bin/perl
#
# Copyright (c) 2016 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.


use strict;
use warnings;

use FindBin;
use File::Find;
require IPC::System::Simple;
use autodie ':all';


BEGIN {
    unshift @INC, '..';
}

use commands;
use Mojo::IOLoop::Server;
use Time::HiRes 'sleep';
use Test::More;
use Test::Warnings;
use POSIX '_exit';

our $mojoport = Mojo::IOLoop::Server->generate_port;

sub wait_for_server {
    my ($ua) = @_;
    for (my $counter = 0; $counter < 20; $counter++) {
        return if (($ua->get("http://localhost:$mojoport/NEVEREVER")->res->code // 0) == 404);
        sleep .1;
    }
    return 1;
}

$bmwqemu::vars{JOBTOKEN} = 'Hallo';

# now this is a game of luck
my ($cpid, $cfd) = commands::start_server($mojoport);

my $spid = fork();
if ($spid == 0) {
    # we need to fake isotovideo here
    my $h = myjsonrpc::read_json($cfd);
    if ($h->{cmd} eq 'version') {
        myjsonrpc::send_json($cfd, {VERSION => 'COOL'});
    }
    _exit(0);
}

my $ua = Mojo::UserAgent->new;
if (wait_for_server($ua)) {
    exit(0);
}

is($ua->get("http://localhost:$mojoport/NEVEREVER")->res->code,          404);
is($ua->get("http://localhost:$mojoport/isotovideo/version")->res->code, 404);
my $get = $ua->get("http://localhost:$mojoport/Hallo/isotovideo/version");
is($get->res->code, 200);
my $json = $get->res->json;
# we only care for its existance
ok(defined $json->{json_cmd_token});
delete $json->{json_cmd_token};
is_deeply($json, {VERSION => 'COOL'});

done_testing;

END {
    return unless $spid;
    kill TERM => $spid;
    waitpid($spid, 0);
    kill TERM => $cpid;
    waitpid($cpid, 0);
    wait_for_server($ua);
}