move Keys class to test helpers for ease of reuse

This commit is contained in:
MKLeb 2022-07-20 18:29:16 -04:00 committed by Megan Wilhite
parent 245991ab2c
commit 1ccf96f633
2 changed files with 38 additions and 37 deletions

View file

@ -2,11 +2,10 @@
Integration tests for salt-ssh py_versions
"""
import logging
import shutil
import subprocess
import pytest
from saltfactories.utils import random_string
from tests.support.helpers import Keys
pytest.importorskip("docker")
@ -19,40 +18,6 @@ pytestmark = [
]
class Keys:
"""
Temporary ssh key pair
"""
def __init__(self, tmp_path_factory):
priv_path = tmp_path_factory.mktemp(".ssh") / "key"
self.priv_path = priv_path
def generate(self):
subprocess.run(
["ssh-keygen", "-q", "-N", "", "-f", str(self.priv_path)], check=True
)
@property
def pub_path(self):
return self.priv_path.with_name("{}.pub".format(self.priv_path.name))
@property
def pub(self):
return self.pub_path.read_text()
@property
def priv(self):
return self.priv_path.read_text()
def __enter__(self):
self.generate()
return self
def __exit__(self, *_):
shutil.rmtree(str(self.priv_path.parent), ignore_errors=True)
@pytest.fixture(scope="module")
def ssh_keys(tmp_path_factory):
"""
@ -129,7 +94,6 @@ def salt_ssh_cli(salt_master, salt_ssh_roster_file, ssh_keys, ssh_docker_contain
)
@pytest.mark.slow_test
def test_py36_target(salt_ssh_cli):
"""
Test that a python >3.6 master can salt ssh to a <3.6 target

View file

@ -1918,3 +1918,40 @@ class CaptureOutput:
return
self._stderr.seek(0)
return self._stderr.read()
class Keys:
"""
Temporary ssh key pair
"""
def __init__(self, tmp_path_factory):
"""
tmp_path_factory is the session scoped pytest fixture of the same name
"""
priv_path = tmp_path_factory.mktemp(".ssh") / "key"
self.priv_path = priv_path
def generate(self):
subprocess.run(
["ssh-keygen", "-q", "-N", "", "-f", str(self.priv_path)], check=True
)
@property
def pub_path(self):
return self.priv_path.with_name("{}.pub".format(self.priv_path.name))
@property
def pub(self):
return self.pub_path.read_text()
@property
def priv(self):
return self.priv_path.read_text()
def __enter__(self):
self.generate()
return self
def __exit__(self, *_):
shutil.rmtree(str(self.priv_path.parent), ignore_errors=True)