Skip to content

Commit 4cc6036

Browse files
committed
test.sh added
1 parent cfe06bb commit 4cc6036

File tree

8 files changed

+130
-14
lines changed

8 files changed

+130
-14
lines changed

adc.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ function output {
6060
}
6161
function httpClient {
6262
debug "$*"
63-
curl -L --connect-timeout 10 "$@"
63+
local TIMEOUT=10
64+
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
65+
TIMEOUT=$CONFIG_HTTP_TIMEOUT
66+
fi
67+
curl -L --connect-timeout $TIMEOUT "$@"
6468
}
6569
SHIFTS=0
6670
declare -i SHIFTS
@@ -100,6 +104,7 @@ function recursiveSource {
100104
}
101105
function apiCall {
102106
local OPTS
107+
local METHOD="GET"
103108
while getopts "X:d:" opt "$@";
104109
do
105110
case "${opt}" in
@@ -161,9 +166,9 @@ function apiCall {
161166
done
162167
debug "Call Controller: -X $METHOD -d $PAYLOAD $ENDPOINT"
163168
if [ -n "$PAYLOAD" ] ; then
164-
echo -X $METHOD -d $PAYLOAD $ENDPOINT
169+
controller_call -X $METHOD -d "$PAYLOAD" "$ENDPOINT"
165170
else
166-
echo -X $METHOD $ENDPOINT
171+
controller_call -X $METHOD $ENDPOINT
167172
fi
168173
}
169174
# __call GET "/controller/rest/applications/\${a}/business-transactions" -a ECommerce
@@ -361,9 +366,9 @@ EOF
361366
CONTROLLER_LOGIN_STATUS=0
362367
function controller_ping {
363368
debug "Ping $CONFIG_CONTROLLER_HOST"
364-
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "TIME_TOTAL=%{time_total}")
369+
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "RESPONSE=%{http_code} TIME_TOTAL=%{time_total}")
365370
debug "RESPONSE: ${PING_RESPONSE}"
366-
if [[ "${PING_RESPONSE/200 OK}" != "$Ping_RESPONSE" ]]; then
371+
if [ -n "$PING_RESPONSE" ] && [[ "${PING_RESPONSE/200 OK}" != "$PING_RESPONSE" ]]; then
367372
local TIME=${PING_RESPONSE##*TIME_TOTAL=}
368373
COMMAND_RESULT="Pong! Time: ${TIME}"
369374
else
@@ -685,6 +690,13 @@ Create a new database collector. You need to provide the following parameters:
685690
-p port
686691
-s password
687692
EOF
693+
function dbmon_get {
694+
apiCall "/controller/restui/databases/collectors/configurations/\${c}" "$@"
695+
}
696+
register dbmon_get Retrieve information about a specific database collector
697+
describe dbmon_get << EOF
698+
Retrieve information about a specific database collector. Provide the collector id as parameter.
699+
EOF
688700
function dbmon_list {
689701
controller_call /controller/restui/databases/collectors/
690702
}
@@ -693,7 +705,7 @@ describe dbmon_list << EOF
693705
List all database collectors
694706
EOF
695707
function dbmon_delete {
696-
apiCall -X POST -d "[\${c}]" /controller/restui/databases/collectors/configuration/batchDelete "$@"
708+
apiCall -X POST -d "[\"\${c}\"]" /controller/restui/databases/collectors/configuration/batchDelete "$@"
697709
}
698710
register dbmon_delete Delete a database collector
699711
describe dbmon_delete << EOF
@@ -851,7 +863,7 @@ else
851863
warning "File ${USER_CONFIG} not found!"
852864
fi
853865
# Parse global options
854-
while getopts "H:C:D:P:S:F:v" opt;
866+
while getopts "H:C:J:D:P:S:F:v" opt;
855867
do
856868
case "${opt}" in
857869
H)

commands/controller/ping.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ CONTROLLER_LOGIN_STATUS=0
44

