Skip to content

Commit

Permalink
test.sh added
Browse files Browse the repository at this point in the history
  • Loading branch information
svrnm committed Nov 8, 2017
1 parent cfe06bb commit 4cc6036
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 14 deletions.
26 changes: 19 additions & 7 deletions adc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ function output {
}
function httpClient {
debug "$*"
curl -L --connect-timeout 10 "$@"
local TIMEOUT=10
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
TIMEOUT=$CONFIG_HTTP_TIMEOUT
fi
curl -L --connect-timeout $TIMEOUT "$@"
}
SHIFTS=0
declare -i SHIFTS
Expand Down Expand Up @@ -100,6 +104,7 @@ function recursiveSource {
}
function apiCall {
local OPTS
local METHOD="GET"
while getopts "X:d:" opt "$@";
do
case "${opt}" in
Expand Down Expand Up @@ -161,9 +166,9 @@ function apiCall {
done
debug "Call Controller: -X $METHOD -d $PAYLOAD $ENDPOINT"
if [ -n "$PAYLOAD" ] ; then
echo -X $METHOD -d $PAYLOAD $ENDPOINT
controller_call -X $METHOD -d "$PAYLOAD" "$ENDPOINT"
else
echo -X $METHOD $ENDPOINT
controller_call -X $METHOD $ENDPOINT
fi
}
# __call GET "/controller/rest/applications/\${a}/business-transactions" -a ECommerce
Expand Down Expand Up @@ -361,9 +366,9 @@ EOF
CONTROLLER_LOGIN_STATUS=0
function controller_ping {
debug "Ping $CONFIG_CONTROLLER_HOST"
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "TIME_TOTAL=%{time_total}")
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "RESPONSE=%{http_code} TIME_TOTAL=%{time_total}")
debug "RESPONSE: ${PING_RESPONSE}"
if [[ "${PING_RESPONSE/200 OK}" != "$Ping_RESPONSE" ]]; then
if [ -n "$PING_RESPONSE" ] && [[ "${PING_RESPONSE/200 OK}" != "$PING_RESPONSE" ]]; then
local TIME=${PING_RESPONSE##*TIME_TOTAL=}
COMMAND_RESULT="Pong! Time: ${TIME}"
else
Expand Down Expand Up @@ -685,6 +690,13 @@ Create a new database collector. You need to provide the following parameters:
-p port
-s password
EOF
function dbmon_get {
apiCall "/controller/restui/databases/collectors/configurations/\${c}" "$@"
}
register dbmon_get Retrieve information about a specific database collector
describe dbmon_get << EOF
Retrieve information about a specific database collector. Provide the collector id as parameter.
EOF
function dbmon_list {
controller_call /controller/restui/databases/collectors/
}
Expand All @@ -693,7 +705,7 @@ describe dbmon_list << EOF
List all database collectors
EOF
function dbmon_delete {
apiCall -X POST -d "[\${c}]" /controller/restui/databases/collectors/configuration/batchDelete "$@"
apiCall -X POST -d "[\"\${c}\"]" /controller/restui/databases/collectors/configuration/batchDelete "$@"
}
register dbmon_delete Delete a database collector
describe dbmon_delete << EOF
Expand Down Expand Up @@ -851,7 +863,7 @@ else
warning "File ${USER_CONFIG} not found!"
fi
# Parse global options
while getopts "H:C:D:P:S:F:v" opt;
while getopts "H:C:J:D:P:S:F:v" opt;
do
case "${opt}" in
H)
Expand Down
4 changes: 2 additions & 2 deletions commands/controller/ping.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ CONTROLLER_LOGIN_STATUS=0

