From c991b6a6d332d68975ddeb714afb57aed02bb42a Mon Sep 17 00:00:00 2001
From: Clara Daia <cdhd12@inf.ufpr.br>
Date: Tue, 14 Jun 2016 11:52:16 -0300
Subject: [PATCH] Add test directory, test script and docker files

---
 .gitignore                      |   2 +
 README.md                       |   3 +-
 src/main.cpp                    |   2 +-
 test/README.md                  |  60 +++++++++++++
 test/docker/debian/Dockerfile   |  17 ++++
 test/docker/opensuse/Dockerfile |  16 ++++
 test/docker/ubuntu/Dockerfile   |  18 ++++
 test/test.sh                    | 144 ++++++++++++++++++++++++++++++++
 8 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 test/README.md
 create mode 100644 test/docker/debian/Dockerfile
 create mode 100644 test/docker/opensuse/Dockerfile
 create mode 100644 test/docker/ubuntu/Dockerfile
 create mode 100755 test/test.sh

diff --git a/.gitignore b/.gitignore
index fcf02863..511c8416 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
 build/*
 bin/*
+test/docker/*/agent/*
+test/log*
diff --git a/README.md b/README.md
index c0f18021..024adac4 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,7 @@ The agent for the SIMMC - Sistema de Monitoramento do Ministério das Comunicaç
 
 requirements
 ------------
+c++ compiler
 cmake
 
 building and compiling
@@ -18,7 +19,7 @@ to generate a `Makefile`, library objects and misterious CMake files, then
 
 	$ make
 	
-to compile. The resulting executable `agent-vx.x` 'will be in `agent/bin`.
+to compile. The resulting executable `agent-vx.x` will be in `agent/bin`.
 Run it with
 
 	$ ./agent-vx.x
diff --git a/src/main.cpp b/src/main.cpp
index 973956b1..3e72ecad 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,7 +4,7 @@
 #include <fstream>
 #include "agent/post.h"
 
-int main(){	
+int main(){
 
 	send_inventory();
 	send_net_bandwidth();
diff --git a/test/README.md b/test/README.md
new file mode 100644
index 00000000..c6b4af56
--- /dev/null
+++ b/test/README.md
@@ -0,0 +1,60 @@
+Requirements
+------------
+
+* docker
+
+Docker
+------
+
+Docker supplies images of main Linux distributions from which one can build
+their own with a single configuration file `Dockerfile`. It looks like a
+virtual machine, but uses the kernel from your currently installed OS.
+
+Running
+-------
+
+    ./test.sh [-c] [-b [IMAGE_NAME]] [-r [IMAGE_NAME]]
+
+The `test.sh` script, run without arguments, executes these three steps:
+
+* rsync the agent directories and files to a directory inside each distribution
+directory - observation: this must be done because docker does not allow
+inclusion of directories by relative paths.
+* build/update all docker images - install requirements, build and compile the
+agent
+* run all docker images - run the agent inside the images, print the JSON
+generated in execution
+
+Building the images and the agent may take some time, so you can use the options 
+
+* `-c`              to copy (rsync) the agent into the distribution directories 
+* `-b [IMAGE_NAME]` to build the docker image specified by IMAGE_NAME or all
+                    of them if none specified 
+* `-r [IMAGE_NAME]` to run the docker image specified by IMAGE_NAME or all
+                    of them if none specified
+
+to execute only part of the flow. Of course, running will not work if the images
+have not been built once previously.
+
+Images
+------
+
+Currently tested images are
+
+* ubuntu
+* debian
+* opensuse
+
+
+Dockerfiles
+-------------------
+
+* Make use of Docker cache when trying stuff: avoid adding instructions
+before long-standing ones if possible. (For example: once docker executes `RUN
+apt-get update && apt-get install ...`, the resulting image is cached and
+subsequential builds with no changes before that instruction will not execute it
+again.)
+
+* Refer to best practices
+    * https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
+    * http://crosbymichael.com/dockerfile-best-practices-take-2.html
diff --git a/test/docker/debian/Dockerfile b/test/docker/debian/Dockerfile
new file mode 100644
index 00000000..802caf81
--- /dev/null
+++ b/test/docker/debian/Dockerfile
@@ -0,0 +1,17 @@
+FROM debian:latest
+
+# install requirements
+RUN apt-get update && apt-get install -y cmake \
+    git \
+    g++
+
+# copy agent directory
+COPY agent home/agent/
+
+# cmake
+RUN /bin/bash -c 'cd home/agent/build ;\
+                  cmake -DPRINT_JSON=ON ..'
+
+# make
+RUN /bin/bash -c 'cd home/agent/build ;\
+                  make'
diff --git a/test/docker/opensuse/Dockerfile b/test/docker/opensuse/Dockerfile
new file mode 100644
index 00000000..0186c2c9
--- /dev/null
+++ b/test/docker/opensuse/Dockerfile
@@ -0,0 +1,16 @@
+FROM opensuse:latest
+
+# install requirements
+RUN zypper --non-interactive install cmake \
+    gcc-c++
+
+# copy agent directory
+COPY agent home/agent/
+
+# cmake
+RUN /bin/bash -c 'cd home/agent/build ; \
+                  cmake -DPRINT_JSON=ON ..'
+
+# make
+RUN /bin/bash -c 'cd home/agent/build ; \
+                  make'
diff --git a/test/docker/ubuntu/Dockerfile b/test/docker/ubuntu/Dockerfile
new file mode 100644
index 00000000..992f1b2d
--- /dev/null
+++ b/test/docker/ubuntu/Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:latest
+
+# install requirements
+RUN apt-get update && apt-get install -y \
+    cmake \
+    g++
+
+# copy agent directory
+COPY agent home/agent/
+
+# cmake
+RUN /bin/bash -c 'cd home/agent/build ; \
+                  cmake -DPRINT_JSON=ON ..'
+
+# make
+RUN /bin/bash -c 'cd home/agent/build ; \
+                  make'
+
diff --git a/test/test.sh b/test/test.sh
new file mode 100755
index 00000000..922be165
--- /dev/null
+++ b/test/test.sh
@@ -0,0 +1,144 @@
+#!/bin/bash
+
+# is docker installed?
+command -v docker >/dev/null 2>&1 || { echo >&2 "It seems docker is not installed. Aborting."; exit 1; }
+
+
+# Check options
+# -c - rsync agent directory into image directory
+# -b - build only specified image / all images if no argument is provided
+# -r - run only specified image / all images if no argument is provided
+
+EXECUTE_ALL=true
+RSYNC_AGENT=false
+BUILD_DOCKER_IMAGES=false
+RUN=false
+
+# current tested images
+declare -A IMAGES_LIST=([ubuntu]=1 [debian]=1 [opensuse]=1)
+
+while (($#))
+do
+    case "$1" in
+        -c)
+            RSYNC_AGENT=true
+            EXECUTE_ALL=false
+            shift 1
+            ;;
+
+        -b)
+            BUILD_DOCKER_IMAGES=true
+            EXECUTE_ALL=false
+            if [[ "$2" = -* ]] || [[ "$2" = "" ]]
+            then
+                # no arguments
+                shift 1
+            else
+                if [[ -n "${IMAGES_LIST[$2]}" ]]
+                then
+                    B_IMAGE_NAME=$2
+                else
+                    echo "Invalid image name: $2" >&2
+                fi
+                shift 2
+            fi
+            ;;
+
+        -r)
+            RUN=true
+            EXECUTE_ALL=false
+            if [[ "$2" = -* ]] || [[ "$2" = "" ]]
+            then
+                # no arguments
+                shift 1
+            else
+                if [[ -n "${IMAGES_LIST[$2]}" ]]
+                then
+                    R_IMAGE_NAME=$2
+                else
+                    echo "Invalid image name: $2" >&2
+                fi
+                shift 2
+            fi
+            ;;
+
+        \?)
+            echo "Invalid option: $2" >&2
+            shift
+            ;;
+    esac
+done
+
+
+# If no options were used, run everything
+if ($EXECUTE_ALL)
+then
+
+    RSYNC_AGENT=true
+    BUILD_DOCKER_IMAGES=true
+    RUN=true
+
+fi
+
+# rsync
+if ($RSYNC_AGENT)
+then
+    printf "**********\n*  COPY  *\n**********\n\n"
+
+    for i in "${!IMAGES_LIST[@]}"
+    do
+        printf "Copying to docker/%s/agent\n" "$i"
+        rsync -au ../* docker/"$i"/agent --exclude test --exclude build/*
+    done
+
+    printf "\n"
+
+fi
+
+
+# build images
+if ($BUILD_DOCKER_IMAGES)
+then
+    printf "***********\n*  BUILD  *\n***********\n\n"
+
+    if [ -z "${B_IMAGE_NAME}" ]
+    then
+        for i in "${!IMAGES_LIST[@]}"
+        do
+            printf "Building %s:agent\n\n" "$i"
+            docker build -t "$i":agent docker/"$i"
+            printf "\n"
+        done
+    else
+        printf "Building %s:agent\n\n" "${B_IMAGE_NAME}"
+        docker build -t "${B_IMAGE_NAME}":agent docker/"${B_IMAGE_NAME}"
+        printf "\n"
+    fi
+
+    printf "\n"
+
+fi
+
+
+# run agent into images
+if ($RUN)
+then
+    printf "*********\n*  RUN  *\n*********\n"
+
+    if [ -z "${R_IMAGE_NAME}" ]
+    then
+        for i in "${!IMAGES_LIST[@]}"
+        do
+            printf "\nRunning %s:agent\n" "$i"
+            printf "******************************************************\n"
+            docker run -it "$i":agent ./home/agent/bin/agent-v0.0
+            printf "******************************************************\n\n"
+        done
+    else
+        printf "\nRunning %s:agent\n\n" "${R_IMAGE_NAME}"
+        printf "******************************************************\n"
+        docker run -it "${R_IMAGE_NAME}":agent ./home/agent/bin/agent-v0.0
+        printf "******************************************************\n"
+    fi
+
+fi
-- 
GitLab