Skip to content

Commit

Permalink
Add tests for bindLoggingPipe
Browse files Browse the repository at this point in the history
Brings in a test from #107, further covering additional
scenarios.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Nov 9, 2021
1 parent 1c6daa7 commit a028941
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
6 changes: 2 additions & 4 deletions executor/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ func bindLoggingPipe(name string, pipe io.Reader, output io.Writer, logPrefix bo

scanner := bufio.NewScanner(pipe)

size := bufio.MaxScanTokenSize

buffer := make([]byte, size)
scanner.Buffer(buffer, size)
buffer := make([]byte, maxBufferSize)
scanner.Buffer(buffer, maxBufferSize)

logFlags := log.Flags()
prefix := log.Prefix()
Expand Down
119 changes: 119 additions & 0 deletions executor/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) OpenFaaS Author(s) 2021. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package executor

import (
"bytes"
"log"
"math"
"os"
"strings"
"testing"
"time"
)

func TestBindLoggingPipe_ErrorsWithLargeToken(t *testing.T) {
input := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`

reader := strings.NewReader(input)

logs := bytes.Buffer{}

log.SetOutput(&logs)
defer func() {
log.SetOutput(os.Stderr)
}()

out := bytes.Buffer{}

maxBufferBytes := 32
addPrefix := false
bindLoggingPipe("TestFunc", reader, &out, addPrefix, maxBufferBytes)

// give the pipe time to actually parse the logs
time.Sleep(500 * time.Millisecond)

got := out.String()
want := ""
if want != got {
t.Fatalf("expected empty string due to error, but got %q", got)
}

wantSt := `bufio.Scanner: token too long`
if !strings.Contains(logs.String(), wantSt) {
t.Fatalf("want text: %q, but not found in: %q", wantSt, logs.String())
}
}

func TestBindLoggingPipe_ReadsValidSize(t *testing.T) {
input := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
validSize := len(input)
reader := strings.NewReader(input)

logs := bytes.Buffer{}

log.SetOutput(&logs)
defer func() {
log.SetOutput(os.Stderr)
}()

out := bytes.Buffer{}

maxBufferBytes := validSize
addPrefix := false
bindLoggingPipe("TestFunc", reader, &out, addPrefix, maxBufferBytes)

// give the pipe time to actually parse the logs
time.Sleep(500 * time.Millisecond)

got := out.String()
want := input
if want != got {
t.Fatalf("want output %q, but got %q", want, got)
}

wantSt := `bufio.Scanner: token too long`
if strings.Contains(logs.String(), wantSt) {
t.Fatalf("Found error %s in output: %q", wantSt, logs.String())
}
}

func TestBindLoggingPipe_ReadsValidSizedLines(t *testing.T) {
input1 := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
input2 := `Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
`
validSize := int(math.Max(float64(len(input1)), float64(len(input2))))

reader := strings.NewReader(input1 + input2)

logs := bytes.Buffer{}

log.SetOutput(&logs)
defer func() {
log.SetOutput(os.Stderr)
}()

out := bytes.Buffer{}

maxBufferBytes := validSize
addPrefix := false
bindLoggingPipe("TestFunc", reader, &out, addPrefix, maxBufferBytes)

// give the pipe time to actually parse the logs
time.Sleep(500 * time.Millisecond)

got := out.String()
want := input1 + input2
if want != got {
t.Fatalf("want output %q, but got %q", want, got)
}

t.Logf(out.String())
wantSt := `bufio.Scanner: token too long`
if strings.Contains(logs.String(), wantSt) {
t.Fatalf("Found error %s in output: %q", wantSt, logs.String())
}
}

0 comments on commit a028941

Please sign in to comment.