Skip to content

Commit

Permalink
Merge pull request #19259 from ceph/wip-rm22280
Browse files Browse the repository at this point in the history
ceph-volume: handle leading whitespace/tabs in ceph.conf

Reviewed-by: Andrew Schoen <aschoen@redhat.com>
  • Loading branch information
andrewschoen committed Dec 1, 2017
2 parents a91f350 + ec7f8a2 commit b7a9c44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/ceph-volume/ceph_volume/configuration.py
Expand Up @@ -2,6 +2,7 @@
import configparser
except ImportError:
import ConfigParser as configparser
import contextlib
import logging
import os
import re
Expand Down Expand Up @@ -31,14 +32,20 @@ def __iter__(self):


def load(abspath=None):
if not os.path.exists(abspath):
raise exceptions.ConfigurationError(abspath=abspath)

parser = Conf()
try:
parser.read_path(abspath)
return parser
ceph_file = open(abspath)
trimmed_conf = _TrimIndentFile(ceph_file)
with contextlib.closing(ceph_file):
parser.readfp(trimmed_conf)
return parser
except configparser.ParsingError as error:
terminal.error('Unable to read configuration file: %s' % abspath)
terminal.error(str(error))
logger.exception('Unable to parse INI-style file: %s' % abspath)
terminal.error(str(error))
raise RuntimeError('Unable to read configuration file: %s' % abspath)


class Conf(configparser.SafeConfigParser):
Expand All @@ -52,9 +59,6 @@ def read_path(self, path):
return self.read(path)

def is_valid(self):
if not os.path.exists(self.path):
raise exceptions.ConfigurationError(abspath=self.path)

try:
self.get('global', 'fsid')
except (configparser.NoSectionError, configparser.NoOptionError):
Expand Down
16 changes: 14 additions & 2 deletions src/ceph-volume/ceph_volume/tests/test_configuration.py
Expand Up @@ -7,6 +7,11 @@
import pytest
from ceph_volume import configuration, exceptions

tabbed_conf = """
[global]
default = 0
"""


class TestConf(object):

Expand Down Expand Up @@ -64,6 +69,13 @@ def test_spaces_and_tabs_are_ignored(self):

class TestLoad(object):

def test_load_from_path(self, tmpdir):
conf_path = os.path.join(str(tmpdir), 'ceph.conf')
with open(conf_path, 'w') as conf:
conf.write(tabbed_conf)
result = configuration.load(conf_path)
assert result.get('global', 'default') == '0'

def test_path_does_not_exist(self):
with pytest.raises(exceptions.ConfigurationError):
conf = configuration.load('/path/does/not/exist/ceph.con')
Expand All @@ -73,7 +85,7 @@ def test_unable_to_read_configuration(self, tmpdir, capsys):
ceph_conf = os.path.join(str(tmpdir), 'ceph.conf')
with open(ceph_conf, 'w') as config:
config.write(']broken] config\n[[')
configuration.load(ceph_conf)
with pytest.raises(RuntimeError):
configuration.load(ceph_conf)
stdout, stderr = capsys.readouterr()
assert 'Unable to read configuration file' in stdout
assert 'File contains no section headers' in stdout

0 comments on commit b7a9c44

Please sign in to comment.