Skip to content

Commit

Permalink
fix: fixes #1270 (#1271)
Browse files Browse the repository at this point in the history
* fix: fixes #1270
  • Loading branch information
danj-replicated authored Jul 20, 2023
1 parent 73201c0 commit 8237ac9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pkg/preflight/validate_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// validatePreflight validates the preflight spec and returns a warning if there is any
func validatePreflight(specs PreflightSpecs) *types.ExitCodeWarning {

if specs.PreflightSpec == nil && specs.HostPreflightSpec == nil {
if specs.PreflightSpec == nil && specs.HostPreflightSpec == nil && specs.UploadResultSpecs == nil {
return types.NewExitCodeWarning("no preflight or host preflight spec was found")
}

Expand All @@ -29,6 +29,15 @@ func validatePreflight(specs PreflightSpecs) *types.ExitCodeWarning {
}
}

if specs.UploadResultSpecs != nil {
for _, preflight := range specs.UploadResultSpecs {
warning := validatePreflightSpecItems(preflight.Spec.Collectors, preflight.Spec.Analyzers)
if warning != nil {
return warning
}
}
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/preflight/validate_specs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestValidatePreflight(t *testing.T) {
"noAnalyzersHostPreflightFile": "troubleshoot_v1beta2_host_preflight_validate_empty_analyzers_gotest.yaml",
"excludedHostCollectorsPreflightFile": "troubleshoot_v1beta2_host_preflight_validate_excluded_collectors_gotest.yaml",
"excludedHostAnalyzersPreflightFile": "troubleshoot_v1beta2_host_preflight_validate_excluded_analyzers_gotest.yaml",
"uploadResultsPreflightFile": "troubleshoot_v1beta2_preflight_validate_spec_with_upload_results_gotest.yaml",
}

tests := []struct {
Expand Down Expand Up @@ -83,6 +84,11 @@ func TestValidatePreflight(t *testing.T) {
preflightSpec: testingFiles["excludedHostAnalyzersPreflightFile"],
wantWarning: types.NewExitCodeWarning("All analyzers were excluded by the applied values"),
},
{
name: "upload-results-preflight",
preflightSpec: testingFiles["uploadResultsPreflightFile"],
wantWarning: nil,
},
}

for _, tt := range tests {
Expand Down
6 changes: 4 additions & 2 deletions test/validate-preflight-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -euo pipefail

tmpdir="$(mktemp -d)"

echo -e "\n========= Running preflights from e2e spec and checking results ========="
./bin/preflight --debug --interactive=false --format=json examples/preflight/e2e.yaml > "$tmpdir/result.json"
if [ $? -ne 0 ]; then
echo "preflight command failed"
Expand Down Expand Up @@ -33,9 +34,10 @@ echo "Failed preflights found"
EXIT_STATUS=1
fi

# test stdin
echo -e "\n========= Running preflights from stdin using e2e spec ========="
cat examples/preflight/e2e.yaml | ./bin/preflight --debug --interactive=false --format=json - > "$tmpdir/result.json"
if [ $? -ne 0 ]; then
EXIT_STATUS=$?
if [ $EXIT_STATUS -ne 0 ]; then
echo "preflight command failed"
exit $EXIT_STATUS
fi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: troubleshoot.sh/v1beta2
kind: Preflight
metadata:
name: go-test-preflight
spec:
uploadResultsTo: http://someurl
collectors:
- data:
name: example.json
data: |
{
"foo": "bar",
"stuff": {
"foo": "bar",
"bar": true
},
"morestuff": [
{
"foo": {
"bar": 123
}
}
]
}
analyzers:
- jsonCompare:
checkName: Compare JSON Example
fileName: example.json
path: "morestuff.[0].foo.bar"
value: |
123
outcomes:
- fail:
when: "false"
message: The collected data does not match the value.
- pass:
when: "true"
message: The collected data matches the value.

0 comments on commit 8237ac9

Please sign in to comment.