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

I'm getting error:

tests/integration/test_api.py::TestJobs::test_create_job_with_secret_volumes_ok PASSEDException ignored in: <function Popen.__del__ at 0x7f14f584c5f0>
Traceback (most recent call last):
  File "/home/ay/.pyenv/versions/3.7.6/lib/python3.7/subprocess.py", line 883, in __del__
    ResourceWarning, source=self)
ResourceWarning: subprocess 2518920 is still running
Exception ignored in: <_io.FileIO name=8 mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.BufferedReader name=8>

@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