Skip to content

Commit

Permalink
Fix duplicated conditions when AS exists
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Manuel Leflet Estrada <jleflete@redhat.com>
  • Loading branch information
jmle committed Feb 21, 2024
1 parent ea6ca68 commit bf9905d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
7 changes: 4 additions & 3 deletions parser/rule_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,11 @@ func (r *RuleParser) getConditions(conditionsInterface []interface{}) ([]engine.
if !ok {
//prepend
conditions = append([]engine.ConditionEntry{ce}, conditions...)
} else {
//insert
conditions = append(conditions[:index+1], conditions[index:]...)
conditions[index] = ce
}
//insert
conditions = append(conditions[:index+1], conditions[index:]...)
conditions[index] = ce
} else if ce.From != "" && ce.As == "" {
chainNameToIndex[ce.From] = len(conditions)
conditions = append(conditions, ce)
Expand Down
96 changes: 96 additions & 0 deletions parser/rule_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,59 @@ func TestLoadRules(t *testing.T) {
},
},
},
{
Name: "chaining should yield the same number of conditions as the rule",
testFileName: "rule-chain-2.yaml",
providerNameClient: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "filecontent",
}},
},
},
ExpectedProvider: map[string]provider.InternalProviderClient{
"builtin": testProvider{
caps: []provider.Capability{{
Name: "filecontent",
}},
},
},
ExpectedRuleSet: map[string]engine.RuleSet{
"konveyor-analysis": {
Rules: []engine.Rule{
{
RuleMeta: engine.RuleMeta{
RuleID: "chaining-rule",
Description: "",
Category: &konveyor.Potential,
},
Perform: engine.Perform{
Message: engine.Message{
Text: &allGoFiles,
Links: []konveyor.Link{},
},
},
When: engine.AndCondition{
Conditions: []engine.ConditionEntry{
{
From: "",
As: "file",
Ignorable: false,
Not: false,
},
{
From: "file",
As: "",
Ignorable: false,
Not: false,
},
},
},
},
},
},
},
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -740,6 +793,7 @@ func TestLoadRules(t *testing.T) {
foundRule = true
}
}
compareWhens(expectedRule.When, rule.When, t)
}
if !foundRule {
t.Errorf("not have matching rule go: %#v, expected rules: %#v", rule, expectedSet.Rules)
Expand All @@ -750,3 +804,45 @@ func TestLoadRules(t *testing.T) {
})
}
}

func compareWhens(w1 engine.Conditional, w2 engine.Conditional, t *testing.T) {
if (w1 == nil && w2 != nil) || (w1 != nil && w2 == nil) {
t.Errorf("rulesets did not have matching when field")
}
if and1, ok := w1.(engine.AndCondition); ok {
and2, ok := w2.(engine.AndCondition)
if !ok {
t.Errorf("rulesets did not have matching when field")
}
compareConditions(and1.Conditions, and2.Conditions, t)
} else if or1, ok := w1.(engine.OrCondition); ok {
or2, ok := w2.(engine.AndCondition)
if !ok {
t.Errorf("rulesets did not have matching when field")
}
compareConditions(or1.Conditions, or2.Conditions, t)
}

}

func compareConditions(cs1 []engine.ConditionEntry, cs2 []engine.ConditionEntry, t *testing.T) {
if len(cs1) != len(cs2) {
t.Errorf("rulesets did not have the same number of conditions")
}
for i := 0; i < len(cs1); i++ {
c1 := cs1[i]
c2 := cs2[i]
if c1.As != c2.As {
t.Errorf("rulesets did not have the same As field")
}
if c1.From != c2.From {
t.Errorf("rulesets did not have the same From field")
}
if c1.Ignorable != c2.Ignorable {
t.Errorf("rulesets did not have the same Ignorable field")
}
if c1.Not != c2.Not {
t.Errorf("rulesets did not have the same Not field")
}
}
}
10 changes: 10 additions & 0 deletions parser/testdata/rule-chain-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- message: "all go files"
ruleID: chaining-rule
when:
and:
- builtin.filecontent:
pattern: spring\.datasource
as: file
- builtin.filecontent:
pattern: value
from: file

0 comments on commit bf9905d

Please sign in to comment.