From 7766bef0b90e37c66a9e7d4b5828a09dd1334edd Mon Sep 17 00:00:00 2001 From: James Page Date: Tue, 20 Sep 2016 14:48:30 +0100 Subject: [PATCH] Fixup py3 compatibility --- ceph/__init__.py | 64 +++++++++++++++-------------- ceph/ceph_broker.py | 4 +- test-requirements.txt | 1 - tox.ini | 12 +++++- unit_tests/test_ceph.py | 6 +-- unit_tests/test_ceph_broker.py | 1 - unit_tests/test_mon_upgrade_roll.py | 7 ++-- 7 files changed, 53 insertions(+), 42 deletions(-) diff --git a/ceph/__init__.py b/ceph/__init__.py index d2d6b86..522e087 100644 --- a/ceph/__init__.py +++ b/ceph/__init__.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import ctypes +import collections import json import random import socket @@ -54,7 +55,8 @@ LEADER = 'leader' PEON = 'peon' QUORUM = [LEADER, PEON] -PACKAGES = ['ceph', 'gdisk', 'ntp', 'btrfs-tools', 'python-ceph', 'radosgw', 'xfsprogs'] +PACKAGES = ['ceph', 'gdisk', 'ntp', 'btrfs-tools', 'python-ceph', + 'radosgw', 'xfsprogs'] LinkSpeed = { "BASE_10": 10, @@ -934,36 +936,36 @@ def get_mds_key(name): caps=mds_caps) -_default_caps = { - 'mon': ['allow rw'], - 'osd': ['allow rwx'] -} +_default_caps = collections.OrderedDict([ + ('mon', ['allow rw']), + ('osd', ['allow rwx']), +]) -admin_caps = { - 'mds': ['allow *'], - 'mon': ['allow *'], - 'osd': ['allow *'] -} +admin_caps = collections.OrderedDict([ + ('mds', ['allow *']), + ('mon', ['allow *']), + ('osd', ['allow *']) +]) -mds_caps = { - 'osd': ['allow *'], - 'mds': ['allow'], - 'mon': ['allow rwx'], -} +mds_caps = collections.OrderedDict([ + ('osd', ['allow *']), + ('mds', ['allow']), + ('mon', ['allow rwx']), +]) -osd_upgrade_caps = { - 'mon': ['allow command "config-key"', - 'allow command "osd tree"', - 'allow command "config-key list"', - 'allow command "config-key put"', - 'allow command "config-key get"', - 'allow command "config-key exists"', - 'allow command "osd out"', - 'allow command "osd in"', - 'allow command "osd rm"', - 'allow command "auth del"', - ] -} +osd_upgrade_caps = collections.OrderedDict([ + ('mon', ['allow command "config-key"', + 'allow command "osd tree"', + 'allow command "config-key list"', + 'allow command "config-key put"', + 'allow command "config-key get"', + 'allow command "config-key exists"', + 'allow command "osd out"', + 'allow command "osd in"', + 'allow command "osd rm"', + 'allow command "auth del"', + ]) +]) def create_named_keyring(entity, name, caps=None): @@ -981,7 +983,7 @@ def create_named_keyring(entity, name, caps=None): 'auth', 'get-or-create', '{entity}.{name}'.format(entity=entity, name=name), ] - for subsystem, subcaps in caps.iteritems(): + for subsystem, subcaps in caps.items(): cmd.extend([subsystem, '; '.join(subcaps)]) log("Calling subprocess.check_output: {}".format(cmd), level=DEBUG) return parse_key(subprocess.check_output(cmd).strip()) # IGNORE:E1103 @@ -1013,7 +1015,7 @@ def get_named_key(name, caps=None, pool_list=None): 'auth', 'get-or-create', 'client.{}'.format(name), ] # Add capabilities - for subsystem, subcaps in caps.iteritems(): + for subsystem, subcaps in caps.items(): if subsystem == 'osd': if pool_list: # This will output a string similar to: @@ -1033,7 +1035,7 @@ def upgrade_key_caps(key, caps): cmd = [ "sudo", "-u", ceph_user(), 'ceph', 'auth', 'caps', key ] - for subsystem, subcaps in caps.iteritems(): + for subsystem, subcaps in caps.items(): cmd.extend([subsystem, '; '.join(subcaps)]) subprocess.check_call(cmd) diff --git a/ceph/ceph_broker.py b/ceph/ceph_broker.py index 51f06c0..0ed9833 100644 --- a/ceph/ceph_broker.py +++ b/ceph/ceph_broker.py @@ -60,8 +60,8 @@ POOL_KEYS = { "write_fadvise_dontneed": [bool], "noscrub": [bool], "nodeep-scrub": [bool], - "hit_set_type": [basestring, ["bloom", "explicit_hash", - "explicit_object"]], + "hit_set_type": [str, ["bloom", "explicit_hash", + "explicit_object"]], "hit_set_count": [int, [1, 1]], "hit_set_period": [int], "hit_set_fpp": [float, [0.0, 1.0]], diff --git a/test-requirements.txt b/test-requirements.txt index ee07a9d..316dc4a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -5,6 +5,5 @@ coverage>=3.6 mock>=1.2 flake8>=2.2.4,<=2.4.1 os-testr>=0.4.1 -charm-tools>=2.0.0 requests==2.6.0 netifaces diff --git a/tox.ini b/tox.ini index c3ac854..cc18059 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = pep8,py27 +envlist = pep8,py27,py34,py35 skipsdist = True [testenv] @@ -15,6 +15,16 @@ basepython = python2.7 deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt +[testenv:py34] +basepython = python3.4 +deps = -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt + +[testenv:py35] +basepython = python3.5 +deps = -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt + [testenv:pep8] basepython = python2.7 deps = -r{toxinidir}/requirements.txt diff --git a/unit_tests/test_ceph.py b/unit_tests/test_ceph.py index 74eea79..2f24116 100644 --- a/unit_tests/test_ceph.py +++ b/unit_tests/test_ceph.py @@ -15,7 +15,7 @@ import mock import unittest -import ceph.ceph as ceph +import ceph class CephTestCase(unittest.TestCase): @@ -26,7 +26,7 @@ class CephTestCase(unittest.TestCase): with mock.patch.object(ceph, "ceph_user", return_value="ceph"): with mock.patch.object(ceph.subprocess, "check_output") \ as subprocess: - with mock.patch.object(ceph, "get_unit_hostname", + with mock.patch.object(ceph.socket, "gethostname", return_value="osd001"): ceph.get_named_key(name="rgw001", pool_list=["rbd", "block"]) @@ -43,7 +43,7 @@ class CephTestCase(unittest.TestCase): with mock.patch.object(ceph, "ceph_user", return_value="ceph"): with mock.patch.object(ceph.subprocess, "check_output") \ as subprocess: - with mock.patch.object(ceph, "get_unit_hostname", + with mock.patch.object(ceph.socket, "gethostname", return_value="osd001"): ceph.get_named_key(name="rgw001") subprocess.assert_called_with( diff --git a/unit_tests/test_ceph_broker.py b/unit_tests/test_ceph_broker.py index 81ce8f7..7772443 100644 --- a/unit_tests/test_ceph_broker.py +++ b/unit_tests/test_ceph_broker.py @@ -41,7 +41,6 @@ class CephBrokerTestCase(unittest.TestCase): def test_process_requests_invalid_api_version(self, mock_log): req = json.dumps({'api-version': 2, 'ops': []}) rc = ceph_broker.process_requests(req) - print "Return: %s" % rc self.assertEqual(json.loads(rc), {'exit-code': 1, 'stderr': 'Missing or invalid api version (2)'}) diff --git a/unit_tests/test_mon_upgrade_roll.py b/unit_tests/test_mon_upgrade_roll.py index c20e9d2..c2a29bb 100644 --- a/unit_tests/test_mon_upgrade_roll.py +++ b/unit_tests/test_mon_upgrade_roll.py @@ -55,7 +55,7 @@ class UpgradeRollingTestCase(unittest.TestCase): @patch('ceph.upgrade_monitor') @patch('ceph.monitor_key_set') def test_lock_and_roll(self, monitor_key_set, upgrade_monitor, log, time): - time.return_value = 1473279502.688828 + time.return_value = 1473279502.69 monitor_key_set.monitor_key_set.return_value = None ceph.lock_and_roll(my_name='ip-192-168-1-2', version='0.94.1', @@ -67,7 +67,9 @@ class UpgradeRollingTestCase(unittest.TestCase): call('monitor_key_set ' 'mon_ip-192-168-1-2_0.94.1_start 1473279502.69'), call('Rolling'), - call('Done') + call('Done'), + call('monitor_key_set ' + 'mon_ip-192-168-1-2_0.94.1_done 1473279502.69'), ]) @patch('ceph.apt_install') @@ -87,7 +89,6 @@ class UpgradeRollingTestCase(unittest.TestCase): local_mons, add_source, apt_update, status_set, log, service_start, service_stop, chownr, apt_install): - print "Upgrading monitor" get_version.return_value = "0.80" config.side_effect = config_side_effect ceph_user.return_value = "ceph"