"""
Integration tests for salt-ssh py_versions
"""
import logging

import pytest
from saltfactories.utils import random_string

from tests.support.helpers import Keys

pytest.importorskip("docker")


log = logging.getLogger(__name__)

pytestmark = [
    pytest.mark.slow_test,
    pytest.mark.skip_if_binaries_missing("dockerd"),
]


@pytest.fixture(scope="module")
def ssh_keys(tmp_path_factory):
    """
    Temporary ssh key fixture
    """
    with Keys(tmp_path_factory) as keys:
        yield keys


@pytest.fixture(scope="module")
def ssh_docker_container(salt_factories, ssh_keys):
    """
    Temporary docker container with python 3.6 and ssh enabled
    """
    container = salt_factories.get_container(
        random_string("ssh-py_versions-"),
        "dwoz1/cicd:ssh",
        container_run_kwargs={
            "ports": {
                "22/tcp": None,
            },
            "environment": {
                "SSH_USER": "centos",
                "SSH_AUTHORIZED_KEYS": ssh_keys.pub,
            },
            "cap_add": "IPC_LOCK",
        },
        pull_before_start=True,
        skip_on_pull_failure=True,
        skip_if_docker_client_not_connectable=True,
    )
    with container.started() as factory:
        yield factory


@pytest.fixture(scope="module")
def ssh_port(ssh_docker_container):
    return ssh_docker_container.get_host_port_binding(22, protocol="tcp")


@pytest.fixture(scope="module")
def salt_ssh_roster_file(ssh_port, ssh_keys, salt_master):
    """
    Temporary roster for ssh docker container
    """
    roster = """
    pyvertest:
      host: localhost
      user: centos
      port: {}
      priv: {}
      ssh_options:
        - StrictHostKeyChecking=no
        - UserKnownHostsFile=/dev/null
    """.format(
        ssh_port, ssh_keys.priv_path
    )
    with pytest.helpers.temp_file(
        "py_versions_roster", roster, salt_master.config_dir
    ) as roster_file:
        yield roster_file


@pytest.fixture(scope="module")
def salt_ssh_cli(salt_master, salt_ssh_roster_file, ssh_keys, ssh_docker_container):
    assert salt_master.is_running()
    assert ssh_docker_container.is_running()
    return salt_master.salt_ssh_cli(
        timeout=180,
        roster_file=salt_ssh_roster_file,
        target_host="localhost",
        client_key=str(ssh_keys.priv_path),
        base_script_args=["--ignore-host-keys"],
    )


def test_py36_target(salt_ssh_cli):
    """
    Test that a python >3.6 master can salt ssh to a <3.6 target
    """
    ret = salt_ssh_cli.run("test.ping", minion_tgt="pyvertest")
    if "kex_exchange_identification" in ret.stdout:
        pytest.skip("Container closed ssh connection, skipping for now")
    assert ret.returncode == 0
    assert ret.data is True

@@ -58,16 +58,42 @@
  logging
+%0Aimport socket%0Aimport time
 %0A%0Aimport
@@ -353,16 +353,598 @@
 %22),%0A%5D%0A%0A%0A
+def check_container_started(timeout_at, container):%0A    sleeptime = 2%0A    while time.time() %3C= timeout_at:%0A        time.sleep(sleeptime)%0A        try:%0A            if not container.is_running():%0A                return False%0A            sock = socket.socket()%0A            sock.connect((%22localhost%22, container.get_check_ports()%5B22%5D))%0A            return True%0A        except Exception as err:  # pylint: disable=broad-except%0A            break%0A        finally:%0A            sock.close()%0A        time.sleep(sleeptime)%0A        sleeptime *= 2%0A    else:%0A        return False%0A    return False%0A%0A%0A
 @pytest.
@@ -1372,22 +1372,62 @@
    %22
-dwoz1/cicd:ssh
+ghcr.io/saltstack/salt-ci-containers/ssh-minion:latest
 %22,%0A 
@@ -1826,22 +1826,94 @@
 e=True,%0A
-
     )%0A
+    container.container_start_check(check_container_started, container)%0A
     with

Use new image for ssh test and wait for container sshd service to start