<commit_before>#!/bin/bash

# Test the mounttabled binary
#
# This test starts a mounttable server with the neighborhood option enabled,
# and mounts 1) the mounttable on itself as 'myself', and 2) www.google.com:80
# as 'google'.
#
# Then it verifies that <mounttable>.Glob(*) and <neighborhood>.Glob(nhname)
# return the correct result.

source "${VEYRON_ROOT}/environment/scripts/lib/shell_test.sh"

build() {
  local -r GO="${REPO_ROOT}/scripts/build/go"
  "${GO}" build veyron/services/mounttable/mounttabled || shell_test::fail "line ${LINENO}: failed to build mounttabled"
  "${GO}" build veyron/tools/mounttable || shell_test::fail "line ${LINENO}: failed to build mounttable"
}

main() {
  cd "${TMPDIR}"
  build

  # Start mounttabled and find its endpoint.
  local NHNAME=test$$
  local MTLOG="${TMPDIR}/mt.log"
  ./mounttabled --address=127.0.0.1:0 --neighborhood_name="${NHNAME}" > "${MTLOG}" 2>&1 &

  for i in 1 2 3 4; do
    local EP=$(grep "Mount table service at:" "${MTLOG}" | sed -e 's/^.*endpoint: //')
    if [[ -n "${EP}" ]]; then
      break
    fi
    sleep 1
  done

  [[ -z "${EP}" ]] && shell_test::fail "line ${LINENO}: no server"

  # Get the neighborhood endpoint from the mounttable.
  local -r NHEP=$(./mounttable glob ${EP} nh | grep ^nh | cut -d' ' -f2)
  [[ -z "${NHEP}" ]] && shell_test::fail "line ${LINENO}: no neighborhood server"

  # Mount objects and verify the result.
  ./mounttable mount "${EP}/myself" "${EP}" 5m > /dev/null || shell_test::fail "line ${LINENO}: failed to mount the mounttable on itself"
  ./mounttable mount "${EP}/google" /www.google.com:80 5m > /dev/null || shell_test::fail "line ${LINENO}: failed to mount www.google.com"

  # <mounttable>.Glob('*')
  local GOT=$(./mounttable glob "${EP}" '*' | sed 's/TTL .m..s/TTL XmXXs/' | sort)
  local WANT="[${EP}]
google /www.google.com:80 (TTL XmXXs)
myself ${EP} (TTL XmXXs)
nh ${NHEP} (TTL XmXXs)"
  if [[ "${GOT}" != "${WANT}" ]]; then
    shell_test::fail "line ${LINENO}: unexpected output. Got ${GOT}, want ${WANT}"
  fi

  # <neighborhood>.Glob('NHNAME')
  GOT=$(./mounttable glob "${NHEP}" "${NHNAME}" | sed 's/TTL .m..s/TTL XmXXs/' | sort)
  WANT="[${NHEP}]
${NHNAME} ${EP} (TTL XmXXs)"
  if [[ "${GOT}" != "${WANT}" ]]; then<commit_msg>veyron/services/mounttable/mounttabled: Fix test.sh<commit_after>#!/bin/bash

# Test the mounttabled binary
#
# This test starts a mounttable server with the neighborhood option enabled,
# and mounts 1) the mounttable on itself as 'myself', and 2) www.google.com:80
# as 'google'.
#
# Then it verifies that <mounttable>.Glob(*) and <neighborhood>.Glob(nhname)
# return the correct result.

source "${VEYRON_ROOT}/environment/scripts/lib/shell_test.sh"

build() {
  local -r GO="${REPO_ROOT}/scripts/build/go"
  "${GO}" build veyron/services/mounttable/mounttabled || shell_test::fail "line ${LINENO}: failed to build mounttabled"
  "${GO}" build veyron/tools/mounttable || shell_test::fail "line ${LINENO}: failed to build mounttable"
}

main() {
  cd "${TMPDIR}"
  build

  # Start mounttabled and find its endpoint.
  local NHNAME=test$$
  local MTLOG="${TMPDIR}/mt.log"
  ./mounttabled --address=127.0.0.1:0 --neighborhood_name="${NHNAME}" > "${MTLOG}" 2>&1 &

  for i in 1 2 3 4; do
    local EP=$(grep "Mount table service at:" "${MTLOG}" | sed -e 's/^.*endpoint: //')
    if [[ -n "${EP}" ]]; then
      break
    fi
    sleep 1
  done

  [[ -z "${EP}" ]] && shell_test::fail "line ${LINENO}: no server"

  # Get the neighborhood endpoint from the mounttable.
  local -r NHEP=$(./mounttable glob ${EP} nh | grep ^nh | cut -d' ' -f2)
  [[ -z "${NHEP}" ]] && shell_test::fail "line ${LINENO}: no neighborhood server"

  # Mount objects and verify the result.
  ./mounttable mount "${EP}/myself" "${EP}" 5m > /dev/null || shell_test::fail "line ${LINENO}: failed to mount the mounttable on itself"
  ./mounttable mount "${EP}/google" /www.google.com:80 5m > /dev/null || shell_test::fail "line ${LINENO}: failed to mount www.google.com"

  # <mounttable>.Glob('*')
  local GOT=$(./mounttable glob "${EP}" '*' | sed -r 's/TTL (.m)?..s/TTL XmXXs/' | sort)
  local WANT="[${EP}]
google /www.google.com:80 (TTL XmXXs)
myself ${EP} (TTL XmXXs)
nh ${NHEP} (TTL XmXXs)"
  if [[ "${GOT}" != "${WANT}" ]]; then
    shell_test::fail "line ${LINENO}: unexpected output. Got ${GOT}, want ${WANT}"
  fi

  # <neighborhood>.Glob('NHNAME')
  GOT=$(./mounttable glob "${NHEP}" "${NHNAME}" | sed 's/TTL .m..s/TTL XmXXs/' | sort)
  WANT="[${NHEP}]
${NHNAME} ${EP} (TTL XmXXs)"
  if [[ "${GOT}" != "${WANT}" ]]; then
{'fraction_non_alphanumeric': 0.19995610184372256, 'fraction_numerical': 0.010974539069359086, 'mean_word_length': 3.8842443729903535, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540784', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1642, 'n_words': 478}