55
function controller_ping {
66
debug "Ping $CONFIG_CONTROLLER_HOST"
7-
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "TIME_TOTAL=%{time_total}")
7+
local PING_RESPONSE=$(httpClient -sI $CONFIG_CONTROLLER_HOST -w "RESPONSE=%{http_code} TIME_TOTAL=%{time_total}")
88
debug "RESPONSE: ${PING_RESPONSE}"
9-
if [[ "${PING_RESPONSE/200 OK}" != "$Ping_RESPONSE" ]]; then
9+
if [ -n "$PING_RESPONSE" ] && [[ "${PING_RESPONSE/200 OK}" != "$PING_RESPONSE" ]]; then
1010
local TIME=${PING_RESPONSE##*TIME_TOTAL=}
1111
COMMAND_RESULT="Pong! Time: ${TIME}"
1212
else

commands/dbmon/delete.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

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

77
register dbmon_delete Delete a database collector

commands/dbmon/get.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
function dbmon_get {
4+
apiCall "/controller/restui/databases/collectors/configurations/\${c}" "$@"
5+
}
6+
7+
register dbmon_get Retrieve information about a specific database collector
8+
describe dbmon_get << EOF
9+
Retrieve information about a specific database collector. Provide the collector id as parameter.
10+
EOF

helpers/apiCall.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
function apiCall {
44
local OPTS
5+
local METHOD="GET"
56

67
while getopts "X:d:" opt "$@";
78
do
@@ -71,9 +72,9 @@ function apiCall {
7172

7273
debug "Call Controller: -X $METHOD -d $PAYLOAD $ENDPOINT"
7374
if [ -n "$PAYLOAD" ] ; then
74-
echo -X $METHOD -d $PAYLOAD $ENDPOINT
75+
controller_call -X $METHOD -d "$PAYLOAD" "$ENDPOINT"
7576
else
76-
echo -X $METHOD $ENDPOINT
77+
controller_call -X $METHOD $ENDPOINT
7778
fi
7879
}
7980

helpers/httpClient.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
function httpClient {
44
debug "$*"
5-
curl -L --connect-timeout 10 "$@"
5+
local TIMEOUT=10
6+
if [ -n "$CONFIG_HTTP_TIMEOUT" ] ; then
7+
TIMEOUT=$CONFIG_HTTP_TIMEOUT
8+
fi
9+
curl -L --connect-timeout $TIMEOUT "$@"
610
}

main.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ source ./commands/metric/get.sh
6868
source ./commands/metric/tree.sh
6969

7070
source ./commands/dbmon/create.sh
71+
source ./commands/dbmon/get.sh
7172
source ./commands/dbmon/list.sh
7273
source ./commands/dbmon/delete.sh
7374

@@ -98,7 +99,7 @@ else
9899
fi
99100

100101
# Parse global options
101-
while getopts "H:C:D:P:S:F:v" opt;
102+
while getopts "H:C:J:D:P:S:F:v" opt;
102103
do
103104
case "${opt}" in
104105
H)

test.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
START=`date +%s`
3+
COOKIE_PATH="/tmp/adc-test-cookie"
4+
SUCCESS_COUNTER=0
5+
TEST_COUNTER=0
6+
7+
declare -i SUCCESS_COUNTER
8+
declare -i TEST_COUNTER
9+
10+
function assert_equals {
11+
TEST_COUNTER=$TEST_COUNTER+1
12+
if [[ "$2" = "$1" ]]; then
13+
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
14+
echo -en "\033[0;32m.\033[0m"
15+
else
16+
echo -e "\n\033[0;31mTest failed: \033[0;33m$1\033[0;31m doesn't equal \033[0;35m$2\033[0m"
17+
fi
18+
}
19+
20+
function assert_contains_substring {
21+
TEST_COUNTER=$TEST_COUNTER+1
22+
if [[ $2 == *$1* ]]; then
23+
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
24+
echo -en "\033[0;32m.\033[0m"
25+
else
26+
echo -e "\n\033[0;31mTest failed: Couldn't find \033[0;33m$1\033[0;31m in \033[0;35m$2\033[0m"
27+
fi
28+
}
29+
30+
function assert_regex {
31+
TEST_COUNTER=$TEST_COUNTER+1
32+
if [[ $2 =~ $1 ]]; then
33+
SUCCESS_COUNTER=$SUCCESS_COUNTER+1
34+
echo -en "\033[0;32m.\033[0m"
35+
else
36+
echo -e "\n\033[0;31mTest failed: Couldn't find \033[0;33m$1\033[0;31m in \033[0;35m$2\033[0m"
37+
fi
38+
}
39+
40+
echo "Sourcing user config for controller host and credentials..."
41+
source "$HOME/.appdynamics/adc/config.sh"
42+
echo "Will use the following controller for testing: $CONFIG_CONTROLLER_HOST"
43+
44+
ADC="./adc.sh -H $CONFIG_CONTROLLER_HOST -C $CONFIG_CONTROLLER_CREDENTIALS -J $COOKIE_PATH"
45+
#### BEGIN TESTS ####
46+
47+
##### Test controller functionality #####
48+
assert_contains_substring "Pong!" "`${ADC} controller ping`"
49+
assert_contains_substring "Login Successful" "`${ADC} controller login`"
50+
assert_contains_substring "<available>true</available>" "`${ADC} controller status`"
51+
assert_regex "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" "`${ADC} controller version`"
52+
53+
##### List different entities #####
54+
assert_contains_substring "<applications>" "`${ADC} application list`"
55+
assert_contains_substring "<tiers>" "`${ADC} tier list -a 5`"
56+
assert_contains_substring "<business-transactions>" "`${ADC} bt list -a 5`"
57+
58+
##### Error handling #####
59+
assert_equals "Error" "`env CONFIG_HTTP_TIMEOUT=1 ./adc.sh -H 127.0.0.2:8009 controller ping`"
60+
61+
##### Database Collector Create, List, Get, Delete #####
62+
CREATE="`${ADC} dbmon create -i adc_test_collector -h localhost -n db -u user -a "Default Database Agent" -t DB2 -p 1555 -s password`"
63+
assert_contains_substring '"name" : "adc_test_collector",' "$CREATE"
64+
assert_contains_substring '"name" : "adc_test_collector",' "`${ADC} dbmon list`"
65+
if [[ $CREATE =~ \"id\"\ \:\ ([0-9]+) ]] ; then
66+
COLLECTOR_ID=${BASH_REMATCH[1]}
67+
assert_contains_substring '"name" : "adc_test_collector",' "`${ADC} dbmon get $COLLECTOR_ID`"
68+
assert_contains_substring '"status" : "SUCCESS",' "`${ADC} dbmon delete $COLLECTOR_ID`"
69+
fi
70+
71+
#### END TESTS ####
72+
73+
declare -i PERCENTAGE
74+
75+
PERCENTAGE=$((($SUCCESS_COUNTER*100)/($TEST_COUNTER)))
76+
77+
if [ $PERCENTAGE -eq 100 ]; then
78+
echo -e "\033[0;32m"
79+
elif [ $PERCENTAGE -ge 80 ]; then
80+
echo -e "\033[0;33m"
81+
else
82+
echo -e "\033[0;31m"
83+
fi
84+
85+
rm $COOKIE_PATH
86+
END=`date +%s`
87+
88+
echo -e "\n$SUCCESS_COUNTER/$TEST_COUNTER ($PERCENTAGE%) tests completed in $((END-START))s.\033[0m"

0 commit comments

Comments
 (0)