Convert charm to Python 3

Change-Id: Id2a87f639619ecbdc9e02960e9530133c6816f78
This commit is contained in:
Alex Kavanagh 2019-03-12 14:56:10 +00:00
parent 9bd02348f4
commit 7cc006d262
13 changed files with 87 additions and 36 deletions

View File

@ -1,4 +1,3 @@
- project:
templates:
- python-charm-jobs
- openstack-python35-jobs-nonvoting
- python35-charm-jobs

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#
@ -17,6 +17,21 @@
import os
import sys
_path = os.path.dirname(os.path.realpath(__file__))
_hooks = os.path.abspath(os.path.join(_path, '../hooks'))
_root = os.path.abspath(os.path.join(_path, '..'))
_lib = os.path.abspath(os.path.join(_path, '../lib'))
def _add_path(path):
if path not in sys.path:
sys.path.insert(1, path)
_add_path(_hooks)
_add_path(_root)
_add_path(_lib)
from charmhelpers.core.hookenv import (
action_fail,
action_set,
@ -59,7 +74,7 @@ def ceilometer_upgrade(args):
action_set({'outcome': e.outcome})
if e.trace:
action_set({'traceback': e.trace})
raise Exception(str(e.message))
raise Exception(str(e))
assess_status(register_configs())

View File

@ -1 +0,0 @@
../charmhelpers

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#
@ -14,9 +14,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
sys.path.append('hooks/')
_path = os.path.dirname(os.path.realpath(__file__))
_hooks = os.path.abspath(os.path.join(_path, '../hooks'))
_root = os.path.abspath(os.path.join(_path, '..'))
def _add_path(path):
if path not in sys.path:
sys.path.insert(1, path)
_add_path(_hooks)
_add_path(_root)
from charmhelpers.contrib.openstack.utils import (
do_action_openstack_upgrade,

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#
@ -19,6 +19,18 @@ import subprocess
import sys
import os
_path = os.path.dirname(os.path.realpath(__file__))
_root = os.path.abspath(os.path.join(_path, '..'))
def _add_path(path):
if path not in sys.path:
sys.path.insert(1, path)
_add_path(_root)
from charmhelpers.fetch import (
apt_install,
apt_update,
@ -415,7 +427,7 @@ def ceilometer_joined():
# This value gets tranformed to a path by the context we need to
# pass the data to agents.
if 'rabbit_ssl_ca' in context:
with open(context['rabbit_ssl_ca']) as fh:
with open(context['rabbit_ssl_ca'], 'rt') as fh:
context['rabbit_ssl_ca'] = base64.b64encode(fh.read())
for relid in relation_ids('ceilometer-service'):
relation_set(relid, context)

View File

@ -1 +0,0 @@
../charmhelpers

View File

@ -16,7 +16,7 @@ check_and_install() {
fi
}
PYTHON="python"
PYTHON="python3"
for dep in ${DEPS[@]}; do
check_and_install ${PYTHON} ${dep}

View File

@ -475,7 +475,7 @@ def get_shared_secret():
secret = str(uuid.uuid4())
set_shared_secret(secret)
else:
with open(SHARED_SECRET, 'r') as secret_file:
with open(SHARED_SECRET, 'rt') as secret_file:
secret = secret_file.read().strip()
return secret
@ -486,7 +486,7 @@ def set_shared_secret(secret):
:param secret: the secret to set
"""
with open(SHARED_SECRET, 'w') as secret_file:
with open(SHARED_SECRET, 'wt') as secret_file:
secret_file.write(secret)

View File

@ -2,7 +2,7 @@
# This file is managed centrally by release-tools and should not be modified
# within individual charm repos.
[tox]
envlist = pep8,py27
envlist = pep8,py3{5,6}
skipsdist = True
[testenv]
@ -16,11 +16,6 @@ commands = stestr run {posargs}
whitelist_externals = juju
passenv = HOME TERM AMULET_* CS_API_*
[testenv:py27]
basepython = python2.7
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py35]
basepython = python3.5
deps = -r{toxinidir}/requirements.txt

View File

@ -12,6 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
sys.path.append('actions')
sys.path.append('hooks')
_path = os.path.dirname(os.path.realpath(__file__))
_actions = os.path.abspath(os.path.join(_path, '../actions'))
_hooks = os.path.abspath(os.path.join(_path, '../hooks'))
_charmhelpers = os.path.abspath(os.path.join(_path, '../charmhelpers'))
_unit_tests = os.path.abspath(os.path.join(_path, '../unit_tests'))
def _add_path(path):
if path not in sys.path:
sys.path.insert(1, path)
_add_path(_actions)
_add_path(_hooks)
_add_path(_charmhelpers)
_add_path(_unit_tests)

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import collections
from mock import patch, MagicMock
import ceilometer_contexts as contexts
@ -100,14 +101,13 @@ class CeilometerContextsTest(CharmTestCase):
def test_mongodb_context_related_replset_multiple_mongo(self):
self.relation_ids.return_value = ['shared-db:0']
related_units = {
'mongodb/0': {'hostname': 'mongodb-0',
'port': 8090,
'replset': 'replset-1'},
'mongodb/1': {'hostname': 'mongodb-1',
'port': 8090,
'replset': 'replset-1'}
}
related_units = collections.OrderedDict(
[('mongodb/0', {'hostname': 'mongodb-0',
'port': 8090,
'replset': 'replset-1'}),
('mongodb/1', {'hostname': 'mongodb-1',
'port': 8090,
'replset': 'replset-1'})])
self.related_units.return_value = [k for k in related_units.keys()]
def relation_get(attr, unit, relid):
@ -206,7 +206,7 @@ class CeilometerContextsTest(CharmTestCase):
def test_get_shared_secret_new(self, exists, uuid4):
exists.return_value = False
uuid4.return_value = 'newsecret'
with patch('__builtin__.open'):
with patch('builtins.open'):
self.assertEqual(utils.get_shared_secret(),
'newsecret')

View File

@ -422,12 +422,10 @@ class CeilometerUtilsTest(CharmTestCase):
utils.ceilometer_upgrade_helper(self.CONFIGS)
mock_ceilometer_upgrade.assert_not_called()
@patch.object(utils, 'subprocess')
@patch.object(utils, 'ceilometer_upgrade')
@patch('charmhelpers.core.hookenv.config')
def test_ceilometer_upgrade_helper_raise(self, mock_config,
mock_ceilometer_upgrade,
mock_subprocess):
mock_ceilometer_upgrade):
self.get_os_codename_install_source.return_value = 'ocata'
self.CONFIGS = MagicMock()
self.CONFIGS.complete_contexts.return_value = [
@ -435,8 +433,15 @@ class CeilometerUtilsTest(CharmTestCase):
'identity-service',
'mongodb'
]
# workaround Py3 constraint that raise only accepts an actual
# exception, so we have to patch CalledProcessError back onto the
# mocked out subprocess module
import subprocess
exc = subprocess.CalledProcessError
mock_ceilometer_upgrade.side_effect = utils.FailedAction("message")
with self.assertRaises(utils.FailedAction):
with patch.object(utils, 'subprocess') as subp, \
self.assertRaises(utils.FailedAction):
subp.CalledProcessError = exc
utils.ceilometer_upgrade_helper(self.CONFIGS)
mock_ceilometer_upgrade.assert_called_once_with(action=True)

View File

@ -30,7 +30,7 @@ def mock_open(filename, contents=None):
return io.StringIO(contents)
else:
return open(*args)
with patch('__builtin__.open', mock_file):
with patch('builtins.open', mock_file):
yield
@ -63,7 +63,7 @@ def get_default_config():
'''
default_config = {}
config = load_config()
for k, v in config.iteritems():
for k, v in config.items():
if 'default' in v:
default_config[k] = v['default']
else: