|
| 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 | + |
0 commit comments