summaryrefslogtreecommitdiff
path: root/nova/tests/compute/test_claims.py
blob: a5b7a0e46cbefab49cbadf33b2f1ed3e2541ae68 (plain)
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
# Copyright (c) 2012 OpenStack Foundation
# 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.

"""Tests for resource tracker claims."""

import re
import uuid

import mock
import six

from nova.compute import claims
from nova import db
from nova import exception
from nova import objects
from nova.openstack.common import jsonutils
from nova.pci import pci_manager
from nova import test
from nova.tests.pci import pci_fakes
from nova.virt import hardware


class FakeResourceHandler(object):
    test_called = False
    usage_is_instance = False

    def test_resources(self, usage, limits):
        self.test_called = True
        self.usage_is_itype = usage.get('name') is 'fakeitype'
        return []


class DummyTracker(object):
    icalled = False
    rcalled = False
    pci_tracker = pci_manager.PciDevTracker()
    ext_resources_handler = FakeResourceHandler()

    def abort_instance_claim(self, *args, **kwargs):
        self.icalled = True

    def drop_resize_claim(self, *args, **kwargs):
        self.rcalled = True

    def new_pci_tracker(self):
        self.pci_tracker = pci_manager.PciDevTracker()


@mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid',
            return_value=objects.InstancePCIRequests(requests=[]))
class ClaimTestCase(test.NoDBTestCase):

    def setUp(self):
        super(ClaimTestCase, self).setUp()
        self.resources = self._fake_resources()
        self.tracker = DummyTracker()

    def _claim(self, limits=None, overhead=None, **kwargs):
        numa_topology = kwargs.pop('numa_topology', None)
        instance = self._fake_instance(**kwargs)
        if numa_topology:
            db_numa_topology = {
                    'id': 1, 'created_at': None, 'updated_at': None,
                    'deleted_at': None, 'deleted': None,
                    'instance_uuid': instance['uuid'],
                    'numa_topology': numa_topology.to_json()
                }
        else:
            db_numa_topology = None
        if overhead is None:
            overhead = {'memory_mb': 0}
        with mock.patch.object(
                db, 'instance_extra_get_by_instance_uuid',
                return_value=db_numa_topology):
            return claims.Claim('context', instance, self.tracker,
                                self.resources, overhead=overhead,
                                limits=limits)

    def _fake_instance(self, **kwargs):
        instance = {
            'uuid': str(uuid.uuid1()),
            'memory_mb': 1024,
            'root_gb': 10,
            'ephemeral_gb': 5,
            'vcpus': 1,
            'system_metadata': {},
            'numa_topology': None
        }
        instance.update(**kwargs)
        return instance

    def _fake_instance_type(self, **kwargs):
        instance_type = {
            'id': 1,
            'name': 'fakeitype',
            'memory_mb': 1,
            'vcpus': 1,
            'root_gb': 1,
            'ephemeral_gb': 2
        }
        instance_type.update(**kwargs)
        return instance_type

    def _fake_resources(self, values=None):
        resources = {
            'memory_mb': 2048,
            'memory_mb_used': 0,
            'free_ram_mb': 2048,
            'local_gb': 20,
            'local_gb_used': 0,
            'free_disk_gb': 20,
            'vcpus': 2,
            'vcpus_used': 0,
            'numa_topology': hardware.VirtNUMAHostTopology(
                cells=[hardware.VirtNUMATopologyCellUsage(1, [1, 2], 512),
                       hardware.VirtNUMATopologyCellUsage(2, [3, 4], 512)]
                ).to_json()
        }
        if values:
            resources.update(values)
        return resources

    # TODO(lxsli): Remove once Py2.6 is deprecated
    def assertRaisesRegexp(self, re_obj, e, fn, *a, **kw):
        try:
            fn(*a, **kw)
            self.fail("Expected exception not raised")
        except e as ee:
            self.assertTrue(re.search(re_obj, six.text_type(ee)))

    def test_memory_unlimited(self, mock_get):
        self._claim(memory_mb=99999999)

    def test_disk_unlimited_root(self, mock_get):
        self._claim(root_gb=999999)

    def test_disk_unlimited_ephemeral(self, mock_get):
        self._claim(ephemeral_gb=999999)

    def test_memory_with_overhead(self, mock_get):
        overhead = {'memory_mb': 8}
        limits = {'memory_mb': 2048}
        self._claim(memory_mb=2040, limits=limits,
                    overhead=overhead)

    def test_memory_with_overhead_insufficient(self, mock_get):
        overhead = {'memory_mb': 9}
        limits = {'memory_mb': 2048}

        self.assertRaises(exception.ComputeResourcesUnavailable,
                          self._claim, limits=limits, overhead=overhead,
                          memory_mb=2040)

    def test_memory_oversubscription(self, mock_get):
        self._claim(memory_mb=4096)

    def test_memory_insufficient(self, mock_get):
        limits = {'memory_mb': 8192}
        self.assertRaises(exception.ComputeResourcesUnavailable,
                          self._claim, limits=limits, memory_mb=16384)

    def test_disk_oversubscription(self, mock_get):
        limits = {'disk_gb': 60}
        self._claim(root_gb=10, ephemeral_gb=40,
                    limits=limits)

    def test_disk_insufficient(self, mock_get):
        limits = {'disk_gb': 45}
        self.assertRaisesRegexp(re.compile("disk", re.IGNORECASE),
                exception.ComputeResourcesUnavailable,
                self._claim, limits=limits, root_gb=10, ephemeral_gb=40)

    def test_disk_and_memory_insufficient(self, mock_get):
        limits = {'disk_gb': 45, 'memory_mb': 8192}
        self.assertRaisesRegexp(re.compile("memory.*disk", re.IGNORECASE),
                exception.ComputeResourcesUnavailable,
                self._claim, limits=limits, root_gb=10, ephemeral_gb=40,
                memory_mb=16384)

    @pci_fakes.patch_pci_whitelist
    def test_pci_pass(self, mock_get):
        dev_dict = {
            'compute_node_id': 1,
            'address': 'a',
            'product_id': 'p',
            'vendor_id': 'v',
            'status': 'available'}
        self.tracker.new_pci_tracker()
        self.tracker.pci_tracker.set_hvdevs([dev_dict])
        claim = self._claim()
        request = objects.InstancePCIRequest(count=1,
            spec=[{'vendor_id': 'v', 'product_id': 'p'}])
        mock_get.return_value = objects.InstancePCIRequests(
            requests=[request])
        self.assertIsNone(claim._test_pci())

    @pci_fakes.patch_pci_whitelist
    def test_pci_fail(self, mock_get):
        dev_dict = {
            'compute_node_id': 1,
            'address': 'a',
            'product_id': 'p',
            'vendor_id': 'v1',
            'status': 'available'}
        self.tracker.new_pci_tracker()
        self.tracker.pci_tracker.set_hvdevs([dev_dict])
        claim = self._claim()
        request = objects.InstancePCIRequest(count=1,
            spec=[{'vendor_id': 'v', 'product_id': 'p'}])
        mock_get.return_value = objects.InstancePCIRequests(
            requests=[request])
        claim._test_pci()

    @pci_fakes.patch_pci_whitelist
    def test_pci_pass_no_requests(self, mock_get):
        dev_dict = {
            'compute_node_id': 1,
            'address': 'a',
            'product_id': 'p',
            'vendor_id': 'v',
            'status': 'available'}
        self.tracker.new_pci_tracker()
        self.tracker.pci_tracker.set_hvdevs([dev_dict])
        claim = self._claim()
        self.assertIsNone(claim._test_pci())

    def test_ext_resources(self, mock_get):
        self._claim()
        self.assertTrue(self.tracker.ext_resources_handler.test_called)
        self.assertFalse(self.tracker.ext_resources_handler.usage_is_itype)

    def test_numa_topology_no_limit(self, mock_get):
        huge_instance = hardware.VirtNUMAInstanceTopology(
                cells=[hardware.VirtNUMATopologyCell(
                    1, set([1, 2, 3, 4, 5]), 2048)])
        self._claim(numa_topology=huge_instance)

    def test_numa_topology_fails(self, mock_get):
        huge_instance = hardware.VirtNUMAInstanceTopology(
                cells=[hardware.VirtNUMATopologyCell(
                    1, set([1, 2, 3, 4, 5]), 2048)])
        limit_topo = hardware.VirtNUMALimitTopology(
                cells=[hardware.VirtNUMATopologyCellLimit(
                            1, [1, 2], 512, cpu_limit=2, memory_limit=512),
                       hardware.VirtNUMATopologyCellLimit(
                            1, [3, 4], 512, cpu_limit=2, memory_limit=512)])
        self.assertRaises(exception.ComputeResourcesUnavailable,
                          self._claim,
                          limits={'numa_topology': limit_topo.to_json()},
                          numa_topology=huge_instance)

    def test_numa_topology_passes(self, mock_get):
        huge_instance = hardware.VirtNUMAInstanceTopology(
                cells=[hardware.VirtNUMATopologyCell(
                    1, set([1, 2, 3, 4, 5]), 2048)])
        limit_topo = hardware.VirtNUMALimitTopology(
                cells=[hardware.VirtNUMATopologyCellLimit(
                            1, [1, 2], 512, cpu_limit=5, memory_limit=4096),
                       hardware.VirtNUMATopologyCellLimit(
                            1, [3, 4], 512, cpu_limit=5, memory_limit=4096)])
        self._claim(limits={'numa_topology': limit_topo.to_json()},
                    numa_topology=huge_instance)

    def test_abort(self, mock_get):
        claim = self._abort()
        self.assertTrue(claim.tracker.icalled)

    def _abort(self):
        claim = None
        try:
            with self._claim(memory_mb=4096) as claim:
                raise test.TestingException("abort")
        except test.TestingException:
            pass

        return claim


