Repository navigation
Expand file tree
/
Copy pathtest-cli-validation.sh
More file actions
executable file
Β·187 lines (157 loc) Β· 6.97 KB
/
Copy pathtest-cli-validation.sh
File metadata and controls
executable file
Β·187 lines (157 loc) Β· 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
echo "π§ͺ Testing Kogaro CLI Validation against Sample Testbed"
echo "========================================================="
# Build kogaro
echo "π¦ Building Kogaro..."
go build .
if [ $? -ne 0 ]; then
echo "β Build failed"
exit 1
fi
echo "β
Build successful"
echo ""
# Run Go tests first
echo "π§ͺ Running automated Go CLI tests..."
go test -v -run TestCLIValidation
test_exit_code=$?
if [ $test_exit_code -ne 0 ]; then
echo "β Go CLI tests failed"
exit 1
fi
echo "β
Go CLI tests passed"
echo ""
# Test Helm template expansion workflow
echo "π― Testing Helm template expansion workflow:"
echo ""
test_helm_files=(
"sample/kogaro-testbed/templates/deployment-missing-configmap.yaml:ConfigMap reference issue"
"sample/kogaro-testbed/templates/deployment-privileged-container.yaml:Security issue"
"sample/kogaro-testbed/templates/service-no-endpoints.yaml:Service connectivity issue"
"sample/kogaro-testbed/templates/ingress-missing-ingressclass.yaml:IngressClass reference issue"
"sample/kogaro-testbed/templates/pod-missing-secret-volume.yaml:Secret reference issue"
)
for test_case in "${test_helm_files[@]}"; do
IFS=':' read -r file description <<< "$test_case"
if [ -f "$file" ]; then
echo "π§ͺ Testing: $description"
echo " File: $file"
# First test: Should fail with Helm template error
echo " Step 1: Testing raw Helm template (should fail)..."
./kogaro --mode=one-off --config="$file" 2>&1 | grep -q "Helm templates"
if [ $? -eq 0 ]; then
echo " β
Correctly detected Helm template"
else
echo " β οΈ Did not detect Helm template as expected"
fi
# Second test: Expand with helm template and validate
echo " Step 2: Expanding Helm template and validating..."
temp_file=$(mktemp)
# Expand the Helm template with values file
helm template test sample/kogaro-testbed/ \
--values sample/kogaro-testbed/values.yaml \
--show-only "templates/$(basename "$file")" > "$temp_file" 2>/dev/null
if [ -s "$temp_file" ]; then
# Create output file for inspection
output_file="validation-output-$(basename "$file" .yaml).txt"
# Run validation on expanded YAML and capture output
./kogaro --mode=one-off --config="$temp_file" --output=ci > "$output_file" 2>&1
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo " β οΈ No issues found in expanded template (expected issues)"
echo " π Output saved to: $output_file"
else
echo " β
Issues detected in expanded template (exit code: $exit_code)"
echo " π Validation output saved to: $output_file"
# Show a preview of the issues found
if grep -q "KOGARO-" "$output_file"; then
echo " π Issues found:"
grep "KOGARO-" "$output_file" | head -3 | sed 's/^/ /'
if [ $(grep -c "KOGARO-" "$output_file") -gt 3 ]; then
echo " ... and $(($(grep -c "KOGARO-" "$output_file") - 3)) more issues"
fi
fi
fi
else
echo " β οΈ Could not expand Helm template (may need values file)"
fi
rm -f "$temp_file"
echo ""
else
echo " β οΈ File not found: $file"
echo ""
fi
done
# Test with different validation modes using expanded templates
echo "ποΈ Testing different validation modes with expanded templates:"
echo ""
# Test expanded templates with various validation settings
test_validation_modes() {
local template_file="$1"
local description="$2"
echo "π Testing: $description"
temp_file=$(mktemp)
# Expand the template with values file
helm template test sample/kogaro-testbed/ \
--values sample/kogaro-testbed/values.yaml \
--show-only "templates/$(basename "$template_file")" > "$temp_file" 2>/dev/null
if [ -s "$temp_file" ]; then
# Create output files for different validation modes
all_output_file="validation-all-$(basename "$template_file" .yaml).txt"
security_output_file="validation-security-$(basename "$template_file" .yaml).txt"
echo " Testing with all validations enabled:"
./kogaro --mode=one-off \
--config="$temp_file" \
--enable-resource-limits-validation=true \
--enable-security-validation=true \
--enable-image-validation=false \
--output=ci > "$all_output_file" 2>&1
all_exit_code=$?
echo " Exit code: $all_exit_code"
echo " π Output saved to: $all_output_file"
echo " Testing with security-only validation:"
./kogaro --mode=one-off \
--config="$temp_file" \
--enable-resource-limits-validation=false \
--enable-secret-validation=false \
--enable-security-validation=true \
--output=ci > "$security_output_file" 2>&1
security_exit_code=$?
echo " Exit code: $security_exit_code"
echo " π Output saved to: $security_output_file"
# Show preview of issues from all validations
if [ $all_exit_code -ne 0 ] && grep -q "KOGARO-" "$all_output_file"; then
echo " π Sample issues found:"
grep "KOGARO-" "$all_output_file" | head -2 | sed 's/^/ /'
fi
else
echo " β οΈ Could not expand template for validation mode testing"
fi
rm -f "$temp_file"
echo ""
}
if command -v helm >/dev/null 2>&1; then
test_validation_modes "sample/kogaro-testbed/templates/deployment-missing-resources.yaml" "Resource limits validation"
test_validation_modes "sample/kogaro-testbed/templates/deployment-privileged-container.yaml" "Security validation"
else
echo "β οΈ Helm not found - skipping validation mode tests with expanded templates"
echo ""
fi
echo "π Summary:"
echo "- β
Go CLI tests verify Helm template detection and error handling"
echo "- β
Helm template workflow tested (expand first, then validate)"
echo "- β
Different validation modes tested on expanded templates"
echo "- π All validation outputs saved to files for inspection"
echo ""
echo "π Generated output files:"
ls -1 validation-*.txt 2>/dev/null | sed 's/^/ /' || echo " No validation output files found"
echo ""
echo "π― Recommended workflow:"
echo " helm template <chart> [options] | kogaro --mode=one-off --config=-"
echo " OR"
echo " helm template <chart> [options] > output.yaml && kogaro --mode=one-off --config=output.yaml"
echo ""
echo "π To inspect specific issues:"
echo " cat validation-output-<template-name>.txt"
echo " grep 'KOGARO-' validation-*.txt"
echo ""
echo "β
CLI testing complete!"