7
7
"bytes"
8
8
"context"
9
9
"crypto/sha256"
10
+ "encoding/json"
10
11
"errors"
11
12
"fmt"
12
13
"io"
@@ -1483,18 +1484,13 @@ func testClient_logger(t *testing.T, proto string) {
1483
1484
1484
1485
// Test that we continue to consume stderr over long lines.
1485
1486
func TestClient_logStderr (t * testing.T ) {
1486
- orig := stdErrBufferSize
1487
- stdErrBufferSize = 32
1488
- defer func () {
1489
- stdErrBufferSize = orig
1490
- }()
1491
-
1492
1487
stderr := bytes.Buffer {}
1493
1488
c := NewClient (& ClientConfig {
1494
1489
Stderr : & stderr ,
1495
1490
Cmd : & exec.Cmd {
1496
1491
Path : "test" ,
1497
1492
},
1493
+ PluginLogBufferSize : 32 ,
1498
1494
})
1499
1495
c .clientWaitGroup .Add (1 )
1500
1496
@@ -1515,3 +1511,55 @@ this line is short
1515
1511
t .Fatalf ("\n expected output: %q\n got output: %q" , msg , read )
1516
1512
}
1517
1513
}
1514
+
1515
+ func TestClient_logStderrParseJSON (t * testing.T ) {
1516
+ logBuf := bytes.Buffer {}
1517
+ c := NewClient (& ClientConfig {
1518
+ Stderr : bytes .NewBuffer (nil ),
1519
+ Cmd : & exec.Cmd {Path : "test" },
1520
+ PluginLogBufferSize : 64 ,
1521
+ Logger : hclog .New (& hclog.LoggerOptions {
1522
+ Name : "test-logger" ,
1523
+ Level : hclog .Trace ,
1524
+ Output : & logBuf ,
1525
+ JSONFormat : true ,
1526
+ }),
1527
+ })
1528
+ c .clientWaitGroup .Add (1 )
1529
+
1530
+ msg := `{"@message": "this is a message", "@level": "info"}
1531
+ {"@message": "this is a large message that is more than 64 bytes long", "@level": "info"}`
1532
+ reader := strings .NewReader (msg )
1533
+
1534
+ c .stderrWaitGroup .Add (1 )
1535
+ c .logStderr (c .config .Cmd .Path , reader )
1536
+ logs := strings .Split (strings .TrimSpace (logBuf .String ()), "\n " )
1537
+
1538
+ wants := []struct {
1539
+ wantLevel string
1540
+ wantMessage string
1541
+ }{
1542
+ {"info" , "this is a message" },
1543
+ {"debug" , `{"@message": "this is a large message that is more than 64 bytes` },
1544
+ {"debug" , ` long", "@level": "info"}` },
1545
+ }
1546
+
1547
+ if len (logs ) != len (wants ) {
1548
+ t .Fatalf ("expected %d logs, got %d" , len (wants ), len (logs ))
1549
+ }
1550
+
1551
+ for i , tt := range wants {
1552
+ l := make (map [string ]interface {})
1553
+ if err := json .Unmarshal ([]byte (logs [i ]), & l ); err != nil {
1554
+ t .Fatal (err )
1555
+ }
1556
+
1557
+ if l ["@level" ] != tt .wantLevel {
1558
+ t .Fatalf ("expected level %q, got %q" , tt .wantLevel , l ["@level" ])
1559
+ }
1560
+
1561
+ if l ["@message" ] != tt .wantMessage {
1562
+ t .Fatalf ("expected message %q, got %q" , tt .wantMessage , l ["@message" ])
1563
+ }
1564
+ }
1565
+ }
0 commit comments