@@ -19,6 +19,7 @@ import (
19
19
"time"
20
20
21
21
docker "github.com/fsouza/go-dockerclient"
22
+ "github.com/hyperledger/fabric/common/flogging/floggingtest"
22
23
"github.com/hyperledger/fabric/common/metrics/disabled"
23
24
"github.com/hyperledger/fabric/common/metrics/metricsfakes"
24
25
"github.com/hyperledger/fabric/common/util"
@@ -28,6 +29,7 @@ import (
28
29
coreutil "github.com/hyperledger/fabric/core/testutil"
29
30
pb "github.com/hyperledger/fabric/protos/peer"
30
31
. "github.com/onsi/gomega"
32
+ "github.com/onsi/gomega/gbytes"
31
33
"github.com/spf13/viper"
32
34
"github.com/stretchr/testify/assert"
33
35
"github.com/stretchr/testify/require"
@@ -179,6 +181,38 @@ func Test_Start(t *testing.T) {
179
181
gt .Expect (err ).NotTo (HaveOccurred ())
180
182
}
181
183
184
+ func Test_streamOutput (t * testing.T ) {
185
+ gt := NewGomegaWithT (t )
186
+
187
+ logger , recorder := floggingtest .NewTestLogger (t )
188
+ containerLogger , containerRecorder := floggingtest .NewTestLogger (t )
189
+
190
+ client := & mockClient {}
191
+ errCh := make (chan error , 1 )
192
+ optsCh := make (chan docker.AttachToContainerOptions , 1 )
193
+ client .attachToContainerStub = func (opts docker.AttachToContainerOptions ) error {
194
+ optsCh <- opts
195
+ return <- errCh
196
+ }
197
+
198
+ streamOutput (logger , client , "container-name" , containerLogger )
199
+
200
+ var opts docker.AttachToContainerOptions
201
+ gt .Eventually (optsCh ).Should (Receive (& opts ))
202
+ gt .Eventually (opts .Success ).Should (BeSent (struct {}{}))
203
+ gt .Eventually (opts .Success ).Should (BeClosed ())
204
+
205
+ fmt .Fprintf (opts .OutputStream , "message-one\n " )
206
+ fmt .Fprintf (opts .OutputStream , "message-two" ) // does not get written
207
+ gt .Eventually (containerRecorder ).Should (gbytes .Say ("message-one" ))
208
+ gt .Consistently (containerRecorder .Entries ).Should (HaveLen (1 ))
209
+
210
+ close (errCh )
211
+ gt .Eventually (recorder ).Should (gbytes .Say ("Container container-name has closed its IO channel" ))
212
+ gt .Consistently (recorder .Entries ).Should (HaveLen (1 ))
213
+ gt .Consistently (containerRecorder .Entries ).Should (HaveLen (1 ))
214
+ }
215
+
182
216
func Test_BuildMetric (t * testing.T ) {
183
217
ccid := ccintf.CCID {Name : "simple" , Version : "1.0" }
184
218
client := & mockClient {}
@@ -373,6 +407,8 @@ func (m *mockBuilder) Build() (io.Reader, error) {
373
407
type mockClient struct {
374
408
noSuchImgErrReturned bool
375
409
pingErr bool
410
+
411
+ attachToContainerStub func (docker.AttachToContainerOptions ) error
376
412
}
377
413
378
414
var getClientErr , createErr , uploadErr , noSuchImgErr , buildErr , removeImgErr ,
@@ -404,6 +440,9 @@ func (c *mockClient) UploadToContainer(id string, opts docker.UploadToContainerO
404
440
}
405
441
406
442
func (c * mockClient ) AttachToContainer (opts docker.AttachToContainerOptions ) error {
443
+ if c .attachToContainerStub != nil {
444
+ return c .attachToContainerStub (opts )
445
+ }
407
446
if opts .Success != nil {
408
447
opts .Success <- struct {}{}
409
448
}
0 commit comments