From 985057ef9fe459956803d2ce8e41b9bf865b233f Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Wed, 31 Aug 2016 01:43:14 +0200
Subject: [PATCH] tests: copy integration test framework over from eris-cm

---
 circle.yml           |   5 +-
 client/core/core.go  |   3 +
 tests/circle_test.sh |  70 +++++++++++++
 tests/docker.sh      | 136 ++++++++++++++++++++++++
 tests/test.sh        | 242 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 455 insertions(+), 1 deletion(-)
 create mode 100644 tests/circle_test.sh
 create mode 100644 tests/docker.sh
 create mode 100644 tests/test.sh

diff --git a/circle.yml b/circle.yml
index b2bacf46..3ed8cfb0 100644
--- a/circle.yml
+++ b/circle.yml
@@ -43,8 +43,11 @@ test:
     # We only wish to test our packages not vendored ones
     - echo "Running unit tests..."
     - cd $GOPATH_REPO && glide novendor | xargs go test -v
-    - echo "Running integration tests..."
+    - echo "Running sociable unit tests..."
     - cd $GOPATH_REPO && glide novendor | xargs go test -v -tags integration
+    - echo "Running integration tests..."
+    - "tests/circle_test.sh | tee $CIRCLE_ARTIFACTS/output.log; test ${PIPESTATUS[0]} -eq 0"
+
 
 
 deployment:
diff --git a/client/core/core.go b/client/core/core.go
index 7f605f52..04dc839d 100644
--- a/client/core/core.go
+++ b/client/core/core.go
@@ -14,6 +14,9 @@
 // You should have received a copy of the GNU General Public License
 // along with Eris-RT.  If not, see <http://www.gnu.org/licenses/>.
 
