psrp - fix unicode handling in Python 2 (#47461)

* psrp - fix unicode handling in Python 2

* skip psrp become test when on Server 2008
This commit is contained in:
Jordan Borean 2018-10-24 05:37:05 +10:00 committed by GitHub
parent 94eab56d51
commit f28b7c7ab1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 21 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- psrp - Fix issue when dealing with unicode values in the output for Python 2

View file

@ -553,26 +553,24 @@ if ($bytes_read -gt 0) {
# TODO: figure out a better way of merging this with the host output # TODO: figure out a better way of merging this with the host output
stdout_list = [] stdout_list = []
for output in pipeline.output: for output in pipeline.output:
# not all pipeline outputs can be casted to a string, we will # Not all pipeline outputs are a string or contain a __str__ value,
# create our own output based on the properties if that is the # we will create our own output based on the properties of the
# case+ # complex object if that is the case.
try: if isinstance(output, GenericComplexObject) and output.to_string is None:
output_msg = str(output) obj_lines = output.property_sets
except TypeError: for key, value in output.adapted_properties.items():
if isinstance(output, GenericComplexObject): obj_lines.append(u"%s: %s" % (key, value))
obj_lines = output.property_sets for key, value in output.extended_properties.items():
for key, value in output.adapted_properties.items(): obj_lines.append(u"%s: %s" % (key, value))
obj_lines.append("%s: %s" % (key, value)) output_msg = u"\n".join(obj_lines)
for key, value in output.extended_properties.items(): else:
obj_lines.append("%s: %s" % (key, value)) output_msg = to_text(output, nonstring='simplerepr')
output_msg = "\n".join(obj_lines)
else:
output_msg = ""
stdout_list.append(output_msg) stdout_list.append(output_msg)
stdout = "\r\n".join(stdout_list) stdout = u"\r\n".join(stdout_list)
if len(self.host.ui.stdout) > 0: if len(self.host.ui.stdout) > 0:
stdout += "\r\n" + "".join(self.host.ui.stdout) stdout += u"\r\n" + u"".join(self.host.ui.stdout)
stderr_list = [] stderr_list = []
for error in pipeline.streams.error: for error in pipeline.streams.error:
@ -604,4 +602,4 @@ if ($bytes_read -gt 0) {
self.host.ui.stdout = [] self.host.ui.stdout = []
self.host.ui.stderr = [] self.host.ui.stderr = []
return rc, stdout, stderr return rc, to_bytes(stdout, encoding='utf-8'), to_bytes(stderr, encoding='utf-8')

View file

@ -0,0 +1,3 @@
[windows:vars]
ansible_connection=psrp
ansible_psrp_cert_validation=ignore

View file

@ -2,14 +2,18 @@
set -eux set -eux
pip install pypsrp python.py -m pip install pypsrp
cd ../connection cd ../connection
INVENTORY=../../inventory.winrm ./test.sh \ INVENTORY=../../inventory.winrm ./test.sh \
-i ../connection_psrp/inventory.ini \
-e target_hosts=winrm \ -e target_hosts=winrm \
-e action_prefix=win_ \ -e action_prefix=win_ \
-e local_tmp=/tmp/ansible-local \ -e local_tmp=/tmp/ansible-local \
-e remote_tmp=c:/windows/temp/ansible-remote \ -e remote_tmp=c:/windows/temp/ansible-remote \
-e ansible_psrp_cert_validation=False \ "$@"
-c psrp \
cd ../connection_psrp
ansible-playbook -i ../../inventory.winrm -i inventory.ini tests.yml \
"$@" "$@"

View file

@ -0,0 +1,61 @@
---
# these are extra tests for psrp that aren't covered under test/integration/targets/connection/*
- name: test out psrp specific tests
hosts: winrm
serial: 1
gather_facts: no
tasks:
- name: test complex objects in raw output
# until PyYAML is upgraded to 4.x we need to use the \U escape for a unicode codepoint
# and enclose in a quote to it translates the \U
raw: "
[PSCustomObject]@{string = 'string'};
[PSCustomObject]@{unicode = 'poo - \U0001F4A9'};
[PSCustomObject]@{integer = 1};
[PSCustomObject]@{list = @(1, 2)};
Get-Service -Name winrm;
Write-Output -InputObject 'string - \U0001F4A9';"
register: raw_out
- name: assert complex objects in raw output
assert:
that:
- raw_out.stdout_lines|count == 6
- "raw_out.stdout_lines[0] == 'string: string'"
- "raw_out.stdout_lines[1] == 'unicode: poo - \U0001F4A9'"
- "raw_out.stdout_lines[2] == 'integer: 1'"
- "raw_out.stdout_lines[3] == \"list: [1, 2]\""
- raw_out.stdout_lines[4] == "winrm"
- raw_out.stdout_lines[5] == "string - \U0001F4A9"
# Become only works on Server 2008 when running with basic auth, skip this host for now as it is too complicated to
# override the auth protocol in the tests.
- name: check if we running on Server 2008
win_shell: '[System.Environment]::OSVersion.Version -ge [Version]"6.1"'
register: os_version
- name: test out become with psrp
win_whoami:
when: os_version|bool
register: whoami_out
become: yes
become_method: runas
become_user: SYSTEM
- name: assert test out become with psrp
assert:
that:
- whoami_out.account.sid == "S-1-5-18"
when: os_version|bool
- name: test out async with psrp
win_shell: Start-Sleep -Seconds 2; Write-Output abc
async: 5
poll: 1
register: async_out
- name: assert est out async with psrp
assert:
that:
- async_out.stdout_lines == ["abc"]