github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/server/grpcservice/client/testing/client_test.sh (about)

     1  #!/bin/bash
     2  # Copyright 2017 Google Inc.
     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  #     https://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  #
    17  # Unit test for client.
    18  #
    19  # This script reserves ports and then starts two processes which communicate
    20  # with each other using these ports:
    21  #
    22  # 1) A loopback python script based on the grpcservice client library.
    23  #
    24  # 2) A tester which is, primarily, a fleetspeak server with the grpcservice
    25  # installed.
    26  #
    27  # Then the tester sends a message through the loopback, as if it came from a
    28  # client, and waits for the looped message to come back to the same client.
    29  #
    30  # (Alternatively, this could be done by spawning loopback with tester, but using
    31  # independent processes is more realistic.)
    32  
    33  # Exit on error.
    34  set -e
    35  
    36  readonly LOOPBACK_MODULE='fleetspeak.server_connector.testing.loopback'
    37  readonly TESTER='src/server/grpcservice/client/testing/tester'
    38  
    39  function randomize_tcp_port {
    40    local readonly MINPORT=32760
    41    local readonly MAXPORT=59759
    42    /bin/echo $(( MINPORT + RANDOM%(MAXPORT-MINPORT+1) ))
    43  }
    44  
    45  readonly MESSAGE_PORT=$(randomize_tcp_port)
    46  readonly ADMIN_PORT=$(randomize_tcp_port)
    47  
    48  # Start loopback in the background, kill when finished. We do not care about its
    49  # exit code.
    50  python -m "${LOOPBACK_MODULE}" \
    51    --fleetspeak_message_listen_address="localhost:${MESSAGE_PORT}" \
    52    --fleetspeak_server="localhost:${ADMIN_PORT}" &
    53  readonly PID=${!}
    54  trap "/bin/kill -- $PID ; wait" EXIT SIGINT
    55  
    56  # If anything goes wrong the tester should return a non-zero exit code and as a
    57  # unit test we should preserve this.
    58  "${TESTER}" \
    59    --admin_addr="localhost:${ADMIN_PORT}" \
    60    --message_addr="localhost:${MESSAGE_PORT}"