|
12 | 12 | # License for the specific language governing permissions and limitations
|
13 | 13 | # under the License.
|
14 | 14 |
|
15 |
| -from unittest import mock |
| 15 | +import os |
| 16 | +import shutil |
| 17 | +import tempfile |
16 | 18 |
|
17 | 19 | import fixtures
|
18 | 20 | from oslo_config import fixture as config_fixture
|
|
24 | 26 | [genericswitch:foo]
|
25 | 27 | device_type = foo_device
|
26 | 28 | spam = eggs
|
| 29 | +""" |
27 | 30 |
|
| 31 | +fake_config_bar = """ |
28 | 32 | [genericswitch:bar]
|
29 | 33 | device_type = bar_device
|
30 | 34 | ham = vikings
|
31 | 35 | """
|
32 | 36 |
|
| 37 | +fake_config_baz = """ |
| 38 | +[genericswitch:baz] |
| 39 | +device_type = baz_device |
| 40 | +truffle = brandy |
| 41 | +""" |
| 42 | + |
33 | 43 |
|
34 | 44 | class TestConfig(fixtures.TestWithFixtures):
|
35 | 45 | def setUp(self):
|
36 | 46 | 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 | + |
37 | 65 | 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}"]) |
50 | 68 |
|
51 | 69 | def test_get_devices(self):
|
52 | 70 | device_list = config.get_devices()
|
53 |
| - self.assertEqual(set(device_list), set(['foo', 'bar'])) |
| 71 | + self.assertEqual(set(device_list), set(['foo', 'bar', 'baz'])) |
54 | 72 | self.assertEqual({"device_type": "foo_device", "spam": "eggs"},
|
55 | 73 | device_list['foo'])
|
56 | 74 | self.assertEqual({"device_type": "bar_device", "ham": "vikings"},
|
57 | 75 | device_list['bar'])
|
| 76 | + self.assertEqual({"device_type": "baz_device", "truffle": "brandy"}, |
| 77 | + device_list['baz']) |
0 commit comments