| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +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-02-21 04:58:50 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func testAdminCmd(cmd cmdType, t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-01-23 06:54:55 +08:00
										 |  |  | 	// reset globals. this is to make sure that the tests are not
 | 
					
						
							|  |  |  | 	// affected by modified globals.
 | 
					
						
							| 
									
										
										
										
											2017-01-08 03:27:01 +08:00
										 |  |  | 	resetTestGlobals() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	rootPath, err := newTestConfig(globalMinioDefaultRegion) | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to create test config - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(rootPath) | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 05:12:47 +08:00
										 |  |  | 	creds := globalServerConfig.GetCredential() | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	token, err := authenticateNode(creds.AccessKey, creds.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	adminServer := adminCmd{} | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	args := LoginRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 		AuthToken:   token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		Version:     globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 		RequestTime: UTCNow(), | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	err = adminServer.Login(&args, &LoginRPCReply{}) | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to login to admin server - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2017-01-15 06:48:52 +08:00
										 |  |  | 		// A test signal receiver
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 		<-globalServiceSignalCh | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 06:54:55 +08:00
										 |  |  | 	sa := SignalServiceArgs{ | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		AuthRPCArgs: AuthRPCArgs{AuthToken: token, Version: globalRPCAPIVersion}, | 
					
						
							| 
									
										
										
										
											2018-01-23 06:54:55 +08:00
										 |  |  | 		Sig:         cmd.toServiceSignal(), | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 23:12:19 +08:00
										 |  |  | 	genReply := AuthRPCReply{} | 
					
						
							|  |  |  | 	switch cmd { | 
					
						
							| 
									
										
										
										
											2018-01-23 06:54:55 +08:00
										 |  |  | 	case restartCmd, stopCmd: | 
					
						
							|  |  |  | 		if err = adminServer.SignalService(&sa, &genReply); err != nil { | 
					
						
							|  |  |  | 			t.Errorf("restartCmd/stopCmd: Expected: <nil>, got: %v", | 
					
						
							|  |  |  | 				err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		err = adminServer.SignalService(&sa, &genReply) | 
					
						
							|  |  |  | 		if err != nil && err.Error() != errUnsupportedSignal.Error() { | 
					
						
							|  |  |  | 			t.Errorf("invalidSignal %s: unexpected error got: %v", | 
					
						
							|  |  |  | 				cmd, err) | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | // TestAdminRestart - test for Admin.Restart RPC service.
 | 
					
						
							| 
									
										
										
										
											2016-12-16 14:26:15 +08:00
										 |  |  | func TestAdminRestart(t *testing.T) { | 
					
						
							|  |  |  | 	testAdminCmd(restartCmd, t) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 06:54:55 +08:00
										 |  |  | // TestAdminStop - test for Admin.Stop RPC service.
 | 
					
						
							|  |  |  | func TestAdminStop(t *testing.T) { | 
					
						
							|  |  |  | 	testAdminCmd(stopCmd, t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TestAdminStatus - test for Admin.Status RPC service (error case)
 | 
					
						
							|  |  |  | func TestAdminStatus(t *testing.T) { | 
					
						
							|  |  |  | 	testAdminCmd(statusCmd, t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 09:45:57 +08:00
										 |  |  | // TestReInitFormat - test for Admin.ReInitFormat RPC service.
 | 
					
						
							|  |  |  | func TestReInitFormat(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	// Reset global variables to start afresh.
 | 
					
						
							|  |  |  | 	resetTestGlobals() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rootPath, err := newTestConfig("us-east-1") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unable to initialize server config. %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(rootPath) | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Initializing objectLayer for HealFormatHandler.
 | 
					
						
							|  |  |  | 	_, xlDirs, xlErr := initTestXLObjLayer() | 
					
						
							|  |  |  | 	if xlErr != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to initialize XL based object layer - %v.", xlErr) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer removeRoots(xlDirs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Set globalEndpoints for a single node XL setup.
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	globalEndpoints = mustGetNewEndpointList(xlDirs...) | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Setup admin rpc server for an XL backend.
 | 
					
						
							|  |  |  | 	globalIsXL = true | 
					
						
							|  |  |  | 	adminServer := adminCmd{} | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 05:12:47 +08:00
										 |  |  | 	creds := globalServerConfig.GetCredential() | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	token, err := authenticateNode(creds.AccessKey, creds.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	args := LoginRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 		AuthToken:   token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		Version:     globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 		RequestTime: UTCNow(), | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	err = adminServer.Login(&args, &LoginRPCReply{}) | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to login to admin server - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	authArgs := AuthRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 		AuthToken: token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		Version:   globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	authReply := AuthRPCReply{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 09:45:57 +08:00
										 |  |  | 	err = adminServer.ReInitFormat(&ReInitFormatArgs{ | 
					
						
							|  |  |  | 		AuthRPCArgs: authArgs, | 
					
						
							|  |  |  | 		DryRun:      false, | 
					
						
							|  |  |  | 	}, &authReply) | 
					
						
							| 
									
										
										
										
											2017-01-23 16:32:55 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected to pass, but failed with %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | // TestGetConfig - Test for GetConfig admin RPC.
 | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | func TestGetConfig(t *testing.T) { | 
					
						
							|  |  |  | 	// Reset global variables to start afresh.
 | 
					
						
							|  |  |  | 	resetTestGlobals() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rootPath, err := newTestConfig("us-east-1") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unable to initialize server config. %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(rootPath) | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	adminServer := adminCmd{} | 
					
						
							| 
									
										
										
										
											2017-11-30 05:12:47 +08:00
										 |  |  | 	creds := globalServerConfig.GetCredential() | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	token, err := authenticateNode(creds.AccessKey, creds.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | 	args := LoginRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 		AuthToken:   token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		Version:     globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 		RequestTime: UTCNow(), | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	reply := LoginRPCReply{} | 
					
						
							|  |  |  | 	err = adminServer.Login(&args, &reply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to login to admin server - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	authArgs := AuthRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 		AuthToken: token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		Version:   globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-02-21 04:58:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	configReply := ConfigReply{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = adminServer.GetConfig(&authArgs, &configReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected GetConfig to pass but failed with %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var config serverConfigV13 | 
					
						
							|  |  |  | 	err = json.Unmarshal(configReply.Config, &config) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected json unmarshal to pass but failed with %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TestWriteAndCommitConfig - test for WriteTmpConfig and CommitConfig
 | 
					
						
							|  |  |  | // RPC handler.
 | 
					
						
							|  |  |  | func TestWriteAndCommitConfig(t *testing.T) { | 
					
						
							|  |  |  | 	// Reset global variables to start afresh.
 | 
					
						
							|  |  |  | 	resetTestGlobals() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rootPath, err := newTestConfig("us-east-1") | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Unable to initialize server config. %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-13 10:25:43 +08:00
										 |  |  | 	defer os.RemoveAll(rootPath) | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	adminServer := adminCmd{} | 
					
						
							| 
									
										
										
										
											2017-11-30 05:12:47 +08:00
										 |  |  | 	creds := globalServerConfig.GetCredential() | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 	token, err := authenticateNode(creds.AccessKey, creds.SecretKey) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 	args := LoginRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 		AuthToken:   token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 		Version:     globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 		RequestTime: UTCNow(), | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	reply := LoginRPCReply{} | 
					
						
							|  |  |  | 	err = adminServer.Login(&args, &reply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to login to admin server - %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Write temporary config.
 | 
					
						
							|  |  |  | 	buf := []byte("hello") | 
					
						
							|  |  |  | 	tmpFileName := mustGetUUID() | 
					
						
							|  |  |  | 	wArgs := WriteConfigArgs{ | 
					
						
							|  |  |  | 		AuthRPCArgs: AuthRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 			AuthToken: token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 			Version:   globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		TmpFileName: tmpFileName, | 
					
						
							|  |  |  | 		Buf:         buf, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = adminServer.WriteTmpConfig(&wArgs, &WriteConfigReply{}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to write temporary config %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected to succeed but failed %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cArgs := CommitConfigArgs{ | 
					
						
							|  |  |  | 		AuthRPCArgs: AuthRPCArgs{ | 
					
						
							| 
									
										
										
										
											2017-09-20 03:37:56 +08:00
										 |  |  | 			AuthToken: token, | 
					
						
							| 
									
										
										
										
											2018-02-07 07:07:17 +08:00
										 |  |  | 			Version:   globalRPCAPIVersion, | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		FileName: tmpFileName, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cReply := CommitConfigReply{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = adminServer.CommitConfig(&cArgs, &cReply) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Failed to commit config file %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |