2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-16 01:26:15 -05:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-01-17 13:02:58 -05:00
|
|
|
"bytes"
|
2020-04-14 20:52:38 -04:00
|
|
|
"context"
|
2016-12-16 01:26:15 -05:00
|
|
|
"encoding/json"
|
2023-03-21 21:35:29 -04:00
|
|
|
"fmt"
|
2017-03-17 12:25:49 -04:00
|
|
|
"io"
|
2016-12-16 01:26:15 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-01-04 02:39:22 -05:00
|
|
|
"net/url"
|
2023-03-21 21:35:29 -04:00
|
|
|
"sort"
|
2018-09-27 20:16:30 -04:00
|
|
|
"sync"
|
2016-12-16 01:26:15 -05:00
|
|
|
"testing"
|
2021-11-12 00:03:02 -05:00
|
|
|
"time"
|
2016-12-16 01:26:15 -05:00
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
2023-01-23 06:12:47 -05:00
|
|
|
"github.com/minio/mux"
|
2016-12-16 01:26:15 -05:00
|
|
|
)
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// adminErasureTestBed - encapsulates subsystems that need to be setup for
|
2017-01-23 03:32:55 -05:00
|
|
|
// admin-handler unit tests.
|
2020-06-12 23:04:01 -04:00
|
|
|
type adminErasureTestBed struct {
|
|
|
|
erasureDirs []string
|
|
|
|
objLayer ObjectLayer
|
|
|
|
router *mux.Router
|
2021-12-02 14:29:16 -05:00
|
|
|
done context.CancelFunc
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// prepareAdminErasureTestBed - helper function that setups a single-node
|
|
|
|
// Erasure backend for admin-handler tests.
|
|
|
|
func prepareAdminErasureTestBed(ctx context.Context) (*adminErasureTestBed, error) {
|
2021-12-02 14:29:16 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-04-14 20:52:38 -04:00
|
|
|
|
2017-01-23 03:32:55 -05:00
|
|
|
// reset global variables to start afresh.
|
|
|
|
resetTestGlobals()
|
2017-02-07 15:51:43 -05:00
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// Set globalIsErasure to indicate that the setup uses an erasure
|
2020-01-15 21:30:32 -05:00
|
|
|
// code backend.
|
2020-06-12 23:04:01 -04:00
|
|
|
globalIsErasure = true
|
2020-01-15 21:30:32 -05:00
|
|
|
|
2017-01-23 03:32:55 -05:00
|
|
|
// Initializing objectLayer for HealFormatHandler.
|
2020-06-12 23:04:01 -04:00
|
|
|
objLayer, erasureDirs, xlErr := initTestErasureObjLayer(ctx)
|
2017-01-23 03:32:55 -05:00
|
|
|
if xlErr != nil {
|
2021-12-02 14:29:16 -05:00
|
|
|
cancel()
|
2017-01-23 03:32:55 -05:00
|
|
|
return nil, xlErr
|
|
|
|
}
|
|
|
|
|
2018-08-15 00:41:47 -04:00
|
|
|
// Initialize minio server config.
|
|
|
|
if err := newTestConfig(globalMinioDefaultRegion, objLayer); err != nil {
|
2021-12-02 14:29:16 -05:00
|
|
|
cancel()
|
2018-08-15 00:41:47 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-08 03:13:02 -05:00
|
|
|
// Initialize boot time
|
2017-03-18 14:28:41 -04:00
|
|
|
globalBootTime = UTCNow()
|
2017-02-08 03:13:02 -05:00
|
|
|
|
2021-01-26 23:47:42 -05:00
|
|
|
globalEndpoints = mustGetPoolEndpoints(erasureDirs...)
|
2017-01-23 03:32:55 -05:00
|
|
|
|
2022-10-14 06:08:40 -04:00
|
|
|
initAllSubsystems(ctx)
|
2019-02-12 04:25:52 -05:00
|
|
|
|
2021-11-17 16:42:08 -05:00
|
|
|
initConfigSubsystem(ctx, objLayer)
|
2018-04-24 18:53:30 -04:00
|
|
|
|
2022-02-07 13:39:57 -05:00
|
|
|
globalIAMSys.Init(ctx, objLayer, globalEtcdClient, 2*time.Second)
|
2021-05-09 11:14:19 -04:00
|
|
|
|
2017-01-23 03:32:55 -05:00
|
|
|
// Setup admin mgmt REST API handlers.
|
2018-04-21 22:23:54 -04:00
|
|
|
adminRouter := mux.NewRouter()
|
2021-07-10 11:32:52 -04:00
|
|
|
registerAdminRouter(adminRouter, true)
|
2017-01-23 03:32:55 -05:00
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
return &adminErasureTestBed{
|
|
|
|
erasureDirs: erasureDirs,
|
|
|
|
objLayer: objLayer,
|
|
|
|
router: adminRouter,
|
2021-12-02 14:29:16 -05:00
|
|
|
done: cancel,
|
2017-01-23 03:32:55 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDown - method that resets the test bed for subsequent unit
|
|
|
|
// tests to start afresh.
|
2020-06-12 23:04:01 -04:00
|
|
|
func (atb *adminErasureTestBed) TearDown() {
|
2021-12-02 14:29:16 -05:00
|
|
|
atb.done()
|
2020-06-12 23:04:01 -04:00
|
|
|
removeRoots(atb.erasureDirs)
|
2017-01-23 03:32:55 -05:00
|
|
|
resetTestGlobals()
|
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// initTestObjLayer - Helper function to initialize an Erasure-based object
|
2017-01-23 03:32:55 -05:00
|
|
|
// layer and set globalObjectAPI.
|
2020-06-12 23:04:01 -04:00
|
|
|
func initTestErasureObjLayer(ctx context.Context) (ObjectLayer, []string, error) {
|
|
|
|
erasureDirs, err := getRandomDisks(16)
|
2018-02-15 20:45:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-01-26 23:47:42 -05:00
|
|
|
endpoints := mustGetPoolEndpoints(erasureDirs...)
|
2018-06-06 15:52:56 -04:00
|
|
|
globalPolicySys = NewPolicySys()
|
2021-01-16 15:08:02 -05:00
|
|
|
objLayer, err := newErasureServerPools(ctx, endpoints)
|
2018-02-15 20:45:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-23 03:32:55 -05:00
|
|
|
// Make objLayer available to all internal services via globalObjectAPI.
|
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalObjectAPI = objLayer
|
|
|
|
globalObjLayerMutex.Unlock()
|
2020-06-12 23:04:01 -04:00
|
|
|
return objLayer, erasureDirs, nil
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// cmdType - Represents different service subcomands like status, stop
|
|
|
|
// and restart.
|
2016-12-16 01:26:15 -05:00
|
|
|
type cmdType int
|
|
|
|
|
|
|
|
const (
|
2019-08-28 18:04:43 -04:00
|
|
|
restartCmd cmdType = iota
|
2018-01-22 17:54:55 -05:00
|
|
|
stopCmd
|
2016-12-16 01:26:15 -05:00
|
|
|
)
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// toServiceSignal - Helper function that translates a given cmdType
|
|
|
|
// value to its corresponding serviceSignal value.
|
2016-12-16 01:26:15 -05:00
|
|
|
func (c cmdType) toServiceSignal() serviceSignal {
|
|
|
|
switch c {
|
|
|
|
case restartCmd:
|
|
|
|
return serviceRestart
|
2018-01-22 17:54:55 -05:00
|
|
|
case stopCmd:
|
|
|
|
return serviceStop
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
2019-08-28 18:04:43 -04:00
|
|
|
return serviceRestart
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
func (c cmdType) toServiceAction() madmin.ServiceAction {
|
2018-01-22 17:54:55 -05:00
|
|
|
switch c {
|
|
|
|
case restartCmd:
|
2019-08-27 14:37:47 -04:00
|
|
|
return madmin.ServiceActionRestart
|
2018-01-22 17:54:55 -05:00
|
|
|
case stopCmd:
|
2019-08-27 14:37:47 -04:00
|
|
|
return madmin.ServiceActionStop
|
2018-01-22 17:54:55 -05:00
|
|
|
}
|
2019-08-28 18:04:43 -04:00
|
|
|
return madmin.ServiceActionRestart
|
2018-01-22 17:54:55 -05:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// testServiceSignalReceiver - Helper function that simulates a
|
|
|
|
// go-routine waiting on service signal.
|
2016-12-16 01:26:15 -05:00
|
|
|
func testServiceSignalReceiver(cmd cmdType, t *testing.T) {
|
|
|
|
expectedCmd := cmd.toServiceSignal()
|
|
|
|
serviceCmd := <-globalServiceSignalCh
|
|
|
|
if serviceCmd != expectedCmd {
|
|
|
|
t.Errorf("Expected service command %v but received %v", expectedCmd, serviceCmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// getServiceCmdRequest - Constructs a management REST API request for service
|
|
|
|
// subcommands for a given cmdType value.
|
2019-08-27 14:37:47 -04:00
|
|
|
func getServiceCmdRequest(cmd cmdType, cred auth.Credentials) (*http.Request, error) {
|
|
|
|
queryVal := url.Values{}
|
|
|
|
queryVal.Set("action", string(cmd.toServiceAction()))
|
2019-11-04 12:30:59 -05:00
|
|
|
resource := adminPathPrefix + adminAPIVersionPrefix + "/service?" + queryVal.Encode()
|
2019-08-27 14:37:47 -04:00
|
|
|
req, err := newTestRequest(http.MethodPost, resource, 0, nil)
|
2016-12-16 01:26:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-04 02:39:22 -05:00
|
|
|
|
|
|
|
// management REST API uses signature V4 for authentication.
|
2016-12-26 13:21:23 -05:00
|
|
|
err = signRequestV4(req, cred.AccessKey, cred.SecretKey)
|
2016-12-16 01:26:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// testServicesCmdHandler - parametrizes service subcommand tests on
|
|
|
|
// cmdType value.
|
2017-01-24 11:08:36 -05:00
|
|
|
func testServicesCmdHandler(cmd cmdType, t *testing.T) {
|
2020-04-14 20:52:38 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
adminTestBed, err := prepareAdminErasureTestBed(ctx)
|
2016-12-16 01:26:15 -05:00
|
|
|
if err != nil {
|
2021-08-03 16:26:57 -04:00
|
|
|
t.Fatal("Failed to initialize a single node Erasure backend for admin handler tests.", err)
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
2017-01-23 03:32:55 -05:00
|
|
|
defer adminTestBed.TearDown()
|
2016-12-16 01:26:15 -05:00
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// Initialize admin peers to make admin RPC calls. Note: In a
|
|
|
|
// single node setup, this degenerates to a simple function
|
|
|
|
// call under the hood.
|
2017-04-11 18:44:27 -04:00
|
|
|
globalMinioAddr = "127.0.0.1:9000"
|
2016-12-16 01:26:15 -05:00
|
|
|
|
2018-09-27 20:16:30 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2018-04-21 22:23:54 -04:00
|
|
|
// Setting up a go routine to simulate ServerRouter's
|
2016-12-16 01:26:15 -05:00
|
|
|
// handleServiceSignals for stop and restart commands.
|
2017-01-17 17:25:59 -05:00
|
|
|
if cmd == restartCmd {
|
2018-09-27 20:16:30 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
testServiceSignalReceiver(cmd, t)
|
|
|
|
}()
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
credentials := globalActiveCred
|
2018-01-22 17:54:55 -05:00
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
req, err := getServiceCmdRequest(cmd, credentials)
|
2016-12-16 01:26:15 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to build service status request %v", err)
|
|
|
|
}
|
2017-01-17 17:25:59 -05:00
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2018-04-21 22:23:54 -04:00
|
|
|
adminTestBed.router.ServeHTTP(rec, req)
|
2016-12-16 01:26:15 -05:00
|
|
|
|
2019-08-27 14:37:47 -04:00
|
|
|
if rec.Code != http.StatusOK {
|
2022-09-19 14:05:16 -04:00
|
|
|
resp, _ := io.ReadAll(rec.Body)
|
2019-08-27 14:37:47 -04:00
|
|
|
t.Errorf("Expected to receive %d status code but received %d. Body (%s)",
|
|
|
|
http.StatusOK, rec.Code, string(resp))
|
|
|
|
}
|
2016-12-16 01:26:15 -05:00
|
|
|
|
2018-09-27 20:16:30 -04:00
|
|
|
// Wait until testServiceSignalReceiver() called in a goroutine quits.
|
|
|
|
wg.Wait()
|
2016-12-16 01:26:15 -05:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// Test for service restart management REST API.
|
2016-12-16 01:26:15 -05:00
|
|
|
func TestServiceRestartHandler(t *testing.T) {
|
2017-01-24 11:08:36 -05:00
|
|
|
testServicesCmdHandler(restartCmd, t)
|
2017-01-17 17:25:59 -05:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:25:49 -04:00
|
|
|
// buildAdminRequest - helper function to build an admin API request.
|
2018-01-22 17:54:55 -05:00
|
|
|
func buildAdminRequest(queryVal url.Values, method, path string,
|
2022-01-02 12:15:06 -05:00
|
|
|
contentLength int64, bodySeeker io.ReadSeeker) (*http.Request, error,
|
|
|
|
) {
|
2018-01-22 17:54:55 -05:00
|
|
|
req, err := newTestRequest(method,
|
2019-11-04 12:30:59 -05:00
|
|
|
adminPathPrefix+adminAPIVersionPrefix+path+"?"+queryVal.Encode(),
|
2018-01-22 17:54:55 -05:00
|
|
|
contentLength, bodySeeker)
|
2017-03-17 12:25:49 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, err
|
2017-03-17 12:25:49 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
cred := globalActiveCred
|
2017-03-17 12:25:49 -04:00
|
|
|
err = signRequestV4(req, cred.AccessKey, cred.SecretKey)
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, err
|
2017-03-17 12:25:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2017-04-07 02:08:33 -04:00
|
|
|
func TestAdminServerInfo(t *testing.T) {
|
2020-04-14 20:52:38 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
adminTestBed, err := prepareAdminErasureTestBed(ctx)
|
2017-04-07 02:08:33 -04:00
|
|
|
if err != nil {
|
2021-08-03 16:26:57 -04:00
|
|
|
t.Fatal("Failed to initialize a single node Erasure backend for admin handler tests.", err)
|
2017-04-07 02:08:33 -04:00
|
|
|
}
|
2020-03-02 19:29:30 -05:00
|
|
|
|
2017-04-07 02:08:33 -04:00
|
|
|
defer adminTestBed.TearDown()
|
|
|
|
|
|
|
|
// Initialize admin peers to make admin RPC calls.
|
2017-04-11 18:44:27 -04:00
|
|
|
globalMinioAddr = "127.0.0.1:9000"
|
2017-04-07 02:08:33 -04:00
|
|
|
|
|
|
|
// Prepare query params for set-config mgmt REST API.
|
|
|
|
queryVal := url.Values{}
|
|
|
|
queryVal.Set("info", "")
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
req, err := buildAdminRequest(queryVal, http.MethodGet, "/info", 0, nil)
|
2017-04-07 02:08:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to construct get-config object request - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2018-04-21 22:23:54 -04:00
|
|
|
adminTestBed.router.ServeHTTP(rec, req)
|
2017-04-07 02:08:33 -04:00
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Errorf("Expected to succeed but failed with %d", rec.Code)
|
|
|
|
}
|
|
|
|
|
2019-12-13 14:33:11 -05:00
|
|
|
results := madmin.InfoMessage{}
|
2017-04-21 10:15:53 -04:00
|
|
|
err = json.NewDecoder(rec.Body).Decode(&results)
|
2017-04-07 02:08:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to decode set config result json %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 14:33:11 -05:00
|
|
|
if results.Region != globalMinioDefaultRegion {
|
|
|
|
t.Errorf("Expected %s, got %s", globalMinioDefaultRegion, results.Region)
|
2017-04-07 02:08:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 04:25:52 -05:00
|
|
|
// TestToAdminAPIErrCode - test for toAdminAPIErrCode helper function.
|
|
|
|
func TestToAdminAPIErrCode(t *testing.T) {
|
2017-02-27 14:40:27 -05:00
|
|
|
testCases := []struct {
|
|
|
|
err error
|
|
|
|
expectedAPIErr APIErrorCode
|
|
|
|
}{
|
|
|
|
// 1. Server not in quorum.
|
|
|
|
{
|
2020-06-12 23:04:01 -04:00
|
|
|
err: errErasureWriteQuorum,
|
2017-02-27 14:40:27 -05:00
|
|
|
expectedAPIErr: ErrAdminConfigNoQuorum,
|
|
|
|
},
|
|
|
|
// 2. No error.
|
|
|
|
{
|
|
|
|
err: nil,
|
|
|
|
expectedAPIErr: ErrNone,
|
|
|
|
},
|
|
|
|
// 3. Non-admin API specific error.
|
|
|
|
{
|
|
|
|
err: errDiskNotFound,
|
2020-04-09 12:30:02 -04:00
|
|
|
expectedAPIErr: toAPIErrorCode(GlobalContext, errDiskNotFound),
|
2017-02-27 14:40:27 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
2020-04-09 12:30:02 -04:00
|
|
|
actualErr := toAdminAPIErrCode(GlobalContext, test.err)
|
2017-02-27 14:40:27 -05:00
|
|
|
if actualErr != test.expectedAPIErr {
|
|
|
|
t.Errorf("Test %d: Expected %v but received %v",
|
|
|
|
i+1, test.expectedAPIErr, actualErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 16:53:27 -04:00
|
|
|
|
|
|
|
func TestExtractHealInitParams(t *testing.T) {
|
|
|
|
mkParams := func(clientToken string, forceStart, forceStop bool) url.Values {
|
|
|
|
v := url.Values{}
|
|
|
|
if clientToken != "" {
|
2020-09-24 11:40:21 -04:00
|
|
|
v.Add(mgmtClientToken, clientToken)
|
2019-08-29 16:53:27 -04:00
|
|
|
}
|
|
|
|
if forceStart {
|
2020-09-24 11:40:21 -04:00
|
|
|
v.Add(mgmtForceStart, "")
|
2019-08-29 16:53:27 -04:00
|
|
|
}
|
|
|
|
if forceStop {
|
2020-09-24 11:40:21 -04:00
|
|
|
v.Add(mgmtForceStop, "")
|
2019-08-29 16:53:27 -04:00
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
qParmsArr := []url.Values{
|
|
|
|
// Invalid cases
|
|
|
|
mkParams("", true, true),
|
|
|
|
mkParams("111", true, true),
|
|
|
|
mkParams("111", true, false),
|
|
|
|
mkParams("111", false, true),
|
|
|
|
// Valid cases follow
|
|
|
|
mkParams("", true, false),
|
|
|
|
mkParams("", false, true),
|
|
|
|
mkParams("", false, false),
|
|
|
|
mkParams("111", false, false),
|
|
|
|
}
|
|
|
|
varsArr := []map[string]string{
|
|
|
|
// Invalid cases
|
2020-09-24 11:40:21 -04:00
|
|
|
{mgmtPrefix: "objprefix"},
|
2019-08-29 16:53:27 -04:00
|
|
|
// Valid cases
|
|
|
|
{},
|
2020-09-24 11:40:21 -04:00
|
|
|
{mgmtBucket: "bucket"},
|
|
|
|
{mgmtBucket: "bucket", mgmtPrefix: "objprefix"},
|
2019-08-29 16:53:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Body is always valid - we do not test JSON decoding.
|
|
|
|
body := `{"recursive": false, "dryRun": true, "remove": false, "scanMode": 0}`
|
|
|
|
|
|
|
|
// Test all combinations!
|
|
|
|
for pIdx, parms := range qParmsArr {
|
|
|
|
for vIdx, vars := range varsArr {
|
2020-12-27 01:58:06 -05:00
|
|
|
_, err := extractHealInitParams(vars, parms, bytes.NewReader([]byte(body)))
|
2019-08-29 16:53:27 -04:00
|
|
|
isErrCase := false
|
|
|
|
if pIdx < 4 || vIdx < 1 {
|
|
|
|
isErrCase = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != ErrNone && !isErrCase {
|
|
|
|
t.Errorf("Got unexpected error: %v %v %v", pIdx, vIdx, err)
|
|
|
|
} else if err == ErrNone && isErrCase {
|
|
|
|
t.Errorf("Got no error but expected one: %v %v", pIdx, vIdx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 21:35:29 -04:00
|
|
|
|
|
|
|
type byResourceUID struct{ madmin.LockEntries }
|
|
|
|
|
|
|
|
func (b byResourceUID) Less(i, j int) bool {
|
|
|
|
toUniqLock := func(entry madmin.LockEntry) string {
|
|
|
|
return fmt.Sprintf("%s/%s", entry.Resource, entry.ID)
|
|
|
|
}
|
|
|
|
return toUniqLock(b.LockEntries[i]) < toUniqLock(b.LockEntries[j])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopLockEntries(t *testing.T) {
|
|
|
|
locksHeld := make(map[string][]lockRequesterInfo)
|
|
|
|
var owners []string
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
owners = append(owners, fmt.Sprintf("node-%d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate DeleteObjects of 10 objects in a single request. i.e same lock
|
|
|
|
// request UID, but 10 different resource names associated with it.
|
|
|
|
var lris []lockRequesterInfo
|
|
|
|
uuid := mustGetUUID()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
resource := fmt.Sprintf("bucket/delete-object-%d", i)
|
|
|
|
lri := lockRequesterInfo{
|
|
|
|
Name: resource,
|
|
|
|
Writer: true,
|
|
|
|
UID: uuid,
|
|
|
|
Owner: owners[i%len(owners)],
|
|
|
|
Group: true,
|
|
|
|
Quorum: 3,
|
|
|
|
}
|
|
|
|
lris = append(lris, lri)
|
|
|
|
locksHeld[resource] = []lockRequesterInfo{lri}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a few concurrent read locks to the mix
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
resource := fmt.Sprintf("bucket/get-object-%d", i)
|
|
|
|
lri := lockRequesterInfo{
|
|
|
|
Name: resource,
|
|
|
|
UID: mustGetUUID(),
|
|
|
|
Owner: owners[i%len(owners)],
|
|
|
|
Quorum: 2,
|
|
|
|
}
|
|
|
|
lris = append(lris, lri)
|
|
|
|
locksHeld[resource] = append(locksHeld[resource], lri)
|
|
|
|
// concurrent read lock, same resource different uid
|
|
|
|
lri.UID = mustGetUUID()
|
|
|
|
lris = append(lris, lri)
|
|
|
|
locksHeld[resource] = append(locksHeld[resource], lri)
|
|
|
|
}
|
|
|
|
|
|
|
|
var peerLocks []*PeerLocks
|
|
|
|
for _, owner := range owners {
|
|
|
|
peerLocks = append(peerLocks, &PeerLocks{
|
|
|
|
Addr: owner,
|
|
|
|
Locks: locksHeld,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var exp madmin.LockEntries
|
|
|
|
for _, lri := range lris {
|
|
|
|
lockType := func(lri lockRequesterInfo) string {
|
|
|
|
if lri.Writer {
|
|
|
|
return "WRITE"
|
|
|
|
}
|
|
|
|
return "READ"
|
|
|
|
}
|
|
|
|
exp = append(exp, madmin.LockEntry{
|
|
|
|
Resource: lri.Name,
|
|
|
|
Type: lockType(lri),
|
|
|
|
ServerList: owners,
|
|
|
|
Owner: lri.Owner,
|
|
|
|
ID: lri.UID,
|
|
|
|
Quorum: lri.Quorum,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
peerLocks []*PeerLocks
|
|
|
|
expected madmin.LockEntries
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
peerLocks: peerLocks,
|
|
|
|
expected: exp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// printEntries := func(entries madmin.LockEntries) {
|
|
|
|
// for i, entry := range entries {
|
|
|
|
// fmt.Printf("%d: %s %s %s %s %v %d\n", i, entry.Resource, entry.ID, entry.Owner, entry.Type, entry.ServerList, entry.Elapsed)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
check := func(exp, got madmin.LockEntries) (int, bool) {
|
|
|
|
if len(exp) != len(got) {
|
|
|
|
return 0, false
|
|
|
|
}
|
2023-04-24 16:28:18 -04:00
|
|
|
sort.Slice(exp, byResourceUID{exp}.Less)
|
|
|
|
sort.Slice(got, byResourceUID{got}.Less)
|
2023-03-21 21:35:29 -04:00
|
|
|
// printEntries(exp)
|
|
|
|
// printEntries(got)
|
|
|
|
for i, e := range exp {
|
|
|
|
if !e.Timestamp.Equal(got[i].Timestamp) {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
// Skip checking elapsed since it's time sensitive.
|
|
|
|
// if e.Elapsed != got[i].Elapsed {
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
if e.Resource != got[i].Resource {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
if e.Type != got[i].Type {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
if e.Source != got[i].Source {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
if e.Owner != got[i].Owner {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
if e.ID != got[i].ID {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
if len(e.ServerList) != len(got[i].ServerList) {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
for j := range e.ServerList {
|
|
|
|
if e.ServerList[j] != got[i].ServerList[j] {
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, true
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
got := topLockEntries(tc.peerLocks, false)
|
|
|
|
if idx, ok := check(tc.expected, got); !ok {
|
|
|
|
t.Fatalf("%d: mismatch at %d \n expected %#v but got %#v", i, idx, tc.expected[idx], got[idx])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|