From f3acd81a314d4c2b491a44dd3df37661910e6c17 Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Wed, 13 Mar 2019 11:26:19 +0100 Subject: [PATCH] Use ``ceph-conf`` to retrieve default values The ``ceph`` command expects connection to a running cluster even if it does not use it. Change-Id: Ied3edf63706e2d48d2ea09056bc6d6508e9e3e0f Closes-Bug: #1819852 --- hooks/utils.py | 13 ++++++------- unit_tests/test_ceph_utils.py | 9 ++++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/hooks/utils.py b/hooks/utils.py index 158d5912..0ac0405f 100644 --- a/hooks/utils.py +++ b/hooks/utils.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import re import socket import subprocess @@ -183,14 +184,12 @@ def get_default_rbd_features(): :returns: Installed Ceph's Default vaule for ``rbd_default_features`` :rtype: int - :raises: subprocess.CalledProcessError + :raises: IndexError, json.JSONDecodeError, subprocess.CalledProcessError """ - output = subprocess.check_output( - ['ceph', '-c', '/dev/null', '--show-config'], - universal_newlines=True) - for line in output.splitlines(): - if 'rbd_default_features' in line: - return int(line.split('=')[1].lstrip().rstrip()) + ceph_conf = json.loads(subprocess.check_output( + ['ceph-conf', '-c', '/dev/null', '-D', '--format', 'json'], + universal_newlines=True)) + return int(ceph_conf['rbd_default_features']) def add_rbd_mirror_features(rbd_features): diff --git a/unit_tests/test_ceph_utils.py b/unit_tests/test_ceph_utils.py index 9765b8b6..076429a3 100644 --- a/unit_tests/test_ceph_utils.py +++ b/unit_tests/test_ceph_utils.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import mock import test_utils @@ -40,13 +41,15 @@ class CephUtilsTestCase(test_utils.CharmTestCase): @mock.patch.object(utils.subprocess, 'check_output') def test_get_default_rbd_features(self, _check_output): - _check_output.return_value = ('a = b\nrbd_default_features = 61\n' - 'c = d\n') + _check_output.return_value = json.dumps( + {'a': 'b', + 'rbd_default_features': '61', + 'c': 'd'}) self.assertEquals( utils.get_default_rbd_features(), 61) _check_output.assert_called_once_with( - ['ceph', '-c', '/dev/null', '--show-config'], + ['ceph-conf', '-c', '/dev/null', '-D', '--format', 'json'], universal_newlines=True) def test_add_mirror_rbd_features(self):