summaryrefslogtreecommitdiffstats
path: root/bin/tests/system/get_ports.sh
blob: 96201f106f6339f17c7a0d271a867ebcf43fc4b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh

# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0.  If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.

# This script is a 'port' broker.  It keeps track of ports given to the
# individual system subtests, so every test is given a unique port range.

get_sorted_test_names() {
  find . -maxdepth 2 -mindepth 2 -type f \( -name "tests.sh" -o -name "tests*.py" \) | cut -d/ -f2 | sort -u
}

total_tests=$(get_sorted_test_names | wc -l)
ports_per_test=20

port_min=5001
port_max=$((32767 - (total_tests * ports_per_test)))

baseport=0
test_index=0
while getopts "p:t:-:" OPT; do
  if [ "$OPT" = "-" ] && [ -n "$OPTARG" ]; then
    OPT="${OPTARG%%=*}"
    OPTARG="${OPTARG#$OPT}"
    OPTARG="${OPTARG#=}"
  fi

  # shellcheck disable=SC2214
  case "$OPT" in
    p | port) baseport=$OPTARG ;;
    t | test)
      test_index=$(get_sorted_test_names | awk "/^${OPTARG}\$/ { print NR }")
      if [ -z "${test_index}" ]; then
        echo "Test '${OPTARG}' not found" >&2
        exit 1
      fi
      ;;
    -) break ;;
    *)
      echo "invalid option" >&2
      exit 1
      ;;
  esac
done

port_pool_size=$((port_max - port_min))
if [ "${baseport}" -eq 0 ]; then
  baseport="$((($(date +%s) / 3600 % port_pool_size) + port_min + (test_index * ports_per_test)))"
fi

echo "export PORT=$((baseport))"
echo "export TLSPORT=$((baseport + 1))"
echo "export HTTPPORT=$((baseport + 2))"
echo "export HTTPSPORT=$((baseport + 3))"
echo "export EXTRAPORT1=$((baseport + 4))"
echo "export EXTRAPORT2=$((baseport + 5))"
echo "export EXTRAPORT3=$((baseport + 6))"
echo "export EXTRAPORT4=$((baseport + 7))"
echo "export EXTRAPORT5=$((baseport + 8))"
echo "export EXTRAPORT6=$((baseport + 9))"
echo "export EXTRAPORT7=$((baseport + 10))"
echo "export EXTRAPORT8=$((baseport + 11))"
echo "export CONTROLPORT=$((baseport + 12))"

# Local Variables:
# sh-basic-offset: 4
# End: