Skip to content

fix: streaming stderr from subprocess started by lambda-builders #563

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

Merged
merged 5 commits into from
Oct 27, 2023
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
19 changes: 16 additions & 3 deletions aws_lambda_builders/workflows/ruby_bundler/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
from os import linesep

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,7 +55,7 @@ def run(self, args, cwd=None):

p = self.osutils.popen(invoke_bundler, stdout=self.osutils.pipe, stderr=self.osutils.pipe, cwd=cwd)

out, _ = p.communicate()
out, err = p.communicate()

if p.returncode != 0:
if p.returncode == GEMFILE_NOT_FOUND:
Expand All @@ -65,7 +66,19 @@ def run(self, args, cwd=None):
if self.osutils.directory_exists(check_dir):
self.osutils.remove_directory(check_dir)
else:
# Bundler has relevant information in stdout, not stderr.
raise BundlerExecutionError(message=out.decode("utf8").strip())
# Bundler can contain information in both stdout and stderr so we check and log both
err_str = err.decode("utf8").strip()
out_str = out.decode("utf8").strip()
if out_str and err_str:
message_out = f"{out_str}{linesep}{err_str}"
LOG.debug(f"Bundler output: {out_str}")
LOG.debug(f"Bundler error: {err_str}")
elif out_str:
message_out = out_str
LOG.debug(f"Bundler output: {out_str}")
else:
message_out = err_str
LOG.debug(f"Bundler error: {err_str}")
raise BundlerExecutionError(message=message_out)

return out.decode("utf8").strip()
22 changes: 21 additions & 1 deletion tests/unit/workflows/ruby_bundler/test_bundler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from unittest.mock import patch
from os import linesep

from aws_lambda_builders.workflows.ruby_bundler.bundler import SubprocessBundler, BundlerExecutionError

Expand Down Expand Up @@ -73,13 +74,32 @@ def test_bundle_file_removed_if_generated(self):
self.osutils.get_bundle_dir.assert_called_once()
self.osutils.remove_directory.assert_called_once()

def test_raises_BundlerExecutionError_with_err_text_if_retcode_is_not_0(self):
def test_raises_BundlerExecutionError_with_stdout_text_if_retcode_is_not_0(self):
self.popen.returncode = 1
self.popen.out = b"some error text\n\n"
self.popen.err = b""
with self.assertRaises(BundlerExecutionError) as raised:
self.under_test.run(["install", "--without", "development", "test"])
self.assertEqual(raised.exception.args[0], "Bundler Failed: some error text")

def test_raises_BundlerExecutionError_with_stderr_text_if_retcode_is_not_0(self):
self.popen.returncode = 1
self.popen.err = b"some error text\n\n"
self.popen.out = b""
with self.assertRaises(BundlerExecutionError) as raised:
self.under_test.run(["install", "--without", "development", "test"])
self.assertEqual(raised.exception.args[0], "Bundler Failed: some error text")

def test_raises_BundlerExecutionError_with_both_stderr_and_stdout_text_if_retcode_is_not_0(self):
self.popen.returncode = 1
self.popen.err = b"some error text from stderr\n\n"
self.popen.out = b"some error text from stdout\n\n"
with self.assertRaises(BundlerExecutionError) as raised:
self.under_test.run(["install", "--without", "development", "test"])
self.assertEqual(
raised.exception.args[0], f"Bundler Failed: some error text from stdout{linesep}some error text from stderr"
)

def test_raises_ValueError_if_args_not_a_list(self):
with self.assertRaises(ValueError) as raised:
self.under_test.run(("install", "--without", "development", "test"))
Expand Down