| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017, 2018, 2019 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08: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-13 10:25:43 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | // Test function to remove lock entries from map only in case they still exist based on name & uid combination
 | 
					
						
							|  |  |  | func TestLockRpcServerRemoveEntryIfExists(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-11 15:50:27 +08:00
										 |  |  | 	testPath, locker, _ := createLockTestServer(t) | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(testPath) | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	lri := lockRequesterInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  | 		Writer:          false, | 
					
						
							|  |  |  | 		Node:            "host", | 
					
						
							|  |  |  | 		ServiceEndpoint: "rpc-path", | 
					
						
							|  |  |  | 		UID:             "0123-4567", | 
					
						
							|  |  |  | 		Timestamp:       UTCNow(), | 
					
						
							|  |  |  | 		TimeLastCheck:   UTCNow(), | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	nlrip := nameLockRequesterInfoPair{name: "name", lri: lri} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// first test by simulating item has already been deleted
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	locker.ll.removeEntryIfExists(nlrip) | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 		gotLri := locker.ll.lockMap["name"] | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		expectedLri := []lockRequesterInfo(nil) | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(expectedLri, gotLri) { | 
					
						
							|  |  |  | 			t.Errorf("Expected %#v, got %#v", expectedLri, gotLri) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// then test normal deletion
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	locker.ll.lockMap["name"] = []lockRequesterInfo{lri} // add item
 | 
					
						
							|  |  |  | 	locker.ll.removeEntryIfExists(nlrip) | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 		gotLri := locker.ll.lockMap["name"] | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2016-10-11 15:50:27 +08:00
										 |  |  | 	testPath, locker, _ := createLockTestServer(t) | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(testPath) | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	lockRequesterInfo1 := lockRequesterInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  | 		Writer:          true, | 
					
						
							|  |  |  | 		Node:            "host", | 
					
						
							|  |  |  | 		ServiceEndpoint: "rpc-path", | 
					
						
							|  |  |  | 		UID:             "0123-4567", | 
					
						
							|  |  |  | 		Timestamp:       UTCNow(), | 
					
						
							|  |  |  | 		TimeLastCheck:   UTCNow(), | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	lockRequesterInfo2 := lockRequesterInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  | 		Writer:          true, | 
					
						
							|  |  |  | 		Node:            "host", | 
					
						
							|  |  |  | 		ServiceEndpoint: "rpc-path", | 
					
						
							|  |  |  | 		UID:             "89ab-cdef", | 
					
						
							|  |  |  | 		Timestamp:       UTCNow(), | 
					
						
							|  |  |  | 		TimeLastCheck:   UTCNow(), | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	locker.ll.lockMap["name"] = []lockRequesterInfo{ | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		lockRequesterInfo1, | 
					
						
							|  |  |  | 		lockRequesterInfo2, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 	lri := locker.ll.lockMap["name"] | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// test unknown uid
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	if locker.ll.removeEntry("name", "unknown-uid", &lri) { | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		t.Errorf("Expected %#v, got %#v", false, true) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	if !locker.ll.removeEntry("name", "0123-4567", &lri) { | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		t.Errorf("Expected %#v, got %#v", true, false) | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 		gotLri := locker.ll.lockMap["name"] | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		expectedLri := []lockRequesterInfo{lockRequesterInfo2} | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(expectedLri, gotLri) { | 
					
						
							|  |  |  | 			t.Errorf("Expected %#v, got %#v", expectedLri, gotLri) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 03:25:04 +08:00
										 |  |  | 	if !locker.ll.removeEntry("name", "89ab-cdef", &lri) { | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		t.Errorf("Expected %#v, got %#v", true, false) | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-02-13 20:59:36 +08:00
										 |  |  | 		gotLri := locker.ll.lockMap["name"] | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | 		expectedLri := []lockRequesterInfo(nil) | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(expectedLri, gotLri) { | 
					
						
							|  |  |  | 			t.Errorf("Expected %#v, got %#v", expectedLri, gotLri) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | // Tests function returning long lived locks.
 | 
					
						
							| 
									
										
										
										
											2016-10-11 01:11:29 +08:00
										 |  |  | func TestLockRpcServerGetLongLivedLocks(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 	ut := UTCNow() | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 	// 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": {{ | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  | 					Writer:          true, | 
					
						
							|  |  |  | 					Node:            "10.1.10.21", | 
					
						
							|  |  |  | 					ServiceEndpoint: "/lock/mnt/disk1", | 
					
						
							|  |  |  | 					UID:             "10000112", | 
					
						
							|  |  |  | 					Timestamp:       ut, | 
					
						
							|  |  |  | 					TimeLastCheck:   ut, | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 				}}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			lockInterval: 1 * time.Minute, | 
					
						
							|  |  |  | 			expectedNSLR: []nameLockRequesterInfoPair{}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Testcase - 2 validates long lived locks, returns at least one list.
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			lockMap: map[string][]lockRequesterInfo{ | 
					
						
							|  |  |  | 				"test": {{ | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  | 					Writer:          true, | 
					
						
							|  |  |  | 					Node:            "10.1.10.21", | 
					
						
							|  |  |  | 					ServiceEndpoint: "/lock/mnt/disk1", | 
					
						
							|  |  |  | 					UID:             "10000112", | 
					
						
							|  |  |  | 					Timestamp:       ut, | 
					
						
							|  |  |  | 					TimeLastCheck:   ut.Add(-2 * time.Minute), | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 				}}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			lockInterval: 1 * time.Minute, | 
					
						
							|  |  |  | 			expectedNSLR: []nameLockRequesterInfoPair{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					name: "test", | 
					
						
							|  |  |  | 					lri: lockRequesterInfo{ | 
					
						
							| 
									
										
										
										
											2019-01-24 23:22:14 +08:00
										 |  |  | 						Writer:          true, | 
					
						
							|  |  |  | 						Node:            "10.1.10.21", | 
					
						
							|  |  |  | 						ServiceEndpoint: "/lock/mnt/disk1", | 
					
						
							|  |  |  | 						UID:             "10000112", | 
					
						
							|  |  |  | 						Timestamp:       ut, | 
					
						
							|  |  |  | 						TimeLastCheck:   ut.Add(-2 * time.Minute), | 
					
						
							| 
									
										
										
										
											2016-09-20 04:14:55 +08:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// 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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |