You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.7 KiB
120 lines
2.7 KiB
package test
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func compareLogs(logFileName string, logsData []string, t *testing.T, testType string) {
|
|
if _, err := os.Stat(logFileName); os.IsNotExist(err) {
|
|
file, err := os.Create(logFileName)
|
|
if err != nil {
|
|
t.Errorf("Failed to create log file: %v", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
writer := bufio.NewWriter(file)
|
|
|
|
jsonData, err := json.MarshalIndent(logsData, "", " ")
|
|
if err != nil {
|
|
t.Errorf("Failed to marshal logsData to JSON: %v", err)
|
|
}
|
|
|
|
_, err = writer.WriteString(string(jsonData))
|
|
if err != nil {
|
|
t.Errorf("Failed to write JSON to log file: %v", err)
|
|
}
|
|
|
|
writer.Flush()
|
|
} else {
|
|
file, err := os.Open(logFileName)
|
|
if err != nil {
|
|
t.Errorf("Failed to open log file: %v", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
reader := bufio.NewReader(file)
|
|
var sb strings.Builder
|
|
for {
|
|
line, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
sb.WriteString(strings.TrimSpace(line))
|
|
break
|
|
}
|
|
t.Errorf("Error reading log file: %v", err)
|
|
return
|
|
}
|
|
sb.WriteString(strings.TrimSpace(line))
|
|
}
|
|
|
|
var fileContent = sb.String()
|
|
|
|
var fileLogsData []string
|
|
err = json.Unmarshal([]byte(fileContent), &fileLogsData)
|
|
if err != nil {
|
|
t.Errorf("Failed to unmarshal JSON from log file: %v", err)
|
|
}
|
|
|
|
if len(fileLogsData) != len(logsData) {
|
|
t.Errorf("Log data length does not match. Got: %d, Expected: %d", len(fileLogsData), len(logsData))
|
|
}
|
|
|
|
var allLinesMatch = true
|
|
|
|
switch testType {
|
|
default:
|
|
for i := range logsData {
|
|
if sortBracketsContent(fileLogsData[i]) != sortBracketsContent(logsData[i]) {
|
|
allLinesMatch = false
|
|
t.Errorf("Line %d does not match. Got: %s, Expected: %s", i+1, sortBracketsContent(fileLogsData[i]), sortBracketsContent(logsData[i]))
|
|
}
|
|
}
|
|
if allLinesMatch {
|
|
t.Log("\nAll lines match the log file.")
|
|
}
|
|
case "request":
|
|
var tmp = make(map[string]struct{})
|
|
for _, v := range fileLogsData {
|
|
if _, ok := tmp[v]; ok {
|
|
delete(tmp, v)
|
|
} else {
|
|
tmp[v] = struct{}{}
|
|
}
|
|
|
|
}
|
|
for _, v := range logsData {
|
|
if _, ok := tmp[v]; ok {
|
|
delete(tmp, v)
|
|
} else {
|
|
tmp[v] = struct{}{}
|
|
}
|
|
|
|
}
|
|
if len(tmp) == 0 {
|
|
t.Log("\nAll lines match the log file.")
|
|
return
|
|
}
|
|
for i := range tmp {
|
|
t.Errorf("Line %s does not match.", i)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func sortBracketsContent(log string) string {
|
|
re := regexp.MustCompile(`\[([^\[\]]*)\]`)
|
|
return re.ReplaceAllStringFunc(log, func(match string) string {
|
|
content := match[1 : len(match)-1]
|
|
parts := strings.Split(content, " ")
|
|
sort.Strings(parts)
|
|
return "[" + strings.Join(parts, " ") + "]"
|
|
})
|
|
}
|