Update to run under Python 3

Updating charm to support execution under Python 3.

Change-Id: I9013860b5eefef05bee5b473e30217229fb6a739
This commit is contained in:
Author Name 2017-11-05 12:47:42 +11:00 committed by Liam Young
parent cfaa38909b
commit b24555e224
20 changed files with 94 additions and 79 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#

View File

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

View File

@ -75,7 +75,7 @@ def _save_flag_file(path, data):
# Wonder if we can move away from this now?
if data is None:
return
with open(path, 'wb') as out:
with open(path, 'wt') as out:
out.write(data)
@ -446,7 +446,7 @@ class CloudComputeContext(context.OSContextGenerator):
'neutron_url': url,
}
missing = [k for k, v in neutron_ctxt.iteritems() if v in ['', None]]
missing = [k for k, v in neutron_ctxt.items() if v in ['', None]]
if missing:
log('Missing required relation settings for Quantum: ' +
' '.join(missing))

View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
#
# Copyright 2016 Canonical Ltd
#

View File

@ -376,12 +376,12 @@ def restart_map():
Constructs a restart map based on charm config settings and relation
state.
'''
return {k: v['services'] for k, v in resource_map().iteritems()}
return {k: v['services'] for k, v in resource_map().items()}
def services():
''' Returns a list of services associated with this charm '''
return list(set(chain(*restart_map().itervalues())))
return list(set(chain(*restart_map().values())))
def register_configs():
@ -406,7 +406,7 @@ def register_configs():
install_alternative(os.path.basename(CEPH_CONF),
CEPH_CONF, ceph_config_file())
for cfg, d in resource_map().iteritems():
for cfg, d in resource_map().items():
configs.register(cfg, d['contexts'])
return configs
@ -524,7 +524,7 @@ def initialize_ssh_keys(user='root'):
if not os.path.isfile(pub_key):
log('Generating missing ssh public key @ %s.' % pub_key)
cmd = ['ssh-keygen', '-y', '-f', priv_key]
p = check_output(cmd).strip()
p = check_output(cmd).decode('UTF-8').strip()
with open(pub_key, 'wb') as out:
out.write(p)
check_output(['chown', '-R', user, ssh_dir])
@ -533,7 +533,7 @@ def initialize_ssh_keys(user='root'):
def set_ppc64_cpu_smt_state(smt_state):
"""Set ppc64_cpu smt state."""
current_smt_state = check_output(['ppc64_cpu', '--smt'])
current_smt_state = check_output(['ppc64_cpu', '--smt']).decode('UTF-8')
# Possible smt state values are integer or 'off'
# Ex. common ppc64_cpu query command output values:
# SMT=8
@ -651,9 +651,10 @@ def import_keystone_ca_cert():
def create_libvirt_secret(secret_file, secret_uuid, key):
uri = LIBVIRT_URIS[config('virt-type')]
if secret_uuid in check_output(['virsh', '-c', uri, 'secret-list']):
cmd = ['virsh', '-c', uri, 'secret-list']
if secret_uuid in check_output(cmd).decode('UTF-8'):
old_key = check_output(['virsh', '-c', uri, 'secret-get-value',
secret_uuid])
secret_uuid]).decode('UTF-8')
old_key = old_key.strip()
if old_key == key:
log('Libvirt secret already exists for uuid %s.' % secret_uuid,
@ -673,7 +674,7 @@ def create_libvirt_secret(secret_file, secret_uuid, key):
def destroy_libvirt_network(netname):
"""Delete a network using virsh net-destroy"""
try:
out = check_output(['virsh', 'net-list']).split('\n')
out = check_output(['virsh', 'net-list']).decode('UTF-8').splitlines()
if len(out) < 3:
return
@ -786,10 +787,10 @@ def git_pre_install():
add_user_to_group('nova', 'libvirtd')
for d in dirs:
mkdir(d, owner='nova', group='nova', perms=0755, force=False)
mkdir(d, owner='nova', group='nova', perms=0o0755, force=False)
for l in logs:
write_file(l, '', owner='nova', group='nova', perms=0644)
write_file(l, '', owner='nova', group='nova', perms=0o0644)
def git_post_install(projects_yaml):

View File

@ -97,7 +97,7 @@ firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% endif -%}
{% if network_manager_config -%}
{% for key, value in network_manager_config.iteritems() -%}
{% for key, value in network_manager_config.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
@ -120,7 +120,7 @@ os_region_name = {{ region }}
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{% for key, value in user_config_flags.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -96,7 +96,7 @@ firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% endif -%}
{% if network_manager_config -%}
{% for key, value in network_manager_config.iteritems() -%}
{% for key, value in network_manager_config.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
@ -116,7 +116,7 @@ volume_api_class = nova.volume.cinder.API
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{% for key, value in user_config_flags.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -78,7 +78,7 @@ firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% endif -%}
{% if network_manager != 'neutron' and network_manager_config -%}
{% for key, value in network_manager_config.iteritems() -%}
{% for key, value in network_manager_config.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
@ -98,7 +98,7 @@ volume_api_class = nova.volume.cinder.API
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{% for key, value in user_config_flags.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -78,7 +78,7 @@ firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% endif -%}
{% if network_manager != 'neutron' and network_manager_config -%}
{% for key, value in network_manager_config.iteritems() -%}
{% for key, value in network_manager_config.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
@ -98,7 +98,7 @@ volume_api_class = nova.volume.cinder.API
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{% for key, value in user_config_flags.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -78,7 +78,7 @@ firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% endif -%}
{% if network_manager != 'neutron' and network_manager_config -%}
{% for key, value in network_manager_config.iteritems() -%}
{% for key, value in network_manager_config.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
@ -99,7 +99,7 @@ volume_api_class = nova.volume.cinder.API
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{% for key, value in user_config_flags.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -79,7 +79,7 @@ firewall_driver = nova.virt.firewall.NoopFirewallDriver
{% endif -%}
{% if network_manager != 'neutron' and network_manager_config -%}
{% for key, value in network_manager_config.iteritems() -%}
{% for key, value in network_manager_config.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}
@ -100,7 +100,7 @@ volume_api_class = nova.volume.cinder.API
{% endif -%}
{% if user_config_flags -%}
{% for key, value in user_config_flags.iteritems() -%}
{% for key, value in user_config_flags.items() -%}
{{ key }} = {{ value }}
{% endfor -%}
{% endif -%}

View File

@ -5,12 +5,12 @@ coverage>=3.6
mock>=1.2
flake8>=2.2.4,<=2.4.1
os-testr>=0.4.1
charm-tools>=2.0.0
charm-tools>=2.0.0;python_version=='2.7'
requests==2.6.0
# BEGIN: Amulet OpenStack Charm Helper Requirements
# Liberty client lower constraints
amulet>=1.14.3,<2.0
bundletester>=0.6.1,<1.0
amulet>=1.14.3,<2.0;python_version=='2.7'
bundletester>=0.6.1,<1.0;python_version=='2.7'
python-ceilometerclient>=1.5.0
python-cinderclient>=1.4.0
python-glanceclient>=1.1.0

11
tox.ini
View File

@ -2,8 +2,9 @@
# This file is managed centrally by release-tools and should not be modified
# within individual charm repos.
[tox]
envlist = pep8,py27
envlist = pep8,py27,py35,py36
skipsdist = True
skip_missing_interpreters = True
[testenv]
setenv = VIRTUAL_ENV={envdir}
@ -18,11 +19,17 @@ passenv = HOME TERM AMULET_* CS_API_*
[testenv:py27]
basepython = python2.7
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands = /bin/true
[testenv:py36]
basepython = python3
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
[testenv:py35]
basepython = python3.5
basepython = python3
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt

View File

@ -16,3 +16,4 @@ import sys
sys.path.append('actions/')
sys.path.append('hooks/')
sys.path.append('unit_tests')

View File

@ -52,7 +52,7 @@ NEUTRON_CONTEXT = {
def fake_log(msg, level=None):
level = level or 'INFO'
print '[juju test log (%s)] %s' % (level, msg)
print('[juju test log ({})] {}'.format(level, msg))
class FakeUnitdata(object):

View File

@ -383,20 +383,18 @@ class NovaComputeRelationsTests(CharmTestCase):
def test_db_joined_with_postgresql(self):
self.is_relation_made.return_value = True
with self.assertRaises(Exception) as context:
msg = ('Attempting to associate a mysql database when there is '
'already associated a postgresql one')
with self.assertRaisesRegexp(Exception, msg):
hooks.db_joined()
self.assertEqual(context.exception.message,
'Attempting to associate a mysql database when there '
'is already associated a postgresql one')
def test_postgresql_joined_with_db(self):
self.is_relation_made.return_value = True
with self.assertRaises(Exception) as context:
msg = ('Attempting to associate a postgresql database when there is '
'already associated a mysql one')
with self.assertRaisesRegexp(Exception, msg):
hooks.pgsql_db_joined()
self.assertEqual(context.exception.message,
'Attempting to associate a postgresql database when'
' there is already associated a mysql one')
@patch.object(hooks, 'CONFIGS')
def test_db_changed_missing_relation_data(self, configs):

View File

@ -27,6 +27,7 @@ from test_utils import (
patch_open
)
VIRSH_NET_LIST = """ Name State Autostart Persistent
----------------------------------------------------------
somenet active yes yes
@ -97,7 +98,7 @@ class NovaComputeUtilsTests(CharmTestCase):
'nova-network',
'nova-compute-kvm'
]
self.assertEqual(ex, result)
self.assertTrue(ex == result)
@patch.object(utils, 'nova_metadata_requirement')
@patch.object(utils, 'network_manager')
@ -116,7 +117,7 @@ class NovaComputeUtilsTests(CharmTestCase):
ex = utils.BASE_PACKAGES + [
'nova-compute-kvm'
]
self.assertEqual(ex, result)
self.assertTrue(ex == result)
@patch.object(utils, 'nova_metadata_requirement')
@patch.object(utils, 'neutron_plugin')
@ -133,7 +134,7 @@ class NovaComputeUtilsTests(CharmTestCase):
self.relation_ids.return_value = []
result = utils.determine_packages()
ex = utils.BASE_PACKAGES + ['nova-compute-kvm']
self.assertEqual(ex, result)
self.assertTrue(ex == result)
@patch.object(utils, 'nova_metadata_requirement')
@patch.object(utils, 'neutron_plugin')
@ -155,7 +156,7 @@ class NovaComputeUtilsTests(CharmTestCase):
self.relation_ids.return_value = []
result = utils.determine_packages()
ex = utils.BASE_PACKAGES + ['nova-compute-kvm', 'qemu-efi']
self.assertEqual(ex, result)
self.assertTrue(ex == result)
@patch.object(utils, 'nova_metadata_requirement')
@patch.object(utils, 'neutron_plugin')
@ -466,7 +467,7 @@ class NovaComputeUtilsTests(CharmTestCase):
user.pw_dir = '/home/' + username
return user
@patch('__builtin__.open')
@patch('builtins.open')
@patch('pwd.getpwnam')
def test_public_ssh_key_not_found(self, getpwnam, _open):
_open.side_effect = Exception
@ -564,7 +565,7 @@ class NovaComputeUtilsTests(CharmTestCase):
with patch_open() as (_open, _file):
utils.import_keystone_ca_cert()
_open.assert_called_with(utils.CA_CERT_PATH, 'wb')
_file.write.assert_called_with('foo_cert\n')
_file.write.assert_called_with(b'foo_cert\n')
check_call.assert_called_with(['update-ca-certificates'])
@patch.object(utils, 'ceph_config_file')
@ -595,10 +596,10 @@ class NovaComputeUtilsTests(CharmTestCase):
renderer.assert_called_with(
openstack_release='havana', templates_dir='templates/')
ex_reg = [
call('/etc/nova/nova.conf', [ctxt1]),
call('/etc/nova/nova-compute.conf', [ctxt2]),
call('/etc/nova/nova.conf', [ctxt1])
]
self.assertEqual(fake_renderer.register.call_args_list, ex_reg)
fake_renderer.register.assert_has_calls(ex_reg, any_order=True)
@patch.object(utils, 'check_call')
def test_enable_shell(self, _check_call):
@ -639,8 +640,9 @@ class NovaComputeUtilsTests(CharmTestCase):
key = 'AQCR2dRUaFQSOxAAC5fr79sLL3d7wVvpbbRFMg=='
old_key = 'AQCR2dRUaFQSOxAAC5fr79sLL3d7wVvpbbRFMg==\n'
self.test_config.set('virt-type', 'kvm')
_check_output.side_effect = [compute_context.CEPH_SECRET_UUID,
old_key]
_check_output.side_effect = [
compute_context.CEPH_SECRET_UUID.encode(),
old_key.encode()]
utils.create_libvirt_secret(utils.CEPH_SECRET,
compute_context.CEPH_SECRET_UUID, key)
expected = [call(['virsh', '-c',
@ -657,7 +659,9 @@ class NovaComputeUtilsTests(CharmTestCase):
key = 'AQCR2dRUaFQSOxAAC5fr79sLL3d7wVvpbbRFMg=='
old_key = 'CCCCCdRUaFQSOxAAC5fr79sLL3d7wVvpbbRFMg=='
self.test_config.set('virt-type', 'kvm')
_check_output.side_effect = [compute_context.CEPH_SECRET_UUID, old_key]
_check_output.side_effect = [
compute_context.CEPH_SECRET_UUID.encode(),
old_key.encode()]
utils.create_libvirt_secret(utils.CEPH_SECRET,
compute_context.CEPH_SECRET_UUID, key)
expected = [call(['virsh', '-c',
@ -695,13 +699,15 @@ class NovaComputeUtilsTests(CharmTestCase):
utils.configure_lxd('nova')
self.assertFalse(_configure_subuid.called)
@patch.object(utils, 'git_default_repos')
@patch.object(utils, 'git_install_requested')
@patch.object(utils, 'git_clone_and_install')
@patch.object(utils, 'git_post_install')
@patch.object(utils, 'git_pre_install')
def test_git_install(self, git_pre, git_post, git_clone_and_install,
git_requested):
git_requested, git_default_repos):
projects_yaml = openstack_origin_git
git_default_repos.return_value = projects_yaml
git_requested.return_value = True
utils.git_install(projects_yaml)
self.assertTrue(git_pre.called)
@ -730,42 +736,42 @@ class NovaComputeUtilsTests(CharmTestCase):
self.assertEqual(add_user_to_group.call_args_list, expected)
expected = [
call('/var/lib/nova', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/buckets', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/CA', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/CA/INTER', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/CA/newcerts', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/CA/private', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/CA/reqs', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/images', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/instances', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/keys', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/networks', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/lib/nova/tmp', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
call('/var/log/nova', owner='nova',
group='nova', perms=0755, force=False),
group='nova', perms=0o0755, force=False),
]
self.assertEqual(mkdir.call_args_list, expected)
expected = [
call('/var/log/nova/nova-api.log', '', owner='nova',
group='nova', perms=0644),
group='nova', perms=0o0644),
call('/var/log/nova/nova-compute.log', '', owner='nova',
group='nova', perms=0644),
group='nova', perms=0o0644),
call('/var/log/nova/nova-manage.log', '', owner='nova',
group='nova', perms=0644),
group='nova', perms=0o0644),
call('/var/log/nova/nova-network.log', '', owner='nova',
group='nova', perms=0644),
group='nova', perms=0o0644),
]
self.assertEqual(write_file.call_args_list, expected)
@ -984,7 +990,7 @@ class NovaComputeUtilsTests(CharmTestCase):
@patch.object(utils, 'check_call')
@patch.object(utils, 'check_output')
def test_destroy_libvirt_network(self, mock_check_output, mock_check_call):
mock_check_output.return_value = VIRSH_NET_LIST
mock_check_output.return_value = VIRSH_NET_LIST.encode()
utils.destroy_libvirt_network('default')
cmd = ['virsh', 'net-destroy', 'default']
mock_check_call.assert_has_calls([call(cmd)])
@ -993,7 +999,7 @@ class NovaComputeUtilsTests(CharmTestCase):
@patch.object(utils, 'check_output')
def test_destroy_libvirt_network_no_exist(self, mock_check_output,
mock_check_call):
mock_check_output.return_value = VIRSH_NET_LIST
mock_check_output.return_value = VIRSH_NET_LIST.encode()
utils.destroy_libvirt_network('defaultX')
self.assertFalse(mock_check_call.called)

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import logging
import unittest
import os
@ -20,6 +21,7 @@ import yaml
from contextlib import contextmanager
from mock import patch, MagicMock
patch('charmhelpers.contrib.openstack.utils.set_os_workload_status').start()
patch('charmhelpers.core.hookenv.status_set').start()
@ -53,7 +55,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:
@ -126,13 +128,13 @@ def patch_open():
yielded.
Yields the mock for "open" and "file", respectively.'''
mock_open = MagicMock(spec=open)
mock_file = MagicMock(spec=file)
mock_open = MagicMock(spec='builtins.open')
mock_file = MagicMock(spec=io.FileIO)
@contextmanager
def stub_open(*args, **kwargs):
mock_open(*args, **kwargs)
yield mock_file
with patch('__builtin__.open', stub_open):
with patch('builtins.open', stub_open):
yield mock_open, mock_file