Skip to content

Instantly share code, notes, and snippets.

@atemate
Created June 24, 2020 15:56
Show Gist options
  • Save atemate/70c8c992249d4fc265cc5ae1d9ec4b2a to your computer and use it in GitHub Desktop.
Save atemate/70c8c992249d4fc265cc5ae1d9ec4b2a to your computer and use it in GitHub Desktop.
@pytest.fixture(scope="session")
async def kube_proxy_url(docker_host: str) -> str:
cmd = "kubectl proxy -p 8084 --address='0.0.0.0' --accept-hosts='.*'"
err = f"Error while running command `{cmd}`"
proc = subprocess.Popen(
cmd,
shell=True,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
close_fds=True,
)
try:
prefix = "Starting to serve on "
line = proc.stdout.readline().decode().strip()
if "error" in line.lower():
raise RuntimeError(f"{err}: Found error in output: `{line}`")
if not line.startswith(prefix):
raise RuntimeError(f"{err}: Unexpected output: `{line}`")
try:
value = line[len(prefix) :]
_, port_str = value.rsplit(":", 1)
port = int(port_str)
except ValueError as e:
raise RuntimeError(f"{err}: Could not parse `{line}`: {e}")
yield f"http://{docker_host}:{port}"
finally:
proc.terminate()
proc.kill()
@atemate
Copy link
Author

atemate commented Jun 24, 2020

should be:

finally:
        proc.terminate()
        proc.wait()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment