pytest-dev / pytest-testinfra

Testinfra test your infrastructures
https://testinfra.readthedocs.io
Apache License 2.0
2.37k stars 355 forks source link

get_encoding failed on windows system (with ansible backend) #654

Open guillaumewatteeux opened 2 years ago

guillaumewatteeux commented 2 years ago

see #653

When i wants to use ansible backend on windows host, i have a problem with ascii decode

Use ansible.windows.win_service_info to retrieve all services

def test_services(host):
    res = host.ansible(
        "ansible.windows.win_service_info", "", check=False
    )
    for service in res['services']:
        print(f"DEBUG {service['name']}")

result:

self = <encodings.ascii.IncrementalDecoder object at 0x7f695f40dfa0>
input = b'{"changed": false, "exists": true, "services": [{"checkpoint": 0, "controls_accepted": [], "dependencies": [], "depe...4d", "type": "network_endpoint"}], "username": "NT AUTHORITY\\\\SYSTEM", "wait_hint_ms": 0, "win32_exit_code": 1077}]}'
final = True

    def decode(self, input, final=False):
>       return codecs.ascii_decode(input, self.errors)[0]
E       UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 72968: ordinal not in range(128)

On windows, a service "Function Discovery Provider Host" contains a description with Web Services – Discovery (character 'En Dash', U+2013) a 'En dash' character in service description ....

I'm using Ansible backend on windows, python don't install on it.

guillaumewatteeux commented 1 year ago

Same issue with community.windows.win_certificate_info module. I have a certificate with Spanish characters in the information.

host.ansible("community.windows.win_certificate_info", ...)
self = <testinfra.backend.ansible.AnsibleBackend object at 0x7ff31cdfe650>
command = "python3 -c 'import locale;print(locale.getpreferredencoding())'"
args = ('python3',), kwargs = {'encoding': None}, host = None
out = {'changed': False, 'module_stderr': 'Exception calling "Create" with "1" argument(s): "At line:4 char:21\r\n+ def _ans...rrorId : BadExpression\r\n ', 'module_stdout': '', 'msg': 'MODULE FAILURE\nSee stdout/stderr for the exact error', ...}

I don't know howto force encoding to UTF-8