From 25cd394bbe5c371dd60328643f2e18b7bfb3d1a2 Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Thu, 5 Jan 2017 22:15:16 +0100 Subject: [PATCH] Add py3.5 support Depends-On: Ibda89b467b461b8833515f50a0cf1cc3064cb917 Change-Id: I68ac61b7d1d0d180a7696ae2f0f75b1a4a969995 --- heat-config-ansible/install.d/hook-ansible.py | 8 +-- .../install.d/hook-apply-config.py | 4 +- .../install.d/hook-cfn-init.py | 4 +- .../install.d/hook-docker-cmd.py | 2 +- .../install.d/hook-docker-compose.py | 6 +-- .../configure.d/50-heat-config-docker-compose | 4 +- heat-config-hiera/install.d/hook-hiera.py | 4 +- heat-config-kubelet/install.d/hook-kubelet.py | 6 +-- heat-config-puppet/install.d/hook-puppet.py | 4 +- heat-config-salt/install.d/hook-salt.py | 2 +- heat-config-script/install.d/hook-script.py | 6 +-- heat-config/bin/heat-config-notify | 2 +- .../configure.d/55-heat-config | 7 ++- tests/common.py | 2 + tests/config-tool-fake.py | 3 +- tests/test_heat_config.py | 2 +- tests/test_heat_config_docker_compose.py | 2 +- tests/test_heat_config_kubelet.py | 2 +- tests/test_heat_config_notify.py | 50 ++++++++++++------- tests/test_hook_ansible.py | 6 +-- tests/test_hook_cfn_init.py | 4 +- tests/test_hook_chef.py | 13 +++-- tests/test_hook_docker_cmd.py | 23 +++++---- tests/test_hook_docker_compose.py | 6 +-- tests/test_hook_puppet.py | 6 +-- tests/test_hook_salt.py | 10 ++-- tests/test_hook_script.py | 4 +- tox.ini | 3 +- 28 files changed, 111 insertions(+), 84 deletions(-) diff --git a/heat-config-ansible/install.d/hook-ansible.py b/heat-config-ansible/install.d/hook-ansible.py index 67336fb..6192b8d 100755 --- a/heat-config-ansible/install.d/hook-ansible.py +++ b/heat-config-ansible/install.d/hook-ansible.py @@ -70,8 +70,8 @@ def main(argv=sys.argv): vars_filename, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as var_file: json.dump(variables, var_file) # Write the executable, 'config', to file - with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: - f.write(c.get('config', '').encode('utf-8')) + with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o600), 'wb') as f: + f.write(c.get('config', '').encode('utf-8', 'replace')) cmd = [ ANSIBLE_CMD, @@ -130,8 +130,8 @@ def main(argv=sys.argv): pass response.update({ - 'deploy_stdout': stdout, - 'deploy_stderr': stderr, + 'deploy_stdout': stdout.decode('utf-8', 'replace'), + 'deploy_stderr': stderr.decode('utf-8', 'replace'), 'deploy_status_code': subproc.returncode, }) diff --git a/heat-config-apply-config/install.d/hook-apply-config.py b/heat-config-apply-config/install.d/hook-apply-config.py index df29cb2..ba1d8d0 100755 --- a/heat-config-apply-config/install.d/hook-apply-config.py +++ b/heat-config-apply-config/install.d/hook-apply-config.py @@ -46,8 +46,8 @@ def main(argv=sys.argv): log.info('Completed apply-config.') response = { - 'deploy_stdout': stdout, - 'deploy_stderr': stderr, + 'deploy_stdout': stdout.decode('utf-8', 'replace'), + 'deploy_stderr': stderr.decode('utf-8', 'replace'), 'deploy_status_code': subproc.returncode, } diff --git a/heat-config-cfn-init/install.d/hook-cfn-init.py b/heat-config-cfn-init/install.d/hook-cfn-init.py index f5d859c..7aa1a9c 100755 --- a/heat-config-cfn-init/install.d/hook-cfn-init.py +++ b/heat-config-cfn-init/install.d/hook-cfn-init.py @@ -70,8 +70,8 @@ def main(argv=sys.argv, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr): log.info('Completed %s' % CFN_INIT_CMD) response = { - 'deploy_stdout': cstdout, - 'deploy_stderr': cstderr, + 'deploy_stdout': cstdout.decode('utf-8', 'replace'), + 'deploy_stderr': cstderr.decode('utf-8', 'replace'), 'deploy_status_code': subproc.returncode, } diff --git a/heat-config-docker-cmd/install.d/hook-docker-cmd.py b/heat-config-docker-cmd/install.d/hook-docker-cmd.py index bd4f73d..dda7bc2 100755 --- a/heat-config-docker-cmd/install.d/hook-docker-cmd.py +++ b/heat-config-docker-cmd/install.d/hook-docker-cmd.py @@ -73,7 +73,7 @@ def main(argv=sys.argv): labels = collections.OrderedDict() labels['deploy_stack_id'] = input_values.get('deploy_stack_id') labels['deploy_resource_name'] = input_values.get('deploy_resource_name') - (stdout, stderr, deploy_status_code) = paunch.apply( + stdout, stderr, deploy_status_code = paunch.apply( cid, config, managed_by='docker-cmd', diff --git a/heat-config-docker-compose/install.d/hook-docker-compose.py b/heat-config-docker-compose/install.d/hook-docker-compose.py index 0ccd51a..62c0113 100755 --- a/heat-config-docker-compose/install.d/hook-docker-compose.py +++ b/heat-config-docker-compose/install.d/hook-docker-compose.py @@ -39,13 +39,13 @@ def write_input_file(file_path, content): prepare_dir(os.path.dirname(file_path)) with os.fdopen(os.open( file_path, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: - f.write(content.encode('utf-8')) + f.write(content) def build_response(deploy_stdout, deploy_stderr, deploy_status_code): return { - 'deploy_stdout': deploy_stdout, - 'deploy_stderr': deploy_stderr, + 'deploy_stdout': deploy_stdout.decode('utf-8', 'replace'), + 'deploy_stderr': deploy_stderr.decode('utf-8', 'replace'), 'deploy_status_code': deploy_status_code, } diff --git a/heat-config-docker-compose/os-refresh-config/configure.d/50-heat-config-docker-compose b/heat-config-docker-compose/os-refresh-config/configure.d/50-heat-config-docker-compose index 017c08e..91778cf 100755 --- a/heat-config-docker-compose/os-refresh-config/configure.d/50-heat-config-docker-compose +++ b/heat-config-docker-compose/os-refresh-config/configure.d/50-heat-config-docker-compose @@ -43,7 +43,6 @@ def main(argv=sys.argv): if not os.path.exists(CONF_FILE): log.error('No config file %s' % CONF_FILE) - return 1 if not os.path.isdir(DOCKER_COMPOSE_DIR): os.makedirs(DOCKER_COMPOSE_DIR, 0o700) @@ -59,6 +58,7 @@ def main(argv=sys.argv): write_compose_config(c) except Exception as e: log.exception(e) + return 1 def cleanup_stale_projects(configs): @@ -109,7 +109,7 @@ def write_compose_config(c): fn = os.path.join(proj_dir, 'docker-compose.yml') with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600), 'w') as f: - f.write(yaml_config.encode('utf-8')) + f.write(yaml_config) if __name__ == '__main__': diff --git a/heat-config-hiera/install.d/hook-hiera.py b/heat-config-hiera/install.d/hook-hiera.py index e94ac3e..a0c30af 100755 --- a/heat-config-hiera/install.d/hook-hiera.py +++ b/heat-config-hiera/install.d/hook-hiera.py @@ -49,7 +49,7 @@ def exit_legacy_hiera_detected(): stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = subproc.communicate() - rs_stdout = stdout.rstrip() + rs_stdout = stdout.rstrip().decode('utf-8', 'replace') if rs_stdout != 'empty': err_msg = ('Legacy hieradata from os-apply-config has been ' 'detected - %s. Please update all of your interfaces ' @@ -96,7 +96,7 @@ def main(argv=sys.argv): # write out the datafiles as YAML if 'datafiles' in c: - for name, data in c['datafiles'].iteritems(): + for name, data in c['datafiles'].items(): hiera_data = os.path.join(HIERA_DATADIR, '%s.json' % name) with os.fdopen(os.open(hiera_data, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, diff --git a/heat-config-kubelet/install.d/hook-kubelet.py b/heat-config-kubelet/install.d/hook-kubelet.py index eee08c1..72ff0f9 100755 --- a/heat-config-kubelet/install.d/hook-kubelet.py +++ b/heat-config-kubelet/install.d/hook-kubelet.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -import cStringIO +import io import json import logging import os @@ -82,13 +82,13 @@ def configure_logging(): handler.setFormatter(formatter) log.addHandler(handler) - deploy_stdout = cStringIO.StringIO() + deploy_stdout = io.StringIO() handler = logging.StreamHandler(deploy_stdout) handler.setFormatter(formatter) handler.setLevel('DEBUG') log.addHandler(handler) - deploy_stderr = cStringIO.StringIO() + deploy_stderr = io.StringIO() handler = logging.StreamHandler(deploy_stderr) handler.setFormatter(formatter) handler.setLevel('WARN') diff --git a/heat-config-puppet/install.d/hook-puppet.py b/heat-config-puppet/install.d/hook-puppet.py index 134eb77..eefdf42 100755 --- a/heat-config-puppet/install.d/hook-puppet.py +++ b/heat-config-puppet/install.d/hook-puppet.py @@ -92,7 +92,7 @@ def main(argv=sys.argv): with os.fdopen(os.open(hiera_data, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o600), 'w') as hiera_file: - hiera_file.write(json.dumps(hiera).encode('utf8')) + hiera_file.write(json.dumps(hiera)) facts['FACTER_deploy_config_name'] = c['name'] fn = os.path.join(WORKING_DIR, '%s.pp' % c['id']) @@ -106,7 +106,7 @@ def main(argv=sys.argv): with os.fdopen(os.open(fn, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o700), 'w') as f: - f.write(c.get('config', '').encode('utf-8')) + f.write(c.get('config', '')) cmd = [PUPPET_CMD, 'apply', '--color', 'false', '--detailed-exitcodes', fn] # This is the default log destination to print out to the console and diff --git a/heat-config-salt/install.d/hook-salt.py b/heat-config-salt/install.d/hook-salt.py index 8c837d4..55fb439 100755 --- a/heat-config-salt/install.d/hook-salt.py +++ b/heat-config-salt/install.d/hook-salt.py @@ -70,7 +70,7 @@ def main(argv=sys.argv): fn = os.path.join(WORKING_DIR, state_file) with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f: - f.write(yaml_config.encode('utf-8')) + f.write(yaml_config) caller = salt.cli.caller.Caller.factory(opts) diff --git a/heat-config-script/install.d/hook-script.py b/heat-config-script/install.d/hook-script.py index 5c9cb4d..0c9a92f 100755 --- a/heat-config-script/install.d/hook-script.py +++ b/heat-config-script/install.d/hook-script.py @@ -59,7 +59,7 @@ def main(argv=sys.argv): env['heat_outputs_path'] = heat_outputs_path with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f: - f.write(c.get('config', '').encode('utf-8')) + f.write(c.get('config', '')) log.debug('Running %s' % fn) subproc = subprocess.Popen([fn], stdout=subprocess.PIPE, @@ -85,8 +85,8 @@ def main(argv=sys.argv): pass response.update({ - 'deploy_stdout': stdout, - 'deploy_stderr': stderr, + 'deploy_stdout': stdout.decode('utf-8', 'replace'), + 'deploy_stderr': stderr.decode('utf-8', 'replace'), 'deploy_status_code': subproc.returncode, }) diff --git a/heat-config/bin/heat-config-notify b/heat-config/bin/heat-config-notify index eba1258..3f115b0 100755 --- a/heat-config/bin/heat-config-notify +++ b/heat-config/bin/heat-config-notify @@ -58,7 +58,7 @@ def trim_response(response, trimmed_values=None): """ trimmed_values = trimmed_values or ('deploy_stdout', 'deploy_stderr') - str_response = json.dumps(response, ensure_ascii=True, encoding='utf-8') + str_response = json.dumps(response, ensure_ascii=True) len_total = len(str_response) offset = MAX_RESPONSE_SIZE - len_total if offset >= 0: diff --git a/heat-config/os-refresh-config/configure.d/55-heat-config b/heat-config/os-refresh-config/configure.d/55-heat-config index f75cbbe..e9988b2 100755 --- a/heat-config/os-refresh-config/configure.d/55-heat-config +++ b/heat-config/os-refresh-config/configure.d/55-heat-config @@ -20,8 +20,6 @@ import stat import subprocess import sys -import requests - HOOKS_DIR_PATHS = ( os.environ.get('HEAT_CONFIG_HOOKS'), '/usr/libexec/heat-config/hooks', @@ -145,7 +143,8 @@ def invoke_hook(c, log): stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = subproc.communicate(input=json.dumps(c)) + stdout, stderr = subproc.communicate( + input=json.dumps(c).encode('utf-8', 'replace')) log.info(stdout) log.debug(stderr) @@ -158,7 +157,7 @@ def invoke_hook(c, log): try: if stdout: - signal_data = json.loads(stdout) + signal_data = json.loads(stdout.decode('utf-8', 'replace')) except ValueError: signal_data = { 'deploy_stdout': stdout, diff --git a/tests/common.py b/tests/common.py index 4b76b91..0eba717 100644 --- a/tests/common.py +++ b/tests/common.py @@ -30,6 +30,8 @@ class RunScriptTest(testtools.TestCase): stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + if input_str: + input_str = input_str.encode('utf-8') stdout, stderr = subproc.communicate(input=input_str) return subproc.returncode, stdout, stderr diff --git a/tests/config-tool-fake.py b/tests/config-tool-fake.py index 46d7862..88f218b 100755 --- a/tests/config-tool-fake.py +++ b/tests/config-tool-fake.py @@ -48,8 +48,7 @@ def main(argv=sys.argv): if isinstance(response, list): response = response[suffix] - for k, v in response.get('files', {}).iteritems(): - open(k, 'w') + for k, v in response.get('files', {}).items(): with open(k, 'w') as f: f.write(v) diff --git a/tests/test_heat_config.py b/tests/test_heat_config.py index b4e3d38..eabb13c 100644 --- a/tests/test_heat_config.py +++ b/tests/test_heat_config.py @@ -157,7 +157,7 @@ class HeatConfigTest(common.RunScriptTest): self.env = os.environ.copy() def write_config_file(self, data): - config_file = tempfile.NamedTemporaryFile() + config_file = tempfile.NamedTemporaryFile(mode='w') config_file.write(json.dumps(data)) config_file.flush() return config_file diff --git a/tests/test_heat_config_docker_compose.py b/tests/test_heat_config_docker_compose.py index 2a4c2c8..5b70a5a 100644 --- a/tests/test_heat_config_docker_compose.py +++ b/tests/test_heat_config_docker_compose.py @@ -90,7 +90,7 @@ class HeatConfigDockerComposeORCTest(common.RunScriptTest): os.chmod(hook_name, 0o755) def write_config_file(self, data): - config_file = tempfile.NamedTemporaryFile() + config_file = tempfile.NamedTemporaryFile(mode='w') config_file.write(json.dumps(data)) config_file.flush() return config_file diff --git a/tests/test_heat_config_kubelet.py b/tests/test_heat_config_kubelet.py index 93455e1..da0f738 100644 --- a/tests/test_heat_config_kubelet.py +++ b/tests/test_heat_config_kubelet.py @@ -117,7 +117,7 @@ class HeatConfigKubeletORCTest(common.RunScriptTest): os.chmod(hook_name, 0o755) def write_config_file(self, data): - config_file = tempfile.NamedTemporaryFile() + config_file = tempfile.NamedTemporaryFile(mode='w') config_file.write(json.dumps(data)) config_file.flush() return config_file diff --git a/tests/test_heat_config_notify.py b/tests/test_heat_config_notify.py index 80bd971..15ca558 100644 --- a/tests/test_heat_config_notify.py +++ b/tests/test_heat_config_notify.py @@ -11,12 +11,13 @@ # License for the specific language governing permissions and limitations # under the License. -import cStringIO +import io import json import tempfile import fixtures import mock +import six from tests import common from tests import heat_config_notify as hcn @@ -76,9 +77,13 @@ class HeatConfigNotifyTest(common.RunScriptTest): super(HeatConfigNotifyTest, self).setUp() self.deployed_dir = self.useFixture(fixtures.TempDir()) hcn.init_logging = mock.MagicMock() + if six.PY2: + self.stdin = io.BytesIO() + else: + self.stdin = io.StringIO() def write_config_file(self, data): - config_file = tempfile.NamedTemporaryFile() + config_file = tempfile.NamedTemporaryFile(mode='w') config_file.write(json.dumps(data)) config_file.flush() return config_file @@ -86,21 +91,23 @@ class HeatConfigNotifyTest(common.RunScriptTest): def test_notify_missing_file(self): signal_data = json.dumps({'foo': 'bar'}) - stdin = cStringIO.StringIO(signal_data) + self.stdin.write(signal_data) + self.stdin.seek(0) with self.write_config_file(self.data_signal_id) as config_file: config_file_name = config_file.name self.assertEqual( - 1, hcn.main(['heat-config-notify', config_file_name], stdin)) + 1, hcn.main(['heat-config-notify', config_file_name], self.stdin)) def test_notify_missing_file_arg(self): signal_data = json.dumps({'foo': 'bar'}) - stdin = cStringIO.StringIO(signal_data) + self.stdin.write(signal_data) + self.stdin.seek(0) self.assertEqual( - 1, hcn.main(['heat-config-notify'], stdin)) + 1, hcn.main(['heat-config-notify'], self.stdin)) def test_notify_signal_id(self): requests = mock.MagicMock() @@ -109,11 +116,13 @@ class HeatConfigNotifyTest(common.RunScriptTest): requests.post.return_value = '[200]' signal_data = json.dumps({'foo': 'bar'}) - stdin = cStringIO.StringIO(signal_data) + self.stdin.write(signal_data) + self.stdin.seek(0) with self.write_config_file(self.data_signal_id) as config_file: self.assertEqual( - 0, hcn.main(['heat-config-notify', config_file.name], stdin)) + 0, + hcn.main(['heat-config-notify', config_file.name], self.stdin)) requests.post.assert_called_once_with( 'mock://192.0.2.3/foo', @@ -127,11 +136,13 @@ class HeatConfigNotifyTest(common.RunScriptTest): requests.post.return_value = '[200]' signal_data = json.dumps({'foo': 'bar'}) - stdin = cStringIO.StringIO(signal_data) + self.stdin.write(signal_data) + self.stdin.seek(0) with self.write_config_file(self.data_signal_id_put) as config_file: self.assertEqual( - 0, hcn.main(['heat-config-notify', config_file.name], stdin)) + 0, + hcn.main(['heat-config-notify', config_file.name], self.stdin)) requests.put.assert_called_once_with( 'mock://192.0.2.3/foo', @@ -144,11 +155,10 @@ class HeatConfigNotifyTest(common.RunScriptTest): requests.post.return_value = '[200]' - stdin = cStringIO.StringIO() - with self.write_config_file(self.data_signal_id) as config_file: self.assertEqual( - 0, hcn.main(['heat-config-notify', config_file.name], stdin)) + 0, + hcn.main(['heat-config-notify', config_file.name], self.stdin)) requests.post.assert_called_once_with( 'mock://192.0.2.3/foo', @@ -161,11 +171,15 @@ class HeatConfigNotifyTest(common.RunScriptTest): requests.post.return_value = '[200]' - stdin = cStringIO.StringIO('{{{"hi') + signal_data = json.dumps({'foo': 'bar'}) + self.stdin.write(signal_data) + self.stdin.write(signal_data[:-3]) + self.stdin.seek(0) with self.write_config_file(self.data_signal_id) as config_file: self.assertEqual( - 0, hcn.main(['heat-config-notify', config_file.name], stdin)) + 0, + hcn.main(['heat-config-notify', config_file.name], self.stdin)) requests.post.assert_called_once_with( 'mock://192.0.2.3/foo', @@ -184,14 +198,16 @@ class HeatConfigNotifyTest(common.RunScriptTest): heatclient.Client.return_value = heat signal_data = json.dumps({'foo': 'bar'}) - stdin = cStringIO.StringIO(signal_data) + self.stdin.write(signal_data) + self.stdin.seek(0) ks.service_catalog.url_for.return_value = 'mock://192.0.2.3/heat' heat.resources.signal.return_value = 'all good' with self.write_config_file(self.data_heat_signal) as config_file: self.assertEqual( - 0, hcn.main(['heat-config-notify', config_file.name], stdin)) + 0, + hcn.main(['heat-config-notify', config_file.name], self.stdin)) ksclient.Client.assert_called_once_with( auth_url='mock://192.0.2.3/auth', diff --git a/tests/test_hook_ansible.py b/tests/test_hook_ansible.py index 986e66b..a25b527 100644 --- a/tests/test_hook_ansible.py +++ b/tests/test_hook_ansible.py @@ -165,7 +165,7 @@ class HookAnsibleTest(common.RunScriptTest): 'deploy_stdout': 'ansible success', 'deploy_stderr': 'thing happened', 'deploy_status_code': 0, - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) ansible_playbook = self.working_dir.join('1234_playbook.yaml') @@ -214,7 +214,7 @@ class HookAnsibleTest(common.RunScriptTest): 'deploy_stdout': 'ansible success', 'deploy_stderr': 'thing happened', 'deploy_status_code': 0, - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) ansible_playbook = self.working_dir.join('1234_playbook.yaml') @@ -248,7 +248,7 @@ class HookAnsibleTest(common.RunScriptTest): 'deploy_stdout': 'ansible failed', 'deploy_stderr': 'bad thing happened', 'deploy_status_code': 4, - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) ansible_playbook = self.working_dir.join('1234_playbook.yaml') diff --git a/tests/test_hook_cfn_init.py b/tests/test_hook_cfn_init.py index 6f6c9a8..d2863ce 100644 --- a/tests/test_hook_cfn_init.py +++ b/tests/test_hook_cfn_init.py @@ -64,7 +64,7 @@ class HookCfnInitTest(common.RunScriptTest): 'deploy_stdout': 'cfn-init success', 'deploy_stderr': 'thing happened', 'deploy_status_code': 0 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) # assert last_metadata was written with cfn-init metadata self.assertEqual( @@ -92,7 +92,7 @@ class HookCfnInitTest(common.RunScriptTest): 'deploy_stdout': '', 'deploy_stderr': 'bad thing happened', 'deploy_status_code': 1 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) self.assertEqual( {'AWS::CloudFormation::Init': {'foo': 'bar'}}, diff --git a/tests/test_hook_chef.py b/tests/test_hook_chef.py index 023c737..05968dc 100644 --- a/tests/test_hook_chef.py +++ b/tests/test_hook_chef.py @@ -13,10 +13,11 @@ import copy import imp +import io import json import logging import mock -import StringIO +import six import sys from tests import common @@ -58,8 +59,12 @@ class HookChefTest(common.RunScriptTest): __file__, '..', 'heat-config-chef/install.d/hook-chef.py') - sys.stdin = StringIO.StringIO() - sys.stdout = StringIO.StringIO() + if six.PY2: + sys.stdin = io.BytesIO() + sys.stdout = io.BytesIO() + else: + sys.stdin = io.StringIO() + sys.stdout = io.StringIO() def tearDown(self): super(HookChefTest, self).tearDown() @@ -77,7 +82,7 @@ class HookChefTest(common.RunScriptTest): data = copy.deepcopy(self.data) data['config'] = '["recipe[apache]"]' hook_chef = self.get_module() - sys.stdin.write(json.dumps(data)) + json.dump(data, sys.stdin) sys.stdin.seek(0) mock_subproc = mock.Mock() mock_popen.return_value = mock_subproc diff --git a/tests/test_hook_docker_cmd.py b/tests/test_hook_docker_cmd.py index 71854dc..9ca6f12 100644 --- a/tests/test_hook_docker_cmd.py +++ b/tests/test_hook_docker_cmd.py @@ -172,7 +172,7 @@ class HookDockerCmdTest(common.RunScriptTest): 'Creating web...\n' 'one.txt\ntwo.txt\nthree.txt', 'deploy_status_code': 0 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = list(self.json_from_files(self.test_state_path, 9)) self.assertEqual([ @@ -308,7 +308,7 @@ class HookDockerCmdTest(common.RunScriptTest): 'deploy_stdout': '', 'deploy_stderr': 'Warning: custom exit code', 'deploy_status_code': 0 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = list(self.json_from_files(self.test_state_path, 5)) self.assertEqual([ @@ -398,7 +398,7 @@ class HookDockerCmdTest(common.RunScriptTest): 'Creating web...\n' 'No such file or directory', 'deploy_status_code': 2 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = list(self.json_from_files(self.test_state_path, 9)) self.assertEqual([ @@ -552,7 +552,7 @@ class HookDockerCmdTest(common.RunScriptTest): 'Creating web...\n' 'one.txt\ntwo.txt\nthree.txt', 'deploy_status_code': 0 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = list(self.json_from_files(self.test_state_path, 11)) db_container_name = state[4]['args'][4] @@ -691,7 +691,8 @@ class HookDockerCmdTest(common.RunScriptTest): }]) }) conf_dir = self.useFixture(fixtures.TempDir()).join() - with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False) as f: + with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False, + mode='w') as f: f.write(json.dumps([self.data])) f.flush() self.env['HEAT_SHELL_CONFIG'] = f.name @@ -740,7 +741,8 @@ class HookDockerCmdTest(common.RunScriptTest): }) # run again with empty config data - with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False) as f: + with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False, + mode='w') as f: f.write(json.dumps([])) f.flush() self.env['HEAT_SHELL_CONFIG'] = f.name @@ -811,7 +813,8 @@ class HookDockerCmdTest(common.RunScriptTest): }]) }) conf_dir = self.useFixture(fixtures.TempDir()).join() - with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False) as f: + with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False, + mode='w') as f: f.write(json.dumps([self.data])) f.flush() self.env['HEAT_SHELL_CONFIG'] = f.name @@ -862,7 +865,8 @@ class HookDockerCmdTest(common.RunScriptTest): new_data = copy.deepcopy(self.data) new_data['config']['web']['image'] = 'yyy' new_data['id'] = 'def456' - with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False) as f: + with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False, + mode='w') as f: f.write(json.dumps([new_data])) f.flush() self.env['HEAT_SHELL_CONFIG'] = f.name @@ -933,7 +937,8 @@ class HookDockerCmdTest(common.RunScriptTest): }]) }) conf_dir = self.useFixture(fixtures.TempDir()).join() - with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False) as f: + with tempfile.NamedTemporaryFile(dir=conf_dir, delete=False, + mode='w') as f: f.write(json.dumps([self.data])) f.flush() self.env['HEAT_SHELL_CONFIG'] = f.name diff --git a/tests/test_hook_docker_compose.py b/tests/test_hook_docker_compose.py index 5ba962d..0064c96 100644 --- a/tests/test_hook_docker_compose.py +++ b/tests/test_hook_docker_compose.py @@ -107,7 +107,7 @@ class HookDockerComposeTest(common.RunScriptTest): 'deploy_stdout': '', 'deploy_stderr': 'Creating abcdef001_db_1...', 'deploy_status_code': 0 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) self.assertEqual( @@ -135,7 +135,7 @@ class HookDockerComposeTest(common.RunScriptTest): 'deploy_stdout': '', 'deploy_stderr': 'env_file_not found...', 'deploy_status_code': 1 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) self.assertEqual( @@ -163,7 +163,7 @@ class HookDockerComposeTest(common.RunScriptTest): 'deploy_stdout': '', 'deploy_stderr': 'Error: image library/xxx:latest not found', 'deploy_status_code': 1 - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) self.assertEqual( diff --git a/tests/test_hook_puppet.py b/tests/test_hook_puppet.py index 7f772c7..92c61f7 100644 --- a/tests/test_hook_puppet.py +++ b/tests/test_hook_puppet.py @@ -93,7 +93,7 @@ class HookPuppetTest(common.RunScriptTest): 'deploy_status_code': 0, 'first_output': 'output 1', 'second_output': 'output 2', - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) puppet_script = self.working_dir.join('1234.pp') @@ -200,7 +200,7 @@ class HookPuppetTest(common.RunScriptTest): 'deploy_stdout': 'puppet failed', 'deploy_stderr': 'bad thing happened', 'deploy_status_code': 4, - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) puppet_script = self.working_dir.join('1234.pp') @@ -256,7 +256,7 @@ class HookPuppetTest(common.RunScriptTest): 'deploy_status_code': 0, 'first_output': 'output 1', 'second_output': 'output 2', - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) puppet_script = self.working_dir.join('1234.pp') diff --git a/tests/test_hook_salt.py b/tests/test_hook_salt.py index b083e3f..f0fcd3c 100644 --- a/tests/test_hook_salt.py +++ b/tests/test_hook_salt.py @@ -97,9 +97,9 @@ class HookSaltTest(common.RunScriptTest): self.assertIsNone(ret['deploy_stderr']) self.assertIsNotNone(ret['deploy_stdout']) resp = yaml.safe_load(ret['deploy_stdout']) - self.assertTrue(resp.values()[0]['result']) + self.assertTrue(list(resp.values())[0]['result']) self.assertEqual({'bar': 'foo', 'foo': 'bar'}, - resp.values()[0]['changes']) + list(resp.values())[0]['changes']) def test_hook_salt_failed(self): @@ -111,7 +111,7 @@ class HookSaltTest(common.RunScriptTest): self.assertEqual(0, returncode) self.assertIsNotNone(stderr) self.assertIsNotNone(stdout) - jsonout = json.loads(stdout) + jsonout = json.loads(stdout.decode('utf-8')) self.assertIsNone(jsonout.get("deploy_stdout"), jsonout.get("deploy_stdout")) self.assertEqual(2, jsonout.get("deploy_status_code")) @@ -128,8 +128,8 @@ class HookSaltTest(common.RunScriptTest): self.assertEqual(0, returncode, stderr) self.assertIsNotNone(stdout) self.assertIsNotNone(stderr) - ret = json.loads(stdout) + ret = json.loads(stdout.decode('utf-8')) self.assertIsNone(ret['deploy_stdout']) self.assertIsNotNone(ret['deploy_stderr']) - resp = yaml.safe_load(ret['deploy_stderr']).values()[0] + resp = list(yaml.safe_load(ret['deploy_stderr']).values())[0] self.assertFalse(resp['result']) diff --git a/tests/test_hook_script.py b/tests/test_hook_script.py index e846149..b1449a5 100644 --- a/tests/test_hook_script.py +++ b/tests/test_hook_script.py @@ -84,7 +84,7 @@ class HookScriptTest(common.RunScriptTest): 'deploy_status_code': 0, 'first_output': 'output 1', 'second_output': 'output 2', - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) script = self.working_dir.join('1234') @@ -116,7 +116,7 @@ class HookScriptTest(common.RunScriptTest): 'deploy_stdout': 'script failed', 'deploy_stderr': 'bad thing happened', 'deploy_status_code': 1, - }, json.loads(stdout)) + }, json.loads(stdout.decode('utf-8'))) state = self.json_from_file(self.test_state_path) script = self.working_dir.join('1234') diff --git a/tox.ini b/tox.ini index 1dffeda..f6722af 100644 --- a/tox.ini +++ b/tox.ini @@ -1,9 +1,10 @@ [tox] -envlist = py27,pep8 +envlist = py27,py35,pep8 minversion = 1.6 skipsdist = True [testenv] +whitelist_externals = bash setenv = VIRTUAL_ENV={envdir} deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt