@@ -24,6 +24,7 @@ import (
24
24
"encoding/json"
25
25
"fmt"
26
26
"io"
27
+ "strings"
27
28
"sync"
28
29
"testing"
29
30
@@ -99,13 +100,14 @@ func setupObservabilitySystemWithConfig(cfg *config) (func(), error) {
99
100
ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
100
101
defer cancel ()
101
102
err = Start (ctx )
102
- if err != nil {
103
- return nil , fmt .Errorf ("error in Start: %v" , err )
104
- }
105
- return func () {
103
+ cleanup := func () {
106
104
End ()
107
105
envconfig .ObservabilityConfig = oldObservabilityConfig
108
- }, nil
106
+ }
107
+ if err != nil {
108
+ return cleanup , fmt .Errorf ("error in Start: %v" , err )
109
+ }
110
+ return cleanup , nil
109
111
}
110
112
111
113
// TestClientRPCEventsLogAll tests the observability system configured with a
@@ -777,18 +779,18 @@ func (s) TestPrecedenceOrderingInConfiguration(t *testing.T) {
777
779
CloudLogging : & cloudLogging {
778
780
ClientRPCEvents : []clientRPCEvents {
779
781
{
780
- Methods : []string {"/ grpc.testing.TestService/UnaryCall" },
782
+ Methods : []string {"grpc.testing.TestService/UnaryCall" },
781
783
MaxMetadataBytes : 30 ,
782
784
MaxMessageBytes : 30 ,
783
785
},
784
786
{
785
- Methods : []string {"/ grpc.testing.TestService/EmptyCall" },
787
+ Methods : []string {"grpc.testing.TestService/EmptyCall" },
786
788
Exclude : true ,
787
789
MaxMetadataBytes : 30 ,
788
790
MaxMessageBytes : 30 ,
789
791
},
790
792
{
791
- Methods : []string {"/ grpc.testing.TestService/*" },
793
+ Methods : []string {"grpc.testing.TestService/*" },
792
794
MaxMetadataBytes : 30 ,
793
795
MaxMessageBytes : 30 ,
794
796
},
@@ -1273,3 +1275,111 @@ func (s) TestMetadataTruncationAccountsKey(t *testing.T) {
1273
1275
}
1274
1276
fle .mu .Unlock ()
1275
1277
}
1278
+
1279
+ // TestMethodInConfiguration tests different method names with an expectation on
1280
+ // whether they should error or not.
1281
+ func (s ) TestMethodInConfiguration (t * testing.T ) {
1282
+ // To skip creating a stackdriver exporter.
1283
+ fle := & fakeLoggingExporter {
1284
+ t : t ,
1285
+ }
1286
+
1287
+ defer func (ne func (ctx context.Context , config * config ) (loggingExporter , error )) {
1288
+ newLoggingExporter = ne
1289
+ }(newLoggingExporter )
1290
+
1291
+ newLoggingExporter = func (ctx context.Context , config * config ) (loggingExporter , error ) {
1292
+ return fle , nil
1293
+ }
1294
+
1295
+ tests := []struct {
1296
+ name string
1297
+ config * config
1298
+ wantErr string
1299
+ }{
1300
+ {
1301
+ name : "leading-slash" ,
1302
+ config : & config {
1303
+ ProjectID : "fake" ,
1304
+ CloudLogging : & cloudLogging {
1305
+ ClientRPCEvents : []clientRPCEvents {
1306
+ {
1307
+ Methods : []string {"/service/method" },
1308
+ },
1309
+ },
1310
+ },
1311
+ },
1312
+ wantErr : "cannot have a leading slash" ,
1313
+ },
1314
+ {
1315
+ name : "wildcard service/method" ,
1316
+ config : & config {
1317
+ ProjectID : "fake" ,
1318
+ CloudLogging : & cloudLogging {
1319
+ ClientRPCEvents : []clientRPCEvents {
1320
+ {
1321
+ Methods : []string {"*/method" },
1322
+ },
1323
+ },
1324
+ },
1325
+ },
1326
+ wantErr : "cannot have service wildcard *" ,
1327
+ },
1328
+ {
1329
+ name : "/ in service name" ,
1330
+ config : & config {
1331
+ ProjectID : "fake" ,
1332
+ CloudLogging : & cloudLogging {
1333
+ ClientRPCEvents : []clientRPCEvents {
1334
+ {
1335
+ Methods : []string {"ser/vice/method" },
1336
+ },
1337
+ },
1338
+ },
1339
+ },
1340
+ wantErr : "only one /" ,
1341
+ },
1342
+ {
1343
+ name : "empty method name" ,
1344
+ config : & config {
1345
+ ProjectID : "fake" ,
1346
+ CloudLogging : & cloudLogging {
1347
+ ClientRPCEvents : []clientRPCEvents {
1348
+ {
1349
+ Methods : []string {"service/" },
1350
+ },
1351
+ },
1352
+ },
1353
+ },
1354
+ wantErr : "method name must be non empty" ,
1355
+ },
1356
+ {
1357
+ name : "normal" ,
1358
+ config : & config {
1359
+ ProjectID : "fake" ,
1360
+ CloudLogging : & cloudLogging {
1361
+ ClientRPCEvents : []clientRPCEvents {
1362
+ {
1363
+ Methods : []string {"service/method" },
1364
+ },
1365
+ },
1366
+ },
1367
+ },
1368
+ wantErr : "" ,
1369
+ },
1370
+ }
1371
+ for _ , test := range tests {
1372
+ t .Run (test .name , func (t * testing.T ) {
1373
+ cleanup , gotErr := setupObservabilitySystemWithConfig (test .config )
1374
+ if cleanup != nil {
1375
+ defer cleanup ()
1376
+ }
1377
+ if gotErr != nil && ! strings .Contains (gotErr .Error (), test .wantErr ) {
1378
+ t .Fatalf ("Start(%v) = %v, wantErr %v" , test .config , gotErr , test .wantErr )
1379
+ }
1380
+ if (gotErr != nil ) != (test .wantErr != "" ) {
1381
+ t .Fatalf ("Start(%v) = %v, wantErr %v" , test .config , gotErr , test .wantErr )
1382
+ }
1383
+ })
1384
+ }
1385
+ }
0 commit comments