Debian Codegraph @

lemonldap-ng-manager/t/16-cli.t

raw
1 use warnings;
2 use File::Copy;
3 use File::Temp 'tempdir';
4 use Test::More;
5 use Test::Output;
6 use JSON;
7 use strict;
8 require 't/test-lib.pm';
9
10 my $tests = 23;
11
12 use_ok('Lemonldap::NG::Common::Cli');
13 use_ok('Lemonldap::NG::Manager::Cli');
14 &cleanConfFiles;
15
16 sub llclient {
17 return Lemonldap::NG::Manager::Cli->new(
18 iniFile => "$main::tmpdir/lemonldap-ng.ini" );
19 }
20
21 sub llcommonClient {
22 return Lemonldap::NG::Common::Cli->new(
23 iniFile => "$main::tmpdir/lemonldap-ng.ini" );
24 }
25
26 my @cmd;
27 my $res;
28
29 # Test 'set' command
30 @cmd = qw(-yes 1 set notification 1);
31 combined_like( sub { llclient->run(@cmd) }, qr/Saved under/, '"addKey" OK' );
32
33 # Test 'get' command
34 @cmd = qw(get notification);
35 $res = Test::Output::stdout_from( sub { llclient->run(@cmd) } );
36 ok( $res =~ /^notification\s+=\s+1$/, '"get notification" OK' )
37 or diag " $res";
38
39 # Test 'addKey' command
40 @cmd = qw(-yes 1 addKey locationRules/test1.example.com ^/reject deny);
41 combined_like( sub { llclient->run(@cmd) }, qr/Saved under/, '"addKey" OK' );
42
43 # Test 'addKey' command with negative number, to make sure the option parser
44 # does not interfere (#3495)
45 @cmd = qw(-yes 1 addKey vhostOptions/test1.example.com vhostServiceTokenTTL -1);
46 combined_like( sub { llclient->run(@cmd) }, qr/Saved under/, '"addKey" OK' );
47
48 # Test 'delKey' command
49 @cmd = qw(-yes 1 delKey locationRules/test1.example.com ^/reject);
50 combined_unlike(
51 sub { llclient->run(@cmd) },
52 qr#'\^/reject' => 'deny'#s,
53 '"delKey" OK'
54 );
55
56 # Test 'get' command with key/subkey
57 @cmd = qw(get locationRules/test1.example.com/default);
58 $res = Test::Output::stdout_from( sub { llclient->run(@cmd) } );
59 ok( $res =~ m#accept#, '"get key/subkey" OK' )
60 or diag "$res";
61
62 # Test 'set' command with key/subkey
63 @cmd = qw(-yes 1 set locationRules/test1.example.com/default deny);
64 combined_like( sub { llclient->run(@cmd) }, qr/Saved under/, '"addKey" OK' );
65
66 # Test 'save' command
67 @cmd = qw(-cfgNum 1 save);
68 $res = Test::Output::stdout_from( sub { llclient->run(@cmd) } );
69 ok( $res =~ /^\s*(\{.*\})\s*$/s, '"save" result looks like JSON' );
70 my $j;
71 eval { $j = JSON::from_json($res) };
72 is( $j->{cfgNum}, 1, "correct version number" );
73 ok( not($@), ' result is JSON' ) or diag "error: $@";
74
75 # Test 'restore' command
76 my $tmpFile = File::Temp->new();
77 print $tmpFile $res;
78 @cmd = ( 'restore', $tmpFile->filename );
79 combined_like(
80 sub { llclient->run(@cmd) },
81 qr/Configuration has been restored as version \d*/s,
82 'New config'
83 );
84
85 # Test 'set' command with force
86 @cmd = qw(-yes 1 -force 1 -cfgNum 2 set useSafeJail 0);
87 combined_like(
88 sub { llclient->run(@cmd) },
89 qr#cfgNum forced with 2#s,
90 '"Force cfgNum" OK'
91 );
92
93 # Test 'info' command with force
94 @cmd = qw(info);
95 combined_like(
96 sub { llcommonClient->run(@cmd) },
97 qr#\bAuthor IP\b#s,
98 '"Author IP" OK'
99 );
100 combined_like( sub { llcommonClient->run(@cmd) }, qr#\bLog\b#s, '"Log" OK' );
101 combined_like( sub { llcommonClient->run(@cmd) },
102 qr#\bVersion\b#s, '"Version" OK' );
103
104 # Test 'rollback' command
105 @cmd = qw(rollback);
106 combined_like(
107 sub { llclient->run(@cmd) },
108 qr/Configuration \d+ has been rolled back/,
109 'Configuration rollback OK'
110 );
111
112 @cmd = qw(-yes 1 merge t/test-merge.json);
113 combined_like(
114 sub { llclient->run(@cmd) },
115 qr/Saved under number \d+/,
116 'Configuration merge OK'
117 );
118
119 # Test 'purge' command
120 my $client = llclient;
121 my $clientConfAcc = llclient->mgr->_confAcc;
122
123 # Generate 3 forged configs
124 &genConfFiles;
125 @cmd = qw(-yes 1 purge --keep-last 2);
126 combined_like(
127 sub { $client->run(@cmd) },
128
129 # If there are 3 configs and we want to keep the last 2, one config is deleted
130 qr/Deleted 1 configs/,
131 'Configuration purge --keep-last OK'
132 );
133 is( $clientConfAcc->available(),
134 2, 'Correct number of configurations after purge last' );
135
136 # Generate 3 forged configs
137 &genConfFiles;
138 $client = llclient;
139 @cmd = qw(-yes 1 purge --keep-recent 5);
140 combined_like(
141 sub { $client->run(@cmd) },
142 qr/Deleted 2 configs/,
143 'Configuration purge --keep-recent OK'
144 );
145
146 is( $clientConfAcc->available(),
147 1, 'Correct number of configurations after purge recent' );
148
149 count($tests);
150 done_testing( count() );
151
152 sub cleanConfFiles {
153 foreach ( 2 .. $tests - 3 ) {
154 unlink "$main::tmpdir/conf/lmConf-$_.json";
155 }
156 }
157
158 # Sub to delete all existing configs and create forged ones
159 sub genConfFiles {
160 for my $config ( $clientConfAcc->available() ) {
161 $clientConfAcc->delete($config);
162 }
163 my $time = time();
164
165 # FIXME: The locationRules is incorrect in the default configuration file,
166 # we need to overwrite it
167 my @configsToStore = (
168 { cfgNum => 1, cfgDate => $time, locationRules => {} },
169 { cfgNum => 2, cfgDate => $time - 10, locationRules => {} },
170 { cfgNum => 3, cfgDate => $time - 20, locationRules => {} },
171 );
172 for my $config (@configsToStore) {
173 $clientConfAcc->store($config);
174 }
175 }