4
4
package api_test
5
5
6
6
import (
7
- "bufio"
8
7
"bytes"
9
8
"fmt"
10
9
"io"
@@ -357,10 +356,9 @@ func (s *clientSuite) TestWatchDebugLogConnected(c *gc.C) {
357
356
// Use the no tail option so we don't try to start a tailing cursor
358
357
// on the oplog when there is no oplog configured in mongo as the tests
359
358
// don't set up mongo in replicaset mode.
360
- reader , err := client .WatchDebugLog (api.DebugLogParams {NoTail : true })
359
+ messages , err := client .WatchDebugLog (api.DebugLogParams {NoTail : true })
361
360
c .Assert (err , jc .ErrorIsNil )
362
- c .Assert (reader , gc .NotNil )
363
- reader .Close ()
361
+ c .Assert (messages , gc .NotNil )
364
362
}
365
363
366
364
func (s * clientSuite ) TestConnectStreamRequiresSlashPathPrefix (c * gc.C ) {
@@ -407,7 +405,8 @@ func (s *clientSuite) TestConnectStreamErrorReadError(c *gc.C) {
407
405
}
408
406
409
407
func (s * clientSuite ) TestWatchDebugLogParamsEncoded (c * gc.C ) {
410
- s .PatchValue (api .WebsocketDialConfig , echoURL (c ))
408
+ catcher := urlCatcher {}
409
+ s .PatchValue (api .WebsocketDialConfig , catcher .recordLocation )
411
410
412
411
params := api.DebugLogParams {
413
412
IncludeEntity : []string {"a" , "b" },
@@ -422,10 +421,10 @@ func (s *clientSuite) TestWatchDebugLogParamsEncoded(c *gc.C) {
422
421
}
423
422
424
423
client := s .APIState .Client ()
425
- reader , err := client .WatchDebugLog (params )
424
+ _ , err := client .WatchDebugLog (params )
426
425
c .Assert (err , jc .ErrorIsNil )
427
426
428
- connectURL := connectURLFromReader ( c , reader )
427
+ connectURL := catcher . location
429
428
values := connectURL .Query ()
430
429
c .Assert (values , jc .DeepEquals , url.Values {
431
430
"includeEntity" : params .IncludeEntity ,
@@ -441,18 +440,18 @@ func (s *clientSuite) TestWatchDebugLogParamsEncoded(c *gc.C) {
441
440
}
442
441
443
442
func (s * clientSuite ) TestConnectStreamAtUUIDPath (c * gc.C ) {
444
- s . PatchValue ( api . WebsocketDialConfig , echoURL ( c ))
445
- // If the server supports it, we should log at "/model/UUID/log"
443
+ catcher := urlCatcher {}
444
+ s . PatchValue ( api . WebsocketDialConfig , catcher . recordLocation )
446
445
environ , err := s .State .Model ()
447
446
c .Assert (err , jc .ErrorIsNil )
448
447
info := s .APIInfo (c )
449
448
info .ModelTag = environ .ModelTag ()
450
449
apistate , err := api .Open (info , api.DialOpts {})
451
450
c .Assert (err , jc .ErrorIsNil )
452
451
defer apistate .Close ()
453
- reader , err : = apistate .ConnectStream ("/path" , nil )
452
+ _ , err = apistate .ConnectStream ("/path" , nil )
454
453
c .Assert (err , jc .ErrorIsNil )
455
- connectURL := connectURLFromReader ( c , reader )
454
+ connectURL := catcher . location
456
455
c .Assert (connectURL .Path , gc .Matches , fmt .Sprintf ("/model/%s/path" , environ .UUID ()))
457
456
}
458
457
@@ -529,25 +528,17 @@ func (r *badReader) Read(p []byte) (n int, err error) {
529
528
return 0 , r .err
530
529
}
531
530
532
- func echoURL (c * gc.C ) func (* websocket.Config ) (base.Stream , error ) {
533
- return func (config * websocket.Config ) (base.Stream , error ) {
534
- pr , pw := io .Pipe ()
535
- go func () {
536
- fmt .Fprintf (pw , "null\n " )
537
- fmt .Fprintf (pw , "%s\n " , config .Location )
538
- }()
539
- return fakeStreamReader {pr }, nil
540
- }
531
+ type urlCatcher struct {
532
+ location * url.URL
541
533
}
542
534
543
- func connectURLFromReader (c * gc.C , rc io.ReadCloser ) * url.URL {
544
- bufReader := bufio .NewReader (rc )
545
- location , err := bufReader .ReadString ('\n' )
546
- c .Assert (err , jc .ErrorIsNil )
547
- connectURL , err := url .Parse (strings .TrimSpace (location ))
548
- c .Assert (err , jc .ErrorIsNil )
549
- rc .Close ()
550
- return connectURL
535
+ func (u * urlCatcher ) recordLocation (config * websocket.Config ) (base.Stream , error ) {
536
+ u .location = config .Location
537
+ pr , pw := io .Pipe ()
538
+ go func () {
539
+ fmt .Fprintf (pw , "null\n " )
540
+ }()
541
+ return fakeStreamReader {pr }, nil
551
542
}
552
543
553
544
type fakeStreamReader struct {
@@ -562,13 +553,13 @@ func (s fakeStreamReader) Close() error {
562
553
}
563
554
564
555
func (s fakeStreamReader ) Write ([]byte ) (int , error ) {
565
- panic ( "not implemented " )
556
+ return 0 , errors . NotImplementedf ( "Write " )
566
557
}
567
558
568
559
func (s fakeStreamReader ) ReadJSON (v interface {}) error {
569
- panic ( "not implemented " )
560
+ return errors . NotImplementedf ( "ReadJSON " )
570
561
}
571
562
572
563
func (s fakeStreamReader ) WriteJSON (v interface {}) error {
573
- panic ( "not implemented " )
564
+ return errors . NotImplementedf ( "WriteJSON " )
574
565
}
0 commit comments