@@ -11,6 +11,7 @@ import (
11
11
"sort"
12
12
"strings"
13
13
"sync"
14
+ "sync/atomic"
14
15
"testing"
15
16
"time"
16
17
@@ -73,22 +74,30 @@ func getAssetsDir(t *testing.T) string {
73
74
74
75
// testingWriter is a WriteSyncer that writes logs to testing.T.
75
76
type testingWriter struct {
76
- t * testing. T
77
+ t atomic. Value
77
78
}
78
79
79
80
func (w * testingWriter ) Write (p []byte ) (n int , err error ) {
80
- w .t .Log (string (p )) // Write the log to testing.T
81
+ w .t .Load ().( * testing. T ). Log (string (p )) // Write the log to testing.T
81
82
return len (p ), nil
82
83
}
83
84
84
85
func (w * testingWriter ) Sync () error {
85
86
return nil
86
87
}
87
88
88
- // NewTestLogger creates a zap.Logger that writes to testing.T.
89
- func NewTestLogger (t * testing.T ) * zap.Logger {
90
- writer := & testingWriter {t : t }
89
+ func (w * testingWriter ) set (t * testing.T ) {
90
+ w .t .Store (t )
91
+ }
92
+
93
+ var (
94
+ writer = & testingWriter {}
95
+ logger = NewTestLogger ()
96
+ )
91
97
98
+ // NewTestLogger creates a zap.Logger which can be used to write to *testing.T
99
+ // on each test, set the *testing.T on the writer.
100
+ func NewTestLogger () * zap.Logger {
92
101
core := zapcore .NewCore (
93
102
zapcore .NewConsoleEncoder (zap .NewDevelopmentEncoderConfig ()),
94
103
zapcore .AddSync (writer ),
@@ -100,7 +109,13 @@ func NewTestLogger(t *testing.T) *zap.Logger {
100
109
return zap .New (core , zap .AddCaller ())
101
110
}
102
111
112
+ func init () {
113
+ log .SetLogger (zapr .NewLogger (logger ))
114
+
115
+ }
116
+
103
117
func TestScenarios (t * testing.T ) {
118
+ writer .set (t )
104
119
os .Setenv ("POD_NAMESPACE" , "gwtest" )
105
120
testEnv := & envtest.Environment {
106
121
CRDDirectoryPaths : []string {
@@ -118,11 +133,6 @@ func TestScenarios(t *testing.T) {
118
133
119
134
ctx , cancel := context .WithCancel (context .Background ())
120
135
t .Cleanup (cancel )
121
- logger := NewTestLogger (t )
122
- t .Cleanup (func () { logger .Sync () })
123
- // t.Cleanup(func() { _ = logger.Sync() })
124
- log .SetLogger (zapr .NewLogger (logger ))
125
-
126
136
ctx = contextutils .WithExistingLogger (ctx , logger .Sugar ())
127
137
128
138
cfg , err := testEnv .Start ()
@@ -212,9 +222,20 @@ func TestScenarios(t *testing.T) {
212
222
}
213
223
for _ , f := range files {
214
224
// run tests with the yaml files (but not -out.yaml files)/s
225
+ parentT := t
215
226
if strings .HasSuffix (f .Name (), ".yaml" ) && ! strings .HasSuffix (f .Name (), "-out.yaml" ) {
216
227
fullpath := filepath .Join ("testdata" , f .Name ())
217
228
t .Run (strings .TrimSuffix (f .Name (), ".yaml" ), func (t * testing.T ) {
229
+ writer .set (t )
230
+ t .Cleanup (func () {
231
+ writer .set (parentT )
232
+ })
233
+ t .Cleanup (func () {
234
+ if t .Failed () {
235
+ j , _ := setupOpts .KrtDebugger .MarshalJSON ()
236
+ t .Logf ("krt state for failed test: %s %s" , t .Name (), string (j ))
237
+ }
238
+ })
218
239
//sadly tests can't run yet in parallel, as ggv2 will add all the k8s services as clusters. this means
219
240
// that we get test pollution.
220
241
// once we change it to only include the ones in the proxy, we can re-enable this
@@ -224,8 +245,6 @@ func TestScenarios(t *testing.T) {
224
245
})
225
246
}
226
247
}
227
-
228
- t .Log ("DONE" )
229
248
}
230
249
231
250
func testScenario (t * testing.T , ctx context.Context , client istiokube.CLIClient , xdsPort int , f string ) {
@@ -363,6 +382,7 @@ func (x *xdsDump) Compare(t *testing.T, other xdsDump) {
363
382
otherc := clusterset [c .Name ]
364
383
if otherc == nil {
365
384
t .Errorf ("cluster %v not found" , c .Name )
385
+ continue
366
386
}
367
387
if ! proto .Equal (c , otherc ) {
368
388
t .Errorf ("cluster %v not equal" , c .Name )
@@ -376,6 +396,7 @@ func (x *xdsDump) Compare(t *testing.T, other xdsDump) {
376
396
otherc := listenerset [c .Name ]
377
397
if otherc == nil {
378
398
t .Errorf ("listener %v not found" , c .Name )
399
+ continue
379
400
}
380
401
if ! proto .Equal (c , otherc ) {
381
402
t .Errorf ("listener %v not equal" , c .Name )
@@ -389,6 +410,7 @@ func (x *xdsDump) Compare(t *testing.T, other xdsDump) {
389
410
otherc := routeset [c .Name ]
390
411
if otherc == nil {
391
412
t .Errorf ("route %v not found" , c .Name )
413
+ continue
392
414
}
393
415
if ! proto .Equal (c , otherc ) {
394
416
t .Errorf ("route %v not equal: %v vs %v" , c .Name , c , otherc )
@@ -590,14 +612,15 @@ func (x *xdsFetcher) getclusters(t *testing.T, ctx context.Context) []*envoyclus
590
612
591
613
epcli , err := cds .StreamClusters (ctx )
592
614
if err != nil {
593
- t .Fatalf ("failed to get eds client: %v" , err )
615
+ t .Fatalf ("failed to get cds client: %v" , err )
594
616
}
595
617
defer epcli .CloseSend ()
596
618
epcli .Send (x .dr )
597
619
dresp , err := epcli .Recv ()
598
620
if err != nil {
599
621
t .Fatalf ("failed to get response from xds server: %v" , err )
600
622
}
623
+ t .Logf ("got cds response. nonce, version: %s %s" , dresp .GetNonce (), dresp .GetVersionInfo ())
601
624
var clusters []* envoycluster.Cluster
602
625
for _ , anyCluster := range dresp .GetResources () {
603
626
@@ -644,14 +667,15 @@ func (x *xdsFetcher) getlisteners(t *testing.T, ctx context.Context) []*envoylis
644
667
645
668
epcli , err := ds .StreamListeners (ctx )
646
669
if err != nil {
647
- t .Fatalf ("failed to get eds client: %v" , err )
670
+ t .Fatalf ("failed to get lds client: %v" , err )
648
671
}
649
672
defer epcli .CloseSend ()
650
673
epcli .Send (x .dr )
651
674
dresp , err := epcli .Recv ()
652
675
if err != nil {
653
676
t .Fatalf ("failed to get response from xds server: %v" , err )
654
677
}
678
+ t .Logf ("got lds response. nonce, version: %s %s" , dresp .GetNonce (), dresp .GetVersionInfo ())
655
679
var resources []* envoylistener.Listener
656
680
for _ , anyResource := range dresp .GetResources () {
657
681
@@ -682,6 +706,7 @@ func (x *xdsFetcher) getendpoints(t *testing.T, ctx context.Context, clusterServ
682
706
if err != nil {
683
707
t .Fatalf ("failed to get response from xds server: %v" , err )
684
708
}
709
+ t .Logf ("got eds response. nonce, version: %s %s" , dresp .GetNonce (), dresp .GetVersionInfo ())
685
710
var clas []* envoyendpoint.ClusterLoadAssignment
686
711
for _ , anyCluster := range dresp .GetResources () {
687
712
@@ -705,7 +730,7 @@ func (x *xdsFetcher) getroutes(t *testing.T, ctx context.Context, rosourceNames
705
730
706
731
epcli , err := eds .StreamRoutes (ctx )
707
732
if err != nil {
708
- t .Fatalf ("failed to get eds client: %v" , err )
733
+ t .Fatalf ("failed to get rds client: %v" , err )
709
734
}
710
735
defer epcli .CloseSend ()
711
736
dr := proto .Clone (x .dr ).(* discovery_v3.DiscoveryRequest )
@@ -715,6 +740,7 @@ func (x *xdsFetcher) getroutes(t *testing.T, ctx context.Context, rosourceNames
715
740
if err != nil {
716
741
t .Fatalf ("failed to get response from xds server: %v" , err )
717
742
}
743
+ t .Logf ("got rds response. nonce, version: %s %s" , dresp .GetNonce (), dresp .GetVersionInfo ())
718
744
var clas []* envoy_config_route_v3.RouteConfiguration
719
745
for _ , anyCluster := range dresp .GetResources () {
720
746
0 commit comments