Skip to content

Commit

Permalink
do-not-track test: wait for stub server to start (#3455)
Browse files Browse the repository at this point in the history
Wait for the stub API server to start before proceeding with the
do-not-track test

Signed-off-by: Alex Couture-Beil <[email protected]>
  • Loading branch information
alexcb authored Nov 27, 2023
1 parent aa5c6a6 commit b47da1d
Showing 1 changed file with 69 additions and 36 deletions.
105 changes: 69 additions & 36 deletions tests/do-not-track/api-earthly-stub-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import socket
import os
import sys
import fcntl
import time
from contextlib import suppress

host = '127.0.0.1'
Expand All @@ -15,44 +17,75 @@
stdout='/dev/null'
stderr='/dev/null'

ready_pipe_r, ready_pipe_w = os.pipe()

# first fork
pid = os.fork()
if pid > 0:
sys.exit(0)
os.close(ready_pipe_w)
fcntl.fcntl(ready_pipe_r, fcntl.F_SETFL, os.O_NONBLOCK)
num_attemps_remaining = 10
while True:
try:
msg = os.read(ready_pipe_r, 1024).decode('utf8')
except BlockingIOError as e:
num_attemps_remaining -= 1
if num_attemps_remaining <= 0:
print('server failed to start')
sys.exit(1)
print('waiting for stub-server to start')
time.sleep(1)
continue
msg = msg
break
if msg == 'ready':
print('stub-server ready')
sys.exit(0)
print(f'unexpected msg "{msg}" while waiting for server to start')
sys.exit(1)

os.chdir('/')
os.setsid()
os.umask(0)
os.close(ready_pipe_r)

# second fork
pid = os.fork()
if pid > 0:
sys.exit(0)

# redirect stdio
sys.stdout.flush()
sys.stderr.flush()
si = os.open(stdin, os.O_RDWR)
so = os.open(stdout, os.O_WRONLY|os.O_TRUNC|os.O_CREAT)
se = os.open(stderr, os.O_WRONLY|os.O_TRUNC|os.O_CREAT)
os.dup2(si, sys.stdin.fileno())
os.dup2(so, sys.stdout.fileno())
os.dup2(se, sys.stderr.fileno())

# write pid to disk
pid = str(os.getpid())
with open(pidfile, 'w') as f:
f.write(pid)

# daemon ready to go

with suppress(FileNotFoundError):
os.remove(server_got_a_connection_path)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
conn, addr = s.accept()
with conn:
with open(server_got_a_connection_path, 'w') as f:
f.write('this should not have happened')
try:
os.chdir('/')
os.setsid()
os.umask(0)

# second fork
pid = os.fork()
if pid > 0:
sys.exit(0)

# redirect stdio
sys.stdout.flush()
sys.stderr.flush()
si = os.open(stdin, os.O_RDWR)
so = os.open(stdout, os.O_WRONLY|os.O_TRUNC|os.O_CREAT)
se = os.open(stderr, os.O_WRONLY|os.O_TRUNC|os.O_CREAT)
os.dup2(si, sys.stdin.fileno())
os.dup2(so, sys.stdout.fileno())
os.dup2(se, sys.stderr.fileno())

# write pid to disk
pid = str(os.getpid())
with open(pidfile, 'w') as f:
f.write(pid)

# daemon ready to go
os.write(ready_pipe_w, 'ready'.encode('utf8'))
os.close(ready_pipe_w)

with suppress(FileNotFoundError):
os.remove(server_got_a_connection_path)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
conn, addr = s.accept()
with conn:
with open(server_got_a_connection_path, 'w') as f:
f.write('this should not have happened')
except Exception as e:
os.write(ready_pipe_w, f'unexpected exception while starting server: {e}'.encode('utf8'))
os.close(ready_pipe_w)
raise

0 comments on commit b47da1d

Please sign in to comment.