github.com/minio/console@v1.3.0/api/admin_health_info_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "context" 21 "encoding/json" 22 "errors" 23 "reflect" 24 "testing" 25 "time" 26 27 madmin "github.com/minio/madmin-go/v3" 28 ) 29 30 func Test_serverHealthInfo(t *testing.T) { 31 var testReceiver chan madmin.HealthInfo 32 33 ctx, cancel := context.WithCancel(context.Background()) 34 defer cancel() 35 client := AdminClientMock{} 36 mockWSConn := mockConn{} 37 deadlineDuration, _ := time.ParseDuration("1h") 38 39 type args struct { 40 deadline time.Duration 41 wsWriteMock func(messageType int, data []byte) error 42 mockMessages []madmin.HealthInfo 43 } 44 tests := []struct { 45 test string 46 args args 47 wantError error 48 }{ 49 { 50 test: "Return simple health info, no errors", 51 args: args{ 52 deadline: deadlineDuration, 53 mockMessages: []madmin.HealthInfo{{}, {}}, 54 wsWriteMock: func(_ int, data []byte) error { 55 // mock connection WriteMessage() no error 56 // emulate that receiver gets the message written 57 var t madmin.HealthInfo 58 _ = json.Unmarshal(data, &t) 59 testReceiver <- t 60 return nil 61 }, 62 }, 63 wantError: nil, 64 }, 65 { 66 test: "Return simple health info2, no errors", 67 args: args{ 68 deadline: deadlineDuration, 69 mockMessages: []madmin.HealthInfo{{}}, 70 wsWriteMock: func(_ int, data []byte) error { 71 // mock connection WriteMessage() no error 72 // emulate that receiver gets the message written 73 var t madmin.HealthInfo 74 _ = json.Unmarshal(data, &t) 75 testReceiver <- t 76 return nil 77 }, 78 }, 79 wantError: nil, 80 }, 81 { 82 test: "Handle error on ws write", 83 args: args{ 84 deadline: deadlineDuration, 85 mockMessages: []madmin.HealthInfo{{}}, 86 wsWriteMock: func(_ int, data []byte) error { 87 // mock connection WriteMessage() no error 88 // emulate that receiver gets the message written 89 var t madmin.HealthInfo 90 _ = json.Unmarshal(data, &t) 91 return errors.New("error on write") 92 }, 93 }, 94 wantError: errors.New("error on write"), 95 }, 96 { 97 test: "Handle error on health function", 98 args: args{ 99 deadline: deadlineDuration, 100 mockMessages: []madmin.HealthInfo{ 101 { 102 Error: "error on healthInfo", 103 }, 104 }, 105 wsWriteMock: func(_ int, data []byte) error { 106 // mock connection WriteMessage() no error 107 // emulate that receiver gets the message written 108 var t madmin.HealthInfo 109 _ = json.Unmarshal(data, &t) 110 return nil 111 }, 112 }, 113 wantError: nil, 114 }, 115 } 116 for _, tt := range tests { 117 tt := tt 118 t.Run(tt.test, func(_ *testing.T) { 119 // make testReceiver channel 120 testReceiver = make(chan madmin.HealthInfo, len(tt.args.mockMessages)) 121 // mock function same for all tests, changes mockMessages 122 minioServerHealthInfoMock = func(_ context.Context, _ []madmin.HealthDataType, 123 _ time.Duration, 124 ) (interface{}, string, error) { 125 info := tt.args.mockMessages[0] 126 return info, madmin.HealthInfoVersion, nil 127 } 128 connWriteMessageMock = tt.args.wsWriteMock 129 err := startHealthInfo(ctx, mockWSConn, client, &deadlineDuration) 130 // close test mock channel 131 close(testReceiver) 132 // check that the TestReceiver got the same number of data from Console. 133 index := 0 134 for info := range testReceiver { 135 if !reflect.DeepEqual(info, tt.args.mockMessages[index]) { 136 t.Errorf("startHealthInfo() got: %v, want: %v", info, tt.args.mockMessages[index]) 137 return 138 } 139 index++ 140 } 141 if !reflect.DeepEqual(err, tt.wantError) { 142 t.Errorf("startHealthInfo() error: %v, wantError: %v", err, tt.wantError) 143 return 144 } 145 }) 146 } 147 }