Files
proxmox-git-pve-common/test/format_test.pl
T
dualfroz dccc97a0ea cli formatter: decode the output of the yaml renderer
YAML::XS::Dump returns UTF-8 encoded octets, but the value returned by
a renderer gets encoded when the text output is printed, like any other
value. So for properties using the 'yaml' renderer, for example the
account details shown by 'pvenode acme account info', any non-ASCII
character got encoded twice and ended up mangled.

Decode the output of YAML::XS::Dump in the renderer, so it returns a
character string like all other renderers, and add a test for it.

Fixes: 99d0252 ("properly encode YAML via YAML::XS")
Signed-off-by: Michal Fox <me@dualfroz.com>
2026-10-02 16:25:55 +02:00

73 lines
2.4 KiB
Perl

#!/usr/bin/perl
use strict;
use warnings;
use lib '../src';
use PVE::JSONSchema;
use PVE::CLIFormatter;
use Encode qw(decode);
use Test::More;
use Test::MockModule;
my $valid_configids = [
'aa', 'a0', 'a_', 'a-', 'a-a', 'a' x 100, 'Aa', 'AA',
];
my $invalid_configids = [
'a', 'a+', '1a', '_a', '-a', '+a', 'A',
];
my $noerr = 1; # easier to test
foreach my $id (@$valid_configids) {
is(PVE::JSONSchema::pve_verify_configid($id, $noerr), $id, 'valid configid');
}
foreach my $id (@$invalid_configids) {
is(PVE::JSONSchema::pve_verify_configid($id, $noerr), undef, 'invalid configid');
}
# test some string rendering
my $render_data = [
["timestamp", 0, undef, "1970-01-01 01:00:00"],
["timestamp", 1612776831, undef, "2021-02-08 10:33:51"],
["timestamp_gmt", 0, undef, "1970-01-01 00:00:00"],
["timestamp_gmt", 1612776831, undef, "2021-02-08 09:33:51"],
["duration", undef, undef, "0s"],
["duration", 0.3, undef, "0s"],
["duration", 0, undef, "0s"],
["duration", 40, undef, "40s"],
["duration", 59.64432, undef, "1m"],
["duration", 110, undef, "1m 50s"],
["duration", 7 * 24 * 3829 * 2, undef, "2w 21h 22m 24s"],
["fraction_as_percentage", 0.412, undef, "41.20%"],
["bytes", 0, undef, "0.00 B"],
["bytes", 1023, 4, "1023.0000 B"],
["bytes", 1024, undef, "1.00 KiB"],
["bytes", 1024 * 1024 * 123 + 1024 * 300, 1, "123.3 MiB"],
["bytes", 1024 * 1024 * 1024 * 1024 * 4 + 1024 * 1024 * 2048 * 8, undef, "4.02 TiB"],
["yaml", { description => "Erkl\x{e4}rung" }, undef, "description: Erkl\x{e4}rung\n"],
];
foreach my $data (@$render_data) {
my ($renderer_name, $p1, $p2, $expected) = @$data;
my $renderer = PVE::JSONSchema::get_renderer($renderer_name);
my $actual = $renderer->($p1, $p2);
is($actual, $expected, "string format '$renderer_name'");
}
# test that non-ASCII characters survive the structured output formats
my $unicode_data = { description => "Erkl\x{e4}rung" };
for my $format (qw(json yaml)) {
open(my $fh, '>', \my $output) or die "failed to open in-memory file - $!\n";
my $stdout = select($fh);
PVE::CLIFormatter::print_api_result(
$unicode_data, undef, undef, { 'output-format' => $format }, {},
);
select($stdout);
close($fh);
like(decode('UTF-8', $output), qr/Erkl\x{e4}rung/, "output format '$format' is UTF-8 encoded");
}
done_testing();