You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.7 KiB
204 lines
5.7 KiB
8 years ago
|
/*
|
||
6 years ago
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
||
8 years ago
|
*
|
||
|
* 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 (
|
||
7 years ago
|
"os"
|
||
8 years ago
|
"reflect"
|
||
6 years ago
|
"sync"
|
||
8 years ago
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
6 years ago
|
// Helper function to create a lock server for testing
|
||
|
func createLockTestServer(t *testing.T) (string, *lockRESTServer, string) {
|
||
|
obj, fsDir, err := prepareFS()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
||
|
t.Fatalf("unable initialize config file, %s", err)
|
||
|
}
|
||
|
|
||
|
locker := &lockRESTServer{
|
||
|
ll: localLocker{
|
||
|
mutex: sync.Mutex{},
|
||
|
serviceEndpoint: "rpc-path",
|
||
|
lockMap: make(map[string][]lockRequesterInfo),
|
||
|
},
|
||
|
}
|
||
|
creds := globalServerConfig.GetCredential()
|
||
|
token, err := authenticateNode(creds.AccessKey, creds.SecretKey)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
return fsDir, locker, token
|
||
|
}
|
||
|
|
||
8 years ago
|
// Test function to remove lock entries from map only in case they still exist based on name & uid combination
|
||
|
func TestLockRpcServerRemoveEntryIfExists(t *testing.T) {
|
||
8 years ago
|
testPath, locker, _ := createLockTestServer(t)
|
||
7 years ago
|
defer os.RemoveAll(testPath)
|
||
8 years ago
|
|
||
|
lri := lockRequesterInfo{
|
||
6 years ago
|
Writer: false,
|
||
|
Node: "host",
|
||
|
ServiceEndpoint: "rpc-path",
|
||
|
UID: "0123-4567",
|
||
|
Timestamp: UTCNow(),
|
||
|
TimeLastCheck: UTCNow(),
|
||
8 years ago
|
}
|
||
|
nlrip := nameLockRequesterInfoPair{name: "name", lri: lri}
|
||
|
|
||
|
// first test by simulating item has already been deleted
|
||
8 years ago
|
locker.ll.removeEntryIfExists(nlrip)
|
||
8 years ago
|
{
|
||
6 years ago
|
gotLri := locker.ll.lockMap["name"]
|
||
8 years ago
|
expectedLri := []lockRequesterInfo(nil)
|
||
|
if !reflect.DeepEqual(expectedLri, gotLri) {
|
||
|
t.Errorf("Expected %#v, got %#v", expectedLri, gotLri)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// then test normal deletion
|
||
8 years ago
|
locker.ll.lockMap["name"] = []lockRequesterInfo{lri} // add item
|
||
|
locker.ll.removeEntryIfExists(nlrip)
|
||
8 years ago
|
{
|
||
6 years ago
|
gotLri := locker.ll.lockMap["name"]
|
||
8 years ago
|
expectedLri := []lockRequesterInfo(nil)
|
||
|
if !reflect.DeepEqual(expectedLri, gotLri) {
|
||
|
t.Errorf("Expected %#v, got %#v", expectedLri, gotLri)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Test function to remove lock entries from map based on name & uid combination
|
||
|
func TestLockRpcServerRemoveEntry(t *testing.T) {
|
||
8 years ago
|
testPath, locker, _ := createLockTestServer(t)
|
||
7 years ago
|
defer os.RemoveAll(testPath)
|
||
8 years ago
|
|
||
|
lockRequesterInfo1 := lockRequesterInfo{
|
||
6 years ago
|
Writer: true,
|
||
|
Node: "host",
|
||
|
ServiceEndpoint: "rpc-path",
|
||
|
UID: "0123-4567",
|
||
|
Timestamp: UTCNow(),
|
||
|
TimeLastCheck: UTCNow(),
|
||
8 years ago
|
}
|
||
|
lockRequesterInfo2 := lockRequesterInfo{
|
||
6 years ago
|
Writer: true,
|
||
|
Node: "host",
|
||
|
ServiceEndpoint: "rpc-path",
|
||
|
UID: "89ab-cdef",
|
||
|
Timestamp: UTCNow(),
|
||
|
TimeLastCheck: UTCNow(),
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
locker.ll.lockMap["name"] = []lockRequesterInfo{
|
||
8 years ago
|
lockRequesterInfo1,
|
||
|
lockRequesterInfo2,
|
||
|
}
|
||
|
|
||
6 years ago
|
lri := locker.ll.lockMap["name"]
|
||
8 years ago
|
|
||
|
// test unknown uid
|
||
8 years ago
|
if locker.ll.removeEntry("name", "unknown-uid", &lri) {
|
||
8 years ago
|
t.Errorf("Expected %#v, got %#v", false, true)
|
||
|
}
|
||
|
|
||
8 years ago
|
if !locker.ll.removeEntry("name", "0123-4567", &lri) {
|
||
8 years ago
|
t.Errorf("Expected %#v, got %#v", true, false)
|
||
|
} else {
|
||
6 years ago
|
gotLri := locker.ll.lockMap["name"]
|
||
8 years ago
|
expectedLri := []lockRequesterInfo{lockRequesterInfo2}
|
||
|
if !reflect.DeepEqual(expectedLri, gotLri) {
|
||
|
t.Errorf("Expected %#v, got %#v", expectedLri, gotLri)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
if !locker.ll.removeEntry("name", "89ab-cdef", &lri) {
|
||
8 years ago
|
t.Errorf("Expected %#v, got %#v", true, false)
|
||
|
} else {
|
||
6 years ago
|
gotLri := locker.ll.lockMap["name"]
|
||
8 years ago
|
expectedLri := []lockRequesterInfo(nil)
|
||
|
if !reflect.DeepEqual(expectedLri, gotLri) {
|
||
|
t.Errorf("Expected %#v, got %#v", expectedLri, gotLri)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
// Tests function returning long lived locks.
|
||
8 years ago
|
func TestLockRpcServerGetLongLivedLocks(t *testing.T) {
|
||
8 years ago
|
ut := UTCNow()
|
||
8 years ago
|
// Collection of test cases for verifying returning valid long lived locks.
|
||
|
testCases := []struct {
|
||
|
lockMap map[string][]lockRequesterInfo
|
||
|
lockInterval time.Duration
|
||
|
expectedNSLR []nameLockRequesterInfoPair
|
||
|
}{
|
||
|
// Testcase - 1 validates long lived locks, returns empty list.
|
||
|
{
|
||
|
lockMap: map[string][]lockRequesterInfo{
|
||
|
"test": {{
|
||
6 years ago
|
Writer: true,
|
||
|
Node: "10.1.10.21",
|
||
|
ServiceEndpoint: "/lock/mnt/disk1",
|
||
|
UID: "10000112",
|
||
|
Timestamp: ut,
|
||
|
TimeLastCheck: ut,
|
||
8 years ago
|
}},
|
||
|
},
|
||
|
lockInterval: 1 * time.Minute,
|
||
|
expectedNSLR: []nameLockRequesterInfoPair{},
|
||
|
},
|
||
|
// Testcase - 2 validates long lived locks, returns at least one list.
|
||
|
{
|
||
|
lockMap: map[string][]lockRequesterInfo{
|
||
|
"test": {{
|
||
6 years ago
|
Writer: true,
|
||
|
Node: "10.1.10.21",
|
||
|
ServiceEndpoint: "/lock/mnt/disk1",
|
||
|
UID: "10000112",
|
||
|
Timestamp: ut,
|
||
|
TimeLastCheck: ut.Add(-2 * time.Minute),
|
||
8 years ago
|
}},
|
||
|
},
|
||
|
lockInterval: 1 * time.Minute,
|
||
|
expectedNSLR: []nameLockRequesterInfoPair{
|
||
|
{
|
||
|
name: "test",
|
||
|
lri: lockRequesterInfo{
|
||
6 years ago
|
Writer: true,
|
||
|
Node: "10.1.10.21",
|
||
|
ServiceEndpoint: "/lock/mnt/disk1",
|
||
|
UID: "10000112",
|
||
|
Timestamp: ut,
|
||
|
TimeLastCheck: ut.Add(-2 * time.Minute),
|
||
8 years ago
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
// Validates all test cases here.
|
||
|
for i, testCase := range testCases {
|
||
|
nsLR := getLongLivedLocks(testCase.lockMap, testCase.lockInterval)
|
||
|
if !reflect.DeepEqual(testCase.expectedNSLR, nsLR) {
|
||
|
t.Errorf("Test %d: Expected %#v, got %#v", i+1, testCase.expectedNSLR, nsLR)
|
||
|
}
|
||
|
}
|
||
|
}
|