Skip to content

Commit

Permalink
Merge pull request #291 from dhellmann/fix-network-isolation-test
Browse files Browse the repository at this point in the history
improve network isolation error handling
  • Loading branch information
mergify[bot] authored Aug 4, 2024
2 parents 5cb4a31 + b0bac30 commit 91172b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/fromager/external_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def detect_network_isolation():
subprocess.check_call(check, stderr=subprocess.STDOUT)


class NetworkIsolationError(subprocess.CalledProcessError):
pass


# based on pyproject_hooks/_impl.py: quiet_subprocess_runner
def run(
cmd: typing.Sequence[str],
Expand Down Expand Up @@ -92,6 +96,17 @@ def run(
output = completed.stdout.decode("utf-8") if completed.stdout else ""
if completed.returncode != 0:
logger.error("%s failed with %s", cmd, output)
raise subprocess.CalledProcessError(completed.returncode, cmd, output)
err_type = subprocess.CalledProcessError
if network_isolation:
# Look for a few common messages that mean there is a network
# isolation problem and change the exception type to make it easier
# for the caller to recognize that case.
for substr in [
"network unreachable",
"Network is unreachable",
]:
if substr in output:
err_type = NetworkIsolationError
raise err_type(completed.returncode, cmd, output)
logger.debug("output: %s", output)
return output
3 changes: 1 addition & 2 deletions tests/test_external_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ def test_external_commands_network_isolation(
not SUPPORTS_NETWORK_ISOLATION, reason="network isolation is not supported"
)
def test_external_commands_network_isolation_real():
with pytest.raises(subprocess.CalledProcessError) as e:
with pytest.raises(external_commands.NetworkIsolationError) as e:
external_commands.run(
["host", "github.com"],
network_isolation=True,
extra_environ={"LC_ALL": "C"},
)
exc = typing.cast(subprocess.CalledProcessError, e.value)
assert exc.returncode == 1
assert "network unreachable" in exc.stdout

0 comments on commit 91172b9

Please sign in to comment.