@ -26,16 +26,26 @@ import (
k8stest "k8s.io/client-go/testing"
)
type concurrentWriter struct {
w io . Writer
// concurrentBuffer is a buffer which implements io.Writer and fmt.Stringer. It
// is useful in tests to allow the output to be read back from the destination
// buffer.
type concurrentBuffer struct {
bytes . Buffer
mu sync . Mutex
}
func ( c w * concurrentWrit er) Write ( p [ ] byte ) ( int , error ) {
c w . mu . Lock ( )
defer c w . mu . Unlock ( )
func ( c b * concurrentBuff er) Write ( p [ ] byte ) ( int , error ) {
c b . mu . Lock ( )
defer c b . mu . Unlock ( )
return cw . w . Write ( p )
return cb . Buffer . Write ( p )
}
func ( cb * concurrentBuffer ) String ( ) string {
cb . mu . Lock ( )
defer cb . mu . Unlock ( )
return cb . Buffer . String ( )
}
type mockPodWatcher struct { err error }
@ -134,10 +144,9 @@ func TestWatcherWithPodWatcher(t *testing.T) {
untypedClient . PrependWatchReactor ( "deployments" , k8stest . DefaultWatchReactor ( deploymentsWatcher , nil ) )
client := logs . KubernetesClient { Typed : typedClient , Untyped : untypedClient }
var buf bytes . Buffer
cw := concurrentWriter { w : & buf }
var cb concurrentBuffer
params := logs . WatcherParams { Name : "mydeployment" , Type : "deployments" , Namespace : "default" }
watcher := logs . NewWatcher ( params , client , logs . NewPodWatcher , & c w , discardLogger ( ) )
watcher := logs . NewWatcher ( params , client , logs . NewPodWatcher , & c b , discardLogger ( ) )
go func ( ) {
deployment := buildDeployment ( t , "mydeployment" )
@ -159,12 +168,12 @@ func TestWatcherWithPodWatcher(t *testing.T) {
err := watcher . Watch ( context . Background ( ) )
require . NoError ( t , err )
lines := bufToLines ( & buf )
lines := bufToLines ( & c b)
require . Len ( t , lines , 2 )
assert . ElementsMatch ( t , [ ] string { "[foo] fake logs" , "[bar] fake logs" } , lines )
}
// bufToLines splits the output buffer removing newline characters.
func bufToLines ( buf * bytes. Buffer) [ ] string {
func bufToLines ( buf * concurrent Buffer) [ ] string {
return strings . Split ( strings . TrimSpace ( buf . String ( ) ) , "\n" )
}