From 08a58ae0255a0ca822e74af21662e3e9ce4ea31f Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 3 Oct 2016 18:45:28 -0700 Subject: [PATCH] Fix for run_command tests now that it returns native strings --- lib/ansible/module_utils/basic.py | 5 +++++ .../module_utils/basic/test_run_command.py | 18 +++++++++++++++--- test/units/module_utils/test_facts.py | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 9e0d1ac9aa9..b183322937c 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -5,6 +5,7 @@ # to the complete work. # # Copyright (c), Michael DeHaan , 2012-2013 +# Copyright (c), Toshio Kuratomi 2016 # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -2240,6 +2241,10 @@ class AnsibleModule(object): # if we're checking for prompts, do it now if prompt_re: if prompt_re.search(stdout) and not data: + if encoding: + stdout = to_native(stdout, encoding=encoding, errors=errors) + else: + stdout = stdout return (257, stdout, "A prompt was encountered while running a command, but no input data was specified") # only break out if no pipes are left to read or # the pipes are completely read and diff --git a/test/units/module_utils/basic/test_run_command.py b/test/units/module_utils/basic/test_run_command.py index 76c2832c790..904f65b6aab 100644 --- a/test/units/module_utils/basic/test_run_command.py +++ b/test/units/module_utils/basic/test_run_command.py @@ -25,6 +25,7 @@ import sys import time from io import BytesIO, StringIO +from ansible.compat.six import PY3 from ansible.compat.tests import unittest from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel @@ -173,13 +174,24 @@ class TestAnsibleModuleRunCommand(unittest.TestCase): self.cmd_out[sentinel.stdout] = BytesIO(b'hello') (rc, stdout, stderr) = self.module.run_command('/bin/cat hello.txt') self.assertEqual(rc, 0) - self.assertEqual(stdout, b'hello') + # module_utils function. On py3 it returns text and py2 it returns + # bytes because it's returning native strings + if PY3: + self.assertEqual(stdout, u'hello') + else: + self.assertEqual(stdout, b'hello') def test_utf8_output(self): self.cmd_out[sentinel.stdout] = BytesIO(u'Žarn§'.encode('utf-8')) self.cmd_out[sentinel.stderr] = BytesIO(u'لرئيسية'.encode('utf-8')) (rc, stdout, stderr) = self.module.run_command('/bin/something_ugly') self.assertEqual(rc, 0) - self.assertEqual(stdout.decode('utf-8'), u'Žarn§') - self.assertEqual(stderr.decode('utf-8'), u'لرئيسية') + # module_utils function. On py3 it returns text and py2 it returns + # bytes because it's returning native strings + if PY3: + self.assertEqual(stdout, u'Žarn§') + self.assertEqual(stderr, u'لرئيسية') + else: + self.assertEqual(stdout.decode('utf-8'), u'Žarn§') + self.assertEqual(stderr.decode('utf-8'), u'لرئيسية') diff --git a/test/units/module_utils/test_facts.py b/test/units/module_utils/test_facts.py index aeea1f900ca..d49e7a3e13b 100644 --- a/test/units/module_utils/test_facts.py +++ b/test/units/module_utils/test_facts.py @@ -405,7 +405,7 @@ MTAB_ENTRIES = \ BIND_MOUNTS = ['/not/a/real/bind_mount'] -FINDMNT_OUTPUT = b""" +FINDMNT_OUTPUT = u""" /sys sysfs sysfs rw,nosuid,nodev,noexec,relatime,seclabel /proc proc proc rw,nosuid,nodev,noexec,relatime /dev devtmpfs devtmpfs rw,nosuid,seclabel,size=8044400k,nr_inodes=2011100,mode=755