/
test_quota.py
2289 lines (2078 loc) · 90.2 KB
/
test_quota.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock
import ddt
from oslo_db.sqlalchemy import enginefacade
from oslo_limit import fixture as limit_fixture
from oslo_utils.fixture import uuidsentinel as uuids
from nova.compute import api as compute
import nova.conf
from nova import context
from nova.db.main import models
from nova import exception
from nova.limit import local as local_limit
from nova.limit import placement as placement_limit
from nova import objects
from nova import quota
from nova import test
from nova.tests import fixtures as nova_fixtures
CONF = nova.conf.CONF
def _get_fake_get_usages(updates=None):
# These values are not realistic (they should all be 0) and are
# only for testing that countable usages get included in the
# results.
usages = {'key_pairs': {'in_use': 2},
'server_group_members': {'in_use': 3},
'instances': {'in_use': 2},
'cores': {'in_use': 4},
'ram': {'in_use': 10 * 1024}}
if updates:
usages.update(updates)
def fake_get_usages(*a, **k):
return usages
return fake_get_usages
class QuotaIntegrationTestCase(test.TestCase):
REQUIRES_LOCKING = True
def setUp(self):
super(QuotaIntegrationTestCase, self).setUp()
self.flags(instances=2,
cores=4,
ram=16384,
group='quota')
self.user_id = 'admin'
self.project_id = 'admin'
self.context = context.RequestContext(self.user_id,
self.project_id,
is_admin=True)
self.flavor = objects.Flavor.get_by_name(self.context, 'm1.small')
self.useFixture(nova_fixtures.GlanceFixture(self))
self.compute_api = compute.API()
def fake_validate_networks(context, requested_networks, num_instances):
return num_instances
# we aren't testing network quota in these tests when creating a server
# so just mock that out and assume network (port) quota is OK
self.compute_api.network_api.validate_networks = (
mock.Mock(side_effect=fake_validate_networks))
def _create_instance(self, flavor_name='m1.large'):
"""Create a test instance in cell1 with an instance mapping."""
cell1 = self.cell_mappings[test.CELL1_NAME]
with context.target_cell(self.context, cell1) as cctxt:
inst = objects.Instance(context=cctxt)
inst.image_id = 'cedef40a-ed67-4d10-800e-17455edce175'
inst.reservation_id = 'r-fakeres'
inst.user_id = self.user_id
inst.project_id = self.project_id
inst.flavor = objects.Flavor.get_by_name(cctxt, flavor_name)
# This is needed for instance quota counting until we have the
# ability to count allocations in placement.
inst.vcpus = inst.flavor.vcpus
inst.memory_mb = inst.flavor.memory_mb
inst.create()
# Create the related instance mapping which will be used in
# _instances_cores_ram_count().
inst_map = objects.InstanceMapping(
self.context, instance_uuid=inst.uuid, project_id=inst.project_id,
user_id=inst.user_id, cell_mapping=cell1)
inst_map.create()
return inst
def test_too_many_instances(self):
for i in range(CONF.quota.instances):
self._create_instance()
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
try:
self.compute_api.create(
self.context, min_count=1, max_count=1,
flavor=self.flavor, image_href=image_uuid)
except exception.OverQuota as e:
expected_kwargs = {'code': 413,
'req': '1, 1, 2048',
'used': '8, 2, 16384',
'allowed': '4, 2, 16384',
'overs': 'cores, instances, ram'}
self.assertEqual(expected_kwargs, e.kwargs)
else:
self.fail('Expected OverQuota exception')
def test_too_many_cores(self):
self._create_instance()
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
try:
self.compute_api.create(
self.context, min_count=1, max_count=1, flavor=self.flavor,
image_href=image_uuid)
except exception.OverQuota as e:
expected_kwargs = {'code': 413,
'req': '1',
'used': '4',
'allowed': '4',
'overs': 'cores'}
self.assertEqual(expected_kwargs, e.kwargs)
else:
self.fail('Expected OverQuota exception')
def test_many_cores_with_unlimited_quota(self):
# Setting cores quota to unlimited:
self.flags(cores=-1, group='quota')
# Default is 20 cores, so create 3x m1.xlarge with
# 8 cores each.
for i in range(3):
self._create_instance(flavor_name='m1.xlarge')
def test_too_many_metadata_items(self):
metadata = {}
for i in range(CONF.quota.metadata_items + 1):
metadata['key%s' % i] = 'value%s' % i
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
self.assertRaises(
exception.OverQuota, self.compute_api.create,
self.context, min_count=1, max_count=1, flavor=self.flavor,
image_href=image_uuid, metadata=metadata)
def _create_with_injected_files(self, files):
api = self.compute_api
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
api.create(
self.context, min_count=1, max_count=1, flavor=self.flavor,
image_href=image_uuid, injected_files=files)
def test_no_injected_files(self):
api = self.compute_api
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
api.create(self.context, flavor=self.flavor, image_href=image_uuid)
def test_max_injected_files(self):
files = []
for i in range(CONF.quota.injected_files):
files.append(('/my/path%d' % i, 'config = test\n'))
self._create_with_injected_files(files) # no OverQuota
def test_too_many_injected_files(self):
files = []
for i in range(CONF.quota.injected_files + 1):
files.append(('/my/path%d' % i, 'my\ncontent%d\n' % i))
self.assertRaises(exception.OverQuota,
self._create_with_injected_files, files)
def test_max_injected_file_content_bytes(self):
max = CONF.quota.injected_file_content_bytes
content = ''.join(['a' for i in range(max)])
files = [('/test/path', content)]
self._create_with_injected_files(files) # no OverQuota
def test_too_many_injected_file_content_bytes(self):
max = CONF.quota.injected_file_content_bytes
content = ''.join(['a' for i in range(max + 1)])
files = [('/test/path', content)]
self.assertRaises(exception.OverQuota,
self._create_with_injected_files, files)
def test_max_injected_file_path_bytes(self):
max = CONF.quota.injected_file_path_length
path = ''.join(['a' for i in range(max)])
files = [(path, 'config = quotatest')]
self._create_with_injected_files(files) # no OverQuota
def test_too_many_injected_file_path_bytes(self):
max = CONF.quota.injected_file_path_length
path = ''.join(['a' for i in range(max + 1)])
files = [(path, 'config = quotatest')]
self.assertRaises(exception.OverQuota,
self._create_with_injected_files, files)
def _test_with_server_group_members(self):
# use a known image uuid to avoid ImageNotFound errors
image_uuid = nova_fixtures.GlanceFixture.image4['id']
instance_group = objects.InstanceGroup(self.context,
policy="anti-affinity")
instance_group.name = "foo"
instance_group.project_id = self.context.project_id
instance_group.user_id = self.context.user_id
instance_group.uuid = uuids.instance_group
instance_group.create()
self.addCleanup(instance_group.destroy)
self.compute_api.create(
self.context, flavor=self.flavor,
image_href=image_uuid,
scheduler_hints={'group': uuids.instance_group},
check_server_group_quota=True)
exc = self.assertRaises(exception.OverQuota, self.compute_api.create,
self.context,
flavor=self.flavor,
image_href=image_uuid,
scheduler_hints={
'group': uuids.instance_group},
check_server_group_quota=True)
return exc
def test_with_server_group_members(self):
self.flags(server_group_members=1, group="quota")
exc = self._test_with_server_group_members()
self.assertEqual("Quota exceeded, too many servers in group", str(exc))
class UnifiedLimitsIntegrationTestCase(QuotaIntegrationTestCase):
"""Test that API and DB resources enforce properly with unified limits.
Note: coverage for instances, cores, ram, and disk is located under
nova/tests/functional/. We don't attempt to test it here as the
PlacementFixture is needed to provide resource usages and it is only
available in the functional tests environment.
Note that any test that will succeed in creating a server also needs to be
able to use the PlacementFixture as cores, ram, and disk quota are enforced
while booting a server. These tests are also located under
nova/tests/functional/.
"""
def setUp(self):
super(UnifiedLimitsIntegrationTestCase, self).setUp()
self.flags(driver="nova.quota.UnifiedLimitsDriver", group="quota")
reglimits = {local_limit.SERVER_METADATA_ITEMS: 128,
local_limit.INJECTED_FILES: 5,
local_limit.INJECTED_FILES_CONTENT: 10 * 1024,
local_limit.INJECTED_FILES_PATH: 255,
local_limit.KEY_PAIRS: 100,
local_limit.SERVER_GROUPS: 10,
local_limit.SERVER_GROUP_MEMBERS: 10,
'servers': 10,
'class:VCPU': 20,
'class:MEMORY_MB': 50 * 1024,
'class:DISK_GB': 100}
self.useFixture(limit_fixture.LimitFixture(reglimits, {}))
def test_too_many_instances(self):
pass
def test_too_many_cores(self):
pass
def test_no_injected_files(self):
pass
def test_max_injected_files(self):
pass
def test_max_injected_file_content_bytes(self):
pass
def test_max_injected_file_path_bytes(self):
pass
def test_with_server_group_members(self):
pass
@enginefacade.transaction_context_provider
class FakeContext(context.RequestContext):
def __init__(self, project_id, quota_class):
super(FakeContext, self).__init__(project_id=project_id,
quota_class=quota_class)
self.is_admin = False
self.user_id = 'fake_user'
self.project_id = project_id
self.quota_class = quota_class
self.read_deleted = 'no'
def elevated(self):
elevated = self.__class__(self.project_id, self.quota_class)
elevated.is_admin = True
return elevated
class FakeDriver(object):
def __init__(self, by_project=None, by_user=None, by_class=None,
reservations=None):
self.called = []
self.by_project = by_project or {}
self.by_user = by_user or {}
self.by_class = by_class or {}
self.reservations = reservations or []
def get_defaults(self, context, resources):
self.called.append(('get_defaults', context, resources))
return resources
def get_class_quotas(self, context, resources, quota_class):
self.called.append(('get_class_quotas', context, resources,
quota_class))
return resources
def get_user_quotas(self, context, resources, project_id, user_id,
quota_class=None, usages=True):
self.called.append(('get_user_quotas', context, resources,
project_id, user_id, quota_class,
usages))
return resources
def get_project_quotas(self, context, resources, project_id,
quota_class=None, usages=True,
remains=False):
self.called.append(('get_project_quotas', context, resources,
project_id, quota_class, usages,
remains))
return resources
def limit_check(self, context, resources, values, project_id=None,
user_id=None):
self.called.append(('limit_check', context, resources,
values, project_id, user_id))
def limit_check_project_and_user(self, context, resources,
project_values=None, user_values=None,
project_id=None, user_id=None):
self.called.append(('limit_check_project_and_user', context, resources,
project_values, user_values, project_id, user_id))
class BaseResourceTestCase(test.TestCase):
def test_no_flag(self):
resource = quota.BaseResource('test_resource')
self.assertEqual(resource.name, 'test_resource')
self.assertIsNone(resource.flag)
self.assertEqual(resource.default, -1)
def test_with_flag(self):
# We know this flag exists, so use it...
self.flags(instances=10, group='quota')
resource = quota.BaseResource('test_resource', 'instances')
self.assertEqual(resource.name, 'test_resource')
self.assertEqual(resource.flag, 'instances')
self.assertEqual(resource.default, 10)
def test_with_flag_no_quota(self):
self.flags(instances=-1, group='quota')
resource = quota.BaseResource('test_resource', 'instances')
self.assertEqual(resource.name, 'test_resource')
self.assertEqual(resource.flag, 'instances')
self.assertEqual(resource.default, -1)
def test_valid_method_call_check_invalid_input(self):
resources = {'dummy': 1}
self.assertRaises(exception.InvalidQuotaMethodUsage,
quota._valid_method_call_check_resources,
resources, 'limit', quota.QUOTAS._resources)
def test_valid_method_call_check_invalid_method(self):
resources = {'key_pairs': 1}
self.assertRaises(exception.InvalidQuotaMethodUsage,
quota._valid_method_call_check_resources,
resources, 'dummy', quota.QUOTAS._resources)
def test_valid_method_call_check_multiple(self):
resources = {'key_pairs': 1, 'dummy': 2}
self.assertRaises(exception.InvalidQuotaMethodUsage,
quota._valid_method_call_check_resources,
resources, 'check', quota.QUOTAS._resources)
resources = {'key_pairs': 1, 'instances': 2, 'dummy': 3}
self.assertRaises(exception.InvalidQuotaMethodUsage,
quota._valid_method_call_check_resources,
resources, 'check', quota.QUOTAS._resources)
def test_valid_method_call_check_wrong_method(self):
resources = {'key_pairs': 1}
engine_resources = {'key_pairs': quota.CountableResource('key_pairs',
None,
'key_pairs')}
self.assertRaises(exception.InvalidQuotaMethodUsage,
quota._valid_method_call_check_resources,
resources, 'bogus', engine_resources)
class QuotaEngineTestCase(test.TestCase):
def test_init(self):
quota_obj = quota.QuotaEngine()
self.assertIsInstance(quota_obj._driver, quota.DbQuotaDriver)
def test_init_override_obj(self):
quota_obj = quota.QuotaEngine(quota_driver=FakeDriver)
self.assertEqual(quota_obj._driver, FakeDriver)
def test_init_with_flag_set(self):
quota_obj = quota.QuotaEngine()
self.assertIsInstance(quota_obj._driver, quota.DbQuotaDriver)
self.flags(group="quota", driver="nova.quota.NoopQuotaDriver")
self.assertIsInstance(quota_obj._driver, quota.NoopQuotaDriver)
self.flags(group="quota", driver="nova.quota.UnifiedLimitsDriver")
self.assertIsInstance(quota_obj._driver, quota.UnifiedLimitsDriver)
self.flags(group="quota", driver="nova.quota.DbQuotaDriver")
self.assertIsInstance(quota_obj._driver, quota.DbQuotaDriver)
def _get_quota_engine(self, driver, resources=None):
resources = resources or [
quota.AbsoluteResource('test_resource4'),
quota.AbsoluteResource('test_resource3'),
quota.AbsoluteResource('test_resource2'),
quota.AbsoluteResource('test_resource1'),
]
return quota.QuotaEngine(quota_driver=driver, resources=resources)
def test_get_defaults(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
result = quota_obj.get_defaults(context)
self.assertEqual(driver.called, [
('get_defaults', context, quota_obj._resources),
])
self.assertEqual(result, quota_obj._resources)
def test_get_class_quotas(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
result1 = quota_obj.get_class_quotas(context, 'test_class')
self.assertEqual(driver.called, [
('get_class_quotas', context, quota_obj._resources,
'test_class'),
])
self.assertEqual(result1, quota_obj._resources)
def test_get_user_quotas(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
result1 = quota_obj.get_user_quotas(context, 'test_project',
'fake_user')
result2 = quota_obj.get_user_quotas(context, 'test_project',
'fake_user',
quota_class='test_class',
usages=False)
self.assertEqual(driver.called, [
('get_user_quotas', context, quota_obj._resources,
'test_project', 'fake_user', None, True),
('get_user_quotas', context, quota_obj._resources,
'test_project', 'fake_user', 'test_class', False),
])
self.assertEqual(result1, quota_obj._resources)
self.assertEqual(result2, quota_obj._resources)
def test_get_project_quotas(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
result1 = quota_obj.get_project_quotas(context, 'test_project')
result2 = quota_obj.get_project_quotas(context, 'test_project',
quota_class='test_class',
usages=False)
self.assertEqual(driver.called, [
('get_project_quotas', context, quota_obj._resources,
'test_project', None, True, False),
('get_project_quotas', context, quota_obj._resources,
'test_project', 'test_class', False, False),
])
self.assertEqual(result1, quota_obj._resources)
self.assertEqual(result2, quota_obj._resources)
def test_count_as_dict_no_resource(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
self.assertRaises(exception.QuotaResourceUnknown,
quota_obj.count_as_dict, context, 'test_resource5',
True, foo='bar')
def test_count_as_dict_wrong_resource(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
self.assertRaises(exception.QuotaResourceUnknown,
quota_obj.count_as_dict, context, 'test_resource1',
True, foo='bar')
def test_count_as_dict(self):
def fake_count_as_dict(context, *args, **kwargs):
self.assertEqual(args, (True,))
self.assertEqual(kwargs, dict(foo='bar'))
return {'project': {'test_resource5': 5}}
context = FakeContext(None, None)
driver = FakeDriver()
resources = [
quota.CountableResource('test_resource5', fake_count_as_dict),
]
quota_obj = self._get_quota_engine(driver, resources)
result = quota_obj.count_as_dict(context, 'test_resource5', True,
foo='bar')
self.assertEqual({'project': {'test_resource5': 5}}, result)
def test_limit_check(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
quota_obj.limit_check(context, test_resource1=4, test_resource2=3,
test_resource3=2, test_resource4=1)
self.assertEqual(driver.called, [
('limit_check', context, quota_obj._resources, dict(
test_resource1=4,
test_resource2=3,
test_resource3=2,
test_resource4=1,
), None, None),
])
def test_limit_check_project_and_user(self):
context = FakeContext(None, None)
driver = FakeDriver()
quota_obj = self._get_quota_engine(driver)
project_values = dict(test_resource1=4, test_resource2=3)
user_values = dict(test_resource3=2, test_resource4=1)
quota_obj.limit_check_project_and_user(context,
project_values=project_values,
user_values=user_values)
self.assertEqual([('limit_check_project_and_user', context,
quota_obj._resources,
dict(test_resource1=4, test_resource2=3),
dict(test_resource3=2, test_resource4=1),
None, None)],
driver.called)
def test_resources(self):
quota_obj = self._get_quota_engine(None)
self.assertEqual(quota_obj.resources,
['test_resource1', 'test_resource2',
'test_resource3', 'test_resource4'])
class DbQuotaDriverTestCase(test.TestCase):
def setUp(self):
super(DbQuotaDriverTestCase, self).setUp()
self.flags(instances=10,
cores=20,
ram=50 * 1024,
metadata_items=128,
injected_files=5,
injected_file_content_bytes=10 * 1024,
injected_file_path_length=255,
server_groups=10,
server_group_members=10,
group='quota'
)
self.driver = quota.DbQuotaDriver()
self.calls = []
self.useFixture(test.TimeOverride())
def test_get_defaults(self):
# Use our pre-defined resources
self._stub_quota_class_get_default()
result = self.driver.get_defaults(None, quota.QUOTAS._resources)
self.assertEqual(result, dict(
instances=5,
cores=20,
ram=25 * 1024,
metadata_items=64,
injected_files=5,
injected_file_content_bytes=5 * 1024,
injected_file_path_bytes=255,
key_pairs=100,
server_groups=10,
server_group_members=10,
security_groups=-1,
security_group_rules=-1,
fixed_ips=-1,
floating_ips=-1,
))
def _stub_quota_class_get_default(self):
# Stub out quota_class_get_default
def fake_qcgd(cls, context):
self.calls.append('quota_class_get_default')
return dict(
instances=5,
ram=25 * 1024,
metadata_items=64,
injected_file_content_bytes=5 * 1024,
)
self.stub_out('nova.objects.Quotas.get_default_class', fake_qcgd)
def _stub_quota_class_get_all_by_name(self):
# Stub out quota_class_get_all_by_name
def fake_qcgabn(cls, context, quota_class):
self.calls.append('quota_class_get_all_by_name')
self.assertEqual(quota_class, 'test_class')
return dict(
instances=5,
ram=25 * 1024,
metadata_items=64,
injected_file_content_bytes=5 * 1024,
)
self.stub_out('nova.objects.Quotas.get_all_class_by_name', fake_qcgabn)
def test_get_class_quotas(self):
self._stub_quota_class_get_all_by_name()
result = self.driver.get_class_quotas(None, quota.QUOTAS._resources,
'test_class')
self.assertEqual(self.calls, ['quota_class_get_all_by_name'])
self.assertEqual(result, dict(
instances=5,
cores=20,
ram=25 * 1024,
metadata_items=64,
injected_files=5,
injected_file_content_bytes=5 * 1024,
injected_file_path_bytes=255,
key_pairs=100,
server_groups=10,
server_group_members=10,
floating_ips=-1,
fixed_ips=-1,
security_groups=-1,
security_group_rules=-1,
))
def _stub_get_by_project_and_user(self):
def fake_qgabpau(context, project_id, user_id):
self.calls.append('quota_get_all_by_project_and_user')
self.assertEqual(project_id, 'test_project')
self.assertEqual(user_id, 'fake_user')
return dict(
cores=10,
injected_files=2,
injected_file_path_bytes=127,
)
def fake_qgabp(context, project_id):
self.calls.append('quota_get_all_by_project')
self.assertEqual(project_id, 'test_project')
return {
'cores': 10,
'injected_files': 2,
'injected_file_path_bytes': 127,
}
self.stub_out('nova.db.main.api.quota_get_all_by_project_and_user',
fake_qgabpau)
self.stub_out('nova.db.main.api.quota_get_all_by_project', fake_qgabp)
self._stub_quota_class_get_all_by_name()
def _get_fake_countable_resources(self):
# Create several countable resources with fake count functions
def fake_instances_cores_ram_count(*a, **k):
return {'project': {'instances': 2, 'cores': 4, 'ram': 1024},
'user': {'instances': 1, 'cores': 2, 'ram': 512}}
def fake_server_group_count(*a, **k):
return {'project': {'server_groups': 5},
'user': {'server_groups': 3}}
resources = {}
resources['key_pairs'] = quota.CountableResource(
'key_pairs', lambda *a, **k: {'user': {'key_pairs': 1}},
'key_pairs')
resources['instances'] = quota.CountableResource(
'instances', fake_instances_cores_ram_count, 'instances')
resources['cores'] = quota.CountableResource(
'cores', fake_instances_cores_ram_count, 'cores')
resources['ram'] = quota.CountableResource(
'ram', fake_instances_cores_ram_count, 'ram')
resources['server_groups'] = quota.CountableResource(
'server_groups', fake_server_group_count, 'server_groups')
resources['server_group_members'] = quota.CountableResource(
'server_group_members',
lambda *a, **k: {'user': {'server_group_members': 7}},
'server_group_members')
resources['floating_ips'] = quota.AbsoluteResource('floating_ips')
resources['fixed_ips'] = quota.AbsoluteResource('fixed_ips')
resources['security_groups'] = quota.AbsoluteResource(
'security_groups')
resources['security_group_rules'] = quota.AbsoluteResource(
'security_group_rules')
return resources
def test_get_usages_for_project(self):
resources = self._get_fake_countable_resources()
actual = self.driver._get_usages(
FakeContext('test_project', 'test_class'), resources,
'test_project')
# key_pairs, server_group_members, and security_group_rules are never
# counted as a usage. Their counts are only for quota limit checking.
expected = {'key_pairs': {'in_use': 0},
'instances': {'in_use': 2},
'cores': {'in_use': 4},
'ram': {'in_use': 1024},
'server_groups': {'in_use': 5},
'server_group_members': {'in_use': 0}}
self.assertEqual(expected, actual)
def test_get_usages_for_user(self):
resources = self._get_fake_countable_resources()
actual = self.driver._get_usages(
FakeContext('test_project', 'test_class'), resources,
'test_project', user_id='fake_user')
# key_pairs, server_group_members, and security_group_rules are never
# counted as a usage. Their counts are only for quota limit checking.
expected = {'key_pairs': {'in_use': 0},
'instances': {'in_use': 1},
'cores': {'in_use': 2},
'ram': {'in_use': 512},
'server_groups': {'in_use': 3},
'server_group_members': {'in_use': 0}}
self.assertEqual(expected, actual)
@mock.patch('nova.quota.DbQuotaDriver._get_usages',
side_effect=_get_fake_get_usages())
def test_get_user_quotas(self, mock_get_usages):
self.maxDiff = None
self._stub_get_by_project_and_user()
ctxt = FakeContext('test_project', 'test_class')
result = self.driver.get_user_quotas(
ctxt, quota.QUOTAS._resources, 'test_project', 'fake_user')
self.assertEqual(self.calls, [
'quota_get_all_by_project_and_user',
'quota_get_all_by_project',
'quota_class_get_all_by_name',
])
mock_get_usages.assert_called_once_with(ctxt, quota.QUOTAS._resources,
'test_project',
user_id='fake_user')
self.assertEqual(result, dict(
instances=dict(
limit=5,
in_use=2,
),
cores=dict(
limit=10,
in_use=4,
),
ram=dict(
limit=25 * 1024,
in_use=10 * 1024,
),
floating_ips=dict(
limit=-1,
in_use=0,
),
fixed_ips=dict(
limit=-1,
in_use=0,
),
metadata_items=dict(
limit=64,
in_use=0,
),
injected_files=dict(
limit=2,
in_use=0,
),
injected_file_content_bytes=dict(
limit=5 * 1024,
in_use=0,
),
injected_file_path_bytes=dict(
limit=127,
in_use=0,
),
security_groups=dict(
limit=-1,
in_use=0,
),
security_group_rules=dict(
limit=-1,
in_use=0,
),
key_pairs=dict(
limit=100,
in_use=2,
),
server_groups=dict(
limit=10,
in_use=0,
),
server_group_members=dict(
limit=10,
in_use=3,
),
))
def _stub_get_by_project_and_user_specific(self):
def fake_quota_get(context, project_id, resource, user_id=None):
self.calls.append('quota_get')
self.assertEqual(project_id, 'test_project')
self.assertEqual(user_id, 'fake_user')
self.assertEqual(resource, 'test_resource')
return dict(
test_resource=dict(in_use=20),
)
self.stub_out('nova.db.main.api.quota_get', fake_quota_get)
def _stub_get_by_project(self):
def fake_qgabp(context, project_id):
self.calls.append('quota_get_all_by_project')
self.assertEqual(project_id, 'test_project')
return dict(
cores=10,
injected_files=2,
injected_file_path_bytes=127,
)
def fake_quota_get_all(context, project_id):
self.calls.append('quota_get_all')
self.assertEqual(project_id, 'test_project')
return [
models.ProjectUserQuota(resource='instances', hard_limit=5),
models.ProjectUserQuota(resource='cores', hard_limit=2),
]
self.stub_out('nova.db.main.api.quota_get_all_by_project', fake_qgabp)
self.stub_out('nova.db.main.api.quota_get_all', fake_quota_get_all)
self._stub_quota_class_get_all_by_name()
self._stub_quota_class_get_default()
@mock.patch('nova.quota.DbQuotaDriver._get_usages',
side_effect=_get_fake_get_usages())
def test_get_project_quotas(self, mock_get_usages):
self.maxDiff = None
self._stub_get_by_project()
ctxt = FakeContext('test_project', 'test_class')
result = self.driver.get_project_quotas(
ctxt, quota.QUOTAS._resources, 'test_project')
self.assertEqual(self.calls, [
'quota_get_all_by_project',
'quota_class_get_all_by_name',
'quota_class_get_default',
])
mock_get_usages.assert_called_once_with(ctxt, quota.QUOTAS._resources,
'test_project')
self.assertEqual(result, dict(
instances=dict(
limit=5,
in_use=2,
),
cores=dict(
limit=10,
in_use=4,
),
ram=dict(
limit=25 * 1024,
in_use=10 * 1024,
),
floating_ips=dict(
limit=-1,
in_use=0,
),
fixed_ips=dict(
limit=-1,
in_use=0,
),
metadata_items=dict(
limit=64,
in_use=0,
),
injected_files=dict(
limit=2,
in_use=0,
),
injected_file_content_bytes=dict(
limit=5 * 1024,
in_use=0,
),
injected_file_path_bytes=dict(
limit=127,
in_use=0,
),
security_groups=dict(
limit=-1,
in_use=0,
),
security_group_rules=dict(
limit=-1,
in_use=0,
),
key_pairs=dict(
limit=100,
in_use=2,
),
server_groups=dict(
limit=10,
in_use=0,
),
server_group_members=dict(
limit=10,
in_use=3,
),
))
@mock.patch('nova.quota.DbQuotaDriver._get_usages',
side_effect=_get_fake_get_usages())
def test_get_project_quotas_with_remains(self, mock_get_usages):
self.maxDiff = None
self._stub_get_by_project()
ctxt = FakeContext('test_project', 'test_class')
result = self.driver.get_project_quotas(
ctxt, quota.QUOTAS._resources, 'test_project', remains=True)
self.assertEqual(self.calls, [
'quota_get_all_by_project',
'quota_class_get_all_by_name',
'quota_class_get_default',
'quota_get_all',
])
mock_get_usages.assert_called_once_with(ctxt, quota.QUOTAS._resources,
'test_project')
self.assertEqual(result, dict(
instances=dict(
limit=5,
in_use=2,
remains=0,
),
cores=dict(
limit=10,
in_use=4,
remains=8,
),
ram=dict(
limit=25 * 1024,
in_use=10 * 1024,
remains=25 * 1024,
),
floating_ips=dict(
limit=-1,
in_use=0,
remains=-1,
),
fixed_ips=dict(
limit=-1,
in_use=0,