source: server/lib/checkrest

qndtest
Last change on this file was e93dfe5, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#839: Use global function to execute commands in the database.

  • Property mode set to 100755
File size: 4.8 KB
Line 
1#!/bin/bash
2
3#/**
4#@file    checktest
5#@brief   Run tests to check the OpenGnsys Server REST functions.
6#@usage   checktest [help]
7#@warning This script inserts test data into the database and deletes it before finishing.
8#@version 1.1.1 - Initial version.
9#@author  Ramón M. Gómez - ETSII Univ. Sevilla
10#@date    2019-10-07
11#*/ ##
12
13PROG=$(basename "$0")
14OPENGNSYS=/opt/opengnsys
15CONFIGFILE=$OPENGNSYS/etc/ogAdmServer.cfg
16RESTURL="https://localhost/opengnsys/rest"
17source $OPENGNSYS/lib/ogfunctions.sh || exit 1
18
19# Test a REST ROUTE. Parameters: Route Headers Data Agent
20function route () {
21    local ROUTE PARAM METHOD HEADER DATA AGENT RET STATUS
22    ROUTE="$1"
23    PARAM="$2"
24    HEADER="$3"
25    DATA="$4"
26    AGENT="$5"
27    METHOD="${DATA:+POST}"
28    METHOD="${METHOD:-GET}"
29    RESP=$(curl -4ks -w "%{http_code}" ${HEADER:+-H "$HEADER"} ${DATA:+-d "$DATA"} ${AGENT:+-A "$AGENT"} "$RESTURL$ROUTE")
30    RET=$?
31    CODE="${RESP: -3}"
32    RESP="${RESP::-3}"
33    [ "$PARAM" ] && PARAM="($PARAM: $(jq -r ".$PARAM // \"not found\"" <<<"$RESP"))"
34    case "$CODE" in
35        200) let OK=OK+1
36             STATUS="OK $PARAM" ;;
37        404) let FAIL=FAIL+1
38             STATUS="FAIL ($CODE: $(echo $RESP))" ;;
39        000) let FAIL=FAIL+1
40             STATUS="FAIL (Connection error: $RET)" ;;
41        *)   let FAIL=FAIL+1
42             STATUS="FAIL ($CODE: $(jq -r '.message|gsub(":.*";"")' <<<"$RESP"))" ;;
43    esac
44    printf "%-40s: %s\n" "- $METHOD $ROUTE" "$STATUS"
45}
46
47# Delete test data from the database.
48function deletedata () {
49    # Delete test data, if exists.
50    DATA="
51DELETE FROM centros, administradores_centros USING centros INNER JOIN administradores_centros
52 WHERE centros.idcentro=administradores_centros.idcentro AND centros.nombrecentro='TestOU';
53DELETE FROM repositorios WHERE nombrerepositorio='TestRepo';
54DELETE FROM aulas WHERE nombreaula='TestLab';
55DELETE FROM ordenadores WHERE nombreordenador='TestClnt';
56"
57    dbexec "$DATA"
58}
59
60# Load test data into the database.
61function loaddata () {
62    # Insert test data.
63    DATA="
64INSERT INTO centros (nombrecentro, identidad, comentarios)
65       VALUES ('TestOU', 1, 'Tests');
66SET @ou_id := LAST_INSERT_ID();
67INSERT INTO administradores_centros (idusuario, idcentro)
68       VALUES (1, @ou_id);
69INSERT INTO repositorios (nombrerepositorio, idcentro, ip)
70       VALUES ('TestRepo', @ou_id, '127.0.0.1');
71SET @repo_id := LAST_INSERT_ID();
72INSERT INTO aulas (nombreaula, idcentro, inremotepc)
73       VALUES ('TestLab', @ou_id, 0);
74SET @lab_id := LAST_INSERT_ID();
75INSERT INTO ordenadores (nombreordenador, idrepositorio, idaula, ip, mac)
76       VALUES ('TestClnt', @id_repo, @lab_id, '127.0.0.1', '001122334455');
77SET @clnt_id := LAST_INSERT_ID();
78"
79    dbexec "$DATA"
80}
81
82function runtests () {
83    local OK FAIL
84
85    let OK=FAIL=0
86    echo "Running tests..."
87    route "/info" "release"
88    route "/status" "cpu.usage"
89    DATA='{"username":"'$USUARIO'","password":"'$PASSWORD'"}'
90    route /login "" "" "$DATA"
91    HEADERS="Authorization: $(jq -r '.apikey' <<<"$RESP")"
92    route /ous
93    OU=$(jq -r '.[] | select(.name=="TestOU").id' <<<"$RESP")
94    route "/ous/$OU" "name" "$HEADERS"
95    route "/ous/$OU/repos" "" "$HEADERS"
96    REPO=$(jq -r '.[] | select(.name=="TestRepo").id' <<<"$RESP")
97    route "/ous/$OU/repos/$REPO" "name" "$HEADERS"
98    route "/ous/$OU/labs" "" "$HEADERS"
99    LAB=$(jq -r '.[] | select(.name=="TestLab").id' <<<"$RESP")
100    route "/ous/$OU/labs/$LAB" "name" "$HEADERS"
101    route "/ous/$OU/labs/$LAB/clients" "" "$HEADERS"
102    CLNT=$(jq -r '.[] | select(.name=="TestClnt").id' <<<"$RESP")
103    route "/ous/$OU/labs/$LAB/clients/$CLNT" "name" "$HEADERS"
104    read -r IP MAC <<<$(jq -r '.ip+" "+([.mac[0:2],.mac[2:4],.mac[4:6],.mac[6:8],.mac[8:10],.mac[10:12]] | join(":"))' <<<"$RESP")
105    route "/ous/$OU/labs/$LAB/clients/$CLNT/status" "status" "$HEADERS"
106    route "/ous/$OU/images" "" "$HEADERS"
107    AGENT="python-requests/test"
108    DATA='{"ip":"'$IP'","mac":"'$MAC'","ostype":"Windows","osversion":"Windows Test","secret":"'$(printf "%.sX" {1..32})'"}'
109    route "/ogagent/started" "" "" "$DATA" "$AGENT"
110    DATA='{"ip":"'$IP'","user":"test","language":"en","ostype":"Windows","osversion":"Windows 10 Test"}'
111    route "/ogagent/loggedin" "" "" "$DATA" "$AGENT"
112    route "/ogagent/loggedout" "" "" "$DATA" "$AGENT"
113    DATA='{"ip":"'$IP'","mac":"'$MAC'","ostype":"Windows","osversion":"Windows Test"}'
114    route "/ogagent/stopped" "" "" "$DATA" "$AGENT"
115    echo "Tests OK: $OK"
116    echo "Tests failed: $FAIL"
117}
118
119
120# Main program.
121
122# Show help.
123[ "$*" == "help" ] && help
124[ "$*" ] && raiseError usage
125# Access control.
126[ "$USER" == "root" ] || raiseError access "Need to be root."
127[ -r $CONFIGFILE ] || raiseError access "Configuration file."
128# Check dependencies.
129which jq &>/dev/null || raiseError notfound "Need to install \"jq\"."
130
131source $CONFIGFILE
132deletedata
133loaddata
134runtests
135deletedata
136
Note: See TracBrowser for help on using the repository browser.