github.com/minio/console@v1.4.1/api/admin_info_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2023 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  	"net/http"
    22  	"net/http/httptest"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/minio/console/pkg/utils"
    27  
    28  	"github.com/minio/console/api/operations"
    29  	systemApi "github.com/minio/console/api/operations/system"
    30  	"github.com/minio/console/models"
    31  	"github.com/minio/madmin-go/v3"
    32  	"github.com/stretchr/testify/assert"
    33  	"github.com/stretchr/testify/suite"
    34  )
    35  
    36  type AdminInfoTestSuite struct {
    37  	suite.Suite
    38  	assert              *assert.Assertions
    39  	currentServer       string
    40  	isServerSet         bool
    41  	isPrometheusRequest bool
    42  	server              *httptest.Server
    43  	adminClient         AdminClientMock
    44  }
    45  
    46  func (suite *AdminInfoTestSuite) SetupSuite() {
    47  	suite.assert = assert.New(suite.T())
    48  	suite.adminClient = AdminClientMock{}
    49  	MinioServerInfoMock = func(_ context.Context) (madmin.InfoMessage, error) {
    50  		return madmin.InfoMessage{
    51  			Servers: []madmin.ServerProperties{{
    52  				Disks: []madmin.Disk{{}},
    53  			}},
    54  			Backend: madmin.ErasureBackend{Type: "mock"},
    55  		}, nil
    56  	}
    57  }
    58  
    59  func (suite *AdminInfoTestSuite) SetupTest() {
    60  	suite.server = httptest.NewServer(http.HandlerFunc(suite.serverHandler))
    61  	suite.currentServer, suite.isServerSet = os.LookupEnv(ConsoleMinIOServer)
    62  	os.Setenv(ConsoleMinIOServer, suite.server.URL)
    63  }
    64  
    65  func (suite *AdminInfoTestSuite) serverHandler(w http.ResponseWriter, _ *http.Request) {
    66  	if suite.isPrometheusRequest {
    67  		w.WriteHeader(200)
    68  	} else {
    69  		w.WriteHeader(400)
    70  	}
    71  }
    72  
    73  func (suite *AdminInfoTestSuite) TearDownSuite() {
    74  }
    75  
    76  func (suite *AdminInfoTestSuite) TearDownTest() {
    77  	if suite.isServerSet {
    78  		os.Setenv(ConsoleMinIOServer, suite.currentServer)
    79  	} else {
    80  		os.Unsetenv(ConsoleMinIOServer)
    81  	}
    82  }
    83  
    84  func (suite *AdminInfoTestSuite) TestRegisterAdminInfoHandlers() {
    85  	api := &operations.ConsoleAPI{}
    86  	suite.assertHandlersAreNil(api)
    87  	registerAdminInfoHandlers(api)
    88  	suite.assertHandlersAreNotNil(api)
    89  }
    90  
    91  func (suite *AdminInfoTestSuite) assertHandlersAreNil(api *operations.ConsoleAPI) {
    92  	suite.assert.Nil(api.SystemAdminInfoHandler)
    93  	suite.assert.Nil(api.SystemDashboardWidgetDetailsHandler)
    94  }
    95  
    96  func (suite *AdminInfoTestSuite) assertHandlersAreNotNil(api *operations.ConsoleAPI) {
    97  	suite.assert.NotNil(api.SystemAdminInfoHandler)
    98  	suite.assert.NotNil(api.SystemDashboardWidgetDetailsHandler)
    99  }
   100  
   101  func (suite *AdminInfoTestSuite) TestSystemAdminInfoHandlerWithError() {
   102  	params, api := suite.initSystemAdminInfoRequest()
   103  	response := api.SystemAdminInfoHandler.Handle(params, &models.Principal{})
   104  	_, ok := response.(*systemApi.AdminInfoDefault)
   105  	suite.assert.True(ok)
   106  }
   107  
   108  func (suite *AdminInfoTestSuite) initSystemAdminInfoRequest() (params systemApi.AdminInfoParams, api operations.ConsoleAPI) {
   109  	registerAdminInfoHandlers(&api)
   110  	params.HTTPRequest = &http.Request{}
   111  	defaultOnly := false
   112  	params.DefaultOnly = &defaultOnly
   113  	return params, api
   114  }
   115  
   116  func (suite *AdminInfoTestSuite) TestSystemDashboardWidgetDetailsHandlerWithError() {
   117  	params, api := suite.initSystemDashboardWidgetDetailsRequest()
   118  	response := api.SystemDashboardWidgetDetailsHandler.Handle(params, &models.Principal{})
   119  	_, ok := response.(*systemApi.DashboardWidgetDetailsDefault)
   120  	suite.assert.True(ok)
   121  }
   122  
   123  func (suite *AdminInfoTestSuite) initSystemDashboardWidgetDetailsRequest() (params systemApi.DashboardWidgetDetailsParams, api operations.ConsoleAPI) {
   124  	registerAdminInfoHandlers(&api)
   125  	params.HTTPRequest = &http.Request{}
   126  	return params, api
   127  }
   128  
   129  func (suite *AdminInfoTestSuite) TestGetUsageWidgetsForDeploymentWithoutError() {
   130  	ctx := context.WithValue(context.Background(), utils.ContextClientIP, "127.0.0.1")
   131  	suite.isPrometheusRequest = true
   132  	res, err := getUsageWidgetsForDeployment(ctx, suite.server.URL, suite.adminClient)
   133  	suite.assert.Nil(err)
   134  	suite.assert.NotNil(res)
   135  	suite.isPrometheusRequest = false
   136  }
   137  
   138  func (suite *AdminInfoTestSuite) TestGetWidgetDetailsWithoutError() {
   139  	ctx := context.WithValue(context.Background(), utils.ContextClientIP, "127.0.0.1")
   140  	suite.isPrometheusRequest = true
   141  	var step int32 = 1
   142  	var start int64
   143  	var end int64 = 1
   144  	res, err := getWidgetDetails(ctx, suite.server.URL, "mock", 1, &step, &start, &end)
   145  	suite.assert.Nil(err)
   146  	suite.assert.NotNil(res)
   147  	suite.isPrometheusRequest = false
   148  }
   149  
   150  func TestAdminInfo(t *testing.T) {
   151  	suite.Run(t, new(AdminInfoTestSuite))
   152  }