2016-12-16 01:26:15 -05:00
|
|
|
/*
|
2019-08-27 14:37:47 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016-2019 MinIO, Inc.
|
2016-12-16 01:26:15 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-01-17 13:02:58 -05:00
|
|
|
"bytes"
|
2018-03-15 16:27:16 -04:00
|
|
|
"context"
|
2016-12-16 01:26:15 -05:00
|
|
|
"encoding/json"
|
2017-03-17 12:25:49 -04:00
|
|
|
"io"
|
2017-01-17 17:25:59 -05:00
|
|
|
"io/ioutil"
|
2016-12-16 01:26:15 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-01-04 02:39:22 -05:00
|
|
|
"net/url"
|
2018-01-11 02:06:36 -05:00
|
|
|
"strings"
|
2018-09-27 20:16:30 -04:00
|
|
|
"sync"
|
2016-12-16 01:26:15 -05:00
|
|
|
"testing"
|
2019-08-29 16:53:27 -04:00
|
|
|
"time"
|
2016-12-16 01:26:15 -05:00
|
|
|
|
2018-04-21 22:23:54 -04:00
|
|
|
"github.com/gorilla/mux"
|
2017-10-31 14:54:32 -04:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2018-01-22 17:54:55 -05:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
2016-12-16 01:26:15 -05:00
|
|
|
)
|
|
|
|
|
2018-01-11 02:06:36 -05:00
|
|
|
var (
|
|
|
|
configJSON = []byte(`{
|
2018-11-30 00:16:17 -05:00
|
|
|
"version": "33",
|
2018-10-09 17:00:01 -04:00
|
|
|
"credential": {
|
|
|
|
"accessKey": "minio",
|
|
|
|
"secretKey": "minio123"
|
|
|
|
},
|
|
|
|
"region": "us-east-1",
|
|
|
|
"worm": "off",
|
|
|
|
"storageclass": {
|
|
|
|
"standard": "",
|
|
|
|
"rrs": ""
|
|
|
|
},
|
|
|
|
"cache": {
|
|
|
|
"drives": [],
|
|
|
|
"expiry": 90,
|
|
|
|
"maxuse": 80,
|
|
|
|
"exclude": []
|
|
|
|
},
|
|
|
|
"kms": {
|
|
|
|
"vault": {
|
|
|
|
"endpoint": "",
|
|
|
|
"auth": {
|
|
|
|
"type": "",
|
|
|
|
"approle": {
|
|
|
|
"id": "",
|
|
|
|
"secret": ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"key-id": {
|
|
|
|
"name": "",
|
|
|
|
"version": 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"notify": {
|
|
|
|
"amqp": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"url": "",
|
|
|
|
"exchange": "",
|
|
|
|
"routingKey": "",
|
|
|
|
"exchangeType": "",
|
|
|
|
"deliveryMode": 0,
|
|
|
|
"mandatory": false,
|
|
|
|
"immediate": false,
|
|
|
|
"durable": false,
|
|
|
|
"internal": false,
|
|
|
|
"noWait": false,
|
2019-07-25 14:20:24 -04:00
|
|
|
"autoDeleted": false,
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"elasticsearch": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"format": "namespace",
|
|
|
|
"url": "",
|
2019-07-11 22:53:20 -04:00
|
|
|
"index": "",
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"kafka": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"brokers": null,
|
|
|
|
"topic": "",
|
2019-05-29 16:19:48 -04:00
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0,
|
2018-10-09 17:00:01 -04:00
|
|
|
"tls": {
|
|
|
|
"enable": false,
|
|
|
|
"skipVerify": false,
|
|
|
|
"clientAuth": 0
|
|
|
|
},
|
|
|
|
"sasl": {
|
|
|
|
"enable": false,
|
|
|
|
"username": "",
|
|
|
|
"password": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mqtt": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"broker": "",
|
|
|
|
"topic": "",
|
|
|
|
"qos": 0,
|
|
|
|
"username": "",
|
|
|
|
"password": "",
|
|
|
|
"reconnectInterval": 0,
|
2019-01-14 02:09:00 -05:00
|
|
|
"keepAliveInterval": 0,
|
2019-02-25 21:01:13 -05:00
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"mysql": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"format": "namespace",
|
|
|
|
"dsnString": "",
|
|
|
|
"table": "",
|
|
|
|
"host": "",
|
|
|
|
"port": "",
|
|
|
|
"user": "",
|
|
|
|
"password": "",
|
2019-07-24 13:18:29 -04:00
|
|
|
"database": "",
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nats": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"address": "",
|
|
|
|
"subject": "",
|
|
|
|
"username": "",
|
|
|
|
"password": "",
|
|
|
|
"token": "",
|
|
|
|
"secure": false,
|
|
|
|
"pingInterval": 0,
|
2019-07-23 13:37:25 -04:00
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0,
|
2018-10-09 17:00:01 -04:00
|
|
|
"streaming": {
|
|
|
|
"enable": false,
|
|
|
|
"clusterID": "",
|
|
|
|
"async": false,
|
|
|
|
"maxPubAcksInflight": 0
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 13:23:13 -05:00
|
|
|
},
|
|
|
|
"nsq": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"nsqdAddress": "",
|
|
|
|
"topic": "",
|
|
|
|
"tls": {
|
|
|
|
"enable": false,
|
|
|
|
"skipVerify": false
|
2019-07-12 05:41:57 -04:00
|
|
|
},
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-11-07 13:23:13 -05:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
},
|
|
|
|
"postgresql": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"format": "namespace",
|
|
|
|
"connectionString": "",
|
|
|
|
"table": "",
|
|
|
|
"host": "",
|
|
|
|
"port": "",
|
|
|
|
"user": "",
|
|
|
|
"password": "",
|
2019-07-24 13:18:29 -04:00
|
|
|
"database": "",
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"redis": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
|
|
|
"format": "namespace",
|
|
|
|
"address": "",
|
|
|
|
"password": "",
|
2019-07-23 13:22:08 -04:00
|
|
|
"key": "",
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"webhook": {
|
|
|
|
"1": {
|
|
|
|
"enable": false,
|
2019-07-05 05:51:41 -04:00
|
|
|
"endpoint": "",
|
|
|
|
"queueDir": "",
|
|
|
|
"queueLimit": 0
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"logger": {
|
|
|
|
"console": {
|
|
|
|
"enabled": true
|
|
|
|
},
|
|
|
|
"http": {
|
|
|
|
"1": {
|
|
|
|
"enabled": false,
|
|
|
|
"endpoint": "https://username:password@example.com/api"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"compress": {
|
|
|
|
"enabled": false,
|
|
|
|
"extensions":[".txt",".log",".csv",".json"],
|
|
|
|
"mime-types":["text/csv","text/plain","application/json"]
|
|
|
|
},
|
|
|
|
"openid": {
|
|
|
|
"jwks": {
|
|
|
|
"url": ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"policy": {
|
|
|
|
"opa": {
|
|
|
|
"url": "",
|
|
|
|
"authToken": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
2018-01-11 02:06:36 -05:00
|
|
|
)
|
2017-02-27 14:40:27 -05:00
|
|
|
|
2017-01-23 03:32:55 -05:00
|
|
|
// adminXLTestBed - encapsulates subsystems that need to be setup for
|
|
|
|
// admin-handler unit tests.
|
|
|
|
type adminXLTestBed struct {
|
2019-02-13 07:59:36 -05:00
|
|
|
xlDirs []string
|
|
|
|
objLayer ObjectLayer
|
|
|
|
router *mux.Router
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareAdminXLTestBed - helper function that setups a single-node
|
|
|
|
// XL backend for admin-handler tests.
|
|
|
|
func prepareAdminXLTestBed() (*adminXLTestBed, error) {
|
|
|
|
// reset global variables to start afresh.
|
|
|
|
resetTestGlobals()
|
2017-02-07 15:51:43 -05:00
|
|
|
|
2017-01-23 03:32:55 -05:00
|
|
|
// Initializing objectLayer for HealFormatHandler.
|
|
|
|
objLayer, xlDirs, xlErr := initTestXLObjLayer()
|
|
|
|
if xlErr != nil {
|
|
|
|
return nil, xlErr
|
|
|
|
}
|
|
|
|
|
2018-08-15 00:41:47 -04:00
|
|
|
// Initialize minio server config.
|
|
|
|
if err := newTestConfig(globalMinioDefaultRegion, objLayer); err != nil {
|
|
|
|
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
|
|
|
|
2017-04-11 18:44:27 -04:00
|
|
|
globalEndpoints = mustGetNewEndpointList(xlDirs...)
|
2017-01-23 03:32:55 -05:00
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Set globalIsXL to indicate that the setup uses an erasure
|
|
|
|
// code backend.
|
2017-01-23 03:32:55 -05:00
|
|
|
globalIsXL = true
|
|
|
|
|
|
|
|
// initialize NSLock.
|
|
|
|
isDistXL := false
|
|
|
|
initNSLock(isDistXL)
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Init global heal state
|
2019-06-09 01:14:07 -04:00
|
|
|
if globalIsXL {
|
|
|
|
globalAllHealState = initHealState()
|
|
|
|
}
|
2018-01-22 17:54:55 -05:00
|
|
|
|
2019-02-12 04:25:52 -05:00
|
|
|
globalConfigSys = NewConfigSys()
|
|
|
|
|
|
|
|
globalIAMSys = NewIAMSys()
|
|
|
|
globalIAMSys.Init(objLayer)
|
2018-03-15 16:03:41 -04:00
|
|
|
|
2019-10-01 20:05:02 -04:00
|
|
|
buckets, err := objLayer.ListBuckets(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
globalPolicySys = NewPolicySys()
|
2019-10-01 20:05:02 -04:00
|
|
|
globalPolicySys.Init(buckets, objLayer)
|
2019-02-12 04:25:52 -05:00
|
|
|
|
|
|
|
globalNotificationSys = NewNotificationSys(globalServerConfig, globalEndpoints)
|
2019-10-01 20:05:02 -04:00
|
|
|
globalNotificationSys.Init(buckets, objLayer)
|
2018-04-24 18:53:30 -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()
|
2019-01-23 14:10:59 -05:00
|
|
|
registerAdminRouter(adminRouter, true, true)
|
2017-01-23 03:32:55 -05:00
|
|
|
|
|
|
|
return &adminXLTestBed{
|
2018-08-15 00:41:47 -04:00
|
|
|
xlDirs: xlDirs,
|
|
|
|
objLayer: objLayer,
|
|
|
|
router: adminRouter,
|
2017-01-23 03:32:55 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDown - method that resets the test bed for subsequent unit
|
|
|
|
// tests to start afresh.
|
|
|
|
func (atb *adminXLTestBed) TearDown() {
|
|
|
|
removeRoots(atb.xlDirs)
|
|
|
|
resetTestGlobals()
|
|
|
|
}
|
|
|
|
|
|
|
|
// initTestObjLayer - Helper function to initialize an XL-based object
|
|
|
|
// layer and set globalObjectAPI.
|
|
|
|
func initTestXLObjLayer() (ObjectLayer, []string, error) {
|
2018-02-15 20:45:57 -05:00
|
|
|
xlDirs, err := getRandomDisks(16)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
endpoints := mustGetNewEndpointList(xlDirs...)
|
2019-10-15 21:35:41 -04:00
|
|
|
format, err := waitForFormatXL(true, endpoints, 1, 16)
|
2018-02-15 20:45:57 -05:00
|
|
|
if err != nil {
|
|
|
|
removeRoots(xlDirs)
|
|
|
|
return nil, nil, err
|
2017-01-23 03:32:55 -05:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
|
2018-06-06 15:52:56 -04:00
|
|
|
globalPolicySys = NewPolicySys()
|
2018-02-15 20:45:57 -05:00
|
|
|
objLayer, err := newXLSets(endpoints, format, 1, 16)
|
|
|
|
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()
|
|
|
|
return objLayer, xlDirs, nil
|
|
|
|
}
|
|
|
|
|
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()))
|
|
|
|
resource := "/minio/admin/v1/service?" + queryVal.Encode()
|
|
|
|
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) {
|
2017-01-23 03:32:55 -05:00
|
|
|
adminTestBed, err := prepareAdminXLTestBed()
|
2016-12-16 01:26:15 -05:00
|
|
|
if err != nil {
|
2017-01-23 03:32:55 -05:00
|
|
|
t.Fatal("Failed to initialize a single node XL backend for admin handler tests.")
|
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
|
|
|
}
|
2017-11-29 16:12:47 -05:00
|
|
|
credentials := globalServerConfig.GetCredential()
|
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 {
|
|
|
|
resp, _ := ioutil.ReadAll(rec.Body)
|
|
|
|
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,
|
2017-03-17 12:25:49 -04:00
|
|
|
contentLength int64, bodySeeker io.ReadSeeker) (*http.Request, error) {
|
2018-01-22 17:54:55 -05:00
|
|
|
|
|
|
|
req, err := newTestRequest(method,
|
|
|
|
"/minio/admin/v1"+path+"?"+queryVal.Encode(),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-29 16:12:47 -05:00
|
|
|
cred := globalServerConfig.GetCredential()
|
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-02-20 15:58:50 -05:00
|
|
|
// TestGetConfigHandler - test for GetConfigHandler.
|
|
|
|
func TestGetConfigHandler(t *testing.T) {
|
|
|
|
adminTestBed, err := prepareAdminXLTestBed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to initialize a single node XL backend for admin handler tests.")
|
|
|
|
}
|
|
|
|
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-02-20 15:58:50 -05:00
|
|
|
|
|
|
|
// Prepare query params for get-config mgmt REST API.
|
|
|
|
queryVal := url.Values{}
|
|
|
|
queryVal.Set("config", "")
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
req, err := buildAdminRequest(queryVal, http.MethodGet, "/config", 0, nil)
|
2017-02-20 15:58:50 -05: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-02-20 15:58:50 -05:00
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Errorf("Expected to succeed but failed with %d", rec.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-02-27 14:40:27 -05:00
|
|
|
|
|
|
|
// TestSetConfigHandler - test for SetConfigHandler.
|
|
|
|
func TestSetConfigHandler(t *testing.T) {
|
|
|
|
adminTestBed, err := prepareAdminXLTestBed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to initialize a single node XL backend for admin handler tests.")
|
|
|
|
}
|
|
|
|
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-02-27 14:40:27 -05:00
|
|
|
|
|
|
|
// Prepare query params for set-config mgmt REST API.
|
|
|
|
queryVal := url.Values{}
|
|
|
|
queryVal.Set("config", "")
|
|
|
|
|
2018-08-15 00:41:47 -04:00
|
|
|
password := globalServerConfig.GetCredential().SecretKey
|
2018-10-09 17:00:01 -04:00
|
|
|
econfigJSON, err := madmin.EncryptData(password, configJSON)
|
2018-08-15 00:41:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
req, err := buildAdminRequest(queryVal, http.MethodPut, "/config",
|
2018-08-15 00:41:47 -04:00
|
|
|
int64(len(econfigJSON)), bytes.NewReader(econfigJSON))
|
2017-02-27 14:40:27 -05:00
|
|
|
if err != nil {
|
2018-01-11 02:06:36 -05:00
|
|
|
t.Fatalf("Failed to construct set-config object request - %v", err)
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2018-04-21 22:23:54 -04:00
|
|
|
adminTestBed.router.ServeHTTP(rec, req)
|
2017-02-27 14:40:27 -05:00
|
|
|
if rec.Code != http.StatusOK {
|
2018-11-07 13:23:13 -05:00
|
|
|
t.Errorf("Expected to succeed but failed with %d, body: %s", rec.Code, rec.Body)
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
|
2018-01-11 02:06:36 -05:00
|
|
|
// Check that a very large config file returns an error.
|
|
|
|
{
|
|
|
|
// Make a large enough config string
|
2018-10-09 17:00:01 -04:00
|
|
|
invalidCfg := []byte(strings.Repeat("A", maxEConfigJSONSize+1))
|
2018-01-22 17:54:55 -05:00
|
|
|
req, err := buildAdminRequest(queryVal, http.MethodPut, "/config",
|
|
|
|
int64(len(invalidCfg)), bytes.NewReader(invalidCfg))
|
2018-01-11 02:06:36 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to construct set-config object request - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2018-04-21 22:23:54 -04:00
|
|
|
adminTestBed.router.ServeHTTP(rec, req)
|
2019-02-13 07:59:36 -05:00
|
|
|
respBody := rec.Body.String()
|
2018-01-11 02:06:36 -05:00
|
|
|
if rec.Code != http.StatusBadRequest ||
|
|
|
|
!strings.Contains(respBody, "Configuration data provided exceeds the allowed maximum of") {
|
|
|
|
t.Errorf("Got unexpected response code or body %d - %s", rec.Code, respBody)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that a config with duplicate keys in an object return
|
|
|
|
// error.
|
|
|
|
{
|
2018-08-15 00:41:47 -04:00
|
|
|
invalidCfg := append(econfigJSON[:len(econfigJSON)-1], []byte(`, "version": "15"}`)...)
|
2018-01-22 17:54:55 -05:00
|
|
|
req, err := buildAdminRequest(queryVal, http.MethodPut, "/config",
|
|
|
|
int64(len(invalidCfg)), bytes.NewReader(invalidCfg))
|
2018-01-11 02:06:36 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to construct set-config object request - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2018-04-21 22:23:54 -04:00
|
|
|
adminTestBed.router.ServeHTTP(rec, req)
|
2019-02-13 07:59:36 -05:00
|
|
|
respBody := rec.Body.String()
|
2018-01-11 02:06:36 -05:00
|
|
|
if rec.Code != http.StatusBadRequest ||
|
2018-10-09 17:00:01 -04:00
|
|
|
!strings.Contains(respBody, "JSON configuration provided is of incorrect format") {
|
2018-01-11 02:06:36 -05:00
|
|
|
t.Errorf("Got unexpected response code or body %d - %s", rec.Code, respBody)
|
|
|
|
}
|
|
|
|
}
|
2017-02-27 14:40:27 -05:00
|
|
|
}
|
|
|
|
|
2017-04-07 02:08:33 -04:00
|
|
|
func TestAdminServerInfo(t *testing.T) {
|
|
|
|
adminTestBed, err := prepareAdminXLTestBed()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to initialize a single node XL backend for admin handler tests.")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
results := []ServerInfo{}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:15:53 -04:00
|
|
|
if len(results) == 0 {
|
|
|
|
t.Error("Expected at least one server info result")
|
2017-04-07 02:08:33 -04:00
|
|
|
}
|
2017-04-21 10:15:53 -04:00
|
|
|
|
|
|
|
for _, serverInfo := range results {
|
2017-05-15 10:28:47 -04:00
|
|
|
if serverInfo.Error != "" {
|
2017-04-21 10:15:53 -04:00
|
|
|
t.Errorf("Unexpected error = %v\n", serverInfo.Error)
|
|
|
|
}
|
|
|
|
if serverInfo.Data.Properties.Region != globalMinioDefaultRegion {
|
|
|
|
t.Errorf("Expected %s, got %s", globalMinioDefaultRegion, serverInfo.Data.Properties.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.
|
|
|
|
{
|
|
|
|
err: errXLWriteQuorum,
|
|
|
|
expectedAPIErr: ErrAdminConfigNoQuorum,
|
|
|
|
},
|
|
|
|
// 2. No error.
|
|
|
|
{
|
|
|
|
err: nil,
|
|
|
|
expectedAPIErr: ErrNone,
|
|
|
|
},
|
|
|
|
// 3. Non-admin API specific error.
|
|
|
|
{
|
|
|
|
err: errDiskNotFound,
|
2018-11-12 14:07:43 -05:00
|
|
|
expectedAPIErr: toAPIErrorCode(context.Background(), errDiskNotFound),
|
2017-02-27 14:40:27 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
2018-11-12 14:07:43 -05:00
|
|
|
actualErr := toAdminAPIErrCode(context.Background(), 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 TestTopLockEntries(t *testing.T) {
|
|
|
|
t1 := UTCNow()
|
|
|
|
t2 := UTCNow().Add(10 * time.Second)
|
|
|
|
peerLocks := []*PeerLocks{
|
|
|
|
{
|
|
|
|
Addr: "1",
|
|
|
|
Locks: map[string][]lockRequesterInfo{
|
|
|
|
"1": {
|
|
|
|
{false, "node2", "ep2", "2", t2, t2, ""},
|
|
|
|
{true, "node1", "ep1", "1", t1, t1, ""},
|
|
|
|
},
|
|
|
|
"2": {
|
|
|
|
{false, "node2", "ep2", "2", t2, t2, ""},
|
|
|
|
{true, "node1", "ep1", "1", t1, t1, ""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addr: "2",
|
|
|
|
Locks: map[string][]lockRequesterInfo{
|
|
|
|
"1": {
|
|
|
|
{false, "node2", "ep2", "2", t2, t2, ""},
|
|
|
|
{true, "node1", "ep1", "1", t1, t1, ""},
|
|
|
|
},
|
|
|
|
"2": {
|
|
|
|
{false, "node2", "ep2", "2", t2, t2, ""},
|
|
|
|
{true, "node1", "ep1", "1", t1, t1, ""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
les := topLockEntries(peerLocks)
|
|
|
|
if len(les) != 2 {
|
|
|
|
t.Fatalf("Did not get 2 results")
|
|
|
|
}
|
|
|
|
if les[0].Timestamp.After(les[1].Timestamp) {
|
|
|
|
t.Fatalf("Got wrong sorted value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractHealInitParams(t *testing.T) {
|
|
|
|
mkParams := func(clientToken string, forceStart, forceStop bool) url.Values {
|
|
|
|
v := url.Values{}
|
|
|
|
if clientToken != "" {
|
|
|
|
v.Add(string(mgmtClientToken), clientToken)
|
|
|
|
}
|
|
|
|
if forceStart {
|
|
|
|
v.Add(string(mgmtForceStart), "")
|
|
|
|
}
|
|
|
|
if forceStop {
|
|
|
|
v.Add(string(mgmtForceStop), "")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{string(mgmtPrefix): "objprefix"},
|
|
|
|
// Valid cases
|
|
|
|
{},
|
|
|
|
{string(mgmtBucket): "bucket"},
|
|
|
|
{string(mgmtBucket): "bucket", string(mgmtPrefix): "objprefix"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
_, err := extractHealInitParams(vars, parms, bytes.NewBuffer([]byte(body)))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|