class ResizeClaimTestCase(ClaimTestCase):

    def setUp(self):
        super(ResizeClaimTestCase, self).setUp()
        self.instance = self._fake_instance()
        self.get_numa_constraint_patch = None

    def _claim(self, limits=None, overhead=None, **kwargs):
        instance_type = self._fake_instance_type(**kwargs)
        numa_constraint = kwargs.pop('numa_topology', None)
        if overhead is None:
            overhead = {'memory_mb': 0}
        with mock.patch.object(
                hardware.VirtNUMAInstanceTopology, 'get_constraints',
                return_value=numa_constraint):
            return claims.ResizeClaim('context', self.instance, instance_type,
                                      {}, self.tracker, self.resources,
                                      overhead=overhead, limits=limits)

    def _set_pci_request(self, claim):
        request = [{'count': 1,
                       'spec': [{'vendor_id': 'v', 'product_id': 'p'}],
                      }]
        claim.instance.update(
            system_metadata={'new_pci_requests': jsonutils.dumps(request)})

    @mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid',
                return_value=objects.InstancePCIRequests(requests=[]))
    def test_ext_resources(self, mock_get):
        self._claim()
        self.assertTrue(self.tracker.ext_resources_handler.test_called)
        self.assertTrue(self.tracker.ext_resources_handler.usage_is_itype)

    @mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid',
                return_value=objects.InstancePCIRequests(requests=[]))
    def test_abort(self, mock_get):
        claim = self._abort()
        self.assertTrue(claim.tracker.rcalled)