Allow duplicate keys in nova_cfg with ConfigParser(strict=False)

Use ConfigParser(strict=False) to allow duplicate keys in nova.conf.
This avoids errors when options like 'alias' are specified multiple times
(as documented in Nova PCI config).

Closes-Bug: #2120507

Change-Id: I6dba02a0b44b89927691f72485cb6347006edc0b
Signed-off-by: Seyeong Kim <seyeong.kim@canonical.com>
This commit is contained in:
Seyeong Kim
2025-08-19 00:05:45 +00:00
parent 7bc0ef9b57
commit b57b99dd7d
2 changed files with 27 additions and 1 deletions

View File

@@ -32,7 +32,14 @@ def _nova_cfg():
:return: Parsed nova config
:rtype: configparser.ConfigParser
"""
nova_cfg = configparser.ConfigParser()
# seyeongkim, bui: ConfigParser raises an error
# when it encounters duplicate keys.
# Use strict=False to avoid this error.
# e.g 'alias' can be specified multiple times
# as shown in the Nova documentation:
# https://docs.openstack.org/nova/latest/configuration/config.html#pci
# The charm itself doesn't use multiple keys, so try to avoid them.
nova_cfg = configparser.ConfigParser(strict=False)
nova_cfg.read('/etc/nova/nova.conf')
return nova_cfg

View File

@@ -11,6 +11,8 @@
# 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.
import configparser
from unittest import TestCase
from unittest.mock import MagicMock, patch
@@ -32,6 +34,23 @@ class NovaHypervisorMock:
self.running_vms = running_vms
class TestCloudUtilsNovaCfg(TestCase):
def test_nova_cfg_allows_duplicate_keys(self):
_conf = """
[pci]
alias = first
alias = second
"""
def fake_read(self, filename, encoding=None):
self.read_string(_conf)
return ['dummy']
with patch.object(configparser.ConfigParser, "read", new=fake_read):
cfg = cloud_utils._nova_cfg()
self.assertEqual(cfg["pci"]["alias"], "second")
class TestCloudUtils(TestCase):
def __init__(self, methodName='runTest'):