github.com/vmware/govmomi@v0.37.2/govc/test/test_helper.bash (about)

     1  # set the following variables only if they've not been set
     2  export GOVC_DATASTORE=${GOVC_DATASTORE-datastore1}
     3  export GOVC_NETWORK=${GOVC_NETWORK-"VM Network"}
     4  
     5  export GOVC_INSECURE=true
     6  export GOVC_PERSIST_SESSION=false
     7  unset GOVC_URL
     8  unset GOVC_DEBUG
     9  unset GOVC_TLS_KNOWN_HOSTS
    10  unset GOVC_TLS_HANDSHAKE_TIMEOUT
    11  unset GOVC_DATACENTER
    12  unset GOVC_HOST
    13  unset GOVC_USERNAME
    14  unset GOVC_PASSWORD
    15  unset GOVC_GUEST_LOGIN
    16  
    17  if [ -z "$BATS_TEST_DIRNAME" ]; then
    18    BATS_TEST_DIRNAME=$(dirname ${BASH_SOURCE})
    19  fi
    20  
    21  # gnu core utils
    22  readlink=$(type -p greadlink readlink | head -1)
    23  xargs=$(type -p gxargs xargs | head -1)
    24  mktemp=$(type -p gmktemp mktemp | head -1)
    25  timeout=$(type -p gtimeout timeout | head -1)
    26  
    27  BATS_TEST_DIRNAME=$($readlink -nf $BATS_TEST_DIRNAME)
    28  
    29  GOVC_IMAGES=$BATS_TEST_DIRNAME/images
    30  TTYLINUX_NAME=ttylinux-pc_i486-16.1
    31  
    32  GOVC_TEST_VMDK_SRC=$GOVC_IMAGES/${TTYLINUX_NAME}-disk1.vmdk
    33  GOVC_TEST_VMDK=govc-images/$(basename $GOVC_TEST_VMDK_SRC)
    34  
    35  GOVC_TEST_ISO_SRC=$GOVC_IMAGES/${TTYLINUX_NAME}.iso
    36  GOVC_TEST_ISO=govc-images/$(basename $GOVC_TEST_ISO_SRC)
    37  
    38  GOVC_TEST_IMG_SRC=$GOVC_IMAGES/floppybird.img
    39  GOVC_TEST_IMG=govc-images/$(basename $GOVC_TEST_IMG_SRC)
    40  
    41  PATH="$GOPATH/bin:$PATH"
    42  
    43  require_docker() {
    44    if ! docker version ; then
    45      skip "docker client not installed"
    46    fi
    47  }
    48  
    49  docker_name() {
    50    echo "vcsim-$1-$(govc object.collect -s "vm/$1" config.uuid)"
    51  }
    52  
    53  vcsim_start() {
    54      GOVC_SIM_ENV="$BATS_TMPDIR/$(new_id)"
    55      export GOVC_SIM_ENV
    56      mkfifo "$GOVC_SIM_ENV"
    57  
    58      if [ -z "$timeout" ] ; then
    59        vcsim -l 127.0.0.1:0 -E "$GOVC_SIM_ENV" "$@" &
    60      else
    61        timeout -s QUIT 5m vcsim -l 127.0.0.1:0 -E "$GOVC_SIM_ENV" "$@" &
    62      fi
    63      pid=$!
    64  
    65      eval "$(head -n1 "$GOVC_SIM_ENV")"
    66      rm -f "$GOVC_SIM_ENV"
    67      GOVC_SIM_PID=$pid
    68  }
    69  
    70  vcsim_stop() {
    71    if kill "$GOVC_SIM_PID" 2>/dev/null ; then
    72      wait "$GOVC_SIM_PID"
    73    fi
    74    unset GOVC_SIM_PID
    75  }
    76  
    77  teardown() {
    78    if [ -n "$GOVC_SIM_PID" ] ; then
    79      vcsim_stop
    80    else
    81      govc ls vm | grep govc-test- | $xargs -r govc vm.destroy
    82      govc datastore.ls | grep govc-test- | awk '{print ($NF)}' | $xargs -n1 -r govc datastore.rm
    83      govc ls "host/*/Resources/govc-test-*" | $xargs -r govc pool.destroy
    84    fi
    85  }
    86  
    87  new_id() {
    88    echo "govc-test-$(vcsim uuidgen)"
    89  }
    90  
    91  import_ttylinux_vmdk() {
    92    govc datastore.mkdir -p govc-images
    93    govc datastore.ls "$GOVC_TEST_VMDK" >/dev/null 2>&1 || \
    94      govc import.vmdk "$GOVC_TEST_VMDK_SRC" govc-images > /dev/null
    95  }
    96  
    97  datastore_upload() {
    98    src=$1
    99    dst=govc-images/$(basename $src)
   100  
   101    govc datastore.mkdir -p govc-images
   102    govc datastore.ls "$dst" >/dev/null 2>&1 || \
   103      govc datastore.upload "$src" "$dst" > /dev/null
   104  }
   105  
   106  upload_img() {
   107    datastore_upload $GOVC_TEST_IMG_SRC
   108  }
   109  
   110  upload_iso() {
   111    datastore_upload $GOVC_TEST_ISO_SRC
   112  }
   113  
   114  new_ttylinux_vm() {
   115    import_ttylinux_vmdk
   116    id=$(new_id)
   117    govc vm.create -m 32 -disk $GOVC_TEST_VMDK -disk.controller ide -on=false $id
   118    echo $id
   119  }
   120  
   121  new_empty_vm() {
   122    id=$(new_id)
   123    govc vm.create -on=false $id
   124    echo $id
   125  }
   126  
   127  vm_power_state() {
   128    govc vm.info "$1" | grep "Power state:" | awk -F: '{print $2}' | collapse_ws
   129  }
   130  
   131  vm_mac() {
   132    govc device.info -vm "$1" ethernet-0 | grep "MAC Address" | awk '{print $NF}'
   133  }
   134  
   135  esx_env() {
   136    if [ -z "$GOVC_TEST_URL" ] ; then
   137      skip "GOVC_TEST_URL not set"
   138    fi
   139  
   140    export GOVC_URL=$GOVC_TEST_URL
   141  }
   142  
   143  vcsim_env_todo() {
   144    skip "not yet supported by vcsim"
   145  }
   146  
   147  # starts vcsim and exports the environment
   148  vcsim_env() {
   149    vcsim_start "$@"
   150  
   151    export GOVC_DATASTORE=LocalDS_0
   152  
   153    if [ "$1" != "-esx" ] && [ "$1" != "-esx=true" ]; then
   154      export GOVC_DATACENTER=DC0 \
   155             GOVC_HOST=/DC0/host/DC0_C0/DC0_C0_H0 \
   156             GOVC_RESOURCE_POOL=/DC0/host/DC0_C0/Resources \
   157             GOVC_NETWORK=/DC0/network/DC0_DVPG0
   158    fi
   159  }
   160  
   161  # remove username/password from $GOVC_URL and set $GOVC_{USERNAME,PASSWORD}
   162  govc_url_to_vars() {
   163    GOVC_USERNAME="$(govc env GOVC_USERNAME)"
   164    GOVC_PASSWORD="$(govc env GOVC_PASSWORD)"
   165    GOVC_URL="$(govc env GOVC_URL)"
   166    export GOVC_URL GOVC_USERNAME GOVC_PASSWORD
   167  
   168    # double check that we removed user/pass
   169    grep -q -v @ <<<"$GOVC_URL"
   170  }
   171  
   172  quit_vnc() {
   173    if [ "$(uname)" = "Darwin" ]; then
   174      osascript <<EOF
   175  tell application "Screen Sharing"
   176     quit
   177  end tell
   178  EOF
   179    fi
   180  }
   181  
   182  open_vnc() {
   183    url=$1
   184    echo "open $url"
   185  
   186    if [ "$(uname)" = "Darwin" ]; then
   187      open $url
   188    fi
   189  }
   190  
   191  # collapse spaces, for example testing against Go's tabwriter output
   192  collapse_ws() {
   193    local line
   194    if [ $# -eq 0 ]; then line="$(cat -)"
   195    else line="$@"
   196    fi
   197    echo "$line" | tr -s ' ' | sed -e 's/^ //'
   198  }
   199  
   200  # the following helpers are borrowed from the test_helper.bash in https://github.com/sstephenson/rbenv
   201  
   202  flunk() {
   203    { if [ "$#" -eq 0 ]; then cat -
   204      else echo "$@"
   205      fi
   206    } >&2
   207    return 1
   208  }
   209  
   210  assert_success() {
   211    if [ "$status" -ne 0 ]; then
   212      flunk "command failed with exit status $status: $output"
   213    elif [ "$#" -gt 0 ]; then
   214      assert_output "$1"
   215    fi
   216  }
   217  
   218  assert_failure() {
   219    if [ "$status" -ne 1 ]; then
   220      flunk $(printf "expected failed exit status=1, got status=%d" $status)
   221    elif [ "$#" -gt 0 ]; then
   222      assert_output "$1"
   223    fi
   224  }
   225  
   226  assert_equal() {
   227    if [ "$(collapse_ws "$1")" != "$(collapse_ws "$2")" ]; then
   228      { echo "expected: $1"
   229        echo "actual:   $2"
   230      } | flunk
   231    fi
   232  }
   233  
   234  assert_output() {
   235    local expected
   236    if [ $# -eq 0 ]; then expected="$(cat -)"
   237    else expected="$1"
   238    fi
   239    assert_equal "$expected" "$output"
   240  }
   241  
   242  assert_matches() {
   243    local pattern="${1}"
   244    local actual="${2}"
   245  
   246    if [ $# -eq 1 ]; then
   247      actual="$output"
   248    fi
   249  
   250    if ! grep -E -q "${pattern}" <<<"${actual}"; then
   251      { echo "pattern: ${pattern}"
   252        echo "actual:  ${actual}"
   253      } | flunk
   254    fi
   255  }
   256  
   257  assert_number() {
   258    assert_matches "^-?[0-9]+$" "$output"
   259  }
   260  
   261  assert_empty() {
   262    local actual="${1}"
   263  
   264    if [ $# -eq 0 ]; then
   265      actual="$(cat -)"
   266    fi
   267  
   268    if [ -n "${actual}" ]; then
   269      { echo "actual: ${actual}"
   270      } | flunk
   271    fi
   272  }
   273  
   274  assert_line() {
   275    if [ "$1" -ge 0 ] 2>/dev/null; then
   276      assert_equal "$2" "$(collapse_ws ${lines[$1]})"
   277    else
   278      local line
   279      for line in "${lines[@]}"; do
   280        if [ "$(collapse_ws $line)" = "$1" ]; then return 0; fi
   281      done
   282      flunk "expected line \`$1'"
   283    fi
   284  }
   285  
   286  refute_line() {
   287    if [ "$1" -ge 0 ] 2>/dev/null; then
   288      local num_lines="${#lines[@]}"
   289      if [ "$1" -lt "$num_lines" ]; then
   290        flunk "output has $num_lines lines"
   291      fi
   292    else
   293      local line
   294      for line in "${lines[@]}"; do
   295        if [ "$line" = "$1" ]; then
   296          flunk "expected to not find line \`$line'"
   297        fi
   298      done
   299    fi
   300  }
   301  
   302  assert() {
   303    if ! "$@"; then
   304      flunk "failed: $*"
   305    fi
   306  }