github.com/minio/console@v1.4.1/api/admin_service_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 "errors" 22 "testing" 23 24 "github.com/minio/madmin-go/v3" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestServiceRestart(t *testing.T) { 29 assert := assert.New(t) 30 adminClient := AdminClientMock{} 31 ctx := context.Background() 32 function := "serviceRestart()" 33 // Test-1 : serviceRestart() restart services no errors 34 // mock function response from listGroups() 35 minioServiceRestartMock = func(_ context.Context) error { 36 return nil 37 } 38 MinioServerInfoMock = func(_ context.Context) (madmin.InfoMessage, error) { 39 return madmin.InfoMessage{}, nil 40 } 41 if err := serviceRestart(ctx, adminClient); err != nil { 42 t.Errorf("Failed on %s:, errors occurred: %s", function, err.Error()) 43 } 44 45 // Test-2 : serviceRestart() returns errors on client.serviceRestart call 46 // and see that the errors is handled correctly and returned 47 minioServiceRestartMock = func(_ context.Context) error { 48 return errors.New("error") 49 } 50 MinioServerInfoMock = func(_ context.Context) (madmin.InfoMessage, error) { 51 return madmin.InfoMessage{}, nil 52 } 53 if err := serviceRestart(ctx, adminClient); assert.Error(err) { 54 assert.Equal("error", err.Error()) 55 } 56 57 // Test-3 : serviceRestart() returns errors on client.serverInfo() call 58 // and see that the errors is handled correctly and returned 59 minioServiceRestartMock = func(_ context.Context) error { 60 return nil 61 } 62 MinioServerInfoMock = func(_ context.Context) (madmin.InfoMessage, error) { 63 return madmin.InfoMessage{}, errors.New("error on server info") 64 } 65 if err := serviceRestart(ctx, adminClient); assert.Error(err) { 66 assert.Equal("error on server info", err.Error()) 67 } 68 }