From 2f4a7483eac98e29a708d170d114ed0160b94421 Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Sun, 8 Jan 2017 00:57:01 +0530 Subject: [PATCH] Test Function to reset globals. (#3538) - Adding reset functions for important global variables. - Using them in tests. --- cmd/admin-handlers_test.go | 22 ++++++++++++++++++++++ cmd/admin-rpc-server_test.go | 4 ++++ cmd/auth-rpc-client_test.go | 4 ++++ cmd/bucket-policy-handlers_test.go | 2 +- cmd/test-utils_test.go | 26 ++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/cmd/admin-handlers_test.go b/cmd/admin-handlers_test.go index 91c7252da..01f3f899f 100644 --- a/cmd/admin-handlers_test.go +++ b/cmd/admin-handlers_test.go @@ -111,6 +111,11 @@ func getServiceCmdRequest(cmd cmdType, cred credential) (*http.Request, error) { // testServicesCmdHandler - parametrizes service subcommand tests on // cmdType value. func testServicesCmdHandler(cmd cmdType, t *testing.T) { + // reset globals. + // this is to make sure that the tests are not affected by modified value. + resetTestGlobals() + // initialize NSLock. + initNSLock(false) // Initialize configuration for access/secret credentials. rootPath, err := newTestConfig("us-east-1") if err != nil { @@ -194,6 +199,12 @@ func TestServiceRestartHandler(t *testing.T) { // Test for locks list management REST API. func TestListLocksHandler(t *testing.T) { + // reset globals. + // this is to make sure that the tests are not affected by modified globals. + resetTestGlobals() + // initialize NSLock. + initNSLock(false) + rootPath, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("Unable to initialize server config. %s", err) @@ -272,6 +283,12 @@ func TestListLocksHandler(t *testing.T) { // Test for locks clear management REST API. func TestClearLocksHandler(t *testing.T) { + // reset globals. + // this is to make sure that the tests are not affected by modified globals. + resetTestGlobals() + // initialize NSLock. + initNSLock(false) + rootPath, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("Unable to initialize server config. %s", err) @@ -347,6 +364,11 @@ func TestClearLocksHandler(t *testing.T) { // Test for lock query param validation helper function. func TestValidateLockQueryParams(t *testing.T) { + // reset globals. + // this is to make sure that the tests are not affected by modified globals. + resetTestGlobals() + // initialize NSLock. + initNSLock(false) // Sample query values for test cases. allValidVal := url.Values{} allValidVal.Set(string(lockBucket), "bucket") diff --git a/cmd/admin-rpc-server_test.go b/cmd/admin-rpc-server_test.go index 6a4a36603..26365d4ab 100644 --- a/cmd/admin-rpc-server_test.go +++ b/cmd/admin-rpc-server_test.go @@ -22,6 +22,10 @@ import ( ) func testAdminCmd(cmd cmdType, t *testing.T) { + // reset globals. + // this is to make sure that the tests are not affected by modified globals. + resetTestGlobals() + rootPath, err := newTestConfig("us-east-1") if err != nil { t.Fatalf("Failed to create test config - %v", err) diff --git a/cmd/auth-rpc-client_test.go b/cmd/auth-rpc-client_test.go index 9b9076d7f..2210ac9d9 100644 --- a/cmd/auth-rpc-client_test.go +++ b/cmd/auth-rpc-client_test.go @@ -20,6 +20,10 @@ import "testing" // Tests authorized RPC client. func TestAuthRPCClient(t *testing.T) { + // reset globals. + // this is to make sure that the tests are not affected by modified globals. + resetTestGlobals() + authCfg := authConfig{ accessKey: "123", secretKey: "123", diff --git a/cmd/bucket-policy-handlers_test.go b/cmd/bucket-policy-handlers_test.go index c4d893e5d..16536de51 100644 --- a/cmd/bucket-policy-handlers_test.go +++ b/cmd/bucket-policy-handlers_test.go @@ -945,7 +945,7 @@ func TestBucketPolicyConditionMatch(t *testing.T) { } for i, tc := range testCases { - t.Run(fmt.Sprintf("Test case %d: Failed.", i+1), func(t *testing.T) { + t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) { // call the function under test and assert the result with the expected result. doesMatch := bucketPolicyConditionMatch(tc.condition, tc.statementCondition) if tc.expectedMatch != doesMatch { diff --git a/cmd/test-utils_test.go b/cmd/test-utils_test.go index 97fd99218..cce866721 100644 --- a/cmd/test-utils_test.go +++ b/cmd/test-utils_test.go @@ -456,6 +456,23 @@ func resetGlobalConfig() { serverConfigMu.Unlock() } +// reset global NSLock. +func resetGlobalNSLock() { + if globalNSMutex != nil { + globalNSMutex = nil + } +} + +// reset global event notifier. +func resetGlobalEventNotifier() { + globalEventNotifier = nil +} + +// reset Global event notifier. +func resetGlobalEventnotify() { + globalEventNotifier = nil +} + // Resets all the globals used modified in tests. // Resetting ensures that the changes made to globals by one test doesn't affect others. func resetTestGlobals() { @@ -465,6 +482,10 @@ func resetTestGlobals() { resetGlobalConfigPath() // Reset Global server config. resetGlobalConfig() + // Reset global NSLock. + resetGlobalNSLock() + // Reset global event notifier. + resetGlobalEventnotify() } // Configure the server for the test run. @@ -1876,6 +1897,11 @@ func ExecObjectLayerAPINilTest(t TestErrHandler, bucketName, objectName, instanc // ExecObjectLayerAPITest - executes object layer API tests. // Creates single node and XL ObjectLayer instance, registers the specified API end points and runs test for both the layers. func ExecObjectLayerAPITest(t *testing.T, objAPITest objAPITestType, endpoints []string) { + // reset globals. + // this is to make sure that the tests are not affected by modified value. + resetTestGlobals() + // initialize NSLock. + initNSLock(false) // initialize the server and obtain the credentials and root. // credentials are necessary to sign the HTTP request. rootPath, err := newTestConfig("us-east-1")