diff --git a/.gitignore b/.gitignore index fcf02863bb44b23a0b1577904265b91ca5d32712..511c84166d644e2abe117202786cd27d3d26a7e1 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 c0f18021bb094081c1f19ef5ff7b81a0e67f8097..024adac4a6517a40d8c7a5549c3fe68c100d81c7 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 973956b177e395510d029c15eb44faa69d82486f..3e72ecadf16949a43b9117e03257a8875cad1d51 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 0000000000000000000000000000000000000000..c6b4af564e5ac10d55825877c4da1043385a215a --- /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 0000000000000000000000000000000000000000..802caf81a28987b84dbafd88abed0dcf6aff7ca1 --- /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 0000000000000000000000000000000000000000..0186c2c9d26b4113b38821e6114a0b33cd4240d3 --- /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 0000000000000000000000000000000000000000..992f1b2d18c239c42d16c0b53af135f6001dfa02 --- /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 0000000000000000000000000000000000000000..922be165d4e9f8fd81c14de3743e78541a11886c --- /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