Skip to content

Commit

Permalink
feat: Make it easier to launch the api server locally without the bac…
Browse files Browse the repository at this point in the history
…kend.
  • Loading branch information
another-rex committed Oct 30, 2024
1 parent 9647b97 commit db9ad4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ run-appengine-staging:
run-api-server:
test -f $(HOME)/.config/gcloud/application_default_credentials.json || (echo "GCP Application Default Credentials not set, try 'gcloud auth login --update-adc'"; exit 1)
cd gcp/api && docker build -f Dockerfile.esp -t osv/esp:latest .
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json
cd gcp/api && $(install-cmd) && GOOGLE_CLOUD_PROJECT=oss-vdb $(run-cmd) python test_server.py $(HOME)/.config/gcloud/application_default_credentials.json $(NOBACKUP)

# TODO: API integration tests.
all-tests: lib-tests worker-tests importer-tests alias-tests appengine-tests vulnfeed-tests
19 changes: 14 additions & 5 deletions gcp/api/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, backend, esp):

def check(self):
"""Check that the server is still up."""
if self.backend.poll():
if self.backend and self.backend.poll():
raise RuntimeError('Backend process died.')

if self.esp.poll():
Expand All @@ -41,7 +41,8 @@ def check(self):
def stop(self):
"""Stop the server."""
self.esp.kill()
self.backend.kill()
if self.backend:
self.backend.kill()


def start_backend(port, log_path):
Expand Down Expand Up @@ -154,12 +155,16 @@ def start_esp(port, backend_port, credential_path, log_path):
return esp_proc


def start(credential_path, port=_ESP_PORT, backend_port=_BACKEND_PORT):
def start(credential_path,
no_backend=False,
port=_ESP_PORT,
backend_port=_BACKEND_PORT):
"""Start the test server."""
backend = None
esp = None
try:
backend = start_backend(_BACKEND_PORT, 'backend.log')
if not no_backend:
backend = start_backend(_BACKEND_PORT, 'backend.log')
esp = start_esp(port, backend_port, credential_path, 'esp.log')
except Exception:
if esp:
Expand All @@ -178,7 +183,11 @@ def start(credential_path, port=_ESP_PORT, backend_port=_BACKEND_PORT):
print(f'Usage: {sys.argv[0]} path/to/credential.json')
sys.exit(1)

server = start(sys.argv[1])
no_backend = False
if len(sys.argv) == 3:
no_backend = sys.argv[2] == '--no-backend'

server = start(sys.argv[1], no_backend=no_backend)
try:
while True:
server.check()
Expand Down

0 comments on commit db9ad4f

Please sign in to comment.