test_helper.bash 4.87 KB
Newer Older
1
export btf=$(basename $BATS_TEST_FILENAME)
2 3
export curl_params="/tmp/.bats-curl_params"
export test_id="/tmp/.bats-test_id"
4 5 6

unset HTTP_STATUS_CODE
unset HTTP_BODY
7

zauberstuhl's avatar
zauberstuhl committed
8 9 10 11 12 13 14 15 16 17 18 19
# will run on every test and
# verify installed dependencies
function setup() {
  # postgresql client
  command -v psql
  [ "$?" -eq 0 ]
  # curl http client
  command -v curl
  [ "$?" -eq 0 ]
  # json parser
  command -v jq
  [ "$?" -eq 0 ]
zauberstuhl's avatar
zauberstuhl committed
20 21 22
  # git binary
  command -v git
  [ "$?" -eq 0 ]
23 24 25 26 27 28 29 30
  # setup database
  docker inspect $(container_id "postgres") > /dev/null || {
    init_database;
  }
  # setup redis
  docker inspect $(container_id "redis") > /dev/null || {
    init_redis;
  }
zauberstuhl's avatar
zauberstuhl committed
31
}
32

33
# fetch "POST" "data1=one&data2=two" "url" "http://server/endpoint"
34 35
function fetch() {
  tmp=$(mktemp)
36 37

  unset params
38 39
  unset field_params

40
  [ -f "$curl_params" ] && params=$(cat $curl_params)
41 42 43 44 45 46 47 48
  if [[ "$3" == "url" ]]; then field_params="-d '$2'"
  else
    IFS='&' read -ra reqparams <<< "$2"
    for reqparam in ${reqparams[@]}; do
      field_params="$field_params -F '$reqparam'"
    done
  fi
  params="$params $field_params -k -D $tmp -s -X $1"
49

50 51
  echo "curl $params $4"
  export HTTP_BODY=$(eval "curl $params $4")
52 53 54 55 56 57
  export HTTP_STATUS_CODE=$(head -n1 $tmp |cut -d' ' -f2)
  echo "HTTP_STATUS_CODE = $HTTP_STATUS_CODE"
  echo "HTTP_BODY = $HTTP_BODY"

  rm -v $tmp
  [ "$?" -eq 0 ]
58 59
}

60 61 62 63 64
# post_form "data1=one&data2=two" "http://server/endpoint"
function post_form() {
  fetch "POST" "$1" "form" "$2"
}

65 66
# post "data1=one&data2=two" "http://server/endpoint"
function post() {
67
  fetch "POST" "$1" "url" "$2"
68 69 70 71
}

# get "http://server/endpoint?data1=one&data2=two"
function get() {
72
  fetch "GET" "" "url" "$1"
73 74 75 76
}

# start_app "g1" "3000" "testing_diaspora:v1.0.1"
function start_app() {
77 78 79 80 81
  params=""
  if [ ! -z ${PRSHA} ]; then
    echo "!!!!!!! Custom build active !!!!!!!"
    echo "Using repository $PRREPO"
    echo "With SHA $PRSHA"
82
    params="-e PROJECT=$PROJECT -e PRSHA=$PRSHA -e PRREPO=$PRREPO"
83
  fi
84 85 86 87 88 89
  cid=$(container_id "$1")
  echo "Starting docker container $cid (thefederation/$3) on port $2"
  docker run --name=$cid $params \
    --link $(container_id "postgres"):postgreshost \
    --link $(container_id "redis"):redishost \
    -e DATABASE=$1 -e PORT=$2 -d thefederation/$3
90 91 92 93
}

# stop_app "g1"
function stop_app() {
94
  docker kill "$(container_id "$1")"
95 96 97 98
}

# remove_app "g1"
function remove_app() {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  docker rm "$(container_id "$1")"
}

# container_id "g1"
function container_id() {
  # set a gloabl test ID for multiple project testing
  if [ ! -f "$test_id" ]; then
    shuf -i 10000-90000 -n 1 > $test_id
  fi
  echo "$(cat $test_id)-$1"
}

# container_ip "g1"
function container_ip() {
  docker inspect \
    -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
    "$(container_id "$1")"
116 117
}

118 119
# wait_for_mastodon "m1"
function wait_for_mastodon() {
120
  cid=$(container_id "$1")
121
  function cmd() {
122
    docker logs $cid 2>&1 |grep 'webpack: Compiled successfully' |wc -l
123 124 125 126
  }
  wait_for cmd "2"
}

127
# wait_for "docker logs g1" "Listening on" 120
128
function wait_for() {
129
  timeout=500
zauberstuhl's avatar
zauberstuhl committed
130
  if [[ "$3" =~ ^[0-9]+$ ]] ; then
131 132
    timeout=$3
  fi
zauberstuhl's avatar
zauberstuhl committed
133 134
  command -v $1 >/dev/null
  if [ "$?" -ne 0 ]; then echo 1; return; fi
zauberstuhl's avatar
zauberstuhl committed
135 136 137

  i=0; while true; do
    if [ "$i" -gt "$timeout" ]; then break; fi
138
    ((i++))
139
    sleep 1
zauberstuhl's avatar
zauberstuhl committed
140 141 142 143
  done & >/dev/null 2>&1
  TIMEOUT_PID=$!

  while true; do
144
    $1 2>&1 |grep -m 1 "$2" >/dev/null
zauberstuhl's avatar
zauberstuhl committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    if [ "$?" -eq 0 ]; then break; fi
    sleep 1
  done & >/dev/null 2>&1
  CMD_PID=$!

  while true; do
    # check timeout child
    kill -0 $TIMEOUT_PID >/dev/null 2>&1
    if [ "$?" -gt 0 ]; then
      kill -9 $CMD_PID
      echo 1
      break
    fi
    # check cmd child
    kill -0 $CMD_PID >/dev/null 2>&1
    if [ "$?" -gt 0 ]; then
      kill -9 $TIMEOUT_PID
      echo 0
      break
    fi
    sleep 1
166 167
  done
}
zauberstuhl's avatar
zauberstuhl committed
168 169 170

# query "g1" "select count(*) from posts;"
function query() {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  out=$(psql -t -h $(container_ip "postgres") -d $1 -U postgres -c "$2")
  code=$?
  echo -ne $out
  return $code
}

# init_database will be triggered on setup()
function init_database() {
  docker run -d --name $(container_id "postgres") postgres:latest
  sleep 5 # timeout for container otherwise docker-logs fails
}

# init_redis will be triggered on setup()
function init_redis() {
  docker run -d --name $(container_id "redis") redis:latest
  sleep 5 # timeout for container otherwise docker-logs fails
}

# create_database "g1"
function create_database() {
  query "postgres" "create database $1;"
}

# drop_database "g1"
function drop_database() {
  query "postgres" "drop database $1;"
zauberstuhl's avatar
zauberstuhl committed
197
}
zauberstuhl's avatar
zauberstuhl committed
198 199 200 201 202

# json_value "ID"
function json_value() {
  echo $HTTP_BODY | jq -r ".$1"
}
zauberstuhl's avatar
zauberstuhl committed
203 204 205 206 207 208

# latest_tag "diaspora"
function latest_tag() {
  git tag |sort -r |while read tag; do
    label=$(echo $tag |cut -d- -f2)
    if [ "$label" == "$1" ]; then
209
      echo :$tag
210
      break
zauberstuhl's avatar
zauberstuhl committed
211 212 213
    fi
  done
}
214 215 216

# rails_runner "d1" "puts 'Hello World!'"
function rails_runner() {
217
  docker exec "$(container_id "$1")" bundle exec rails runner "$2"
218
}