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-09-19 16:14:55 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-10-14 06:08:40 -04:00
|
|
|
"context"
|
2017-08-12 22:25:43 -04:00
|
|
|
"os"
|
2016-09-19 16:14:55 -04:00
|
|
|
"reflect"
|
2023-03-23 13:26:21 -04:00
|
|
|
"sync"
|
2016-09-19 16:14:55 -04:00
|
|
|
"testing"
|
2020-09-25 22:21:52 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/dsync"
|
2016-09-19 16:14:55 -04:00
|
|
|
)
|
|
|
|
|
2019-04-18 02:16:27 -04:00
|
|
|
// Helper function to create a lock server for testing
|
2022-10-14 06:08:40 -04:00
|
|
|
func createLockTestServer(ctx context.Context, t *testing.T) (string, *lockRESTServer, string) {
|
|
|
|
obj, fsDir, err := prepareFS(ctx)
|
2019-04-18 02:16:27 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = newTestConfig(globalMinioDefaultRegion, obj); err != nil {
|
|
|
|
t.Fatalf("unable initialize config file, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
locker := &lockRESTServer{
|
2023-03-23 13:26:21 -04:00
|
|
|
ll: &localLocker{
|
|
|
|
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
|
2024-07-22 03:04:48 -04: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) {
|
2022-10-14 06:08:40 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
testPath, locker, _ := createLockTestServer(ctx, t)
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(testPath)
|
2016-10-10 13:11:29 -04:00
|
|
|
|
|
|
|
lockRequesterInfo1 := lockRequesterInfo{
|
2021-03-03 21:36:43 -05:00
|
|
|
Owner: "owner",
|
|
|
|
Writer: true,
|
|
|
|
UID: "0123-4567",
|
2024-07-24 06:24:01 -04:00
|
|
|
Timestamp: UTCNow().UnixNano(),
|
|
|
|
TimeLastRefresh: UTCNow().UnixNano(),
|
2016-10-10 13:11:29 -04:00
|
|
|
}
|
|
|
|
lockRequesterInfo2 := lockRequesterInfo{
|
2021-03-03 21:36:43 -05:00
|
|
|
Owner: "owner",
|
|
|
|
Writer: true,
|
|
|
|
UID: "89ab-cdef",
|
2024-07-24 06:24:01 -04:00
|
|
|
Timestamp: UTCNow().UnixNano(),
|
|
|
|
TimeLastRefresh: UTCNow().UnixNano(),
|
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
|
2020-09-25 22:21:52 -04:00
|
|
|
if locker.ll.removeEntry("name", dsync.LockArgs{
|
|
|
|
Owner: "owner",
|
|
|
|
UID: "unknown-uid",
|
|
|
|
}, &lri) {
|
2016-10-10 13:11:29 -04:00
|
|
|
t.Errorf("Expected %#v, got %#v", false, true)
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
if !locker.ll.removeEntry("name", dsync.LockArgs{
|
|
|
|
Owner: "owner",
|
|
|
|
UID: "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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:21:52 -04:00
|
|
|
if !locker.ll.removeEntry("name", dsync.LockArgs{
|
|
|
|
Owner: "owner",
|
|
|
|
UID: "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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|