1
1
// This file is part of MinIO Console Server
2
- // Copyright (c) 2021 MinIO, Inc.
2
+ // Copyright (c) 2023 MinIO, Inc.
3
3
//
4
4
// This program is free software: you can redistribute it and/or modify
5
5
// it under the terms of the GNU Affero General Public License as published by
@@ -18,42 +18,137 @@ package restapi
18
18
19
19
import (
20
20
"context"
21
- "errors"
21
+ "net/http"
22
+ "net/http/httptest"
23
+ "os"
22
24
"testing"
23
25
26
+ "github.com/minio/console/models"
27
+ "github.com/minio/console/restapi/operations"
28
+ systemApi "github.com/minio/console/restapi/operations/system"
24
29
"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"
26
32
)
27
33
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 {}
32
47
minioServerInfoMock = func (ctx context.Context ) (madmin.InfoMessage , error ) {
33
48
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
+ },
37
57
}, nil
38
58
}
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 )
48
72
}
49
- assert . Nil ( err , "Error should have been nil" )
73
+ }
50
74
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 )
54
83
}
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
+ }
55
97
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 ))
59
154
}
0 commit comments