2016-09-19 16:14:55 -04:00
|
|
|
/*
|
2019-04-18 02:16:27 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
2016-09-19 16:14:55 -04: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-08-12 22:25:43 -04:00
|
|
|
"os"
|
2016-09-19 16:14:55 -04:00
|
|
|
"reflect"
|
2019-04-18 02:16:27 -04:00
|
|
|
"sync"
|
2016-09-19 16:14:55 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-04-18 02:16:27 -04:00
|
|
|
// 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{
|
2019-08-28 19:12:57 -04:00
|
|
|
ll: &localLocker{
|
2019-11-13 15:17:45 -05:00
|
|
|
mutex: sync.Mutex{},
|
|
|
|
lockMap: make(map[string][]lockRequesterInfo),
|
2019-04-18 02:16:27 -04:00
|
|
|
},
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
creds := globalActiveCred
|
2020-01-30 21:59:22 -05:00
|
|
|
token, err := authenticateNode(creds.AccessKey, creds.SecretKey, "")
|
2019-04-18 02:16:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return fsDir, locker, token
|
|
|
|
}
|
|
|
|
|
2016-10-10 13:11:29 -04:00
|
|
|
// Test function to remove lock entries from map based on name & uid combination
|
|
|
|
func TestLockRpcServerRemoveEntry(t *testing.T) {
|
2016-10-11 03:50:27 -04:00
|
|
|
testPath, locker, _ := createLockTestServer(t)
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(testPath)
|
2016-10-10 13:11:29 -04:00
|
|
|
|
|
|
|
lockRequesterInfo1 := lockRequesterInfo{
|
2019-11-13 15:17:45 -05:00
|
|
|
Writer: true,
|
|
|
|
UID: "0123-4567",
|
|
|
|
Timestamp: UTCNow(),
|
|
|
|
TimeLastCheck: UTCNow(),
|
2016-10-10 13:11:29 -04:00
|
|
|
}
|
|
|
|
lockRequesterInfo2 := lockRequesterInfo{
|
2019-11-13 15:17:45 -05:00
|
|
|
Writer: true,
|
|
|
|
UID: "89ab-cdef",
|
|
|
|
Timestamp: UTCNow(),
|
|
|
|
TimeLastCheck: UTCNow(),
|
2016-10-10 13:11:29 -04:00
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
locker.ll.lockMap["name"] = []lockRequesterInfo{
|
2016-10-10 13:11:29 -04:00
|
|
|
lockRequesterInfo1,
|
|
|
|
lockRequesterInfo2,
|
|
|
|
}
|
|
|
|
|
2019-02-13 07:59:36 -05:00
|
|
|
lri := locker.ll.lockMap["name"]
|
2016-10-10 13:11:29 -04:00
|
|
|
|
|
|
|
// test unknown uid
|
2017-06-05 15:25:04 -04:00
|
|
|
if locker.ll.removeEntry("name", "unknown-uid", &lri) {
|
2016-10-10 13:11:29 -04:00
|
|
|
t.Errorf("Expected %#v, got %#v", false, true)
|
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
if !locker.ll.removeEntry("name", "0123-4567", &lri) {
|
2016-10-10 13:11:29 -04:00
|
|
|
t.Errorf("Expected %#v, got %#v", true, false)
|
|
|
|
} else {
|
2019-02-13 07:59:36 -05:00
|
|
|
gotLri := locker.ll.lockMap["name"]
|
2016-10-10 13:11:29 -04:00
|
|
|
expectedLri := []lockRequesterInfo{lockRequesterInfo2}
|
|
|
|
if !reflect.DeepEqual(expectedLri, gotLri) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", expectedLri, gotLri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 15:25:04 -04:00
|
|
|
if !locker.ll.removeEntry("name", "89ab-cdef", &lri) {
|
2016-10-10 13:11:29 -04:00
|
|
|
t.Errorf("Expected %#v, got %#v", true, false)
|
|
|
|
} else {
|
2019-02-13 07:59:36 -05:00
|
|
|
gotLri := locker.ll.lockMap["name"]
|
2016-10-10 13:11:29 -04:00
|
|
|
expectedLri := []lockRequesterInfo(nil)
|
|
|
|
if !reflect.DeepEqual(expectedLri, gotLri) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", expectedLri, gotLri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|