@@ -22,7 +22,6 @@ import (
2222 "fmt"
2323 "testing"
2424
25- "github.com/gorilla/websocket"
2625 "github.com/minio/minio/pkg/madmin"
2726 "github.com/stretchr/testify/assert"
2827)
@@ -39,11 +38,13 @@ func TestAdminConsoleLog(t *testing.T) {
3938 assert := assert .New (t )
4039 adminClient := adminClientMock {}
4140 mockWSConn := mockConn {}
42- function := "startConsoleLog()"
41+ function := "startConsoleLog(ctx, )"
42+ ctx := context .Background ()
4343
4444 testReceiver := make (chan madmin.LogInfo , 5 )
4545 textToReceive := "test message"
4646 testStreamSize := 5
47+ isClosed := false // testReceiver is closed?
4748
4849 // Test-1: Serve Console with no errors until Console finishes sending
4950 // define mock function behavior for minio server Console
@@ -63,26 +64,24 @@ func TestAdminConsoleLog(t *testing.T) {
6364 }(ch )
6465 return ch
6566 }
66- // mock function of conn.ReadMessage(), no error on read
67- connReadMessageMock = func () (messageType int , p []byte , err error ) {
68- return 0 , []byte {}, nil
69- }
7067 writesCount := 1
7168 // mock connection WriteMessage() no error
7269 connWriteMessageMock = func (messageType int , data []byte ) error {
7370 // emulate that receiver gets the message written
7471 var t madmin.LogInfo
7572 _ = json .Unmarshal (data , & t )
7673 if writesCount == testStreamSize {
77- // for testing we need to close the receiver channel
78- close (testReceiver )
74+ if ! isClosed {
75+ close (testReceiver )
76+ isClosed = true
77+ }
7978 return nil
8079 }
8180 testReceiver <- t
8281 writesCount ++
8382 return nil
8483 }
85- if err := startConsoleLog (mockWSConn , adminClient ); err != nil {
84+ if err := startConsoleLog (ctx , mockWSConn , adminClient ); err != nil {
8685 t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
8786 }
8887 // check that the TestReceiver got the same number of data from Console.
@@ -94,50 +93,11 @@ func TestAdminConsoleLog(t *testing.T) {
9493 connWriteMessageMock = func (messageType int , data []byte ) error {
9594 return fmt .Errorf ("error on write" )
9695 }
97- if err := startConsoleLog (mockWSConn , adminClient ); assert .Error (err ) {
96+ if err := startConsoleLog (ctx , mockWSConn , adminClient ); assert .Error (err ) {
9897 assert .Equal ("error on write" , err .Error ())
9998 }
10099
101- // Test-3: error happens while reading, unexpected Close Error should return error.
102- connWriteMessageMock = func (messageType int , data []byte ) error {
103- return nil
104- }
105- // mock function of conn.ReadMessage(), returns unexpected Close Error CloseAbnormalClosure
106- connReadMessageMock = func () (messageType int , p []byte , err error ) {
107- return 0 , []byte {}, & websocket.CloseError {Code : websocket .CloseAbnormalClosure , Text : "" }
108- }
109- if err := startConsoleLog (mockWSConn , adminClient ); assert .Error (err ) {
110- assert .Equal ("websocket: close 1006 (abnormal closure)" , err .Error ())
111- }
112-
113- // Test-4: error happens while reading, expected Close Error NormalClosure
114- // expected Close Error should not return an error, just end Console.
115- connReadMessageMock = func () (messageType int , p []byte , err error ) {
116- return 0 , []byte {}, & websocket.CloseError {Code : websocket .CloseNormalClosure , Text : "" }
117- }
118- if err := startConsoleLog (mockWSConn , adminClient ); err != nil {
119- t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
120- }
121-
122- // Test-5: error happens while reading, expected Close Error CloseGoingAway
123- // expected Close Error should not return an error, just return.
124- connReadMessageMock = func () (messageType int , p []byte , err error ) {
125- return 0 , []byte {}, & websocket.CloseError {Code : websocket .CloseGoingAway , Text : "" }
126- }
127- if err := startConsoleLog (mockWSConn , adminClient ); err != nil {
128- t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
129- }
130-
131- // Test-6: error happens while reading, non Close Error Type should be returned as
132- // error
133- connReadMessageMock = func () (messageType int , p []byte , err error ) {
134- return 0 , []byte {}, fmt .Errorf ("error on read" )
135- }
136- if err := startConsoleLog (mockWSConn , adminClient ); assert .Error (err ) {
137- assert .Equal ("error on read" , err .Error ())
138- }
139-
140- // Test-7: error happens on GetLogs Minio, Console should stop
100+ // Test-3: error happens on GetLogs Minio, Console should stop
141101 // and error shall be returned.
142102 minioGetLogsMock = func (ctx context.Context , node string , lineCnt int , logKind string ) <- chan madmin.LogInfo {
143103 ch := make (chan madmin.LogInfo )
@@ -156,12 +116,10 @@ func TestAdminConsoleLog(t *testing.T) {
156116 }(ch )
157117 return ch
158118 }
159- // mock function of conn.ReadMessage(), no error on read, should stay unless
160- // context is done.
161- connReadMessageMock = func () (messageType int , p []byte , err error ) {
162- return 0 , []byte {}, nil
119+ connWriteMessageMock = func (messageType int , data []byte ) error {
120+ return nil
163121 }
164- if err := startConsoleLog (mockWSConn , adminClient ); assert .Error (err ) {
122+ if err := startConsoleLog (ctx , mockWSConn , adminClient ); assert .Error (err ) {
165123 assert .Equal ("error on Console" , err .Error ())
166124 }
167125}
0 commit comments