+// eris-db/client/core is intended to be imported by a client library
+// that builds on the eris-db blockchain node. 
+
 package core
 
 import (
diff --git a/tests/circle_test.sh b/tests/circle_test.sh
new file mode 100644
index 00000000..4619715d
--- /dev/null
+++ b/tests/circle_test.sh
@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+# ----------------------------------------------------------
+# PURPOSE
+
+# This is the test manager for epm to be ran from circle ci.
+# It will run the testing sequence for eris-db using docker.
+
+# ----------------------------------------------------------
+# REQUIREMENTS
+
+# docker installed locally
+# docker-machine installed locally
+# eris installed locally
+
+# ----------------------------------------------------------
+# USAGE
+
+# circle_test.sh
+
+# ----------------------------------------------------------
+# Set defaults
+
+uuid=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
+machine="eris-test-edb-$uuid"
+ver=$(cat version/version.go | tail -n 1 | cut -d ' ' -f 4 | tr -d '"')
+start=`pwd`
+
+# ----------------------------------------------------------
+# Get machine sorted
+
+echo "Setting up a Machine for eris-cm Testing"
+docker-machine create --driver amazonec2 $machine 1>/dev/null
+if [ "$?" -ne 0 ]
+then
+  echo "Failed to create The Machine for eris-db Testing"
+  exit 1
+fi
+docker-machine scp tests/docker.sh ${machine}:
+if [ "$?" -ne 0 ]
+then
+  echo "Failed to copy the 'docker.sh' script into the container"
+  exit 1
+fi
+docker-machine ssh $machine sudo env DOCKER_VERSION=$DOCKER_VERSION '$HOME/docker.sh'
+if [ "$?" -ne 0 ]
+then
+  echo "Failed to install Docker client into the container"
+  exit 1
+fi
+eval $(docker-machine env $machine)
+echo "Machine setup."
+echo
+docker version
+echo
+
+# ----------------------------------------------------------
+# Run integration tests
+
+tests/test.sh
+test_exit=$?
+
+# ----------------------------------------------------------
+# Cleanup
+
+echo
+echo
+echo "Cleaning up"
+docker-machine rm --force $machine
+cd $start
+exit $test_exit
diff --git a/tests/docker.sh b/tests/docker.sh
new file mode 100644
index 00000000..b6d53e85
--- /dev/null
+++ b/tests/docker.sh
@@ -0,0 +1,136 @@
+#!/usr/bin/env bash
+# -----------------------------------------------------------------------------
+# PURPOSE
+
+# This script will setup docker.
+
+# **NOTE** -- This script is used by Eris to provision test box backends which
+# we need to be on a specific version of docker. It will likely be unuseful to
+# you.
+
+# If you are looking for a quick and easy way to set up eris on a cloud machin
+# please see https://github.com/eris-ltd/common/cloud/chains/setup/setup.sh
+
+# If $DOCKER_VERSION is set then the host will use that.
+
+# -----------------------------------------------------------------------------
+# LICENSE
+
+# The MIT License (MIT)
+# Copyright (c) 2016-Present Eris Industries, Ltd.
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+
+# -----------------------------------------------------------------------------
+# REQUIREMENTS
+
+# Ubuntu
+
+# -----------------------------------------------------------------------------
+# USAGE
+
+# docker.sh
+
+# -----------------------------------------------------------------------------
+# Set defaults
+
+default_docker="1.9.0"
+
+# -----------------------------------------------------------------------------
+# Check Ubuntu
+
+#read -p "This script only works on Ubuntu (and does no checking). It may work on some debians. Do you wish to proceed? (y/n) " -n 1 -r
+#echo
+#if [[ ! $REPLY =~ ^[Yy]$ ]]
+#then
+#  echo "OK. Not doing anything. Bye."
+#  exit 1
+#fi
+#echo "You confirmed you are on Ubuntu (or waived compatibility)."
+
+# ----------------------------------------------------------------------------
+# Check sudo
+
+if [[ "$USER" != "root" ]]
+then
+  echo "OK. Not doing anything. Bye."
+  exit 1
+fi
+echo "Privileges confirmed."
+
+# ----------------------------------------------------------------------------
+# Check Docker Version to Install
+
+if [ -z "$DOCKER_VERSION" ]
+then
+  echo "You do not have the \$DOCKER_VERSION set. Trying via hostname (an Eris paradigm)."
+  export DOCKER_VERSION=$(hostname | cut -d'-' -f4)
+  if [[ "$DOCKER_VERSION" == `hostname` ]]
+  then
+    read -p "I cannot find the Docker Version to Install. You can rerun me with \$DOCKER_VERSION set or use the defaults. Would you like the defaults? (y/n) " -n 1 -r
+    echo
+    if [[ $REPLY =~ ^[Yy]$ ]]
+    then
+      export DOCKER_VERSION="$default_docker"
+    fi
+  fi
+fi
+echo "Will install Docker for Version: $DOCKER_VERSION"
+echo
+echo
+
+# ---------------------------------------------------------------------------
+# Install Docker Version
+
+echo
+wget -qO- https://get.docker.io/gpg | apt-key add -
+sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
+apt-get update -qq
+apt-get install -qqy docker-engine
+service docker stop
+docker_place=$(which docker)
+rm "$docker_place"
+curl -sSL --ssl-req -o "$docker_place" https://get.docker.com/builds/Linux/x86_64/docker-$DOCKER_VERSION
+chmod 755 "$docker_place"
+echo
+echo "Docker installed"
+
+# ---------------------------------------------------------------------------
+# Restart Docker
+
+echo "Restarting Newly Installed Docker"
+echo
+service docker start
+sleep 3 # boot time
+
+# ---------------------------------------------------------------------------
+# Check User Needs to be Added to Docker group
+
+echo
+echo
+usermod -a -G docker $USER
+
+# ---------------------------------------------------------------------------
+# Cleanup
+
+echo
+echo
+echo "All set"
+echo
+echo
diff --git a/tests/test.sh b/tests/test.sh
new file mode 100644
index 00000000..99599d20
--- /dev/null
+++ b/tests/test.sh
@@ -0,0 +1,242 @@
+#!/usr/bin/env bash
+
+# Copyright 2015, 2016 Eris Industries (UK) Ltd.
+# This file is part of Eris-RT
+
+# Eris-RT is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# Eris-RT is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with Eris-RT.  If not, see <http://www.gnu.org/licenses/>.
+
+# ----------------------------------------------------------
+# PURPOSE
+
+# This is the integration test manager for eris-db. It will
+# run the integration testing sequence for eris-db using docker
+# and the dependent eris components within the eris platform
+# for eris-db.  Specifically eris-db and the eris-db client
+# require a key management component for signing transactions
+# and validating blocks.
+
+# ----------------------------------------------------------
+# REQUIREMENTS
+
+# eris installed locally
+
+# ----------------------------------------------------------
+# USAGE
+
+# test.sh
+
+# ----------------------------------------------------------
+# Set defaults
+
+# Where are the Things?
+
+name=eris-db
+base=github.com/eris-ltd/$name
+repo=`pwd`
+if [ "$CIRCLE_BRANCH" ]
+then
+  ci=true
+  linux=true
+elif [ "$TRAVIS_BRANCH" ]
+then
+  ci=true
+  osx=true
+elif [ "$APPVEYOR_REPO_BRANCH" ]
+then
+  ci=true
+  win=true
+else
+  repo=$GOPATH/src/$base
+  ci=false
+fi
+
+branch=${CIRCLE_BRANCH:=master}
+branch=${branch/-/_}
+branch=${branch/\//_}
+
+# Other variables
+was_running=0
+test_exit=0
+chains_dir=$HOME/.eris/chains
+
+export ERIS_PULL_APPROVE="true"
+export ERIS_MIGRATE_APPROVE="true"
+
+# ---------------------------------------------------------------------------
+# Needed functionality
+
+ensure_running(){
+  if [[ "$(eris services ls -qr | grep $1)" == "$1" ]]
+  then
+    echo "$1 already started. Not starting."
+    was_running=1
+  else
+    echo "Starting service: $1"
+    eris services start $1 1>/dev/null
+    early_exit
+    sleep 3 # boot time
+  fi
+}
+
+early_exit(){
+  if [ $? -eq 0 ]
+  then
+    return 0
+  fi
+
+  echo "There was an error duing setup; keys were not properly imported. Exiting."
+  if [ "$was_running" -eq 0 ]
+  then
+    if [ "$ci" = true ]
+    then
+      eris services stop keys
+    else
+      eris services stop -r keys
+    fi
+  fi
+  exit 1
+}
+
+get_uuid() {
+  if [[ "$(uname -s)" == "Linux" ]]
+  then
+    uuid=$(cat /proc/sys/kernel/random/uuid | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
+  elif [[ "$(uname -s)" == "Darwin" ]]
+  then
+    uuid=$(uuidgen | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
+  else
+    uuid="2231587f0fe5"
+  fi
+  echo $uuid
+}
+
+test_build() {
+  echo ""
+  echo "Building eris-cm in a docker container."
+  set -e
+  tests/build_tool.sh 1>/dev/null
+  set +e
+  if [ $? -ne 0 ]
+  then
+    echo "Could not build eris-cm. Debug via by directly running [`pwd`/tests/build_tool.sh]"
+    exit 1
+  fi
+  echo "Build complete."
+  echo ""
+}
+
+test_setup(){
+  echo "Getting Setup"
+  if [ "$ci" = true ]
+  then
+    eris init --yes --pull-images=true --testing=true 1>/dev/null
+  fi
+
+  ensure_running keys
+  echo "Setup complete"
+}
+
+check_test(){
+  # check chain is running
+  chain=( $(eris chains ls --quiet --running | grep $uuid) )
+  if [ ${#chain[@]} -ne 1 ]
+  then
+    echo "chain does not appear to be running"
+    echo
+    ls -la $dir_to_use
+    test_exit=1
+    return 1
+  fi
+
+  # check results file exists
+  if [ ! -e "$chains_dir/$uuid/accounts.csv" ]
+  then
+    echo "accounts.csv not present"
+    ls -la $chains_dir/$uuid
+    pwd
+    ls -la $chains_dir
+    test_exit=1
+    return 1
+  fi
+
+  # check genesis.json
+  genOut=$(cat $dir_to_use/genesis.json | sed 's/[[:space:]]//g')
+  genIn=$(eris chains plop $uuid genesis | sed 's/[[:space:]]//g')
+  if [[ "$genOut" != "$genIn" ]]
+  then
+    test_exit=1
+    echo "genesis.json's do not match"
+    echo
+    echo "expected"
+    echo
+    echo -e "$genOut"
+    echo
+    echo "received"
+    echo
+    echo -e "$genIn"
+    echo
+    echo "difference"
+    echo
+    diff  <(echo "$genOut" ) <(echo "$genIn") | colordiff
+    return 1
+  fi
+
+  # check priv_validator
+  privOut=$(cat $dir_to_use/priv_validator.json | tr '\n' ' ' | sed 's/[[:space:]]//g' | set 's/(,\"last_height\":[^0-9]+,\"last_round\":[^0-9]+,\"last_step\":[^0-9]+//g' )
+  privIn=$(eris data exec $uuid "cat /home/eris/.eris/chains/$uuid/priv_validator.json" | tr '\n' ' ' | sed 's/[[:space:]]//g' | set 's/(,\"last_height\":[^0-9]+,\"last_round\":[^0-9]+,\"last_step\":[^0-9]+//g' )
+  if [[ "$privOut" != "$privIn" ]]
+  then
+    test_exit=1
+    echo "priv_validator.json's do not match"
+    echo
+    echo "expected"
+    echo
+    echo -e "$privOut"
+    echo
+    echo "received"
+    echo
+    echo -e "$privIn"
+    echo
+    echo "difference"
+    echo
+    diff  <(echo "$privOut" ) <(echo "$privIn") | colordiff
+    return 1
+  fi
+}
+
+# ---------------------------------------------------------------------------
+# Get the things build and dependencies turned on
+
+echo "Hello! I'm the marmot that tests the eris-db tooling"
+start=`pwd`
+cd $repo
+test_setup
+test_build
+echo
+
+# ---------------------------------------------------------------------------
+# Go ahead with node integration tests 
+
+# TODO
+
+# ---------------------------------------------------------------------------
+# Go ahead with client integration tests !
+
+echo "Running Client Tests..."
+perform_client_tests
+
+# ---------------------------------------------------------------------------
+# Cleaning up
+
+test_teardown
\ No newline at end of file
-- 
GitLab