Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error messages for Assertions (#1726)
* Improve error messages for Assertions Specifically for Assertions which define "Message". Now the test failure properly notifies the user that the Assertion was looking for violations for a particular message. Also add several unit tests for Assertions to cover these cases. Unit test coverage is now 100% for assertions.go. We could probably add more tests for assertions, but I'd rather that be its own PR. What I've added is for the behavior changed by this PR. Also add validation that "violations" isn't set to a negative number. Fixes #1639 Signed-off-by: Will Beason <willbeason@google.com> * Fix linter errors Signed-off-by: Will Beason <willbeason@google.com>
- Loading branch information
Will Beason (he/him)
committed
Dec 9, 2021
1 parent
18aed9e
commit 2362f7a
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package gktest | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/frameworks/constraint/pkg/types" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func TestAssertion_Run(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
assertion *Assertion | ||
results []*types.Result | ||
wantErr error | ||
}{{ | ||
name: "default to expect violation", | ||
assertion: &Assertion{}, | ||
results: nil, | ||
wantErr: ErrNumViolations, | ||
}, { | ||
name: "no violations", | ||
assertion: &Assertion{ | ||
Violations: intStrFromInt(0), | ||
}, | ||
results: nil, | ||
wantErr: nil, | ||
}, { | ||
name: "negative violations", | ||
assertion: &Assertion{ | ||
Violations: intStrFromInt(-1), | ||
}, | ||
results: nil, | ||
wantErr: ErrInvalidYAML, | ||
}, { | ||
name: "violation with message", | ||
assertion: &Assertion{ | ||
Violations: intStrFromInt(1), | ||
Message: pointer.StringPtr("message"), | ||
}, | ||
results: nil, | ||
wantErr: ErrNumViolations, | ||
}, { | ||
name: "no violations with message", | ||
assertion: &Assertion{ | ||
Violations: intStrFromStr("no"), | ||
Message: pointer.StringPtr("message"), | ||
}, | ||
results: nil, | ||
wantErr: nil, | ||
}, { | ||
name: "fail no violations with message", | ||
assertion: &Assertion{ | ||
Violations: intStrFromStr("no"), | ||
Message: pointer.StringPtr("message"), | ||
}, | ||
results: []*types.Result{{ | ||
Msg: "message", | ||
}}, | ||
wantErr: ErrNumViolations, | ||
}} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.assertion.Run(tt.results) | ||
|
||
if !errors.Is(err, tt.wantErr) { | ||
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |