]> git.proxmox.com Git - pve-installer.git/blame - test/ui2-stdio.pl
tests: code-style and error handling fixes for ui2stdio
[pve-installer.git] / test / ui2-stdio.pl
CommitLineData
a054ca67
CH
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use JSON qw(from_json);
7use Test::More;
8
9use Proxmox::Log;
10use Proxmox::UI;
11
928de506
TL
12pipe(my $parent_reader, my $child_writer) || die "failed to create r/w pipe - $!\n";
13pipe(my $child_reader, my $parent_writer) || die "failed to create w/r pipe - $!\n";
a054ca67
CH
14
15$parent_writer->autoflush(1);
16$child_writer->autoflush(1);
17$child_reader->autoflush(1);
18
19
928de506
TL
20my $child_pid = fork() // die "fork failed - $!\n";
21
22if ($child_pid) {
a054ca67
CH
23 # parent, the hypothetical low-level installer
24 close($parent_reader);
25 close($parent_writer);
26
27 # "mock" stdin and stdout for Proxmox::UI::StdIO
28 *STDIN = $child_reader;
29 *STDOUT = $child_writer;
30
31 Proxmox::Log::init('&STDERR'); # log to stderr
32 Proxmox::UI::init_stdio({}, {});
33
34 Proxmox::UI::message('foo');
35 Proxmox::UI::error('bar');
36 is(Proxmox::UI::prompt('baz?'), 1, 'prompt should get positive answer');
37 is(Proxmox::UI::prompt('not baz? :('), '', 'prompt should get negative answer');
38
39 Proxmox::UI::finished(1, 'install successful');
40 Proxmox::UI::finished(0, 'install failed');
41
42 Proxmox::UI::progress(0.2, 0, 1, '20% done');
43 Proxmox::UI::progress(0.2, 0, 1);
44 Proxmox::UI::progress(0.99, 0, 1, '99% done');
45 Proxmox::UI::progress(1, 0, 1, 'done');
46
47 waitpid($child_pid, 0);
48 done_testing();
49} else {
50 # child, e.g. the TUI
a054ca67
CH
51 close($child_reader);
52 close($child_writer);
53
a054ca67
CH
54 my $next_msg = sub {
55 chomp(my $msg = <$parent_reader>);
56 return from_json($msg, { utf8 => 1 });
57 };
58
928de506
TL
59 is_deeply($next_msg->(), { type => 'message', message => 'foo' }, 'should receive message');
60 is_deeply($next_msg->(), { type => 'error', message => 'bar' }, 'should receive error');
a054ca67 61
928de506 62 is_deeply($next_msg->(), { type => 'prompt', query => 'baz?' }, 'prompt works');
a054ca67
CH
63 print $parent_writer "{\"type\":\"prompt-answer\",\"answer\":\"ok\"}\n";
64
928de506 65 is_deeply($next_msg->(), { type => 'prompt', query => 'not baz? :(' }, 'prompt works');
a054ca67
CH
66 print $parent_writer "{\"type\":\"prompt-answer\",\"answer\":\"cancel\"}\n";
67
68 is_deeply(
928de506 69 $next_msg->(), { type => 'finished', state => 'ok', message => 'install successful'},
a054ca67
CH
70 'should receive successful finished message');
71
72 is_deeply(
928de506 73 $next_msg->(), { type => 'finished', state => 'err', message => 'install failed'},
a054ca67
CH
74 'should receive failed finished message');
75
76 is_deeply(
928de506 77 $next_msg->(), { type => 'progress', ratio => 0.2, text => '20% done' },
a054ca67
CH
78 'should get 20% done progress message');
79
80 is_deeply(
928de506 81 $next_msg->(), { type => 'progress', ratio => 0.2, text => '' },
a054ca67
CH
82 'should get progress continuation message');
83
84 is_deeply(
928de506 85 $next_msg->(), { type => 'progress', ratio => 0.99, text => '99% done' },
a054ca67
CH
86 'should get 99% done progress message');
87
88 is_deeply(
928de506 89 $next_msg->(), { type => 'progress', ratio => 1, text => 'done' },
a054ca67
CH
90 'should get 100% done progress message');
91
92 done_testing();
93 exit(0);
94}
95