Skip to content

Agent Mesh

5.1 KB
Raw Blame
#!/usr/bin/env bats

load "lib/container_mesh.bash"

setup() {
  mesh_setup_env
  CLEANUP_NETWORKS=()
  DISCONNECT_CONTAINERS=()
}

teardown() {
  if [[ "${BATS_TEST_COMPLETED:-0}" -ne 1 ]]; then
    mkdir -p tests/e2e/logs
    local ids
    ids="$(docker ps -aq --filter "name=mesh-")"
    for id in ${ids}; do
      local name
      name="$(docker inspect -f '{{.Name}}' "${id}" | tr -d '/')"
      docker logs "${id}" > "tests/e2e/logs/${name}.log" 2>&1 || true
    done
  fi

  local item
  for item in "${DISCONNECT_CONTAINERS[@]}"; do
    local container="${item%%:*}"
    local net="${item#*:}"
    docker network disconnect "${net}" "${container}" >/dev/null 2>&1 || true
  done
  DISCONNECT_CONTAINERS=()

  mesh_cleanup_env
  local net
  for net in "${CLEANUP_NETWORKS[@]}"; do
    docker network rm "${net}" >/dev/null 2>&1 || true
  done
  CLEANUP_NETWORKS=()
}

@test "nodes in isolated networks discover and connect via relay" {
  run mesh_start_mock_oidc
  [[ "$status" -eq 0 ]]

  run mesh_start_router
  [[ "$status" -eq 0 ]]

  local MESH_NETWORK_2="${MESH_PREFIX}-2-net"
  docker network create "${MESH_NETWORK_2}"
  CLEANUP_NETWORKS+=("${MESH_NETWORK_2}")

  local router_node
  router_node=$(kubectl --context="${KUBECONTEXT}" get pod agentmesh-router-0 -o jsonpath='{.spec.nodeName}')
  local oidc_node
  oidc_node=$(kubectl --context="${KUBECONTEXT}" get pod -l app=mock-oidc -o jsonpath='{.items[0].spec.nodeName}')

  docker network connect "${MESH_NETWORK_2}" "${router_node}"
  DISCONNECT_CONTAINERS+=("${router_node}:${MESH_NETWORK_2}")
  if [[ "${oidc_node}" != "${router_node}" ]]; then
    docker network connect "${MESH_NETWORK_2}" "${oidc_node}"
    DISCONNECT_CONTAINERS+=("${oidc_node}:${MESH_NETWORK_2}")
  fi

  # HTTP backend on Node 1's side (default network). It exists before the
  # node: services are declared in the node's configuration, there is no
  # runtime registration endpoint.
  docker run -d \
    --name "${MESH_PREFIX}-http-service" \
    --network "${MESH_NETWORK}" \
    python:3.12 python3 -c '
from http.server import HTTPServer, BaseHTTPRequestHandler
class S(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "application/json")
        self.end_headers()
        self.wfile.write(b"{\"status\":\"success\"}")
HTTPServer(("0.0.0.0", 8000), S).serve_forever()
'
  MESH_CONTAINERS+=("${MESH_PREFIX}-http-service")

  # Wait for http-service to be listening
  local i
  for ((i=0; i<30; i++)); do
    if docker run --rm --network "${MESH_NETWORK}" python:3.12 python3 -c "import urllib.request; urllib.request.urlopen('http://${MESH_PREFIX}-http-service:8000')" >/dev/null 2>&1; then
      break
    fi
    sleep 1
  done

  local node1_cfg="${BATS_TEST_TMPDIR}/node1-services.yaml"
  cat > "${node1_cfg}" <<EOF
version: "v1alpha1"
services:
  - type: "mcp"
    name: "http-tool"
    description: "test http service"
    target_url: "http://${MESH_PREFIX}-http-service:8000"
EOF
  # The node container runs as a non-root user; a restrictive umask would
  # otherwise make the mounted config unreadable inside it.
  chmod 644 "${node1_cfg}"

  mesh_start_node "1" "--enable-relay=true --log-level=debug" "${node1_cfg}"

  OLD_NET=$MESH_NETWORK
  MESH_NETWORK=$MESH_NETWORK_2
  mesh_start_node "2" "--log-level=debug"
  MESH_NETWORK=$OLD_NET

  # Gate on MCP actually answering: get_mesh_info polls below go through it, and
  # a not-yet-listening sidecar burns discovery budget on empty responses.
  run mesh_wait_for_mcp_ready "1" 30
  [[ "$status" -eq 0 ]]
  local node1_peer_id node2_peer_id
  node1_peer_id=$(mesh_node_peer_id 1)
  [[ -n "${node1_peer_id}" ]]

  OLD_NET=$MESH_NETWORK
  MESH_NETWORK=$MESH_NETWORK_2
  run mesh_wait_for_mcp_ready "2" 30
  [[ "$status" -eq 0 ]]
  node2_peer_id=$(mesh_node_peer_id 2)
  [[ -n "${node2_peer_id}" ]]
  MESH_NETWORK=$OLD_NET

  # 2. Test HTTP Datapath: Node 2 calls Node 1's HTTP service via Node 2's
  # egress proxy (isolated network 2). The nodes are on networks that cannot
  # reach each other, so the call is what connects them, through the router.
  for ((i=0; i<30; i++)); do
    run docker run --rm --network "${MESH_NETWORK_2}" python:3.12 python3 -c "
import urllib.request
req = urllib.request.Request(
    \"http://${MESH_PREFIX}-node-2:8080/mesh/${node1_peer_id}/mcp/http-tool/\",
    headers={\"X-Mesh-Authentication\": \"Bearer secret-token\"}
)
with urllib.request.urlopen(req) as response:
    print(response.read().decode(\"utf-8\"))
"
    if [[ "$status" -eq 0 ]] && [[ "$output" == *"{\"status\":\"success\"}"* ]]; then
      break
    fi
    sleep 1
  done

  [[ "$status" -eq 0 ]]
  [[ "$output" == *"{\"status\":\"success\"}"* ]]

  # The call left the two connected, through the relay. Assert on the
  # specific peer, not on set size: a relayed connection can leave extra
  # entries in connected_peers.
  run mesh_wait_for_peer_connection "1" "${node2_peer_id}" 30
  [[ "$status" -eq 0 ]]

  OLD_NET=$MESH_NETWORK
  MESH_NETWORK=$MESH_NETWORK_2
  run mesh_wait_for_peer_connection "2" "${node1_peer_id}" 30
  [[ "$status" -eq 0 ]]
  MESH_NETWORK=$OLD_NET

  docker network rm "${MESH_NETWORK_2}" >/dev/null 2>&1 || true
}