Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test fix: allow test scenarios run more than one time #10395

Merged
merged 10 commits into from
Nov 26, 2024
56 changes: 41 additions & 15 deletions projects/gateway2/setup/ggv2setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -73,22 +74,30 @@ func getAssetsDir(t *testing.T) string {

// testingWriter is a WriteSyncer that writes logs to testing.T.
type testingWriter struct {
t *testing.T
t atomic.Value
}

func (w *testingWriter) Write(p []byte) (n int, err error) {
w.t.Log(string(p)) // Write the log to testing.T
w.t.Load().(*testing.T).Log(string(p)) // Write the log to testing.T
return len(p), nil
}

func (w *testingWriter) Sync() error {
return nil
}

// NewTestLogger creates a zap.Logger that writes to testing.T.
func NewTestLogger(t *testing.T) *zap.Logger {
writer := &testingWriter{t: t}
func (w *testingWriter) set(t *testing.T) {
w.t.Store(t)
}

var (
writer = &testingWriter{}
logger = NewTestLogger()
)

// NewTestLogger creates a zap.Logger which can be used to write to *testing.T
// on each test, set the *testing.T on the writer.
func NewTestLogger() *zap.Logger {
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
zapcore.AddSync(writer),
Expand All @@ -100,7 +109,13 @@ func NewTestLogger(t *testing.T) *zap.Logger {
return zap.New(core, zap.AddCaller())
}

func init() {
log.SetLogger(zapr.NewLogger(logger))

}

func TestScenarios(t *testing.T) {
writer.set(t)
os.Setenv("POD_NAMESPACE", "gwtest")
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{
Expand All @@ -118,11 +133,6 @@ func TestScenarios(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
logger := NewTestLogger(t)
t.Cleanup(func() { logger.Sync() })
// t.Cleanup(func() { _ = logger.Sync() })
log.SetLogger(zapr.NewLogger(logger))

ctx = contextutils.WithExistingLogger(ctx, logger.Sugar())

cfg, err := testEnv.Start()
Expand Down Expand Up @@ -212,9 +222,20 @@ func TestScenarios(t *testing.T) {
}
for _, f := range files {
// run tests with the yaml files (but not -out.yaml files)/s
parentT := t
if strings.HasSuffix(f.Name(), ".yaml") && !strings.HasSuffix(f.Name(), "-out.yaml") {
fullpath := filepath.Join("testdata", f.Name())
t.Run(strings.TrimSuffix(f.Name(), ".yaml"), func(t *testing.T) {
writer.set(t)
t.Cleanup(func() {
writer.set(parentT)
})
t.Cleanup(func() {
if t.Failed() {
j, _ := setupOpts.KrtDebugger.MarshalJSON()
t.Logf("krt state for failed test: %s %s", t.Name(), string(j))
}
})
//sadly tests can't run yet in parallel, as ggv2 will add all the k8s services as clusters. this means
// that we get test pollution.
// once we change it to only include the ones in the proxy, we can re-enable this
Expand All @@ -224,8 +245,6 @@ func TestScenarios(t *testing.T) {
})
}
}

t.Log("DONE")
}

func testScenario(t *testing.T, ctx context.Context, client istiokube.CLIClient, xdsPort int, f string) {
Expand Down Expand Up @@ -363,6 +382,7 @@ func (x *xdsDump) Compare(t *testing.T, other xdsDump) {
otherc := clusterset[c.Name]
if otherc == nil {
t.Errorf("cluster %v not found", c.Name)
continue
}
if !proto.Equal(c, otherc) {
t.Errorf("cluster %v not equal", c.Name)
Expand All @@ -376,6 +396,7 @@ func (x *xdsDump) Compare(t *testing.T, other xdsDump) {
otherc := listenerset[c.Name]
if otherc == nil {
t.Errorf("listener %v not found", c.Name)
continue
}
if !proto.Equal(c, otherc) {
t.Errorf("listener %v not equal", c.Name)
Expand All @@ -389,6 +410,7 @@ func (x *xdsDump) Compare(t *testing.T, other xdsDump) {
otherc := routeset[c.Name]
if otherc == nil {
t.Errorf("route %v not found", c.Name)
continue
}
if !proto.Equal(c, otherc) {
t.Errorf("route %v not equal: %v vs %v", c.Name, c, otherc)
Expand Down Expand Up @@ -590,14 +612,15 @@ func (x *xdsFetcher) getclusters(t *testing.T, ctx context.Context) []*envoyclus

epcli, err := cds.StreamClusters(ctx)
if err != nil {
t.Fatalf("failed to get eds client: %v", err)
t.Fatalf("failed to get cds client: %v", err)
}
defer epcli.CloseSend()
epcli.Send(x.dr)
dresp, err := epcli.Recv()
if err != nil {
t.Fatalf("failed to get response from xds server: %v", err)
}
t.Logf("got cds response. nonce, version: %s %s", dresp.GetNonce(), dresp.GetVersionInfo())
var clusters []*envoycluster.Cluster
for _, anyCluster := range dresp.GetResources() {

Expand Down Expand Up @@ -644,14 +667,15 @@ func (x *xdsFetcher) getlisteners(t *testing.T, ctx context.Context) []*envoylis

epcli, err := ds.StreamListeners(ctx)
if err != nil {
t.Fatalf("failed to get eds client: %v", err)
t.Fatalf("failed to get lds client: %v", err)
}
defer epcli.CloseSend()
epcli.Send(x.dr)
dresp, err := epcli.Recv()
if err != nil {
t.Fatalf("failed to get response from xds server: %v", err)
}
t.Logf("got lds response. nonce, version: %s %s", dresp.GetNonce(), dresp.GetVersionInfo())
var resources []*envoylistener.Listener
for _, anyResource := range dresp.GetResources() {

Expand Down Expand Up @@ -682,6 +706,7 @@ func (x *xdsFetcher) getendpoints(t *testing.T, ctx context.Context, clusterServ
if err != nil {
t.Fatalf("failed to get response from xds server: %v", err)
}
t.Logf("got eds response. nonce, version: %s %s", dresp.GetNonce(), dresp.GetVersionInfo())
var clas []*envoyendpoint.ClusterLoadAssignment
for _, anyCluster := range dresp.GetResources() {

Expand All @@ -705,7 +730,7 @@ func (x *xdsFetcher) getroutes(t *testing.T, ctx context.Context, rosourceNames

epcli, err := eds.StreamRoutes(ctx)
if err != nil {
t.Fatalf("failed to get eds client: %v", err)
t.Fatalf("failed to get rds client: %v", err)
}
defer epcli.CloseSend()
dr := proto.Clone(x.dr).(*discovery_v3.DiscoveryRequest)
Expand All @@ -715,6 +740,7 @@ func (x *xdsFetcher) getroutes(t *testing.T, ctx context.Context, rosourceNames
if err != nil {
t.Fatalf("failed to get response from xds server: %v", err)
}
t.Logf("got rds response. nonce, version: %s %s", dresp.GetNonce(), dresp.GetVersionInfo())
var clas []*envoy_config_route_v3.RouteConfiguration
for _, anyCluster := range dresp.GetResources() {

Expand Down