Skip to content

Commit

Permalink
add test for ssh-agent key add
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
  • Loading branch information
fmoessbauer authored and jan-kiszka committed Mar 13, 2024
1 parent 5f4407f commit aac16e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
'KAS_TARGET',
'KAS_TASK',
'KAS_PREMIRRORS',
'SSH_PRIVATE_KEY',
'SSH_PRIVATE_KEY_FILE',
'SSH_AUTH_SOCK',
]

Expand Down
26 changes: 25 additions & 1 deletion tests/test_environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import shutil
import pathlib
import subprocess
import re
import pytest
from kas import kas
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {}
Expand Down

0 comments on commit aac16e0

Please sign in to comment.