function controller_ping {
debug "Ping $CONFIG_CONTROLLER_HOST"
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "TIME_TOTAL=%{time_total}")
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "RESPONSE=%{http_code} TIME_TOTAL=%{time_total}")
debug "RESPONSE: ${PING_RESPONSE}"
if [[ "${PING_RESPONSE/200 OK}" != "$Ping_RESPONSE" ]]; then
if [ -n "$PING_RESPONSE" ] && [[ "${PING_RESPONSE/200 OK}" != "$PING_RESPONSE" ]]; then
local TIME=${PING_RESPONSE##*TIME_TOTAL=}
COMMAND_RESULT="Pong! Time: ${TIME}"
else
Expand Down
2 changes: 1 addition & 1 deletion commands/dbmon/delete.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

function dbmon_delete {
apiCall -X POST -d "[\${c}]" /controller/restui/databases/collectors/configuration/batchDelete "$@"
apiCall -X POST -d "[\"\${c}\"]" /controller/restui/databases/collectors/configuration/batchDelete "$@"
}

register dbmon_delete Delete a database collector
Expand Down
10 changes: 10 additions & 0 deletions commands/dbmon/get.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

function dbmon_get {
apiCall "/controller/restui/databases/collectors/configurations/\${c}" "$@"
}

register dbmon_get Retrieve information about a specific database collector
describe dbmon_get << EOF
Retrieve information about a specific database collector. Provide the collector id as parameter.
EOF
5 changes: 3 additions & 2 deletions helpers/apiCall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

function apiCall {
local OPTS
local METHOD="GET"

while getopts "X:d:" opt "$@";
do
Expand Down Expand Up @@ -71,9 +72,9 @@ function apiCall {

debug "Call Controller: -X $METHOD -d $PAYLOAD $ENDPOINT"
if [ -n "$PAYLOAD" ] ; then
echo -X $METHOD -d $PAYLOAD $ENDPOINT
controller_call -X $METHOD -d "$PAYLOAD" "$ENDPOINT"
else
echo -X $METHOD $ENDPOINT
controller_call -X $METHOD $ENDPOINT
fi
}

Expand Down
6 changes: 5 additions & 1 deletion helpers/httpClient.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

function httpClient {
debug "$*"
curl -L --connect-timeout 10 "$@"
local TIMEOUT=10
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
TIMEOUT=$CONFIG_HTTP_TIMEOUT
fi
curl -L --connect-timeout $TIMEOUT "$@"
}
3 changes: 2 additions & 1 deletion main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ source ./commands/metric/get.sh
source ./commands/metric/tree.sh

source ./commands/dbmon/create.sh
source ./commands/dbmon/get.sh
source ./commands/dbmon/list.sh
source ./commands/dbmon/delete.sh

Expand Down Expand Up @@ -98,7 +99,7 @@ else
fi

# Parse global options
while getopts "H:C:D:P:S:F:v" opt;
while getopts "H:C:J:D:P:S:F:v" opt;
do
case "${opt}" in
H)
Expand Down
88 changes: 88 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash
START=`date +%s`
COOKIE_PATH="/tmp/adc-test-cookie"
SUCCESS_COUNTER=0
TEST_COUNTER=0

declare -i SUCCESS_COUNTER
declare -i TEST_COUNTER

function assert_equals {
TEST_COUNTER=$TEST_COUNTER+1
if [[ "$2" = "$1" ]]; then
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
echo -en "\033[0;32m.\033[0m"
else
echo -e "\n\033[0;31mTest failed: \033[0;33m$1\033[0;31m doesn't equal \033[0;35m$2\033[0m"
fi
}

function assert_contains_substring {
TEST_COUNTER=$TEST_COUNTER+1
if [[ $2 == *$1* ]]; then
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
echo -en "\033[0;32m.\033[0m"
else
echo -e "\n\033[0;31mTest failed: Couldn't find \033[0;33m$1\033[0;31m in \033[0;35m$2\033[0m"
fi
}

function assert_regex {
TEST_COUNTER=$TEST_COUNTER+1
if [[ $2 =~ $1 ]]; then
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
echo -en "\033[0;32m.\033[0m"
else
echo -e "\n\033[0;31mTest failed: Couldn't find \033[0;33m$1\033[0;31m in \033[0;35m$2\033[0m"
fi
}

echo "Sourcing user config for controller host and credentials..."
source "$HOME/.appdynamics/adc/config.sh"
echo "Will use the following controller for testing: $CONFIG_CONTROLLER_HOST"

ADC="./adc.sh -H $CONFIG_CONTROLLER_HOST -C $CONFIG_CONTROLLER_CREDENTIALS -J $COOKIE_PATH"
#### BEGIN TESTS ####

##### Test controller functionality #####
assert_contains_substring "Pong!" "`${ADC} controller ping`"
assert_contains_substring "Login Successful" "`${ADC} controller login`"
assert_contains_substring "<available>true</available>" "`${ADC} controller status`"
assert_regex "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" "`${ADC} controller version`"

##### List different entities #####
assert_contains_substring "<applications>" "`${ADC} application list`"
assert_contains_substring "<tiers>" "`${ADC} tier list -a 5`"
assert_contains_substring "<business-transactions>" "`${ADC} bt list -a 5`"

##### Error handling #####
assert_equals "Error" "`env CONFIG_HTTP_TIMEOUT=1 ./adc.sh -H 127.0.0.2:8009 controller ping`"

##### Database Collector Create, List, Get, Delete #####
CREATE="`${ADC} dbmon create -i adc_test_collector -h localhost -n db -u user -a "Default Database Agent" -t DB2 -p 1555 -s password`"
assert_contains_substring '"name" : "adc_test_collector",' "$CREATE"
assert_contains_substring '"name" : "adc_test_collector",' "`${ADC} dbmon list`"
if [[ $CREATE =~ \"id\"\ \:\ ([0-9]+) ]] ; then
COLLECTOR_ID=${BASH_REMATCH[1]}
assert_contains_substring '"name" : "adc_test_collector",' "`${ADC} dbmon get $COLLECTOR_ID`"
assert_contains_substring '"status" : "SUCCESS",' "`${ADC} dbmon delete $COLLECTOR_ID`"
fi

#### END TESTS ####

declare -i PERCENTAGE

PERCENTAGE=$((($SUCCESS_COUNTER*100)/($TEST_COUNTER)))

if [ $PERCENTAGE -eq 100 ]; then
echo -e "\033[0;32m"
elif [ $PERCENTAGE -ge 80 ]; then
echo -e "\033[0;33m"
else
echo -e "\033[0;31m"
fi

rm $COOKIE_PATH
END=`date +%s`

echo -e "\n$SUCCESS_COUNTER/$TEST_COUNTER ($PERCENTAGE%) tests completed in $((END-START))s.\033[0m"

0 comments on commit 4cc6036

Please sign in to comment.