#!/usr/bin/env bash

##
# NOTE: This test is designed to run in bazel!
##

set -euox pipefail

TEST_COMMAND="scripts/vehicle/get_public_ip_address.sh"

function test_successful_script() {
  remote_command(){
      echo ${@}; return 0;
  }

  if source ${TEST_COMMAND} > /dev/null; then
      echo " Script correctly exited normal execution."
  else
      echo " Script failed execution."
      exit 1
  fi
}

function test_filter_address() {
  remote_command(){
      echo ${@}; return 0;
  }

  source ${TEST_COMMAND};
  # This test data was grabbed from a kitt troubleshooting thread.
  output=$(echo "eth2      Link encap:Ethernet  HWaddr 3c:fd:fe:71:ff:4a
          inet addr:10.66.12.147  Bcast:10.66.12.255  Mask:255.255.255.0
          inet6 addr: fe80::3efd:feff:fe71:ff4a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:685416 errors:0 dropped:0 overruns:0 frame:0
          TX packets:174847 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:936125642 (936.1 MB)  TX bytes:28005597 (28.0 MB)
" | filter_address)

  if [[ ${output} = "10.66.12.147" ]]; then
      echo " Successfully parsed IP Address"
  else
      echo " Failed to parse IP Address"
      exit 1
  fi
}

test_successful_script
test_filter_address
