mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Merge pull request #1494 from cbrom/core_cautils_datastructuremethods_tests
Code Improvement on isRuleKubescapeVersionCompatible function
This commit is contained in:
@@ -59,25 +59,25 @@ func (policies *Policies) Set(frameworks []reporthandling.Framework, version str
|
||||
// returns true only if rule doesn't have the "until" attribute
|
||||
func isRuleKubescapeVersionCompatible(attributes map[string]interface{}, version string) bool {
|
||||
if from, ok := attributes["useFromKubescapeVersion"]; ok && from != nil {
|
||||
if version != "" {
|
||||
if sfrom, ok := from.(string); ok {
|
||||
if semver.Compare(version, sfrom) == -1 {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
switch sfrom := from.(type) {
|
||||
case string:
|
||||
if version != "" && semver.Compare(version, sfrom) == -1 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
if until, ok := attributes["useUntilKubescapeVersion"]; ok && until != nil {
|
||||
if version == "" {
|
||||
default:
|
||||
// Handle case where useFromKubescapeVersion is not a string
|
||||
return false
|
||||
}
|
||||
if suntil, ok := until.(string); ok {
|
||||
if semver.Compare(version, suntil) >= 0 {
|
||||
}
|
||||
|
||||
if until, ok := attributes["useUntilKubescapeVersion"]; ok && until != nil {
|
||||
switch suntil := until.(type) {
|
||||
case string:
|
||||
if version == "" || semver.Compare(version, suntil) >= 0 {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
// Handle case where useUntilKubescapeVersion is not a string
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIsRuleKubescapeVersionCompatible(t *testing.T) {
|
||||
// should not crash when the value of useUntilKubescapeVersion is not a string
|
||||
buildNumberMock := "v1.0.135"
|
||||
assert.False(t, isRuleKubescapeVersionCompatible(rule_invalid_from.Attributes, buildNumberMock))
|
||||
assert.False(t, isRuleKubescapeVersionCompatible(rule_invalid_from.Attributes, buildNumberMock))
|
||||
assert.False(t, isRuleKubescapeVersionCompatible(rule_invalid_until.Attributes, buildNumberMock))
|
||||
// should use only rules that don't have "until"
|
||||
buildNumberMock = ""
|
||||
assert.False(t, isRuleKubescapeVersionCompatible(rule_v1_0_131.Attributes, buildNumberMock))
|
||||
|
||||
Reference in New Issue
Block a user