kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/testdata/start_http_service.sh (about)

     1  #!/bin/bash
     2  # Copyright 2015 The Kythe Authors. All rights reserved.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #   http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  #
    16  # This script is expected to be inlined into another script via source.
    17  #
    18  # It expects the following variables to be set:
    19  #   KYTHE_BIN    - where the Kythe binaries can be found
    20  #   OUT_DIR      - temporary output directory for the test
    21  #   TEST_ENTRIES - Kythe entries for test (mutually exclusive with TEST_JSON)
    22  #   TEST_JSON    - json dump of Kythe entries for test
    23  #
    24  # It will set or clobber the following variables:
    25  #   COUNTDOWN
    26  #   LISTEN_AT - the server's hostname:port
    27  #   KYTHE_ENTRYSTREAM   - binary path to the entrystream tool
    28  #   KYTHE_HTTP_SERVER   - binary path to the http_server tool
    29  #   KYTHE_WRITE_ENTRIES - binary path to the write_entries tool
    30  #   KYTHE_WRITE_TABLES  - binary path to the write_tables tool
    31  #   PORT_FILE - the name of a file that contains the listening port
    32  #   XREFS_URI - the URI of the running xrefs service
    33  #
    34  # It will destroy the following directories:
    35  #   "${OUT_DIR}/gs"
    36  #   "${OUT_DIR}/tables"
    37  #
    38  # It depends on the following Kythe tool targets:
    39  #   "//kythe/go/platform/tools:entrystream",
    40  #   "//kythe/go/storage/tools:write_tables",
    41  #   "//kythe/go/storage/tools:write_entries",
    42  #   "//kythe/go/test/tools/http_server",
    43  
    44  set -e
    45  
    46  : ${KYTHE_WRITE_TABLES?:missing write_tables}
    47  : ${KYTHE_WRITE_ENTRIES?:missing write_entries}
    48  : ${KYTHE_ENTRYSTREAM?:missing entrystream}
    49  : ${KYTHE_HTTP_SERVER?:missing http_server}
    50  
    51  PORT_FILE="${OUT_DIR:?no output directory for test}/service_port"
    52  
    53  ENTRYSTREAM_ARGS=
    54  if [[ -z "$TEST_ENTRIES" ]]; then
    55    TEST_ENTRIES="$TEST_JSON"
    56    ENTRYSTREAM_ARGS=-read_format=json
    57  fi
    58  CAT=cat
    59  if [[ "$TEST_ENTRIES" == *.gz ]]; then
    60    CAT="gunzip -c"
    61  fi
    62  
    63  rm -rf -- "${OUT_DIR:?no output directory for test}/gs"
    64  rm -rf -- "${OUT_DIR:?no output directory for test}/tables"
    65  rm -f -- "${PORT_FILE}"
    66  mkdir -p "${OUT_DIR}/gs"
    67  mkdir -p "${OUT_DIR}/tables"
    68  $CAT "${TEST_ENTRIES:?no test entries for test}" | \
    69    "${KYTHE_ENTRYSTREAM}" $ENTRYSTREAM_ARGS \
    70    | "${KYTHE_WRITE_ENTRIES}" -graphstore "${OUT_DIR}/gs" 2>/dev/null
    71  "${KYTHE_WRITE_TABLES}" --graphstore "${OUT_DIR}/gs" --out "${OUT_DIR}/tables"
    72  # TODO(zarko): test against GRPC server implementation
    73  COUNTDOWN=16
    74  "${KYTHE_HTTP_SERVER}" -serving_table "${OUT_DIR}/tables" \
    75      -port_file="${PORT_FILE}" >&2 &
    76  while :; do
    77    if [[ -e "${PORT_FILE}" && "$(cat ${PORT_FILE} | wc -l)" -eq 1 ]]; then
    78      LISTEN_AT="localhost:$(< ${PORT_FILE})"
    79      trap 'curl -s "$LISTEN_AT/quitquitquit" || true' EXIT ERR INT
    80      break
    81    fi
    82    if [[ $((COUNTDOWN--)) -eq 0 ]]; then
    83      echo "Aborting (launching server took too long)" >&2
    84      exit 1
    85    fi
    86    sleep 1s
    87  done
    88  COUNTDOWN=16
    89  while :; do
    90    curl -sf "$LISTEN_AT/alive" >/dev/null && break
    91    echo "Waiting for server ($COUNTDOWN seconds remaining)..." >&2
    92    sleep 1s
    93    if [[ $((COUNTDOWN--)) -eq 0 ]]; then
    94      echo "Aborting (launching server took too long)" >&2
    95      exit 1
    96    fi
    97  done
    98  
    99  XREFS_URI="http://${LISTEN_AT}"