diff --git a/.gitignore b/.gitignore index 511c84166d644e2abe117202786cd27d3d26a7e1..f515d16927b9120b742fc99e92996aa4c95a4b12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build/* bin/* test/docker/*/agent/* +test/docker/*/*.sh test/log* diff --git a/src/linux/get_user_count.cpp b/src/linux/get_user_count.cpp index 17e703c91d0a7a498b7d93428df913d4a21330e2..ec430899efaf7310c776d954ab4dbab521e2baa9 100644 --- a/src/linux/get_user_count.cpp +++ b/src/linux/get_user_count.cpp @@ -426,7 +426,6 @@ static struct user_log * last_modified () { if ((p = (struct utmplist *)malloc(sizeof(struct utmplist))) == NULL) { fprintf(stderr, "ERROR: out of memory\n"); return NULL; - } memcpy(&p->ut, &ut, sizeof(struct utmp)); p->next = utmplist; @@ -576,7 +575,7 @@ int get_user_count () { strftime (yesterday,7,"%y%m%d",timeinfo); yesterday[6] = '\0'; - + for(i = u; i; i = u){ if(!strcmp(i->logout,"999999")){ if(strcmp(i->login,yesterday) <= 0){ diff --git a/test/README.md b/test/README.md index 6ae50d50268fc085e0b9287eeb7ed14c3dd5cb8b..7c917774c3d18c77ffefa5a282b94d9dc0b7f60f 100644 --- a/test/README.md +++ b/test/README.md @@ -8,7 +8,7 @@ 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. +virtual machine, but uses the kernel of the hosting OS. Running ------- @@ -17,21 +17,22 @@ Running 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. +* copy the agent directories and files, plus other needed scripts, 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 +agent, and run other needed scripts * 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 and through a lot of output, so you can use the options +Building the images and the agent may take some time and throw a lot of output, +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 +* `-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 * `-q` to run in quiet mode: redirect stdout to log.txt during steps "copy" and "build" @@ -53,7 +54,7 @@ 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 +subsequent builds with no changes before that instruction will not execute it again.) * Refer to best practices diff --git a/test/create_users.sh b/test/create_users.sh new file mode 100755 index 0000000000000000000000000000000000000000..6abc25d78bc0b7689bd4cf074d785105b2d5990b --- /dev/null +++ b/test/create_users.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +for username in user1 user2 user3 user4 +do +password="" +pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) +useradd -m -p $pass $username +if [ $? -eq 0 ] +then + echo "$username has been added to system!" +else + echo "Failed to add $username!" +fi + +{ +/usr/bin/expect << EOF +spawn login $username +sleep 2 +expect "Password: " +send "\r" +expect "\$ " +send "exit\r" +exit +EOF +} + +userdel $username +if [ $? -eq 0 ] +then + echo "$username has been deleted!" +else + echo "Failed to delete $username!" +fi + +done diff --git a/test/docker/debian/Dockerfile b/test/docker/debian/Dockerfile index 802caf81a28987b84dbafd88abed0dcf6aff7ca1..4eaff3908c689790ddc182d7d661f53ded616401 100644 --- a/test/docker/debian/Dockerfile +++ b/test/docker/debian/Dockerfile @@ -2,6 +2,8 @@ FROM debian:latest # install requirements RUN apt-get update && apt-get install -y cmake \ + expect \ + faketime \ git \ g++ @@ -15,3 +17,8 @@ RUN /bin/bash -c 'cd home/agent/build ;\ # make RUN /bin/bash -c 'cd home/agent/build ;\ make' + +# create, login as and delete users +COPY create_users.sh home/create_users.sh +RUN /bin/bash -c "cd home ; \ + faketime 'yesterday' ./create_users.sh" diff --git a/test/docker/opensuse/Dockerfile b/test/docker/opensuse/Dockerfile index 0186c2c9d26b4113b38821e6114a0b33cd4240d3..1bc1c2c2aec59048c32d5476310f333cdd1d2956 100644 --- a/test/docker/opensuse/Dockerfile +++ b/test/docker/opensuse/Dockerfile @@ -1,7 +1,10 @@ FROM opensuse:latest # install requirements -RUN zypper --non-interactive install cmake \ +COPY faketime-0.9.6-4.mga6.x86_64.rpm home/faketime-0.9.6-4.mga6.x86_64.rpm +RUN zypper --non-interactive --no-gpg-checks install cmake \ + expect \ + /home/faketime-0.9.6-4.mga6.x86_64.rpm \ gcc-c++ # copy agent directory @@ -14,3 +17,14 @@ RUN /bin/bash -c 'cd home/agent/build ; \ # make RUN /bin/bash -c 'cd home/agent/build ; \ make' + +# create /var/log/wtmp +RUN touch /var/log/wtmp +RUN chown root.tty /var/log/wtmp +RUN chmod 664 /var/log/wtmp + + +# create, login as and delete users +COPY create_users.sh home/create_users.sh +RUN /bin/bash -c "cd home ; \ + faketime 'yesterday' ./create_users.sh" diff --git a/test/docker/opensuse/faketime-0.9.6-4.mga6.x86_64.rpm b/test/docker/opensuse/faketime-0.9.6-4.mga6.x86_64.rpm new file mode 100644 index 0000000000000000000000000000000000000000..a2a55a90195b156b6bb17bc96f10a018d38a96d7 Binary files /dev/null and b/test/docker/opensuse/faketime-0.9.6-4.mga6.x86_64.rpm differ diff --git a/test/docker/ubuntu/Dockerfile b/test/docker/ubuntu/Dockerfile index 992f1b2d18c239c42d16c0b53af135f6001dfa02..a559fbcc85f1ce14141d16cc826f89bf962a927d 100644 --- a/test/docker/ubuntu/Dockerfile +++ b/test/docker/ubuntu/Dockerfile @@ -3,6 +3,8 @@ FROM ubuntu:latest # install requirements RUN apt-get update && apt-get install -y \ cmake \ + expect \ + faketime \ g++ # copy agent directory @@ -16,3 +18,7 @@ RUN /bin/bash -c 'cd home/agent/build ; \ RUN /bin/bash -c 'cd home/agent/build ; \ make' +# create, login as and delete users +COPY create_users.sh home/create_users.sh +RUN /bin/bash -c "cd home ; \ + faketime 'yesterday' ./create_users.sh" diff --git a/test/test.sh b/test/test.sh index fe40c702e399fae9512668b5d831c58dd97b46ed..2394c71c4783c41b12fa74d23dc30e473bb1911f 100755 --- a/test/test.sh +++ b/test/test.sh @@ -8,12 +8,14 @@ command -v docker >/dev/null 2>&1 || { echo >&2 "It seems docker is not installe # -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 -# -q - redirect stdout of rsync and docker build to log.txt file +# -q - redirect stdout of rsync and docker build to specified file / log.txt if +# no argument is provided EXECUTE_ALL=true -RSYNC_AGENT=false +RSYNC=false BUILD_DOCKER_IMAGES=false RUN=false +QUIET_MODE=false # current tested images declare -A IMAGES_LIST=([ubuntu]=1 [debian]=1 [opensuse]=1) @@ -22,7 +24,7 @@ while (($#)) do case "$1" in -c) - RSYNC_AGENT=true + RSYNC=true EXECUTE_ALL=false shift 1 ;; @@ -65,11 +67,18 @@ do -q) QUIET_MODE=true - shift 1 + if [[ "$2" = -* ]] || [[ "$2" = "" ]] + then + # no arguments + shift 1 + else + LOG_FILE_NAME=$2 + shift 2 + fi ;; - \?) - echo "Invalid option: $2" >&2 + \?) + echo "Invalid option: $2" >&2 shift ;; esac @@ -80,7 +89,7 @@ done if ($EXECUTE_ALL) then - RSYNC_AGENT=true + RSYNC=true BUILD_DOCKER_IMAGES=true RUN=true @@ -92,11 +101,16 @@ then # save stdout exec 5<&1 # redirect - exec 1> log.txt + if [ -z "${LOG_FILE_NAME}" ] + then + exec 1> log.txt + else + exec 1> "${LOG_FILE_NAME}" + fi fi # rsync -if ($RSYNC_AGENT) +if ($RSYNC) then printf "**********\n* COPY *\n**********\n\n" @@ -104,6 +118,9 @@ then do printf "Copying to docker/%s/agent\n" "$i" rsync -au ../* docker/"$i"/agent --delete --exclude test --exclude build/* + printf "Copying to docker/%s/create_users.sh\n\n" "$i" + rsync -u create_users.sh docker/"$i"/create_users.sh + done printf "\n"