]> git.proxmox.com Git - pve-common.git/blame - test/format_test.pl
cgroup: cpu quota: fix resetting period length for v1
[pve-common.git] / test / format_test.pl
CommitLineData
e37d5924
DJ
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use lib '../src';
7use PVE::JSONSchema;
57b33852 8use PVE::CLIFormatter;
e37d5924
DJ
9
10use Test::More;
11use Test::MockModule;
12
13my $valid_configids = [
14 'aa', 'a0', 'a_', 'a-', 'a-a', 'a'x100, 'Aa', 'AA',
15];
16my $invalid_configids = [
17 'a', 'a+', '1a', '_a', '-a', '+a', 'A',
18];
19
20my $noerr = 1; # easier to test
21foreach my $id (@$valid_configids) {
22 is(PVE::JSONSchema::pve_verify_configid($id, $noerr), $id, 'valid configid');
23}
24foreach my $id (@$invalid_configids) {
25 is(PVE::JSONSchema::pve_verify_configid($id, $noerr), undef, 'invalid configid');
26}
27
57b33852
SR
28# test some string rendering
29my $render_data = [
30 ["timestamp", 0, undef, "1970-01-01 01:00:00"],
31 ["timestamp", 1612776831, undef, "2021-02-08 10:33:51"],
32 ["timestamp_gmt", 0, undef, "1970-01-01 00:00:00"],
33 ["timestamp_gmt", 1612776831, undef, "2021-02-08 09:33:51"],
4997835b
SR
34 ["duration", undef, undef, "0s"],
35 ["duration", 0.3, undef, "0s"],
36 ["duration", 0, undef, "0s"],
57b33852 37 ["duration", 40, undef, "40s"],
4997835b 38 ["duration", 59.64432, undef, "1m"],
57b33852
SR
39 ["duration", 110, undef, "1m 50s"],
40 ["duration", 7*24*3829*2, undef, "2w 21h 22m 24s"],
41 ["fraction_as_percentage", 0.412, undef, "41.20%"],
42 ["bytes", 0, undef, "0.00 B"],
43 ["bytes", 1023, 4, "1023.0000 B"],
44 ["bytes", 1024, undef, "1.00 KiB"],
45 ["bytes", 1024*1024*123 + 1024*300, 1, "123.3 MiB"],
46 ["bytes", 1024*1024*1024*1024*4 + 1024*1024*2048*8, undef, "4.02 TiB"],
47];
48
49foreach my $data (@$render_data) {
50 my ($renderer_name, $p1, $p2, $expected) = @$data;
51 my $renderer = PVE::JSONSchema::get_renderer($renderer_name);
52 my $actual = $renderer->($p1, $p2);
53 is($actual, $expected, "string format '$renderer_name'");
54}
55
56done_testing();