2016-12-16 01:26:15 -05:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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 (
|
|
|
|
"encoding/json"
|
2017-01-04 02:39:22 -05:00
|
|
|
"fmt"
|
2016-12-16 01:26:15 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-01-04 02:39:22 -05:00
|
|
|
"net/url"
|
2016-12-16 01:26:15 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
router "github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
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 (
|
|
|
|
statusCmd cmdType = iota
|
|
|
|
stopCmd
|
|
|
|
restartCmd
|
|
|
|
)
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// String - String representation for cmdType
|
2016-12-16 01:26:15 -05:00
|
|
|
func (c cmdType) String() string {
|
|
|
|
switch c {
|
|
|
|
case statusCmd:
|
|
|
|
return "status"
|
|
|
|
case stopCmd:
|
|
|
|
return "stop"
|
|
|
|
case restartCmd:
|
|
|
|
return "restart"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// apiMethod - Returns the HTTP method corresponding to the admin REST
|
|
|
|
// API for a given cmdType value.
|
2016-12-16 01:26:15 -05:00
|
|
|
func (c cmdType) apiMethod() string {
|
|
|
|
switch c {
|
|
|
|
case statusCmd:
|
|
|
|
return "GET"
|
|
|
|
case restartCmd:
|
|
|
|
return "POST"
|
|
|
|
}
|
|
|
|
return "GET"
|
|
|
|
}
|
|
|
|
|
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 statusCmd:
|
|
|
|
return serviceStatus
|
|
|
|
case restartCmd:
|
|
|
|
return serviceRestart
|
|
|
|
}
|
|
|
|
return serviceStatus
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func getServiceCmdRequest(cmd cmdType, cred credential) (*http.Request, error) {
|
2016-12-16 01:26:15 -05:00
|
|
|
req, err := newTestRequest(cmd.apiMethod(), "/?service", 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-04 02:39:22 -05:00
|
|
|
|
|
|
|
// minioAdminOpHeader is to identify the request as a
|
|
|
|
// management REST API request.
|
2016-12-16 01:26:15 -05:00
|
|
|
req.Header.Set(minioAdminOpHeader, cmd.String())
|
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.
|
2016-12-16 01:26:15 -05:00
|
|
|
func testServicesCmdHandler(cmd cmdType, t *testing.T) {
|
2017-01-07 14:27:01 -05:00
|
|
|
// reset globals.
|
|
|
|
// this is to make sure that the tests are not affected by modified value.
|
|
|
|
resetTestGlobals()
|
|
|
|
// initialize NSLock.
|
|
|
|
initNSLock(false)
|
2017-01-04 02:39:22 -05:00
|
|
|
// Initialize configuration for access/secret credentials.
|
2016-12-16 01:26:15 -05:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize server config. %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
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.
|
2016-12-16 01:26:15 -05:00
|
|
|
eps, err := parseStorageEndpoints([]string{"http://localhost"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse storage end point - %v", err)
|
|
|
|
}
|
2017-01-04 02:39:22 -05:00
|
|
|
|
|
|
|
// Set globalMinioAddr to be able to distinguish local endpoints from remote.
|
|
|
|
globalMinioAddr = eps[0].Host
|
2016-12-16 01:26:15 -05:00
|
|
|
initGlobalAdminPeers(eps)
|
|
|
|
|
|
|
|
if cmd == statusCmd {
|
|
|
|
// Initializing objectLayer and corresponding
|
|
|
|
// []StorageAPI since DiskInfo() method requires it.
|
2017-01-16 20:05:00 -05:00
|
|
|
objLayer, fsDirs, fsErr := prepareXL()
|
2016-12-16 01:26:15 -05:00
|
|
|
if fsErr != nil {
|
|
|
|
t.Fatalf("failed to initialize XL based object layer - %v.", fsErr)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
defer removeRoots(fsDirs)
|
2016-12-16 01:26:15 -05:00
|
|
|
globalObjLayerMutex.Lock()
|
|
|
|
globalObjectAPI = objLayer
|
|
|
|
globalObjLayerMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting up a go routine to simulate ServerMux's
|
|
|
|
// handleServiceSignals for stop and restart commands.
|
|
|
|
switch cmd {
|
|
|
|
case stopCmd, restartCmd:
|
|
|
|
go testServiceSignalReceiver(cmd, t)
|
|
|
|
}
|
|
|
|
credentials := serverConfig.GetCredential()
|
|
|
|
adminRouter := router.NewRouter()
|
|
|
|
registerAdminRouter(adminRouter)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
2017-01-04 02:39:22 -05: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)
|
|
|
|
}
|
|
|
|
adminRouter.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
if cmd == statusCmd {
|
|
|
|
expectedInfo := newObjectLayerFn().StorageInfo()
|
|
|
|
receivedInfo := StorageInfo{}
|
|
|
|
if jsonErr := json.Unmarshal(rec.Body.Bytes(), &receivedInfo); jsonErr != nil {
|
|
|
|
t.Errorf("Failed to unmarshal StorageInfo - %v", jsonErr)
|
|
|
|
}
|
|
|
|
if expectedInfo != receivedInfo {
|
|
|
|
t.Errorf("Expected storage info and received storage info differ, %v %v", expectedInfo, receivedInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Errorf("Expected to receive %d status code but received %d",
|
|
|
|
http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
// Test for service status management REST API.
|
2016-12-16 01:26:15 -05:00
|
|
|
func TestServiceStatusHandler(t *testing.T) {
|
|
|
|
testServicesCmdHandler(statusCmd, t)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
testServicesCmdHandler(restartCmd, t)
|
|
|
|
}
|
2017-01-04 02:39:22 -05:00
|
|
|
|
|
|
|
// Test for locks list management REST API.
|
|
|
|
func TestListLocksHandler(t *testing.T) {
|
2017-01-07 14:27:01 -05:00
|
|
|
// reset globals.
|
|
|
|
// this is to make sure that the tests are not affected by modified globals.
|
|
|
|
resetTestGlobals()
|
|
|
|
// initialize NSLock.
|
|
|
|
initNSLock(false)
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize server config. %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
// Initialize admin peers to make admin RPC calls.
|
|
|
|
eps, err := parseStorageEndpoints([]string{"http://localhost"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse storage end point - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set globalMinioAddr to be able to distinguish local endpoints from remote.
|
|
|
|
globalMinioAddr = eps[0].Host
|
|
|
|
initGlobalAdminPeers(eps)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
bucket string
|
|
|
|
prefix string
|
|
|
|
relTime string
|
|
|
|
expectedStatus int
|
|
|
|
}{
|
|
|
|
// Test 1 - valid testcase
|
|
|
|
{
|
|
|
|
bucket: "mybucket",
|
|
|
|
prefix: "myobject",
|
|
|
|
relTime: "1s",
|
|
|
|
expectedStatus: 200,
|
|
|
|
},
|
|
|
|
// Test 2 - invalid duration
|
|
|
|
{
|
|
|
|
bucket: "mybucket",
|
|
|
|
prefix: "myprefix",
|
|
|
|
relTime: "invalidDuration",
|
|
|
|
expectedStatus: 400,
|
|
|
|
},
|
|
|
|
// Test 3 - invalid bucket name
|
|
|
|
{
|
|
|
|
bucket: `invalid\\Bucket`,
|
|
|
|
prefix: "myprefix",
|
|
|
|
relTime: "1h",
|
|
|
|
expectedStatus: 400,
|
|
|
|
},
|
|
|
|
// Test 4 - invalid prefix
|
|
|
|
{
|
|
|
|
bucket: "mybucket",
|
|
|
|
prefix: `invalid\\Prefix`,
|
|
|
|
relTime: "1h",
|
|
|
|
expectedStatus: 400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
adminRouter := router.NewRouter()
|
|
|
|
registerAdminRouter(adminRouter)
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
|
|
|
queryStr := fmt.Sprintf("&bucket=%s&prefix=%s&older-than=%s", test.bucket, test.prefix, test.relTime)
|
|
|
|
req, err := newTestRequest("GET", "/?lock"+queryStr, 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d - Failed to construct list locks request - %v", i+1, err)
|
|
|
|
}
|
|
|
|
req.Header.Set(minioAdminOpHeader, "list")
|
|
|
|
|
|
|
|
cred := serverConfig.GetCredential()
|
|
|
|
err = signRequestV4(req, cred.AccessKey, cred.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d - Failed to sign list locks request - %v", i+1, err)
|
|
|
|
}
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
adminRouter.ServeHTTP(rec, req)
|
|
|
|
if test.expectedStatus != rec.Code {
|
|
|
|
t.Errorf("Test %d - Expected HTTP status code %d but received %d", i+1, test.expectedStatus, rec.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for locks clear management REST API.
|
|
|
|
func TestClearLocksHandler(t *testing.T) {
|
2017-01-07 14:27:01 -05:00
|
|
|
// reset globals.
|
|
|
|
// this is to make sure that the tests are not affected by modified globals.
|
|
|
|
resetTestGlobals()
|
|
|
|
// initialize NSLock.
|
|
|
|
initNSLock(false)
|
|
|
|
|
2017-01-04 02:39:22 -05:00
|
|
|
rootPath, err := newTestConfig("us-east-1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to initialize server config. %s", err)
|
|
|
|
}
|
|
|
|
defer removeAll(rootPath)
|
|
|
|
|
|
|
|
// Initialize admin peers to make admin RPC calls.
|
|
|
|
eps, err := parseStorageEndpoints([]string{"http://localhost"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse storage end point - %v", err)
|
|
|
|
}
|
|
|
|
initGlobalAdminPeers(eps)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
bucket string
|
|
|
|
prefix string
|
|
|
|
relTime string
|
|
|
|
expectedStatus int
|
|
|
|
}{
|
|
|
|
// Test 1 - valid testcase
|
|
|
|
{
|
|
|
|
bucket: "mybucket",
|
|
|
|
prefix: "myobject",
|
|
|
|
relTime: "1s",
|
|
|
|
expectedStatus: 200,
|
|
|
|
},
|
|
|
|
// Test 2 - invalid duration
|
|
|
|
{
|
|
|
|
bucket: "mybucket",
|
|
|
|
prefix: "myprefix",
|
|
|
|
relTime: "invalidDuration",
|
|
|
|
expectedStatus: 400,
|
|
|
|
},
|
|
|
|
// Test 3 - invalid bucket name
|
|
|
|
{
|
|
|
|
bucket: `invalid\\Bucket`,
|
|
|
|
prefix: "myprefix",
|
|
|
|
relTime: "1h",
|
|
|
|
expectedStatus: 400,
|
|
|
|
},
|
|
|
|
// Test 4 - invalid prefix
|
|
|
|
{
|
|
|
|
bucket: "mybucket",
|
|
|
|
prefix: `invalid\\Prefix`,
|
|
|
|
relTime: "1h",
|
|
|
|
expectedStatus: 400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
adminRouter := router.NewRouter()
|
|
|
|
registerAdminRouter(adminRouter)
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
|
|
|
queryStr := fmt.Sprintf("&bucket=%s&prefix=%s&older-than=%s", test.bucket, test.prefix, test.relTime)
|
|
|
|
req, err := newTestRequest("POST", "/?lock"+queryStr, 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d - Failed to construct clear locks request - %v", i+1, err)
|
|
|
|
}
|
|
|
|
req.Header.Set(minioAdminOpHeader, "clear")
|
|
|
|
|
|
|
|
cred := serverConfig.GetCredential()
|
|
|
|
err = signRequestV4(req, cred.AccessKey, cred.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d - Failed to sign clear locks request - %v", i+1, err)
|
|
|
|
}
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
adminRouter.ServeHTTP(rec, req)
|
|
|
|
if test.expectedStatus != rec.Code {
|
|
|
|
t.Errorf("Test %d - Expected HTTP status code %d but received %d", i+1, test.expectedStatus, rec.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for lock query param validation helper function.
|
|
|
|
func TestValidateLockQueryParams(t *testing.T) {
|
2017-01-07 14:27:01 -05:00
|
|
|
// reset globals.
|
|
|
|
// this is to make sure that the tests are not affected by modified globals.
|
|
|
|
resetTestGlobals()
|
|
|
|
// initialize NSLock.
|
|
|
|
initNSLock(false)
|
2017-01-04 02:39:22 -05:00
|
|
|
// Sample query values for test cases.
|
|
|
|
allValidVal := url.Values{}
|
|
|
|
allValidVal.Set(string(lockBucket), "bucket")
|
|
|
|
allValidVal.Set(string(lockPrefix), "prefix")
|
|
|
|
allValidVal.Set(string(lockOlderThan), "1s")
|
|
|
|
|
|
|
|
invalidBucketVal := url.Values{}
|
|
|
|
invalidBucketVal.Set(string(lockBucket), `invalid\\Bucket`)
|
|
|
|
invalidBucketVal.Set(string(lockPrefix), "prefix")
|
|
|
|
invalidBucketVal.Set(string(lockOlderThan), "invalidDuration")
|
|
|
|
|
|
|
|
invalidPrefixVal := url.Values{}
|
|
|
|
invalidPrefixVal.Set(string(lockBucket), "bucket")
|
|
|
|
invalidPrefixVal.Set(string(lockPrefix), `invalid\\PRefix`)
|
|
|
|
invalidPrefixVal.Set(string(lockOlderThan), "invalidDuration")
|
|
|
|
|
|
|
|
invalidOlderThanVal := url.Values{}
|
|
|
|
invalidOlderThanVal.Set(string(lockBucket), "bucket")
|
|
|
|
invalidOlderThanVal.Set(string(lockPrefix), "prefix")
|
|
|
|
invalidOlderThanVal.Set(string(lockOlderThan), "invalidDuration")
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
qVals url.Values
|
|
|
|
apiErr APIErrorCode
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
qVals: invalidBucketVal,
|
|
|
|
apiErr: ErrInvalidBucketName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
qVals: invalidPrefixVal,
|
|
|
|
apiErr: ErrInvalidObjectName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
qVals: invalidOlderThanVal,
|
|
|
|
apiErr: ErrInvalidDuration,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
qVals: allValidVal,
|
|
|
|
apiErr: ErrNone,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
|
|
|
_, _, _, apiErr := validateLockQueryParams(test.qVals)
|
|
|
|
if apiErr != test.apiErr {
|
|
|
|
t.Errorf("Test %d - Expected error %v but received %v", i+1, test.apiErr, apiErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|