Skip to content

Commit 756f004

Browse files
authored
Add tests for admin info (#2553)
1 parent 1cb2fca commit 756f004

File tree

2 files changed

+128
-44
lines changed

2 files changed

+128
-44
lines changed

restapi/admin_info.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/minio/console/models"
3434
"github.com/minio/console/restapi/operations"
3535
systemApi "github.com/minio/console/restapi/operations/system"
36-
"github.com/minio/madmin-go/v2"
3736
)
3837

3938
func registerAdminInfoHandlers(api *operations.ConsoleAPI) {
@@ -94,18 +93,13 @@ func GetAdminInfo(ctx context.Context, client MinioAdmin) (*UsageInfo, error) {
9493
}
9594

9695
var usedSpace int64
97-
for _, serv := range serverInfo.Servers {
98-
for _, disk := range serv.Disks {
99-
usedSpace += int64(disk.UsedSpace)
100-
}
101-
}
102-
10396
// serverArray contains the serverProperties which describe the servers in the network
10497
var serverArray []*models.ServerProperties
10598
for _, serv := range serverInfo.Servers {
10699
drives := []*models.ServerDrives{}
107100

108101
for _, drive := range serv.Disks {
102+
usedSpace += int64(drive.UsedSpace)
109103
drives = append(drives, &models.ServerDrives{
110104
State: drive.State,
111105
UUID: drive.UUID,
@@ -905,15 +899,15 @@ func getAdminInfoResponse(session *models.Principal, params systemApi.AdminInfoP
905899
return nil, ErrorWithContext(ctx, err)
906900
}
907901

908-
sessionResp, err2 := getUsageWidgetsForDeployment(ctx, prometheusURL, mAdmin)
902+
sessionResp, err2 := getUsageWidgetsForDeployment(ctx, prometheusURL, AdminClient{Client: mAdmin})
909903
if err2 != nil {
910904
return nil, ErrorWithContext(ctx, err2)
911905
}
912906

913907
return sessionResp, nil
914908
}
915909

916-
func getUsageWidgetsForDeployment(ctx context.Context, prometheusURL string, mAdmin *madmin.AdminClient) (*models.AdminInfoResponse, error) {
910+
func getUsageWidgetsForDeployment(ctx context.Context, prometheusURL string, adminClient MinioAdmin) (*models.AdminInfoResponse, error) {
917911
prometheusStatus := models.AdminInfoResponseAdvancedMetricsStatusAvailable
918912
if prometheusURL == "" {
919913
prometheusStatus = models.AdminInfoResponseAdvancedMetricsStatusNotConfigured
@@ -927,10 +921,6 @@ func getUsageWidgetsForDeployment(ctx context.Context, prometheusURL string, mAd
927921
doneCh := make(chan error)
928922
go func() {
929923
defer close(doneCh)
930-
// create a minioClient interface implementation
931-
// defining the client to be used
932-
adminClient := AdminClient{Client: mAdmin}
933-
934924
// serialize output
935925
usage, err := GetAdminInfo(ctx, adminClient)
936926
if err != nil {
@@ -1039,11 +1029,6 @@ func getAdminInfoWidgetResponse(params systemApi.DashboardWidgetDetailsParams) (
10391029
prometheusJobID := getPrometheusJobID()
10401030
prometheusExtraLabels := getPrometheusExtraLabels()
10411031

1042-
// We test if prometheus URL is reachable. this is meant to avoid unuseful calls and application hang.
1043-
if !testPrometheusURL(ctx, prometheusURL) {
1044-
return nil, ErrorWithContext(ctx, errors.New("Prometheus URL is unreachable"))
1045-
}
1046-
10471032
selector := fmt.Sprintf(`job="%s"`, prometheusJobID)
10481033
if strings.TrimSpace(prometheusExtraLabels) != "" {
10491034
selector = fmt.Sprintf(`job="%s",%s`, prometheusJobID, prometheusExtraLabels)
@@ -1052,6 +1037,10 @@ func getAdminInfoWidgetResponse(params systemApi.DashboardWidgetDetailsParams) (
10521037
}
10531038

10541039
func getWidgetDetails(ctx context.Context, prometheusURL string, selector string, widgetID int32, step *int32, start *int64, end *int64) (*models.WidgetDetails, *models.Error) {
1040+
// We test if prometheus URL is reachable. this is meant to avoid unuseful calls and application hang.
1041+
if !testPrometheusURL(ctx, prometheusURL) {
1042+
return nil, ErrorWithContext(ctx, errors.New("prometheus URL is unreachable"))
1043+
}
10551044
labelResultsCh := make(chan LabelResults)
10561045

10571046
for _, lbl := range labels {

restapi/admin_info_test.go

Lines changed: 121 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This file is part of MinIO Console Server
2-
// Copyright (c) 2021 MinIO, Inc.
2+
// Copyright (c) 2023 MinIO, Inc.
33
//
44
// This program is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU Affero General Public License as published by
@@ -18,42 +18,137 @@ package restapi
1818

1919
import (
2020
"context"
21-
"errors"
21+
"net/http"
22+
"net/http/httptest"
23+
"os"
2224
"testing"
2325

26+
"github.com/minio/console/models"
27+
"github.com/minio/console/restapi/operations"
28+
systemApi "github.com/minio/console/restapi/operations/system"
2429
"github.com/minio/madmin-go/v2"
25-
asrt "github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/assert"
31+
"github.com/stretchr/testify/suite"
2632
)
2733

28-
func TestAdminInfo(t *testing.T) {
29-
assert := asrt.New(t)
30-
adminClient := adminClientMock{}
31-
// Test-1 : getAdminInfo() returns proper information
34+
type AdminInfoTestSuite struct {
35+
suite.Suite
36+
assert *assert.Assertions
37+
currentServer string
38+
isServerSet bool
39+
isPrometheusRequest bool
40+
server *httptest.Server
41+
adminClient adminClientMock
42+
}
43+
44+
func (suite *AdminInfoTestSuite) SetupSuite() {
45+
suite.assert = assert.New(suite.T())
46+
suite.adminClient = adminClientMock{}
3247
minioServerInfoMock = func(ctx context.Context) (madmin.InfoMessage, error) {
3348
return madmin.InfoMessage{
34-
Buckets: madmin.Buckets{Count: 10},
35-
Objects: madmin.Objects{Count: 10},
36-
Usage: madmin.Usage{Size: 10},
49+
Servers: []madmin.ServerProperties{{
50+
Disks: []madmin.Disk{{}},
51+
}},
52+
Backend: map[string]interface{}{
53+
"backendType": "mock",
54+
"rrSCParity": 0.0,
55+
"standardSCParity": 0.0,
56+
},
3757
}, nil
3858
}
39-
ctx, cancel := context.WithCancel(context.Background())
40-
defer cancel()
41-
serverInfo, err := GetAdminInfo(ctx, adminClient)
42-
assert.NotNil(serverInfo, "server info was returned nil")
43-
if serverInfo != nil {
44-
var actual64 int64 = 10
45-
assert.Equal(serverInfo.Buckets, actual64, "Incorrect bucket count")
46-
assert.Equal(serverInfo.Objects, actual64, "Incorrect object count")
47-
assert.Equal(serverInfo.Usage, actual64, "Incorrect usage size")
59+
}
60+
61+
func (suite *AdminInfoTestSuite) SetupTest() {
62+
suite.server = httptest.NewServer(http.HandlerFunc(suite.serverHandler))
63+
suite.currentServer, suite.isServerSet = os.LookupEnv(ConsoleMinIOServer)
64+
os.Setenv(ConsoleMinIOServer, suite.server.URL)
65+
}
66+
67+
func (suite *AdminInfoTestSuite) serverHandler(w http.ResponseWriter, r *http.Request) {
68+
if suite.isPrometheusRequest {
69+
w.WriteHeader(200)
70+
} else {
71+
w.WriteHeader(400)
4872
}
49-
assert.Nil(err, "Error should have been nil")
73+
}
5074

51-
// Test-2 : getAdminInfo(ctx) fails for whatever reason
52-
minioServerInfoMock = func(ctx context.Context) (madmin.InfoMessage, error) {
53-
return madmin.InfoMessage{}, errors.New("some reason")
75+
func (suite *AdminInfoTestSuite) TearDownSuite() {
76+
}
77+
78+
func (suite *AdminInfoTestSuite) TearDownTest() {
79+
if suite.isServerSet {
80+
os.Setenv(ConsoleMinIOServer, suite.currentServer)
81+
} else {
82+
os.Unsetenv(ConsoleMinIOServer)
5483
}
84+
}
85+
86+
func (suite *AdminInfoTestSuite) TestRegisterAdminInfoHandlers() {
87+
api := &operations.ConsoleAPI{}
88+
suite.assertHandlersAreNil(api)
89+
registerAdminInfoHandlers(api)
90+
suite.assertHandlersAreNotNil(api)
91+
}
92+
93+
func (suite *AdminInfoTestSuite) assertHandlersAreNil(api *operations.ConsoleAPI) {
94+
suite.assert.Nil(api.SystemAdminInfoHandler)
95+
suite.assert.Nil(api.SystemDashboardWidgetDetailsHandler)
96+
}
5597

56-
serverInfo, err = GetAdminInfo(ctx, adminClient)
57-
assert.Nil(serverInfo, "server info was not returned nil")
58-
assert.NotNil(err, "An error should have ben returned")
98+
func (suite *AdminInfoTestSuite) assertHandlersAreNotNil(api *operations.ConsoleAPI) {
99+
suite.assert.NotNil(api.SystemAdminInfoHandler)
100+
suite.assert.NotNil(api.SystemDashboardWidgetDetailsHandler)
101+
}
102+
103+
func (suite *AdminInfoTestSuite) TestSystemAdminInfoHandlerWithError() {
104+
params, api := suite.initSystemAdminInfoRequest()
105+
response := api.SystemAdminInfoHandler.Handle(params, &models.Principal{})
106+
_, ok := response.(*systemApi.AdminInfoDefault)
107+
suite.assert.True(ok)
108+
}
109+
110+
func (suite *AdminInfoTestSuite) initSystemAdminInfoRequest() (params systemApi.AdminInfoParams, api operations.ConsoleAPI) {
111+
registerAdminInfoHandlers(&api)
112+
params.HTTPRequest = &http.Request{}
113+
defaultOnly := false
114+
params.DefaultOnly = &defaultOnly
115+
return params, api
116+
}
117+
118+
func (suite *AdminInfoTestSuite) TestSystemDashboardWidgetDetailsHandlerWithError() {
119+
params, api := suite.initSystemDashboardWidgetDetailsRequest()
120+
response := api.SystemDashboardWidgetDetailsHandler.Handle(params, &models.Principal{})
121+
_, ok := response.(*systemApi.DashboardWidgetDetailsDefault)
122+
suite.assert.True(ok)
123+
}
124+
125+
func (suite *AdminInfoTestSuite) initSystemDashboardWidgetDetailsRequest() (params systemApi.DashboardWidgetDetailsParams, api operations.ConsoleAPI) {
126+
registerAdminInfoHandlers(&api)
127+
params.HTTPRequest = &http.Request{}
128+
return params, api
129+
}
130+
131+
func (suite *AdminInfoTestSuite) TestGetUsageWidgetsForDeploymentWithoutError() {
132+
ctx := context.Background()
133+
suite.isPrometheusRequest = true
134+
res, err := getUsageWidgetsForDeployment(ctx, suite.server.URL, suite.adminClient)
135+
suite.assert.Nil(err)
136+
suite.assert.NotNil(res)
137+
suite.isPrometheusRequest = false
138+
}
139+
140+
func (suite *AdminInfoTestSuite) TestGetWidgetDetailsWithoutError() {
141+
ctx := context.Background()
142+
suite.isPrometheusRequest = true
143+
var step int32 = 1
144+
var start int64
145+
var end int64 = 1
146+
res, err := getWidgetDetails(ctx, suite.server.URL, "mock", 1, &step, &start, &end)
147+
suite.assert.Nil(err)
148+
suite.assert.NotNil(res)
149+
suite.isPrometheusRequest = false
150+
}
151+
152+
func TestAdminInfo(t *testing.T) {
153+
suite.Run(t, new(AdminInfoTestSuite))
59154
}

0 commit comments

Comments
 (0)