Skip to content

Commit

Permalink
test: ensure that environment is preserved in subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsgruk committed Nov 29, 2024
1 parent 429f4ef commit ac3ef8b
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions tests/integration/juju.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import json
import os
import subprocess
import time
from typing import Dict, List
Expand All @@ -18,7 +19,9 @@ def model_name(cls):
def status(cls):
args = ["status", "--format", "json"]
result = cls.cli(*args)
return json.loads(result.stdout)
if result:
return json.loads(result.stdout)
return {}

@classmethod
def deploy(
Expand Down Expand Up @@ -61,8 +64,10 @@ def integrate(cls, requirer: str, provider: str):
def run(cls, unit: str, action: str):
args = ["run", "--format", "json", unit, action]
act = cls.cli(*args)
result = json.loads(act.stdout)
return result[unit]["results"]
if act:
result = json.loads(act.stdout)
return result[unit]["results"]
return {}

@classmethod
def wait_for_idle(cls, applications: List[str], timeout: int):
Expand All @@ -81,15 +86,24 @@ def wait_for_idle(cls, applications: List[str], timeout: int):

@classmethod
def cli(cls, *args):
proc = subprocess.run(
["/snap/bin/juju", *args],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env={"NO_COLOR": "true"},
)
return proc
proc = None
try:
proc = subprocess.run(
["/snap/bin/juju", *args],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=os.environ.copy().update({"NO_COLOR": "true"}),
)
return proc
except subprocess.CalledProcessError as e:
# This is obviously a bit messy, but this is only called during test code and it
# makes debugging a lot simpler.
print(e.stdout)
print(e.stderr)
finally:
return proc

@classmethod
def _unit_statuses(cls, application: str):
Expand Down

0 comments on commit ac3ef8b

Please sign in to comment.