Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): Improve test coverage of crypto.py #4268

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading