From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <c.heiss@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 3E37FB8BDA
 for <pve-devel@lists.proxmox.com>; Wed,  6 Dec 2023 12:35:44 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 1F9016DB
 for <pve-devel@lists.proxmox.com>; Wed,  6 Dec 2023 12:35:14 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Wed,  6 Dec 2023 12:35:13 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 0576241025
 for <pve-devel@lists.proxmox.com>; Wed,  6 Dec 2023 12:35:13 +0100 (CET)
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed,  6 Dec 2023 12:34:53 +0100
Message-ID: <20231206113456.411898-5-c.heiss@proxmox.com>
X-Mailer: git-send-email 2.42.0
In-Reply-To: <20231206113456.411898-1-c.heiss@proxmox.com>
References: <20231206113456.411898-1-c.heiss@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.003 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pve-devel] [PATCH installer 4/6] test: add tests for UI^2 stdio
 protocol
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 06 Dec 2023 11:35:44 -0000

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