Skip to content

Commit 0b1d2de

Browse files
Adding tests (#12)
Signed-off-by: Panagiotis Georgiadis <pgeorgia@redhat.com> Co-authored-by: Regina Scott <50851526+reginapizza@users.noreply.github.com> Co-authored-by: Regina Scott <rescott@redhat.com>
1 parent 8418263 commit 0b1d2de

File tree

6 files changed

+404
-12
lines changed

6 files changed

+404
-12
lines changed

gather_gitops.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ exit_if_not_openshift() {
111111
fi
112112
}
113113

114-
function getNamespaces() {
114+
get_namespaces() {
115115
local namespaces
116116
local default="openshift-gitops"
117117
local clusterScopedInstances
@@ -127,7 +127,7 @@ function getNamespaces() {
127127
fi
128128
else
129129
mkdir -p "$GITOPS_DIR"
130-
echo "Error: getNamespaces- No gitops instances found, please check your cluster configuration." > "${GITOPS_DIR}"/must-gather-script-errors.yaml 2>&1
130+
echo "Error: get_namespaces- No gitops instances found, please check your cluster configuration." > "$GITOPS_DIR"/must-gather-script-errors.yaml 2>&1
131131
fi
132132

133133
local argocdInstances
@@ -267,7 +267,7 @@ function main() {
267267
exit_if_not_openshift
268268

269269
echo " * Checking for GitOps Namespaces..."
270-
getNamespaces
270+
get_namespaces
271271

272272
echo " * Getting OpenShift Cluster Version..."
273273
run_and_log "oc version" "$GITOPS_DIR/oc-version.txt"
@@ -405,20 +405,20 @@ main "$@"
405405
echo
406406
echo
407407
if [ $ERROR_COUNTER -gt 0 ]; then
408-
echo "There were $ERROR_COUNTER errors"
409-
echo "Please check the error log file for more details: $ERROR_LOG"
408+
echo "There were $ERROR_COUNTER errors"
409+
echo "Please check the error log file for more details: $ERROR_LOG"
410410
else
411-
echo "All commands executed successfully!"
412-
if [ $NO_OUTPUT_COUNTER -gt 0 ]; then
413-
echo " * NOTE: $NO_OUTPUT_COUNTER commands did not produce any output (see: $NO_OUTPUT_LOG)"
414-
fi
415-
echo "You can find all the commands that were executed in the log file: $ALL_COMMANDS_LOG"
416-
exit 0
411+
echo "All commands executed successfully!"
412+
if [ $NO_OUTPUT_COUNTER -gt 0 ]; then
413+
echo " * NOTE: $NO_OUTPUT_COUNTER commands did not produce any output (see: $NO_OUTPUT_LOG)"
414+
fi
415+
echo "You can find all the commands that were executed in the log file: $ALL_COMMANDS_LOG"
416+
exit 0
417417
fi
418418

419419
echo "All other commands were successfully executed!"
420420
if [ $NO_OUTPUT_COUNTER -gt 0 ]; then
421421
echo " * NOTE: $NO_OUTPUT_COUNTER commands did not produce any output (see: $NO_OUTPUT_LOG)"
422-
fi
422+
fi
423423
echo "You can find all the commands that were executed in the log file: $ALL_COMMANDS_LOG"
424424

test/suite.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
3+
# Set testdata directories
4+
TESTDATA_ACTUAL="testdata/actual"
5+
TESTDATA_EXPECTED="testdata/expected"
6+
7+
# Create actual folder if it doesn't exist
8+
if [ ! -d "$TESTDATA_ACTUAL" ]; then
9+
mkdir -p "$TESTDATA_ACTUAL"
10+
fi
11+
12+
# Set color codes
13+
COLOR_GREEN="\033[32m"
14+
COLOR_RED="\033[31m"
15+
COLOR_CYAN="\033[36m"
16+
COLOR_RESET="\033[0m"
17+
18+
# Print welcome message
19+
echo -e "${COLOR_CYAN}Starting must-gather test suite...${COLOR_RESET}"
20+
21+
# --------------------------------------------------------- #
22+
23+
# Check if there are any test files (excluding this script)
24+
if ! ls test*.sh >/dev/null; then
25+
echo -e " ${COLOR_RED}No test files found.${COLOR_RESET}"
26+
exit 1
27+
fi
28+
29+
echo -e " ${COLOR_GREEN}Test files found.${COLOR_RESET}"
30+
31+
# Check if kubectl is installed
32+
if ! command -v kubectl >/dev/null; then
33+
echo -e " ${COLOR_RED}kubectl not found. Please install kubectl and try again.${COLOR_RESET}"
34+
exit 1
35+
fi
36+
37+
echo -e " ${COLOR_GREEN}kubectl binary is installed.${COLOR_RESET}"
38+
39+
# Check if kubectl can access the cluster
40+
if ! kubectl get nodes >/dev/null; then
41+
echo -e " ${COLOR_RED}kubectl failed to access the cluster. Please check your kubectl configuration and try again.${COLOR_RESET}"
42+
exit 1
43+
fi
44+
45+
echo -e " ${COLOR_GREEN}kubectl is working correctly against the cluster.${COLOR_RESET}"
46+
47+
# --------------------------------------------------------- #
48+
echo
49+
50+
# Run the gather_gitops.sh command and check the exit status
51+
echo -e "${COLOR_CYAN}Running MustGather against the cluster...${COLOR_RESET}"
52+
if ! ../gather_gitops.sh --base-collection-path "$TESTDATA_ACTUAL/mustgather_output"; then
53+
echo -e " ${COLOR_RED}Test failed: command exited with an error${COLOR_RESET}"
54+
echo -e " ${COLOR_RED}This is a big issue, the must gather fails by default${COLOR_RESET}"
55+
exit 1
56+
fi
57+
echo -e " ${COLOR_GREEN}Test passed: command ran successfully${COLOR_RESET}"
58+
59+
# --------------------------------------------------------- #
60+
61+
# Print message
62+
echo
63+
echo -e "${COLOR_CYAN}Running individual test files...${COLOR_RESET}"
64+
65+
# Initialize variables
66+
TEST_COUNTER=0
67+
FAILED_TESTS=0
68+
69+
# Loop over all the test files
70+
for TEST_FILE in ./test*.sh; do
71+
# Increment the test counter
72+
((TEST_COUNTER++))
73+
74+
# Execute the test and save the output
75+
TEST_NAME=$(basename "$TEST_FILE" .sh)
76+
sh "$TEST_FILE" > "$TESTDATA_ACTUAL/$TEST_NAME.actual"
77+
78+
# Check if the output file exists and has content
79+
if ! [ -f "$TESTDATA_ACTUAL/$TEST_NAME.actual" ]; then
80+
echo "Test failed: output file is empty or does not exist"
81+
echo "This is a problem with the test itself (e.g. poorly written), not the must-gather tool"
82+
exit 1
83+
fi
84+
85+
# Compare the actual and expected outputs
86+
if diff "$TESTDATA_ACTUAL/$TEST_NAME.actual" "$TESTDATA_EXPECTED/$TEST_NAME.expected" >/dev/null; then
87+
echo -e " TEST $TEST_COUNTER: ${COLOR_GREEN}$TEST_NAME PASSED${COLOR_RESET}"
88+
else
89+
echo -e " TEST $TEST_COUNTER: ${COLOR_RED}$TEST_NAME FAILED${COLOR_RESET}"
90+
((FAILED_TESTS++))
91+
# Print the diff if there is a mismatch
92+
echo -e "=== ACTUAL ==="
93+
cat "$TESTDATA_ACTUAL/$TEST_NAME.actual"
94+
echo -e "=== EXPECTED ==="
95+
cat "$TESTDATA_EXPECTED/$TEST_NAME.expected"
96+
fi
97+
done
98+
99+
# Print the final message
100+
echo
101+
echo
102+
echo
103+
if [ "$FAILED_TESTS" -eq 0 ]; then
104+
echo -e "${COLOR_GREEN}Test suite successfully completed without errors!${COLOR_RESET}"
105+
else
106+
echo -e "${COLOR_RED}Testing failed. $FAILED_TESTS test(s) failed.${COLOR_RESET}"
107+
fi
108+

test/test1.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Check if the number of files for expected is the same as actual
4+
cd testdata/actual/mustgather_output/ && tree | tail -n -1

test/test2.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
# Check if the mustgather_output tree for expected is the same as actual tree output
4+
# sed looks for the uid of the resource and replaces it with xxxxxx-xxxxx
5+
# sed also removes the lines for files for events because the presence of those can be impacted by external sources resulting in flaky tests
6+
cd testdata/actual/mustgather_output/ && tree | sed -r '
7+
s/([a-zA-Z0-9-]+(-[a-zA-Z0-9]+)?)-[a-f0-9]{4,}(-[a-z0-9]+)?(\.[a-zA-Z0-9_-]+)?/\1-xxxxxx-xxxxx\4/g
8+
/warning-events.txt/d
9+
/error-events.txt/d
10+
/all-events.txt/d
11+
'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
29 directories, 240 files

0 commit comments

Comments
 (0)