forked from chroe/gitlink-cli
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package rules
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestCIDiagnosisPatterns(t *testing.T) {
|
|
tests := []struct {
|
|
log string
|
|
pattern string
|
|
transient bool
|
|
}{
|
|
{"cannot find package github.com/foo/bar", "cannot find package", false},
|
|
{"syntax error: unexpected token at line 42", "syntax error", false},
|
|
{"permission denied: unable to access /tmp/build", "permission denied", false},
|
|
{"connection refused: dial tcp 10.0.0.1:8080", "connection refused", true},
|
|
{"out of memory: process killed", "out of memory", false},
|
|
{"No such file or directory: .devops/build.yml", "No such file", false},
|
|
{"FAIL: TestLogin (0.23s)", "exit status 1", false},
|
|
{"timeout: deadline exceeded after 300s", "timeout", true},
|
|
{"undefined: UserService in main.go:15", "undefined:", false},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
d := diagnoseLog(tc.log)
|
|
if d.Pattern != tc.pattern {
|
|
t.Errorf("log=%q: expected pattern %q, got %q", tc.log, tc.pattern, d.Pattern)
|
|
}
|
|
if d.Transient != tc.transient {
|
|
t.Errorf("log=%q: expected transient=%v, got %v", tc.log, tc.transient, d.Transient)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCIDiagnosisNoFailures(t *testing.T) {
|
|
upstream := map[string]interface{}{
|
|
"ci-builds": map[string]interface{}{
|
|
"data": []interface{}{
|
|
map[string]interface{}{"id": "1", "status": "success", "log": "build passed"},
|
|
},
|
|
},
|
|
}
|
|
|
|
resp, err := CIDiagnosisRule(upstream, "ci-diagnosis")
|
|
if err != nil {
|
|
t.Fatalf("CIDiagnosisRule failed: %v", err)
|
|
}
|
|
analysis := resp.Analysis.(map[string]interface{})
|
|
if msg := analysis["message"]; msg != "no failed builds found" {
|
|
t.Fatalf("expected 'no failed builds found', got %v", msg)
|
|
}
|
|
}
|
|
|
|
func TestCIDiagnosisWithFailures(t *testing.T) {
|
|
upstream := map[string]interface{}{
|
|
"ci-builds": map[string]interface{}{
|
|
"data": []interface{}{
|
|
map[string]interface{}{"id": "1", "status": "failed", "log": "connection refused"},
|
|
},
|
|
},
|
|
"commits": map[string]interface{}{
|
|
"data": []interface{}{},
|
|
},
|
|
}
|
|
|
|
resp, err := CIDiagnosisRule(upstream, "ci-diagnosis")
|
|
if err != nil {
|
|
t.Fatalf("CIDiagnosisRule failed: %v", err)
|
|
}
|
|
analysis := resp.Analysis.(map[string]interface{})
|
|
if v := analysis["total_failures"]; v.(int) != 1 {
|
|
t.Fatalf("expected 1 failure, got %v", v)
|
|
}
|
|
}
|