Merge pull request #516 from helmfile/use-log-cap-helper-in-test-apply-hooks

Use log capturing helper in TestApply_hooks
This commit is contained in:
yxxhero 2022-11-13 16:33:20 +08:00 committed by GitHub
commit 235917c11c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 34 deletions

View File

@ -1,14 +1,12 @@
package app
import (
"bufio"
"bytes"
"io"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/variantdev/vals"
"go.uber.org/zap"
"github.com/helmfile/helmfile/pkg/exectest"
"github.com/helmfile/helmfile/pkg/helmexec"
@ -54,35 +52,9 @@ func TestApply_hooks(t *testing.T) {
ReleasesMutex: &sync.Mutex{},
}
bs := &bytes.Buffer{}
func() {
bs := runWithLogCapture(t, tc.logLevel, func(t *testing.T, logger *zap.SugaredLogger) {
t.Helper()
logReader, logWriter := io.Pipe()
logFlushed := &sync.WaitGroup{}
// Ensure all the log is consumed into `bs` by calling `logWriter.Close()` followed by `logFlushed.Wait()`
logFlushed.Add(1)
go func() {
scanner := bufio.NewScanner(logReader)
for scanner.Scan() {
bs.Write(scanner.Bytes())
bs.WriteString("\n")
}
logFlushed.Done()
}()
defer func() {
// This is here to avoid data-trace on bytes buffer `bs` to capture logs
if err := logWriter.Close(); err != nil {
panic(err)
}
logFlushed.Wait()
}()
logger := helmexec.NewLogger(logWriter, tc.logLevel)
valsRuntime, err := vals.New(vals.Options{CacheSize: 32})
if err != nil {
t.Errorf("unexpected error creating vals runtime: %v", err)
@ -155,7 +127,7 @@ func TestApply_hooks(t *testing.T) {
}
}
}
}()
})
if tc.log != "" {
actual := bs.String()

View File

@ -52,7 +52,7 @@ func TestApply_3(t *testing.T) {
ReleasesMutex: &sync.Mutex{},
}
bs := runWithLogCapture(t, func(t *testing.T, logger *zap.SugaredLogger) {
bs := runWithLogCapture(t, "debug", func(t *testing.T, logger *zap.SugaredLogger) {
t.Helper()
valsRuntime, err := vals.New(vals.Options{CacheSize: 32})

View File

@ -17,7 +17,7 @@ import (
"github.com/helmfile/helmfile/pkg/helmexec"
)
func runWithLogCapture(t *testing.T, f func(*testing.T, *zap.SugaredLogger)) *bytes.Buffer {
func runWithLogCapture(t *testing.T, logLevel string, f func(*testing.T, *zap.SugaredLogger)) *bytes.Buffer {
t.Helper()
bs := &bytes.Buffer{}
@ -44,7 +44,7 @@ func runWithLogCapture(t *testing.T, f func(*testing.T, *zap.SugaredLogger)) *by
logFlushed.Wait()
}()
logger := helmexec.NewLogger(logWriter, "debug")
logger := helmexec.NewLogger(logWriter, logLevel)
f(t, logger)