From aac16e066c88364d0ee60faad9d587deb90490f2 Mon Sep 17 00:00:00 2001 From: Felix Moessbauer Date: Tue, 12 Mar 2024 16:57:00 +0100 Subject: [PATCH] add test for ssh-agent key add This adds a basic test that checks if the key (provided via env-var or file) can be added to the internal ssh-agent. Signed-off-by: Felix Moessbauer Signed-off-by: Jan Kiszka --- tests/conftest.py | 2 ++ tests/test_environment_variables.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index fd39d2e6..d52f7194 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -31,6 +31,8 @@ 'KAS_TARGET', 'KAS_TASK', 'KAS_PREMIRRORS', + 'SSH_PRIVATE_KEY', + 'SSH_PRIVATE_KEY_FILE', 'SSH_AUTH_SOCK', ] diff --git a/tests/test_environment_variables.py b/tests/test_environment_variables.py index 22b93dda..4da83941 100644 --- a/tests/test_environment_variables.py +++ b/tests/test_environment_variables.py @@ -24,6 +24,7 @@ import os import shutil import pathlib +import subprocess import re import pytest from kas import kas @@ -53,7 +54,7 @@ def test_build_dir_can_be_specified_by_environment_variable(monkeykas, tmpdir): assert os.path.exists(os.path.join(build_dir, 'conf')) -def test_ssh_agent_setup(monkeykas, tmpdir): +def test_ssh_agent_setup(monkeykas, tmpdir, capsys): conf_dir = str(tmpdir / 'test_ssh_agent_setup') shutil.copytree('tests/test_environment_variables', conf_dir) monkeykas.chdir(conf_dir) @@ -73,6 +74,29 @@ def test_ssh_agent_setup(monkeykas, tmpdir): with pytest.raises(ArgsCombinationError): kas.kas(['checkout', 'test.yml']) + privkey_file = f'{tmpdir}/id_ecdsa_test' + genkey_cmd = ['ssh-keygen', '-f', privkey_file, '-N', '', '-t', 'ecdsa'] + subprocess.check_call(genkey_cmd) + # ensure we also get the info messages + log = kas.logging.getLogger() + log.setLevel(kas.logging.INFO) + # flush the captured output + capsys.readouterr() + with monkeykas.context() as mp: + mp.setenv('SSH_PRIVATE_KEY_FILE', privkey_file) + kas.kas(['checkout', 'test.yml']) + out = capsys.readouterr().err + assert 'adding SSH key from file' in out + assert 'ERROR' not in out + + with monkeykas.context() as mp: + privkey = pathlib.Path(privkey_file).read_text() + mp.setenv('SSH_PRIVATE_KEY', privkey) + kas.kas(['checkout', 'test.yml']) + out = capsys.readouterr().err + assert 'adding SSH key from env-var' in out + assert 'ERROR' not in out + def _get_env_from_file(filename): env = {}