|
| 1 | +#!/usr/bin/env bash |
| 2 | +# vim: ts=4 |
| 3 | + |
| 4 | +# Copyright 2025 Comcast Cable Communications Management, LLC |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# SPDX-License-Identifier: Apache-2.0 |
| 19 | + |
| 20 | +set -eu |
| 21 | + |
| 22 | +die() { |
| 23 | + echo "$@" >/dev/stderr |
| 24 | + exit 1 |
| 25 | +} |
| 26 | + |
| 27 | +kill-rec() { |
| 28 | + local pid="$1" i= children= |
| 29 | + if children="$(pgrep -P "$pid")"; then |
| 30 | + for i in $children; do |
| 31 | + kill-rec $i |
| 32 | + done |
| 33 | + fi |
| 34 | + kill -9 "$pid" |
| 35 | +} |
| 36 | + |
| 37 | +specOpenRpc= |
| 38 | +specAppOpenRpc= |
| 39 | +mockPath= |
| 40 | +mockConfig= |
| 41 | +mockPort=9998 |
| 42 | +testExe= |
| 43 | + |
| 44 | +while [[ -n ${1:-} ]]; do |
| 45 | + case $1 in |
| 46 | + --mock) mockPath="$2"; shift;; |
| 47 | + --port) mockPort="$2"; shift;; |
| 48 | + --config) mockConfig="$2"; shift;; |
| 49 | + --openrpc) specOpenRpc="$2"; shift;; |
| 50 | + --app-openrpc) specAppOpenRpc="$2"; shift;; |
| 51 | + --test-exe) testExe="$2"; shift;; |
| 52 | + *) break;; |
| 53 | + esac; shift |
| 54 | +done |
| 55 | + |
| 56 | +[[ -e "$mockPath" ]] || die "mock-firebolt not installed" |
| 57 | +[[ -e "$specOpenRpc" ]] || die "OpenRPC spec '$specOpenRpc' not found" |
| 58 | +[[ -e "$specAppOpenRpc" ]] || die "OpenRPC App spec '$specAppOpenRpc' not found" |
| 59 | +[[ -e "$mockConfig" ]] || die "Config '$mockConfig' not found" |
| 60 | +[[ -e "$testExe" ]] || die "Executable for cpp_test '$testExe' not found" |
| 61 | + |
| 62 | +cfgFile=$mockPath/server/src/.mf.config.json |
| 63 | + |
| 64 | +source "$NVM_DIR/nvm.sh" |
| 65 | + |
| 66 | +echo "Updating config for mock, $cfgFile" |
| 67 | +jq ' |
| 68 | + (.supportedOpenRPCs[] | select(.name=="core")).fileName = "'"$specOpenRpc"'" |
| 69 | + | (.supportedToAppOpenRPCs[] | select(.name=="coreToApp")).fileName = "'"$specAppOpenRpc"'" |
| 70 | +' \ |
| 71 | + $mockConfig > $cfgFile |
| 72 | + |
| 73 | +echo "Starting mock-server, $mockPath/server" |
| 74 | +cd $mockPath/server |
| 75 | +npm start & |
| 76 | +mock_pid=$! |
| 77 | +echo "Mock started at pid: $mock_pid" |
| 78 | + |
| 79 | +# Setup cleanup trap to ensure mock server is always stopped |
| 80 | +cleanup() { |
| 81 | + echo "Cleaning up mock server (pid: $mock_pid)" |
| 82 | + kill-rec $mock_pid 2>/dev/null || true |
| 83 | +} |
| 84 | +trap cleanup EXIT |
| 85 | + |
| 86 | +sleep 1 |
| 87 | +try=0 |
| 88 | +maxTries=10 |
| 89 | +while ! nc -z localhost $mockPort >/dev/null 2>&1; do |
| 90 | + (( try < maxTries )) || die "Mock server not ready after $try tries" |
| 91 | + (( ++try )) |
| 92 | + printf "%d/%d Waiting for mock-server to be up&ready\n" $try $maxTries |
| 93 | + sleep 1 |
| 94 | +done |
| 95 | + |
| 96 | +echo "Starting cpp_test with -mock and -auto flags" |
| 97 | +cd "$(dirname "$testExe")" |
| 98 | + |
| 99 | +# Capture exit code without triggering set -e |
| 100 | +set +e |
| 101 | +"./$(basename "$testExe")" -mock -auto |
| 102 | +exitCode=$? |
| 103 | +set -e |
| 104 | + |
| 105 | +exit $exitCode |
0 commit comments