| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Test goss by using it to validate ssh service is online |
| 4 |
|
| 5 |
expected_success_output="Count: 4, Failed: 0, Skipped: 0" |
| 6 |
expected_fail_output="Count: 4, Failed: 1, Skipped: 0" |
| 7 |
|
| 8 |
|
| 9 |
# Start ssh, and set goss acknowledge ssh active is correct |
| 10 |
systemctl start ssh |
| 11 |
|
| 12 |
goss add package openssh-server |
| 13 |
goss add service ssh |
| 14 |
|
| 15 |
|
| 16 |
# Confirm that goss validation succeeds with ssh active |
| 17 |
initial_valid_output=$(goss validate) |
| 18 |
initial_return_code=$? |
| 19 |
|
| 20 |
grep -q "$expected_success_output" <<< "$initial_valid_output" |
| 21 |
initial_output_correct=$? |
| 22 |
|
| 23 |
if [[ $initial_output_correct -ne 0 ]]; then |
| 24 |
echo "Initial validation failed, expected:" |
| 25 |
echo $expected_success_output |
| 26 |
echo "found:" |
| 27 |
grep "Count" <<< "$initial_valid_output" |
| 28 |
exit 1 |
| 29 |
elif [[ $initial_return_code -ne 0 ]]; then |
| 30 |
echo "Initial validation failed: bad return code of $initial_return_code" |
| 31 |
exit 1 |
| 32 |
fi |
| 33 |
|
| 34 |
# Confirm that goss validation fails with ssh inactive |
| 35 |
systemctl stop ssh |
| 36 |
|
| 37 |
invalid_output=$(goss validate) |
| 38 |
invalid_return_code=$? |
| 39 |
|
| 40 |
grep -q "$expected_fail_output" <<< "$invalid_output" |
| 41 |
invalid_output_correct=$? |
| 42 |
|
| 43 |
if [[ $invalid_output_correct -ne 0 ]]; then |
| 44 |
echo "ssh offline validation failed, expected:" |
| 45 |
echo $expected_fail_output |
| 46 |
echo "found:" |
| 47 |
grep "Count" <<< "$invalid_output" |
| 48 |
exit 1 |
| 49 |
elif [[ $invalid_return_code -ne 1 ]]; then |
| 50 |
echo "ssh offline validation failed: bad return code of $invalid_return_code" |
| 51 |
exit 1 |
| 52 |
fi |
| 53 |
|
| 54 |
# Confirm that goss validation succeeds with ssh reactivated |
| 55 |
systemctl start ssh |
| 56 |
|
| 57 |
final_valid_output=$(goss validate) |
| 58 |
final_return_code=$? |
| 59 |
|
| 60 |
grep -q "$expected_success_output" <<< "$final_valid_output" |
| 61 |
final_output_correct=$? |
| 62 |
|
| 63 |
if [[ $final_output_correct -ne 0 ]]; then |
| 64 |
echo "Final validation failed, expected:" |
| 65 |
echo $expected_success_output |
| 66 |
echo "found:" |
| 67 |
grep "Count" <<< "$final_valid_output" |
| 68 |
exit 1 |
| 69 |
elif [[ $final_return_code -ne 0 ]]; then |
| 70 |
echo "Final validation failed: bad return code of $final_return_code" |
| 71 |
exit 1 |
| 72 |
fi |