pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

Testinfra fails if the "~/.ansible/cp" path doesn't exist

Open koef opened this issue 2 years ago • 2 comments

Hello,

After updating to release v9.0.0, we've noticed a regression. When the "~/.ansible/cp" path is missing, tests seem to fail with the following error:

self = <testinfra.backend.ssh.SshBackend object at 0x10199f3d0>
command = 'uname -s'

    def run_ssh(self, command: str) -> base.CommandResult:
        cmd, cmd_args = self._build_ssh_command(command)
        out = self.run_local(" ".join(cmd), *cmd_args)
        out.command = self.encode(command)
        if out.rc == 255:
            # ssh exits with the exit status of the remote command or with 255
            # if an error occurred.
>           raise RuntimeError(out)
E           RuntimeError: CommandResult(command=b'uname -s', exit_status=255, stdout=None, stderr=b"Warning: Permanently added '192.168.1.87' (ED25519) to the list of known hosts.\r\nunix_listener: cannot bind to path /Users/user/.ansible/cp/192.168.1.87-22-some-user.8yRLDJr64HeCjBh0: No such file or directory\r\n")

cmd        = ['ssh',
 '-F %s',
 '-o User=%s',
 '-o Port=%s',
 '-o ConnectTimeout=10',
 '-o ControlMaster=auto -o ControlPersist=60s',
 '-o ControlPath=~/.ansible/cp/%%h-%%p-%%r',
 '%s %s']
cmd_args   = ['../../../_molecule_resources/default/ssh_config',
 'some-user',
 '22',
 '192.168.1.87',
 'uname -s']
command    = 'uname -s'
out        = CommandResult(command=b'uname -s', exit_status=255, stdout=None, stderr=b"Warning: Permanently added '192.168.1.87' (ED25519) to the list of known hosts.\r\nunix_listener: cannot bind to path /Users/user/.ansible/cp/192.168.1.87-22-some-user.8yRLDJr64HeCjBh0: No such file or directory\r\n")
self       = <testinfra.backend.ssh.SshBackend object at 0x10199f3d0>

I believe the root cause might be associated with PR #713.

It might be beneficial for Testinfra to verify if the "directory" exists prior to it's utilization.

Thank you for looking into this.

koef avatar Sep 18 '23 07:09 koef

ControlPath will be set if control_path is set in section [ssh_connection] in ´ansible.cfg. You can uncomment control_path` to deactivate this feature.

Your suggestion sounds plausible. I'll see how it can be implemented.

CarstenGrohmann avatar Apr 30 '24 10:04 CarstenGrohmann

I've added a check for the existance of the Ansible SSH control path (default ~/.ansible/cp).

I hope this helps.

CarstenGrohmann avatar Apr 30 '24 14:04 CarstenGrohmann