public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 4/6] test: add tests for UI^2 stdio protocol
Date: Wed,  6 Dec 2023 12:34:53 +0100	[thread overview]
Message-ID: <20231206113456.411898-5-c.heiss@proxmox.com> (raw)
In-Reply-To: <20231206113456.411898-1-c.heiss@proxmox.com>

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
 test/Makefile     |  6 ++-
 test/ui2-stdio.pl | 96 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 1 deletion(-)
 create mode 100755 test/ui2-stdio.pl

diff --git a/test/Makefile b/test/Makefile
index fb80fc4..eef8830 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -3,8 +3,12 @@ all:
 export PERLLIB=..
 
 .PHONY: check
-check: test-zfs-arc-max
+check: test-zfs-arc-max test-ui2-stdio
 
 .PHONY: test-zfs-arc-max
 test-zfs-arc-max:
 	./zfs-arc-max.pl
+
+.PHONY: test-ui2-stdio
+test-ui2-stdio:
+	./ui2-stdio.pl
diff --git a/test/ui2-stdio.pl b/test/ui2-stdio.pl
new file mode 100755
index 0000000..f1c38e8
--- /dev/null
+++ b/test/ui2-stdio.pl
@@ -0,0 +1,96 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use JSON qw(from_json);
+use Test::More;
+
+use Proxmox::Log;
+use Proxmox::UI;
+
+pipe(my $parent_reader, my $child_writer) || die;
+pipe(my $child_reader, my $parent_writer) || die;
+
+$parent_writer->autoflush(1);
+$child_writer->autoflush(1);
+$child_reader->autoflush(1);
+
+
+if (my $child_pid = fork()) {
+    # parent, the hypothetical low-level installer
+    close($parent_reader);
+    close($parent_writer);
+
+    # "mock" stdin and stdout for Proxmox::UI::StdIO
+    *STDIN = $child_reader;
+    *STDOUT = $child_writer;
+
+    Proxmox::Log::init('&STDERR'); # log to stderr
+    Proxmox::UI::init_stdio({}, {});
+
+    Proxmox::UI::message('foo');
+    Proxmox::UI::error('bar');
+    is(Proxmox::UI::prompt('baz?'), 1, 'prompt should get positive answer');
+    is(Proxmox::UI::prompt('not baz? :('), '', 'prompt should get negative answer');
+
+    Proxmox::UI::finished(1, 'install successful');
+    Proxmox::UI::finished(0, 'install failed');
+
+    Proxmox::UI::progress(0.2, 0, 1, '20% done');
+    Proxmox::UI::progress(0.2, 0, 1);
+    Proxmox::UI::progress(0.99, 0, 1, '99% done');
+    Proxmox::UI::progress(1, 0, 1, 'done');
+
+    waitpid($child_pid, 0);
+    done_testing();
+} else {
+    # child, e.g. the TUI
+    die 'failed to fork?' if !defined($child_pid);
+    close($child_reader);
+    close($child_writer);
+
+    use Test::More;
+
+    my $next_msg = sub {
+	chomp(my $msg = <$parent_reader>);
+	return from_json($msg, { utf8 => 1 });
+    };
+
+    is_deeply(&$next_msg(), { type => 'message', message => 'foo' }, 'should receive message');
+    is_deeply(&$next_msg(), { type => 'error', message => 'bar' }, 'should receive error');
+
+    is_deeply(&$next_msg(), { type => 'prompt', query => 'baz?' }, 'prompt works');
+    print $parent_writer "{\"type\":\"prompt-answer\",\"answer\":\"ok\"}\n";
+
+    is_deeply(&$next_msg(), { type => 'prompt', query => 'not baz? :(' }, 'prompt works');
+    print $parent_writer "{\"type\":\"prompt-answer\",\"answer\":\"cancel\"}\n";
+
+    is_deeply(
+	&$next_msg(), { type => 'finished', state => 'ok', message => 'install successful'},
+	'should receive successful finished message');
+
+    is_deeply(
+	&$next_msg(), { type => 'finished', state => 'err', message => 'install failed'},
+	'should receive failed finished message');
+
+    is_deeply(
+	&$next_msg(), { type => 'progress', ratio => 0.2, text => '20% done' },
+	'should get 20% done progress message');
+
+    is_deeply(
+	&$next_msg(), { type => 'progress', ratio => 0.2, text => '' },
+	'should get progress continuation message');
+
+    is_deeply(
+	&$next_msg(), { type => 'progress', ratio => 0.99, text => '99% done' },
+	'should get 99% done progress message');
+
+    is_deeply(
+	&$next_msg(), { type => 'progress', ratio => 1, text => 'done' },
+	'should get 100% done progress message');
+
+    done_testing();
+    exit(0);
+}
+
-- 
2.42.0





  parent reply	other threads:[~2023-12-06 11:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-06 11:34 [pve-devel] [PATCH installer 0/6] switch low-level installer protocol to json Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 1/6] low-level: align wording of finish message Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 2/6] ui: stdio: log error if display_html() is called on stdio backend Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 3/6] tui, ui: switch over to JSON-based protocol Christoph Heiss
2024-02-24 16:55   ` Thomas Lamprecht
2023-12-06 11:34 ` Christoph Heiss [this message]
2023-12-06 11:34 ` [pve-devel] [PATCH installer 5/6] buildsys: setup proper test environment for testsuite Christoph Heiss
2023-12-06 11:34 ` [pve-devel] [PATCH installer 6/6] tui: install progress: add tests for UI^2 stdio protocol Christoph Heiss
2024-01-24  9:54 ` [pve-devel] [PATCH installer 0/6] switch low-level installer protocol to json Christoph Heiss
2024-02-26 14:07 ` [pve-devel] applied-series: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231206113456.411898-5-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal