Skip to content

Commit

Permalink
feat(client): Improve test coverage of crypto.py (#4268)
Browse files Browse the repository at this point in the history
* Card ID: CCT-967

While existing tests catch basic invalid playbook scenarios, this
update aims to create more tests that cover specific verification
failure points. Adding new unit tests helps "freeze" expected
verification behavior, making it easier to detect unintended changes in
the future.

Signed-off-by: pkoprda <[email protected]>
  • Loading branch information
pkoprda authored Nov 14, 2024
1 parent b88a6e8 commit 01e512a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions insights/client/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def _supports_cleanup_socket(self):
)
stdout, stderr = version_process.communicate()
if version_process.returncode != 0:
stderr = stderr.decode("utf-8") if isinstance(stderr, bytes) else stderr
stderr = "\n".join("stderr: {line}".format(line=line) for line in stderr.split("\n") if len(line))
logger.debug("could not query for gpg version:\n{err}".format(err=stderr))
return False
Expand Down
26 changes: 26 additions & 0 deletions insights/tests/client/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ def test_no_file(file):
key=home + "/key.public.gpg",
)

assert not os.path.isfile(home + file)
assert result.ok is False
assert result.return_code > 0
assert "file '{path}' does not exist".format(path=home + file) == result.stderr

shutil.rmtree(home)


@mock.patch("insights.client.crypto.GPGCommand.TEMPORARY_GPG_HOME_PARENT_DIRECTORY", "/tmp/")
def test_invalid_public_key():
Expand Down Expand Up @@ -277,3 +280,26 @@ def communicate(self, *args, **kwargs):
result = command._supports_cleanup_socket()

assert result == supports


@mock.patch("insights.client.crypto.GPGCommand.TEMPORARY_GPG_HOME_PARENT_DIRECTORY", "/tmp/")
@mock.patch("subprocess.Popen")
@mock.patch.object(crypto.GPGCommand, "_cleanup", return_value=None)
def test_invalid_gpg_setup(mock_cleanup, mock_popen):
"""An invalid GPG setup can be detected."""
gpg_command = crypto.GPGCommand(command=[], key=os.path.join("/dummy", "key"))

# Mock the process
mock_process = mock.Mock()
mock_process.communicate.return_value = (b"", b"gpg setup failed")
mock_process.returncode = 1
mock_popen.return_value = mock_process

# Run the test
result = gpg_command.evaluate() # type: crypto.GPGCommandResult

# Verify the results
assert not result.ok
assert "" == result.stdout
assert "gpg setup failed" in result.stderr
assert 1 == result.return_code

0 comments on commit 01e512a

Please sign in to comment.