Merge pull request #514 from helmfile/log-snapshot-test-helper
Introduce a new test helper for easier log snapshot testing
This commit is contained in:
		
						commit
						667c3723f0
					
				|  | @ -1,14 +1,12 @@ | ||||||
| package app | package app | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"bufio" |  | ||||||
| 	"bytes" |  | ||||||
| 	"io" |  | ||||||
| 	"sync" | 	"sync" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
| 	"github.com/variantdev/vals" | 	"github.com/variantdev/vals" | ||||||
|  | 	"go.uber.org/zap" | ||||||
| 
 | 
 | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||||
|  | @ -54,35 +52,9 @@ func TestApply_3(t *testing.T) { | ||||||
| 			ReleasesMutex:        &sync.Mutex{}, | 			ReleasesMutex:        &sync.Mutex{}, | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		bs := &bytes.Buffer{} | 		bs := runWithLogCapture(t, func(t *testing.T, logger *zap.SugaredLogger) { | ||||||
| 
 |  | ||||||
| 		func() { |  | ||||||
| 			t.Helper() | 			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, "debug") |  | ||||||
| 
 |  | ||||||
| 			valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) | 			valsRuntime, err := vals.New(vals.Options{CacheSize: 32}) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				t.Errorf("unexpected error creating vals runtime: %v", err) | 				t.Errorf("unexpected error creating vals runtime: %v", err) | ||||||
|  | @ -156,7 +128,7 @@ func TestApply_3(t *testing.T) { | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		}() | 		}) | ||||||
| 
 | 
 | ||||||
| 		if tc.log != "" { | 		if tc.log != "" { | ||||||
| 			actual := bs.String() | 			actual := bs.String() | ||||||
|  |  | ||||||
|  | @ -1,15 +1,56 @@ | ||||||
| package app | package app | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"bufio" | ||||||
|  | 	"bytes" | ||||||
|  | 	"io" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | 	"sync" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/google/go-cmp/cmp" | 	"github.com/google/go-cmp/cmp" | ||||||
|  | 	"go.uber.org/zap" | ||||||
|  | 
 | ||||||
|  | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | func runWithLogCapture(t *testing.T, f func(*testing.T, *zap.SugaredLogger)) *bytes.Buffer { | ||||||
|  | 	t.Helper() | ||||||
|  | 
 | ||||||
|  | 	bs := &bytes.Buffer{} | ||||||
|  | 
 | ||||||
|  | 	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, "debug") | ||||||
|  | 
 | ||||||
|  | 	f(t, logger) | ||||||
|  | 
 | ||||||
|  | 	return bs | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func assertLogEqualsToSnapshot(t *testing.T, data string) { | func assertLogEqualsToSnapshot(t *testing.T, data string) { | ||||||
| 	t.Helper() | 	t.Helper() | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue