]>
Commit | Line | Data |
---|---|---|
9edb99a5 WB |
1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Test::MockModule; | |
7 | use Test::More; | |
8 | ||
9 | use lib ('.', '..'); | |
10 | use PVE::RPCEnvironment; | |
11 | use PVE::Cluster; | |
12 | use PVE::Storage; | |
13 | ||
14 | my $datacenter_cfg = <<'EOF'; | |
15 | bwlimit: default=100,move=80,restore=60 | |
16 | EOF | |
17 | ||
18 | my $storage_cfg = <<'EOF'; | |
19 | dir: nolimit | |
20 | path /dir/a | |
21 | ||
22 | dir: d50 | |
23 | path /dir/b | |
24 | bwlimit default=50 | |
25 | ||
26 | dir: d50m40r30 | |
27 | path /dir/c | |
28 | bwlimit default=50,move=40,restore=30 | |
29 | ||
30 | dir: d20m40r30 | |
31 | path /dir/c | |
32 | bwlimit default=20,move=40,restore=30 | |
33 | ||
34 | dir: d200m400r300 | |
35 | path /dir/c | |
36 | bwlimit default=200,move=400,restore=300 | |
37 | ||
38 | dir: d10 | |
39 | path /dir/d | |
40 | bwlimit default=10 | |
41 | ||
42 | dir: m50 | |
43 | path /dir/e | |
44 | bwlimit move=50 | |
45 | ||
46 | dir: d200 | |
47 | path /dir/f | |
48 | bwlimit default=200 | |
49 | ||
50 | EOF | |
51 | ||
52 | my $permissions = { | |
53 | 'user1@test' => {}, | |
54 | 'user2@test' => { '/' => ['Sys.Modify'], }, | |
55 | 'user3@test' => { '/storage' => ['Datastore.Allocate'], }, | |
56 | 'user4@test' => { '/storage/d20m40r30' => ['Datastore.Allocate'], }, | |
57 | }; | |
58 | ||
59 | my $pve_cluster_module; | |
60 | $pve_cluster_module = Test::MockModule->new('PVE::Cluster'); | |
61 | $pve_cluster_module->mock( | |
62 | cfs_update => sub {}, | |
63 | get_config => sub { | |
64 | my ($file) = @_; | |
65 | if ($file eq 'datacenter.cfg') { | |
66 | return $datacenter_cfg; | |
67 | } elsif ($file eq 'storage.cfg') { | |
68 | return $storage_cfg; | |
69 | } | |
70 | die "TODO: mock get_config($file)\n"; | |
71 | }, | |
72 | ); | |
73 | ||
74 | my $rpcenv_module; | |
75 | $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment'); | |
76 | $rpcenv_module->mock( | |
77 | check => sub { | |
78 | my ($env, $user, $path, $perms, $noerr) = @_; | |
79 | return 1 if $user eq 'root@pam'; | |
80 | my $userperms = $permissions->{$user} | |
81 | or die "no permissions defined for user $user\n"; | |
82 | if (defined(my $pathperms = $userperms->{$path})) { | |
83 | foreach my $pp (@$pathperms) { | |
84 | foreach my $reqp (@$perms) { | |
85 | return 1 if $pp eq $reqp; | |
86 | } | |
87 | } | |
88 | } | |
89 | die "permission denied\n" if !$noerr; | |
90 | return 0; | |
91 | }, | |
92 | ); | |
93 | ||
94 | my $rpcenv = PVE::RPCEnvironment->init('pub'); | |
95 | ||
96 | my @tests = ( | |
97 | [ user => 'root@pam' ], | |
77445e9b WB |
98 | [ ['unknown', ['nolimit'], undef], 100, 'root / generic default limit, requesting default' ], |
99 | [ ['move', ['nolimit'], undef], 80, 'root / specific default limit, requesting default (move)' ], | |
100 | [ ['restore', ['nolimit'], undef], 60, 'root / specific default limit, requesting default (restore)' ], | |
101 | [ ['unknown', ['d50m40r30'], undef], 50, 'root / storage default limit' ], | |
102 | [ ['move', ['d50m40r30'], undef], 40, 'root / specific storage limit (move)' ], | |
103 | [ ['restore', ['d50m40r30'], undef], 30, 'root / specific storage limit (restore)' ], | |
104 | [ ['unknown', ['nolimit'], 0], 0, 'root / generic default limit' ], | |
105 | [ ['move', ['nolimit'], 0], 0, 'root / specific default limit (move)' ], | |
106 | [ ['restore', ['nolimit'], 0], 0, 'root / specific default limit (restore)' ], | |
107 | [ ['unknown', ['d50m40r30'], 0], 0, 'root / storage default limit' ], | |
108 | [ ['move', ['d50m40r30'], 0], 0, 'root / specific storage limit (move)' ], | |
109 | [ ['restore', ['d50m40r30'], 0], 0, 'root / specific storage limit (restore)' ], | |
e71336ad SI |
110 | [ ['migrate', undef, 100], 100, 'root / undef storage (migrate)' ], |
111 | [ ['migrate', [], 100], 100, 'root / no storage (migrate)' ], | |
9edb99a5 WB |
112 | |
113 | [ user => 'user1@test' ], | |
114 | [ ['unknown', ['nolimit'], undef], 100, 'generic default limit' ], | |
115 | [ ['move', ['nolimit'], undef], 80, 'specific default limit (move)' ], | |
116 | [ ['restore', ['nolimit'], undef], 60, 'specific default limit (restore)' ], | |
117 | [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit' ], | |
118 | [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit (move)' ], | |
119 | [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit (restore)' ], | |
120 | [ ['unknown', ['d200m400r300'], undef], 200, 'storage default limit above datacenter limits' ], | |
121 | [ ['move', ['d200m400r300'], undef], 400, 'specific storage limit above datacenter limits (move)' ], | |
122 | [ ['restore', ['d200m400r300'], undef], 300, 'specific storage limit above datacenter limits (restore)' ], | |
123 | [ ['unknown', ['d50'], undef], 50, 'storage default limit' ], | |
124 | [ ['move', ['d50'], undef], 50, 'storage default limit (move)' ], | |
125 | [ ['restore', ['d50'], undef], 50, 'storage default limit (restore)' ], | |
126 | ||
127 | [ user => 'user2@test' ], | |
128 | [ ['unknown', ['nolimit'], 0], 0, 'generic default limit with Sys.Modify, passing unlimited' ], | |
77445e9b WB |
129 | [ ['unknown', ['nolimit'], undef], 100, 'generic default limit with Sys.Modify' ], |
130 | [ ['move', ['nolimit'], undef], 80, 'specific default limit with Sys.Modify (move)' ], | |
131 | [ ['restore', ['nolimit'], undef], 60, 'specific default limit with Sys.Modify (restore)' ], | |
132 | [ ['restore', ['nolimit'], 0], 0, 'specific default limit with Sys.Modify, passing unlimited (restore)' ], | |
133 | [ ['move', ['nolimit'], 0], 0, 'specific default limit with Sys.Modify, passing unlimited (move)' ], | |
9edb99a5 | 134 | [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit with Sys.Modify' ], |
9edb99a5 | 135 | [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit with Sys.Modify (restore)' ], |
77445e9b | 136 | [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit with Sys.Modify (move)' ], |
9edb99a5 WB |
137 | |
138 | [ user => 'user3@test' ], | |
77445e9b | 139 | [ ['unknown', ['nolimit'], undef], 100, 'generic default limit with privileges on /' ], |
9edb99a5 WB |
140 | [ ['unknown', ['nolimit'], 80], 80, 'generic default limit with privileges on /, passing an override value' ], |
141 | [ ['unknown', ['nolimit'], 0], 0, 'generic default limit with privileges on /, passing unlimited' ], | |
77445e9b WB |
142 | [ ['move', ['nolimit'], undef], 80, 'specific default limit with privileges on / (move)' ], |
143 | [ ['move', ['nolimit'], 0], 0, 'specific default limit with privileges on /, passing unlimited (move)' ], | |
144 | [ ['restore', ['nolimit'], undef], 60, 'specific default limit with privileges on / (restore)' ], | |
145 | [ ['restore', ['nolimit'], 0], 0, 'specific default limit with privileges on /, passing unlimited (restore)' ], | |
146 | [ ['unknown', ['d50m40r30'], 0], 0, 'storage default limit with privileges on /, passing unlimited' ], | |
147 | [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit with privileges on /' ], | |
148 | [ ['unknown', ['d50m40r30'], 0], 0, 'storage default limit with privileges on, passing unlimited /' ], | |
149 | [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit with privileges on / (move)' ], | |
150 | [ ['move', ['d50m40r30'], 0], 0, 'specific storage limit with privileges on, passing unlimited / (move)' ], | |
151 | [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit with privileges on / (restore)' ], | |
152 | [ ['restore', ['d50m40r30'], 0], 0, 'specific storage limit with privileges on /, passing unlimited (restore)' ], | |
9edb99a5 WB |
153 | |
154 | [ user => 'user4@test' ], | |
155 | [ ['unknown', ['nolimit'], 10], 10, 'generic default limit with privileges on a different storage, passing lower override' ], | |
156 | [ ['unknown', ['nolimit'], undef], 100, 'generic default limit with privileges on a different storage' ], | |
157 | [ ['unknown', ['nolimit'], 0], 100, 'generic default limit with privileges on a different storage, passing unlimited' ], | |
158 | [ ['move', ['nolimit'], undef], 80, 'specific default limit with privileges on a different storage (move)' ], | |
159 | [ ['restore', ['nolimit'], undef], 60, 'specific default limit with privileges on a different storage (restore)' ], | |
160 | [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit with privileges on a different storage' ], | |
161 | [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit with privileges on a different storage (move)' ], | |
162 | [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit with privileges on a different storage (restore)' ], | |
77445e9b | 163 | [ ['unknown', ['d20m40r30'], undef], 20, 'storage default limit with privileges on that storage' ], |
9edb99a5 | 164 | [ ['unknown', ['d20m40r30'], 0], 0, 'storage default limit with privileges on that storage, passing unlimited' ], |
77445e9b WB |
165 | [ ['move', ['d20m40r30'], undef], 40, 'specific storage limit with privileges on that storage (move)' ], |
166 | [ ['move', ['d20m40r30'], 0], 0, 'specific storage limit with privileges on that storage, passing unlimited (move)' ], | |
167 | [ ['move', ['d20m40r30'], 10], 10, 'specific storage limit with privileges on that storage, passing low override (move)' ], | |
168 | [ ['move', ['d20m40r30'], 300], 300, 'specific storage limit with privileges on that storage, passing high override (move)' ], | |
169 | [ ['restore', ['d20m40r30'], undef], 30, 'specific storage limit with privileges on that storage (restore)' ], | |
170 | [ ['restore', ['d20m40r30'], 0], 0, 'specific storage limit with privileges on that storage, passing unlimited (restore)' ], | |
171 | [ ['unknown', ['d50m40r30', 'd20m40r30'], 0], 50, 'multiple storages default limit with privileges on one of them, passing unlimited' ], | |
172 | [ ['move', ['d50m40r30', 'd20m40r30'], 0], 40, 'multiple storages specific limit with privileges on one of them, passing unlimited (move)' ], | |
173 | [ ['restore', ['d50m40r30', 'd20m40r30'], 0], 30, 'multiple storages specific limit with privileges on one of them, passing unlimited (restore)' ], | |
174 | [ ['unknown', ['d50m40r30', 'd20m40r30'], undef], 20, 'multiple storages default limit with privileges on one of them' ], | |
9edb99a5 WB |
175 | [ ['unknown', ['d10', 'd20m40r30'], undef], 10, 'multiple storages default limit with privileges on one of them (storage limited)' ], |
176 | [ ['move', ['d10', 'd20m40r30'], undef], 10, 'multiple storages specific limit with privileges on one of them (storage limited) (move)' ], | |
177 | [ ['restore', ['d10', 'd20m40r30'], undef], 10, 'multiple storages specific limit with privileges on one of them (storage limited) (restore)' ], | |
178 | [ ['restore', ['d10', 'd20m40r30'], 5], 5, 'multiple storages specific limit (storage limited) (restore), passing lower override' ], | |
179 | [ ['restore', ['d200', 'd200m400r300'], 65], 65, 'multiple storages specific limit (storage limited) (restore), passing lower override' ], | |
180 | [ ['restore', ['d200', 'd200m400r300'], 400], 200, 'multiple storages specific limit (storage limited) (restore), passing higher override' ], | |
181 | [ ['restore', ['d200', 'd200m400r300'], 0], 200, 'multiple storages specific limit (storage limited) (restore), passing unlimited' ], | |
182 | [ ['restore', ['d200', 'd200m400r300'], 1], 1, 'multiple storages specific limit (storage limited) (restore), passing 1' ], | |
183 | [ ['restore', ['d10', 'd20m40r30'], 500], 10, 'multiple storages specific limit with privileges on one of them (storage limited) (restore), passing higher override' ], | |
77445e9b WB |
184 | [ ['unknown', ['nolimit', 'd20m40r30'], 0], 100, 'multiple storages default limit with privileges on one of them, passing unlimited (default limited)' ], |
185 | [ ['move', ['nolimit', 'd20m40r30'], 0], 80, 'multiple storages specific limit with privileges on one of them, passing unlimited (default limited) (move)' ], | |
186 | [ ['restore', ['nolimit', 'd20m40r30'], 0], 60, 'multiple storages specific limit with privileges on one of them, passing unlimited (default limited) (restore)' ], | |
187 | [ ['unknown', ['nolimit', 'd20m40r30'], undef], 20, 'multiple storages default limit with privileges on one of them (default limited)' ], | |
188 | [ ['move', ['nolimit', 'd20m40r30'], undef], 40, 'multiple storages specific limit with privileges on one of them (default limited) (move)' ], | |
189 | [ ['restore', ['nolimit', 'd20m40r30'], undef], 30, 'multiple storages specific limit with privileges on one of them (default limited) (restore)' ], | |
190 | [ ['restore', ['d20m40r30', 'm50'], 200], 60, 'multiple storages specific limit with privileges on one of them (global default limited) (restore)' ], | |
e71336ad | 191 | [ ['move', ['nolimit', undef ], 40] , 40, 'multiple storages one undefined, passing 100 (move)' ], |
9edb99a5 WB |
192 | ); |
193 | ||
194 | foreach my $t (@tests) { | |
195 | my ($args, $expected, $description) = @$t; | |
196 | if (!ref($args)) { | |
197 | if ($args eq 'user') { | |
198 | $rpcenv->set_user($expected); | |
199 | } else { | |
200 | die "not a test specification\n"; | |
201 | } | |
202 | next; | |
203 | } | |
204 | is(PVE::Storage::get_bandwidth_limit(@$args), $expected, $description); | |
205 | } | |
206 | done_testing(); |