Skip to content

Commit

Permalink
Fixed a couple of issues in log marker prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
theseion committed Mar 21, 2022
1 parent 4a483f2 commit 5bfa47e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
17 changes: 11 additions & 6 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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,
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions runner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
14 changes: 11 additions & 3 deletions waflog/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 5bfa47e

Please sign in to comment.