From 5bfa47ebbe034cd46d2205ce37a9de4d743c41b7 Mon Sep 17 00:00:00 2001 From: Max Leske Date: Mon, 14 Mar 2022 20:04:43 +0100 Subject: [PATCH] Fixed a couple of issues in log marker prototype --- runner/run.go | 17 +++++++++++------ runner/run_test.go | 13 +++++++++---- waflog/read.go | 14 +++++++++++--- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/runner/run.go b/runner/run.go index 1890fe3..e603e87 100644 --- a/runner/run.go +++ b/runner/run.go @@ -87,7 +87,7 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests [] log.Fatal().Msgf("ftw/run: can't connect to destination %+v - unexpected error found. Is your waf running?", dest) } - startMarker := markAndFlush(client, config.FTWConfig, testIndex, stageIndex) + startMarker := markAndFlush(client, dest, config.FTWConfig, testIndex, stageIndex) ftwcheck.SetStartMarker(startMarker) req = getRequestFromTest(testRequest) @@ -98,7 +98,7 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests [] client.StopTrackingTime() - endMarker := markAndFlush(client, config.FTWConfig, testIndex, stageIndex) + endMarker := markAndFlush(client, dest, config.FTWConfig, testIndex, stageIndex) ftwcheck.SetEndMarker(endMarker) ftwcheck.SetRoundTripTime(client.GetRoundTripTime().StartTime(), client.GetRoundTripTime().StopTime()) @@ -125,7 +125,7 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests [] return printSummary(output, stats) } -func markAndFlush(client *ftwhttp.Client, ftwConfig *config.FTWConfiguration, testIndex int, stageIndex int) string { +func markAndFlush(client *ftwhttp.Client, dest *ftwhttp.Destination, ftwConfig *config.FTWConfiguration, testIndex int, stageIndex int) string { var req *ftwhttp.Request var logLines = &waflog.FTWLogLines{ FileName: ftwConfig.LogFile, @@ -146,15 +146,20 @@ func markAndFlush(client *ftwhttp.Client, ftwConfig *config.FTWConfiguration, te stageId := fmt.Sprintf("%d-%d", testIndex, stageIndex) headers := &ftwhttp.Header{"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost", "X-CRS-Test": stageId} - // create a new request req = ftwhttp.NewRequest(rline, *headers, nil, true) for range [20]int{} { - _, err := client.Do(*req) + err := client.NewConnection(*dest) if err != nil { - log.Fatal().Msg("ftw/run: can't connect to destination - unexpected error found. Is your waf running?") + log.Fatal().Msgf("ftw/run: can't connect to destination %+v - unexpected error found. Is your waf running?", dest) } + + _, err = client.Do(*req) + if err != nil { + log.Fatal().Msgf("ftw/run: failed sending request to %+v - unexpected error found. Is your waf running?", dest) + } + marker, found := logLines.CheckLogForMarker(stageId) if found { return marker diff --git a/runner/run_test.go b/runner/run_test.go index 3e6e941..9dbffce 100644 --- a/runner/run_test.go +++ b/runner/run_test.go @@ -2,12 +2,14 @@ package runner import ( "fmt" + "io/fs" "net/http" "net/http/httptest" "os" "strconv" "strings" "testing" + "time" "github.com/fzipi/go-ftw/config" "github.com/fzipi/go-ftw/ftwhttp" @@ -248,11 +250,13 @@ tests: ` // Error checking omitted for brevity -func newTestServer() *httptest.Server { - +func newTestServer(logFilePath string) *httptest.Server { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) _, _ = w.Write([]byte("Hello, client")) + + logMessage := fmt.Sprintf("%d, request line: %s %s %s, headers: %s", time.Now().UnixMicro(), r.Method, r.RequestURI, r.Proto, r.Header) + os.WriteFile(logFilePath, []byte(logMessage), fs.ModeAppend) })) return ts @@ -277,7 +281,8 @@ func TestRun(t *testing.T) { config.FTWConfig.LogFile = logName // setup test webserver (not a waf) - server := newTestServer() + //FIXME: some of these requests will not go to the test server so they won't be logged and we won't be able to check the log + server := newTestServer(logName) d, err := ftwhttp.DestinationFromString(server.URL) if err != nil { t.Fatalf("Failed to parse destination") @@ -561,7 +566,7 @@ func TestFailedTestsRun(t *testing.T) { config.FTWConfig.LogFile = logName // setup test webserver (not a waf) - server := newTestServer() + server := newTestServer(logName) d, err := ftwhttp.DestinationFromString(server.URL) if err != nil { t.Fatalf("Failed to parse destination") diff --git a/waflog/read.go b/waflog/read.go index 73c78e6..b8e7d20 100644 --- a/waflog/read.go +++ b/waflog/read.go @@ -180,13 +180,21 @@ func (ll *FTWLogLines) CheckLogForMarker(stageId string) (string, bool) { } scanner := backscanner.NewOptions(logfile, int(fi.Size()), backscannerOptions) stageIdBytes := []byte(stageId) - crsHeaderBytes := []byte("X-CRS-Test") - line, _, err := scanner.LineBytes() + crsHeaderBytes := []byte("x-crs-test") + + line := []byte{} + // find the last non-empty line + for err == nil && len(line) == 0 { + line, _, err = scanner.LineBytes() + } if err != nil { - if err != io.EOF { + if err == io.EOF { + return "", false + } else { log.Trace().Err(err) } } + line = bytes.ToLower(line) if bytes.Contains(line, crsHeaderBytes) && bytes.Contains(line, stageIdBytes) { return string(line), true }