forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-commands.t
executable file
·126 lines (107 loc) · 3.32 KB
/
07-commands.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl
#
# Copyright (c) 2016-2018 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 Test::Mojo;
use Devel::Cover;
use POSIX '_exit';
our $mojoport = Mojo::IOLoop::Server->generate_port;
my $base_url = "http://localhost:$mojoport";
sub wait_for_server {
my ($ua) = @_;
for (my $counter = 0; $counter < 20; $counter++) {
return if (($ua->get("$base_url/NEVEREVER")->res->code // 0) == 404);
sleep .1;
}
return 1;
}
$bmwqemu::vars{JOBTOKEN} = 'Hallo';
# now this is a game of luck
my ($cserver, $cfd) = commands::start_server($mojoport);
my $spid = fork();
if ($spid == 0) {
# we need to fake isotovideo here
while (1) {
my $json = myjsonrpc::read_json($cfd);
my $cmd = delete $json->{cmd};
if ($cmd eq 'version') {
myjsonrpc::send_json($cfd, {VERSION => 'COOL'});
}
elsif ($cmd) {
myjsonrpc::send_json($cfd, {response_for => $cmd, %$json});
}
}
_exit(0);
}
# create test user agent and wait for server
my $t = Test::Mojo->new;
if (wait_for_server($t->ua)) {
exit(0);
}
subtest 'failure if jobtoken wrong' => sub {
$t->get_ok("$base_url/NEVEREVER")->status_is(404);
$t->get_ok("$base_url/isotovideo/version")->status_is(404);
};
subtest 'query isotovideo version' => sub {
$t->get_ok("$base_url/Hallo/isotovideo/version");
$t->status_is(200);
# we only care whether 'json_cmd_token' exists
$t->json_has('/json_cmd_token');
delete $t->tx->res->json->{json_cmd_token};
$t->json_is({VERSION => 'COOL'});
};
subtest 'web socket route' => sub {
$t->websocket_ok("$base_url/Hallo/ws");
$t->send_ok(
{
json => {
cmd => 'set_pause_at_test',
name => 'installation-welcome',
}
},
'command passed to isotovideo'
);
$t->message_ok('result from isotovideo is passed back');
$t->json_message_is('/response_for' => 'set_pause_at_test');
$t->json_message_is('/name' => 'installation-welcome');
subtest 'broadcast messages to websocket clients' => sub {
my $t2 = Test::Mojo->new;
$t2->post_ok("$base_url/Hallo/broadcast", json => {
stopping_test_execution => 'foo',
});
$t2->status_is(200);
$t->message_ok('message from broadcast route received');
$t->json_message_is('/stopping_test_execution' => 'foo');
};
$t->finish_ok();
};
kill TERM => $spid;
waitpid($spid, 0);
eval { $cserver->stop() };
done_testing;