Fix for run_command tests now that it returns native strings

This commit is contained in:
Toshio Kuratomi 2016-10-03 18:45:28 -07:00
parent 125a8d3c65
commit 08a58ae025
3 changed files with 21 additions and 4 deletions

View file

@ -5,6 +5,7 @@
# to the complete work. # to the complete work.
# #
# Copyright (c), Michael DeHaan <michael.dehaan@gmail.com>, 2012-2013 # Copyright (c), Michael DeHaan <michael.dehaan@gmail.com>, 2012-2013
# Copyright (c), Toshio Kuratomi <tkuratomi@ansible.com> 2016
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without modification, # 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 we're checking for prompts, do it now
if prompt_re: if prompt_re:
if prompt_re.search(stdout) and not data: 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") 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 # only break out if no pipes are left to read or
# the pipes are completely read and # the pipes are completely read and

View file

@ -25,6 +25,7 @@ import sys
import time import time
from io import BytesIO, StringIO from io import BytesIO, StringIO
from ansible.compat.six import PY3
from ansible.compat.tests import unittest from ansible.compat.tests import unittest
from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel
@ -173,6 +174,11 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
self.cmd_out[sentinel.stdout] = BytesIO(b'hello') self.cmd_out[sentinel.stdout] = BytesIO(b'hello')
(rc, stdout, stderr) = self.module.run_command('/bin/cat hello.txt') (rc, stdout, stderr) = self.module.run_command('/bin/cat hello.txt')
self.assertEqual(rc, 0) self.assertEqual(rc, 0)
# 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') self.assertEqual(stdout, b'hello')
def test_utf8_output(self): def test_utf8_output(self):
@ -180,6 +186,12 @@ class TestAnsibleModuleRunCommand(unittest.TestCase):
self.cmd_out[sentinel.stderr] = BytesIO(u'لرئيسية'.encode('utf-8')) self.cmd_out[sentinel.stderr] = BytesIO(u'لرئيسية'.encode('utf-8'))
(rc, stdout, stderr) = self.module.run_command('/bin/something_ugly') (rc, stdout, stderr) = self.module.run_command('/bin/something_ugly')
self.assertEqual(rc, 0) self.assertEqual(rc, 0)
# 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(stdout.decode('utf-8'), u'Žarn§')
self.assertEqual(stderr.decode('utf-8'), u'لرئيسية') self.assertEqual(stderr.decode('utf-8'), u'لرئيسية')

View file

@ -405,7 +405,7 @@ MTAB_ENTRIES = \
BIND_MOUNTS = ['/not/a/real/bind_mount'] BIND_MOUNTS = ['/not/a/real/bind_mount']
FINDMNT_OUTPUT = b""" FINDMNT_OUTPUT = u"""
/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime,seclabel /sys sysfs sysfs rw,nosuid,nodev,noexec,relatime,seclabel
/proc proc proc rw,nosuid,nodev,noexec,relatime /proc proc proc rw,nosuid,nodev,noexec,relatime
/dev devtmpfs devtmpfs rw,nosuid,seclabel,size=8044400k,nr_inodes=2011100,mode=755 /dev devtmpfs devtmpfs rw,nosuid,seclabel,size=8044400k,nr_inodes=2011100,mode=755