]> git.proxmox.com Git - pve-storage.git/blob - test/run_bwlimit_tests.pl
add Storage::get_bandwidth_limit helper
[pve-storage.git] / test / run_bwlimit_tests.pl
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' ],
98 [ ['unknown', ['nolimit'], undef], undef, 'root / generic default limit' ],
99 [ ['move', ['nolimit'], undef], undef, 'root / specific default limit (move)' ],
100 [ ['restore', ['nolimit'], undef], undef, 'root / specific default limit (restore)' ],
101 [ ['unknown', ['d50m40r30'], undef], undef, 'root / storage default limit' ],
102 [ ['move', ['d50m40r30'], undef], undef, 'root / specific storage limit (move)' ],
103 [ ['restore', ['d50m40r30'], undef], undef, 'root / specific storage limit (restore)' ],
104
105 [ user => 'user1@test' ],
106 [ ['unknown', ['nolimit'], undef], 100, 'generic default limit' ],
107 [ ['move', ['nolimit'], undef], 80, 'specific default limit (move)' ],
108 [ ['restore', ['nolimit'], undef], 60, 'specific default limit (restore)' ],
109 [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit' ],
110 [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit (move)' ],
111 [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit (restore)' ],
112 [ ['unknown', ['d200m400r300'], undef], 200, 'storage default limit above datacenter limits' ],
113 [ ['move', ['d200m400r300'], undef], 400, 'specific storage limit above datacenter limits (move)' ],
114 [ ['restore', ['d200m400r300'], undef], 300, 'specific storage limit above datacenter limits (restore)' ],
115 [ ['unknown', ['d50'], undef], 50, 'storage default limit' ],
116 [ ['move', ['d50'], undef], 50, 'storage default limit (move)' ],
117 [ ['restore', ['d50'], undef], 50, 'storage default limit (restore)' ],
118
119 [ user => 'user2@test' ],
120 [ ['unknown', ['nolimit'], 0], 0, 'generic default limit with Sys.Modify, passing unlimited' ],
121 [ ['unknown', ['nolimit'], undef], undef, 'generic default limit with Sys.Modify' ],
122 [ ['restore', ['nolimit'], undef], undef, 'specific default limit with Sys.Modify (restore)' ],
123 [ ['move', ['nolimit'], undef], undef, 'specific default limit with Sys.Modify (move)' ],
124 [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit with Sys.Modify' ],
125 [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit with Sys.Modify (move)' ],
126 [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit with Sys.Modify (restore)' ],
127
128 [ user => 'user3@test' ],
129 [ ['unknown', ['nolimit'], 80], 80, 'generic default limit with privileges on /, passing an override value' ],
130 [ ['unknown', ['nolimit'], 0], 0, 'generic default limit with privileges on /, passing unlimited' ],
131 [ ['unknown', ['nolimit'], undef], undef, 'generic default limit with privileges on /' ],
132 [ ['move', ['nolimit'], undef], undef, 'specific default limit with privileges on / (move)' ],
133 [ ['restore', ['nolimit'], undef], undef, 'specific default limit with privileges on / (restore)' ],
134 [ ['unknown', ['d50m20r20'], 0], 0, 'storage default limit with privileges on /, passing unlimited' ],
135 [ ['unknown', ['d50m20r20'], undef], undef, 'storage default limit with privileges on /' ],
136 [ ['move', ['d50m20r20'], undef], undef, 'specific storage limit with privileges on / (move)' ],
137 [ ['restore', ['d50m20r20'], undef], undef, 'specific storage limit with privileges on / (restore)' ],
138
139 [ user => 'user4@test' ],
140 [ ['unknown', ['nolimit'], 10], 10, 'generic default limit with privileges on a different storage, passing lower override' ],
141 [ ['unknown', ['nolimit'], undef], 100, 'generic default limit with privileges on a different storage' ],
142 [ ['unknown', ['nolimit'], 0], 100, 'generic default limit with privileges on a different storage, passing unlimited' ],
143 [ ['move', ['nolimit'], undef], 80, 'specific default limit with privileges on a different storage (move)' ],
144 [ ['restore', ['nolimit'], undef], 60, 'specific default limit with privileges on a different storage (restore)' ],
145 [ ['unknown', ['d50m40r30'], undef], 50, 'storage default limit with privileges on a different storage' ],
146 [ ['move', ['d50m40r30'], undef], 40, 'specific storage limit with privileges on a different storage (move)' ],
147 [ ['restore', ['d50m40r30'], undef], 30, 'specific storage limit with privileges on a different storage (restore)' ],
148 [ ['unknown', ['d20m40r30'], undef], undef, 'storage default limit with privileges on that storage' ],
149 [ ['unknown', ['d20m40r30'], 0], 0, 'storage default limit with privileges on that storage, passing unlimited' ],
150 [ ['move', ['d20m40r30'], undef], undef, 'specific storage limit with privileges on that storage (move)' ],
151 [ ['restore', ['d20m40r30'], undef], undef, 'specific storage limit with privileges on that storage (restore)' ],
152 [ ['unknown', ['d50m40r30', 'd20m40r30'], undef], 50, 'multiple storages default limit with privileges on one of them' ],
153 [ ['move', ['d50m40r30', 'd20m40r30'], undef], 40, 'multiple storages specific limit with privileges on one of them (move)' ],
154 [ ['restore', ['d50m40r30', 'd20m40r30'], undef], 30, 'multiple storages specific limit with privileges on one of them (restore)' ],
155 [ ['unknown', ['d10', 'd20m40r30'], undef], 10, 'multiple storages default limit with privileges on one of them (storage limited)' ],
156 [ ['move', ['d10', 'd20m40r30'], undef], 10, 'multiple storages specific limit with privileges on one of them (storage limited) (move)' ],
157 [ ['restore', ['d10', 'd20m40r30'], undef], 10, 'multiple storages specific limit with privileges on one of them (storage limited) (restore)' ],
158 [ ['restore', ['d10', 'd20m40r30'], 5], 5, 'multiple storages specific limit (storage limited) (restore), passing lower override' ],
159 [ ['restore', ['d200', 'd200m400r300'], 65], 65, 'multiple storages specific limit (storage limited) (restore), passing lower override' ],
160 [ ['restore', ['d200', 'd200m400r300'], 400], 200, 'multiple storages specific limit (storage limited) (restore), passing higher override' ],
161 [ ['restore', ['d200', 'd200m400r300'], 0], 200, 'multiple storages specific limit (storage limited) (restore), passing unlimited' ],
162 [ ['restore', ['d200', 'd200m400r300'], 1], 1, 'multiple storages specific limit (storage limited) (restore), passing 1' ],
163 [ ['restore', ['d10', 'd20m40r30'], 500], 10, 'multiple storages specific limit with privileges on one of them (storage limited) (restore), passing higher override' ],
164 [ ['unknown', ['nolimit', 'd20m40r30'], undef], 100, 'multiple storages default limit with privileges on one of them (default limited)' ],
165 [ ['move', ['nolimit', 'd20m40r30'], undef], 80, 'multiple storages specific limit with privileges on one of them (default limited) (move)' ],
166 [ ['restore', ['nolimit', 'd20m40r30'], undef], 60, 'multiple storages specific limit with privileges on one of them (default limited) (restore)' ],
167 [ ['restore', ['d20m40r30', 'm50'], 200], 60, 'multiple storages specific limit with privileges on one of them (default limited) (restore)' ],
168 );
169
170 foreach my $t (@tests) {
171 my ($args, $expected, $description) = @$t;
172 if (!ref($args)) {
173 if ($args eq 'user') {
174 $rpcenv->set_user($expected);
175 } else {
176 die "not a test specification\n";
177 }
178 next;
179 }
180 is(PVE::Storage::get_bandwidth_limit(@$args), $expected, $description);
181 }
182 done_testing();