Skip to content

Commit

Permalink
Addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
avilevy18 committed Oct 30, 2023
1 parent a7c4abf commit 15f54c4
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions transformation_test/transformation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package transformation_test
import (
"bytes"
"context"
"embed"
"flag"
"fmt"
"io"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/GoogleCloudPlatform/ops-agent/confgenerator/fluentbit"
"github.com/goccy/go-yaml"
"github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
"gotest.tools/v3/golden"
)

Expand All @@ -34,9 +34,6 @@ var (
flbPath = flag.String("flb", os.Getenv("FLB"), "Fluent-bit path")
)

//go:embed testdata
var testdataDir embed.FS

type transformationTest []loggingProcessor
type loggingProcessor struct {
confgenerator.LoggingProcessor
Expand All @@ -52,19 +49,18 @@ func TestTransformationTests(t *testing.T) {
t.Skip("--flb not supplied")
}

allTests, err := testdataDir.ReadDir("testdata")
allTests, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}

for _, dir := range allTests {
dir := dir
if !dir.IsDir() {
continue
}
t.Run(dir.Name(), func(t *testing.T) {
t.Parallel()
if !dir.IsDir() {
t.Fatal("testdata folder must only contain folders")
}

testStartTime := time.Now()
// Unmarshal transformation_config.yaml
var transformationConfig transformationTest
Expand Down Expand Up @@ -106,13 +102,19 @@ func TestTransformationTests(t *testing.T) {
t.Fatal("Failed to start command:", err)
}

// read stderr
slurp, _ := io.ReadAll(stderr)
t.Logf("stderr: %s\n", slurp)
// stderr and stdout need to be read in parallel to prevent deadlock
var eg errgroup.Group
eg.Go(func() error {
// read stderr
slurp, _ := io.ReadAll(stderr)
t.Logf("stderr: %s\n", slurp)
return nil
})

// read and unmarshal output
var data []map[string]any
out, _ := io.ReadAll(stdout)
_ = eg.Wait()

err := yaml.Unmarshal(out, &data)
if err != nil {
Expand Down Expand Up @@ -155,16 +157,11 @@ func checkOutput(t *testing.T, name string, got []map[string]any) {
t.Fatalf("got(-)/want(+):\n%s", diff)
}
}

func readFileFromTestDir(filePath string) ([]byte, error) {
return testdataDir.ReadFile(filepath.Join("testdata", filePath))
}

func readTransformationConfig(dir string) (transformationTest, error) {
var transformationTestData []byte
var config transformationTest

transformationTestData, err := readFileFromTestDir(filepath.Join(dir, "config.yaml"))
transformationTestData, err := os.ReadFile(filepath.Join("testdata", dir, "config.yaml"))
if err != nil {
return config, err
}
Expand Down

0 comments on commit 15f54c4

Please sign in to comment.