-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More fixes and tests for unreachable rules, v0.6.2
- Loading branch information
Showing
5 changed files
with
124 additions
and
10 deletions.
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
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 |
---|---|---|
@@ -1,20 +1,117 @@ | ||
package checks | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/abulimov/haproxy-lint/lib" | ||
) | ||
|
||
func TestCheckUnreachableRules(t *testing.T) { | ||
lines, err := lib.GetConfig("../testdata/haproxy.cfg", "") | ||
if err != nil { | ||
t.Fatalf("Failed to read test data: %v", err) | ||
tests := []struct { | ||
// Test description. | ||
name string | ||
// Parameters. | ||
s *lib.Section | ||
// Expected results. | ||
want []lib.Problem | ||
}{ | ||
{ | ||
name: "ACL with 'or' should't match", | ||
s: &lib.Section{ | ||
Type: "frontend", | ||
Name: "name", | ||
Line: 0, | ||
Content: map[int]string{ | ||
1: "use_backend undefined-servers if h_test", | ||
2: "use_backend servers if h_test or { ssl_fc }", | ||
3: "use_backend servers if h_test h_some", | ||
}, | ||
}, | ||
want: []lib.Problem{ | ||
{ | ||
Col: 0, | ||
Line: 1, | ||
Severity: "warning", | ||
Message: "This line shadows rule 'use_backend servers if h_test h_some' on line 3", | ||
}, | ||
{ | ||
Col: 0, | ||
Line: 3, | ||
Severity: "warning", | ||
Message: "Unreachable rule - 'use_backend undefined-servers if h_test' on line 1 will match first", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple ACLs should match", | ||
s: &lib.Section{ | ||
Type: "frontend", | ||
Name: "name", | ||
Line: 0, | ||
Content: map[int]string{ | ||
1: "use_backend undefined-servers if h_test", | ||
2: "use_backend servers if h_test { ssl_fc }", | ||
3: "use_backend servers if h_test h_some", | ||
}, | ||
}, | ||
want: []lib.Problem{ | ||
{ | ||
Col: 0, | ||
Line: 1, | ||
Severity: "warning", | ||
Message: "This line shadows rule 'use_backend servers if h_test { ssl_fc }' on line 2", | ||
}, | ||
{ | ||
Col: 0, | ||
Line: 2, | ||
Severity: "warning", | ||
Message: "Unreachable rule - 'use_backend undefined-servers if h_test' on line 1 will match first", | ||
}, | ||
{ | ||
Col: 0, | ||
Line: 1, | ||
Severity: "warning", | ||
Message: "This line shadows rule 'use_backend servers if h_test h_some' on line 3", | ||
}, | ||
{ | ||
Col: 0, | ||
Line: 3, | ||
Severity: "warning", | ||
Message: "Unreachable rule - 'use_backend undefined-servers if h_test' on line 1 will match first", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "General acl with 'or' should be found", | ||
s: &lib.Section{ | ||
Type: "frontend", | ||
Name: "name", | ||
Line: 0, | ||
Content: map[int]string{ | ||
1: "use_backend undefined-servers if h_test or { ssl_fc }", | ||
2: "use_backend servers if h_test h_some", | ||
}, | ||
}, | ||
want: []lib.Problem{ | ||
{ | ||
Col: 0, | ||
Line: 1, | ||
Severity: "warning", | ||
Message: "This line shadows rule 'use_backend servers if h_test h_some' on line 2", | ||
}, | ||
{ | ||
Col: 0, | ||
Line: 2, | ||
Severity: "warning", | ||
Message: "Unreachable rule - 'use_backend undefined-servers if h_test or { ssl_fc }' on line 1 will match first", | ||
}, | ||
}, | ||
}, | ||
} | ||
sections := lib.GetSection("frontend", lines) | ||
problems := CheckUnreachableRules(sections[1]) | ||
|
||
if len(problems) != 2 { | ||
t.Errorf("Expected %d problems, got %d", 2, len(problems)) | ||
for _, tt := range tests { | ||
if got := CheckUnreachableRules(tt.s); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("%q. CheckUnreachableRules() = %v, want %v", tt.name, got, tt.want) | ||
} | ||
} | ||
} |
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