Skip to content

Commit 258b5b3

Browse files
authored
Merge pull request #116 from stackhpc/upstream/master-2025-05-12
Synchronise master with upstream
2 parents 50c6bbf + 96c1f93 commit 258b5b3

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

networking_generic_switch/config.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
import glob
16+
import os
17+
1518
from oslo_config import cfg
1619
from oslo_log import log as logging
1720

@@ -39,13 +42,24 @@
3942
CONF.register_opts(ngs_opts, group='ngs')
4043

4144

45+
def config_files():
46+
"""Generate which yields all config files in the required order"""
47+
for config_file in CONF.config_file:
48+
yield config_file
49+
for config_dir in CONF.config_dir:
50+
config_dir_glob = os.path.join(config_dir, '*.conf')
51+
for config_file in sorted(glob.glob(config_dir_glob)):
52+
yield config_file
53+
54+
4255
def get_devices():
4356
"""Parse supplied config files and fetch defined supported devices."""
4457

4558
device_tag = 'genericswitch:'
4659
devices = {}
4760

48-
for filename in CONF.config_file:
61+
for filename in config_files():
62+
LOG.debug(f'Searching for genericswitch config in: {filename}')
4963
sections = {}
5064
parser = cfg.ConfigParser(filename, sections)
5165
try:
@@ -54,6 +68,7 @@ def get_devices():
5468
continue
5569
for parsed_item, parsed_value in sections.items():
5670
if parsed_item.startswith(device_tag):
71+
LOG.debug(f'Found genericswitch config: {parsed_item}')
5772
dev_id = parsed_item.partition(device_tag)[2]
5873
device_cfg = {k: v[0] for k, v
5974
in parsed_value.items()}

networking_generic_switch/tests/unit/test_config.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from unittest import mock
15+
import os
16+
import shutil
17+
import tempfile
1618

1719
import fixtures
1820
from oslo_config import fixture as config_fixture
@@ -24,34 +26,52 @@
2426
[genericswitch:foo]
2527
device_type = foo_device
2628
spam = eggs
29+
"""
2730

31+
fake_config_bar = """
2832
[genericswitch:bar]
2933
device_type = bar_device
3034
ham = vikings
3135
"""
3236

37+
fake_config_baz = """
38+
[genericswitch:baz]
39+
device_type = baz_device
40+
truffle = brandy
41+
"""
42+
3343

3444
class TestConfig(fixtures.TestWithFixtures):
3545
def setUp(self):
3646
super(TestConfig, self).setUp()
47+
48+
config_file_foo = tempfile.NamedTemporaryFile(
49+
suffix=".conf", prefix="ngs-", delete=False).name
50+
self.addCleanup(os.remove, config_file_foo)
51+
52+
config_dir = tempfile.mkdtemp('-ngs', 'bar-')
53+
self.addCleanup(shutil.rmtree, config_dir)
54+
55+
config_file_bar = os.path.join(config_dir, 'bar.conf')
56+
config_file_baz = os.path.join(config_dir, 'baz.conf')
57+
58+
with open(config_file_foo, 'w') as f:
59+
f.write(fake_config)
60+
with open(config_file_bar, 'w') as f:
61+
f.write(fake_config_bar)
62+
with open(config_file_baz, 'w') as f:
63+
f.write(fake_config_baz)
64+
3765
self.cfg = self.useFixture(config_fixture.Config())
38-
self._patch_open()
39-
self.cfg.conf(args=["--config-file=/some/config/path"])
40-
41-
def _patch_open(self):
42-
m = mock.mock_open(read_data=fake_config)
43-
# NOTE(pas-ha) mocks and iterators work differently in Py2 and Py3
44-
# http://bugs.python.org/issue21258
45-
m.return_value.__iter__ = lambda self: self
46-
m.return_value.__next__ = lambda self: next(iter(self.readline, ''))
47-
patcher = mock.patch('oslo_config.cfg.open', m)
48-
patcher.start()
49-
self.addCleanup(patcher.stop)
66+
self.cfg.conf(args=[f"--config-file={config_file_foo}",
67+
f"--config-dir={config_dir}"])
5068

5169
def test_get_devices(self):
5270
device_list = config.get_devices()
53-
self.assertEqual(set(device_list), set(['foo', 'bar']))
71+
self.assertEqual(set(device_list), set(['foo', 'bar', 'baz']))
5472
self.assertEqual({"device_type": "foo_device", "spam": "eggs"},
5573
device_list['foo'])
5674
self.assertEqual({"device_type": "bar_device", "ham": "vikings"},
5775
device_list['bar'])
76+
self.assertEqual({"device_type": "baz_device", "truffle": "brandy"},
77+
device_list['baz'])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Switch configuration groups were only found in files specified by
5+
`--config-file` arguments. Configuration files in `--config-dir`
6+
directories are now also included.

0 commit comments

Comments
 (0)