From 262ee39e14075068a0208bfcb478d95d65658794 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Tue, 17 Sep 2019 13:40:55 +0530 Subject: [PATCH] ceph-volume: fix warnings raised by pytest Warnings are related to configparser. Update tests too. Fixes: https://tracker.ceph.com/issues/41907 Signed-off-by: Rishabh Dave (cherry picked from commit 31a0c7e77bf5d0193e90e6693b424ca5305b47cc) --- src/ceph-volume/ceph_volume/configuration.py | 22 ++++++++++++++++--- .../ceph_volume/tests/test_configuration.py | 10 ++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ceph-volume/ceph_volume/configuration.py b/src/ceph-volume/ceph_volume/configuration.py index 6379ef67a7d82..a9b908e1c0582 100644 --- a/src/ceph-volume/ceph_volume/configuration.py +++ b/src/ceph-volume/ceph_volume/configuration.py @@ -8,6 +8,14 @@ import os import re from ceph_volume import terminal, conf from ceph_volume import exceptions +from sys import version_info as sys_version_info + +if sys_version_info.major >= 3: + conf_parentclass = configparser.ConfigParser +elif sys_version_info.major < 3: + conf_parentclass = configparser.SafeConfigParser +else: + raise RuntimeError('Not expecting python version > 3 yet.') logger = logging.getLogger(__name__) @@ -50,7 +58,7 @@ def load(abspath=None): ceph_file = open(abspath) trimmed_conf = _TrimIndentFile(ceph_file) with contextlib.closing(ceph_file): - parser.readfp(trimmed_conf) + parser.read_conf(trimmed_conf) conf.ceph = parser return parser except configparser.ParsingError as error: @@ -59,9 +67,9 @@ def load(abspath=None): raise RuntimeError('Unable to read configuration file: %s' % abspath) -class Conf(configparser.SafeConfigParser): +class Conf(conf_parentclass): """ - Subclasses from SafeConfigParser to give a few helpers for Ceph + Subclasses from ConfigParser to give a few helpers for Ceph configuration. """ @@ -215,3 +223,11 @@ class Conf(configparser.SafeConfigParser): for name, val in options.items(): if isinstance(val, list): options[name] = '\n'.join(val) + + def read_conf(self, conffile): + if sys_version_info.major >= 3: + self.read_file(conffile) + elif sys_version_info.major < 3: + self.readfp(conffile) + else: + raise RuntimeError('Not expecting python version > 3 yet.') diff --git a/src/ceph-volume/ceph_volume/tests/test_configuration.py b/src/ceph-volume/ceph_volume/tests/test_configuration.py index 2e26ead7c82ed..9af6cd9bedf81 100644 --- a/src/ceph-volume/ceph_volume/tests/test_configuration.py +++ b/src/ceph-volume/ceph_volume/tests/test_configuration.py @@ -28,13 +28,13 @@ class TestConf(object): def test_get_non_existing_list(self): cfg = configuration.Conf() cfg.is_valid = lambda: True - cfg.readfp(self.conf_file) + cfg.read_conf(self.conf_file) assert cfg.get_list('global', 'key') == [] def test_get_non_existing_list_get_default(self): cfg = configuration.Conf() cfg.is_valid = lambda: True - cfg.readfp(self.conf_file) + cfg.read_conf(self.conf_file) assert cfg.get_list('global', 'key', ['a']) == ['a'] def test_get_rid_of_comments(self): @@ -45,7 +45,7 @@ class TestConf(object): default = 0 # this is a comment """)) - cfg.readfp(conf_file) + cfg.read_conf(conf_file) assert cfg.get_list('foo', 'default') == ['0'] def test_gets_split_on_commas(self): @@ -56,7 +56,7 @@ class TestConf(object): default = 0,1,2,3 # this is a comment """)) - cfg.readfp(conf_file) + cfg.read_conf(conf_file) assert cfg.get_list('foo', 'default') == ['0', '1', '2', '3'] def test_spaces_and_tabs_are_ignored(self): @@ -67,7 +67,7 @@ class TestConf(object): default = 0, 1, 2 ,3 # this is a comment """)) - cfg.readfp(conf_file) + cfg.read_conf(conf_file) assert cfg.get_list('foo', 'default') == ['0', '1', '2', '3'] -- 2.39.5