mirror of https://github.com/google/oss-fuzz.git
85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
|
# Copyright 2020 Google Inc.
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
#
|
||
|
################################################################################
|
||
|
"""Utility functions for testing cloud functions."""
|
||
|
import os
|
||
|
import subprocess
|
||
|
import threading
|
||
|
|
||
|
import requests
|
||
|
|
||
|
DATASTORE_READY_INDICATOR = b'is now running'
|
||
|
DATASTORE_EMULATOR_PORT = 8432
|
||
|
EMULATOR_TIMEOUT = 20
|
||
|
TEST_PROJECT_ID = 'test-project'
|
||
|
|
||
|
|
||
|
def start_datastore_emulator():
|
||
|
"""Start Datastore emulator."""
|
||
|
return subprocess.Popen([
|
||
|
'gcloud',
|
||
|
'beta',
|
||
|
'emulators',
|
||
|
'datastore',
|
||
|
'start',
|
||
|
'--consistency=1.0',
|
||
|
'--host-port=localhost:' + str(DATASTORE_EMULATOR_PORT),
|
||
|
'--project=' + TEST_PROJECT_ID,
|
||
|
'--no-store-on-disk',
|
||
|
],
|
||
|
stdout=subprocess.PIPE,
|
||
|
stderr=subprocess.STDOUT)
|
||
|
|
||
|
|
||
|
def wait_for_emulator_ready(proc,
|
||
|
emulator,
|
||
|
indicator,
|
||
|
timeout=EMULATOR_TIMEOUT):
|
||
|
"""Wait for emulator to be ready."""
|
||
|
|
||
|
def _read_thread(proc, ready_event):
|
||
|
"""Thread to continuously read from the process stdout."""
|
||
|
ready = False
|
||
|
while True:
|
||
|
line = proc.stdout.readline()
|
||
|
if not line:
|
||
|
break
|
||
|
if not ready and indicator in line:
|
||
|
ready = True
|
||
|
ready_event.set()
|
||
|
|
||
|
# Wait for process to become ready.
|
||
|
ready_event = threading.Event()
|
||
|
thread = threading.Thread(target=_read_thread, args=(proc, ready_event))
|
||
|
thread.daemon = True
|
||
|
thread.start()
|
||
|
if not ready_event.wait(timeout):
|
||
|
raise RuntimeError(
|
||
|
'{} emulator did not get ready in time.'.format(emulator))
|
||
|
return thread
|
||
|
|
||
|
|
||
|
def reset_ds_emulator():
|
||
|
"""Reset ds emulator/clean all entities."""
|
||
|
req = requests.post(
|
||
|
'http://localhost:{}/reset'.format(DATASTORE_EMULATOR_PORT))
|
||
|
req.raise_for_status()
|
||
|
|
||
|
|
||
|
def cleanup_emulator(ds_emulator):
|
||
|
"""Cleanup the system processes made by ds emulator."""
|
||
|
del ds_emulator #To do, find a better way to cleanup emulator
|
||
|
os.system('pkill -f datastore')
|