From e58af8e32d415e112e491abce9f4e8b8d882fb80 Mon Sep 17 00:00:00 2001 From: zhaolihui Date: Tue, 1 Nov 2016 16:09:04 +0800 Subject: [PATCH] Fix pci_alias that include white spaces When the PCI alias was getting translated to a PCI request, all the white spaces were getting removed. So the pci_passthrough:alias specified in the flavor never matched the device in pci_alias. This patch fixes the bug by only stripping the white spaces at the beginning or the end of the elements Conflicts: nova/tests/unit/pci/test_request.py NOTE(lyarwood): Test conflict caused by the reorganisation of PCI options as part of I886045ab4e6bdb8418fd1ccdcd811417ecb4ad4a. Change-Id: I2888b03faf6c25bcb6011db40100d925972c2231 Closes-Bug: #1638200 (cherry picked from commit 9b88e755d93200811487d2f778735869f8fe07af) --- nova/pci/request.py | 6 ++++-- nova/tests/unit/pci/test_request.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/nova/pci/request.py b/nova/pci/request.py index 3712f5877ec3..f9703b9962b6 100644 --- a/nova/pci/request.py +++ b/nova/pci/request.py @@ -103,7 +103,9 @@ def _get_alias_from_config(): for jsonspecs in jaliases: spec = jsonutils.loads(jsonspecs) jsonschema.validate(spec, _ALIAS_SCHEMA) - name = spec.pop("name") + # It should keep consistent behaviour in configuration + # and extra specs to call strip() function. + name = spec.pop("name").strip() dev_type = spec.pop('device_type', None) if dev_type: spec['dev_type'] = dev_type @@ -130,8 +132,8 @@ def _translate_alias_to_requests(alias_spec): pci_aliases = _get_alias_from_config() pci_requests = [] # list of a specs dict - alias_spec = alias_spec.replace(' ', '') for name, count in [spec.split(':') for spec in alias_spec.split(',')]: + name = name.strip() if name not in pci_aliases: raise exception.PciRequestAliasNotDefined(alias=name) else: diff --git a/nova/tests/unit/pci/test_request.py b/nova/tests/unit/pci/test_request.py index 8424c4445cd6..4437a6c21611 100644 --- a/nova/tests/unit/pci/test_request.py +++ b/nova/tests/unit/pci/test_request.py @@ -52,6 +52,14 @@ _fake_alias3 = """{ "device_type": "type-PF" }""" +_fake_alias4 = """{ + "name": " Cirrus Logic ", + "capability_type": "pci", + "product_id": "0ff2", + "vendor_id": "10de", + "device_type": "type-PCI" + }""" + class AliasTestCase(test.NoDBTestCase): def test_good_alias(self): @@ -202,6 +210,28 @@ class AliasTestCase(test.NoDBTestCase): set([p.count for p in requests.requests])) self._verify_result(expect_request, requests.requests) + def test_get_pci_requests_from_flavor_including_space(self): + self.flags(pci_alias=[_fake_alias3, _fake_alias4]) + expect_request = [ + {'count': 4, + 'spec': [{'vendor_id': '10de', 'product_id': '0ff2', + 'dev_type': "type-PCI", + 'capability_type': 'pci'}], + 'alias_name': 'Cirrus Logic'}, + + {'count': 3, + 'spec': [{'vendor_id': '8086', 'product_id': '1111', + 'dev_type': "type-PF", + 'capability_type': 'pci'}], + 'alias_name': 'IntelNIC'}, ] + + flavor = {'extra_specs': {"pci_passthrough:alias": + " Cirrus Logic : 4, IntelNIC: 3"}} + requests = request.get_pci_requests_from_flavor(flavor) + self.assertEqual(set([3, 4]), + set([p.count for p in requests.requests])) + self._verify_result(expect_request, requests.requests) + def test_get_pci_requests_from_flavor_no_extra_spec(self): self.flags(pci_alias=[_fake_alias1, _fake_alias3]) flavor = {}