From a0289419078824f0a070860f84a6b383eb4f2169 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Mon, 8 Nov 2021 15:36:04 +0000 Subject: [PATCH] Add tests for bindLoggingPipe Brings in a test from #107, further covering additional scenarios. Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- executor/logging.go | 6 +- executor/logging_test.go | 119 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 executor/logging_test.go diff --git a/executor/logging.go b/executor/logging.go index a76ba364..5886a498 100644 --- a/executor/logging.go +++ b/executor/logging.go @@ -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() diff --git a/executor/logging_test.go b/executor/logging_test.go new file mode 100644 index 00000000..f39f9a6b --- /dev/null +++ b/executor/logging_test.go @@ -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()) + } +}