From cf022de4d53769f48e7323a99bcc3079a983fd7f Mon Sep 17 00:00:00 2001 From: Aditya Manthramurthy Date: Thu, 10 Nov 2016 19:43:04 -0500 Subject: [PATCH] Add tests for s3PeerAPIHandlers (Fixes #3067) (#3242) --- cmd/s3-peer-rpc-handlers_test.go | 109 +++++++++++++++++++++++++++++++ cmd/test-utils_test.go | 40 +++++++++++- 2 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 cmd/s3-peer-rpc-handlers_test.go diff --git a/cmd/s3-peer-rpc-handlers_test.go b/cmd/s3-peer-rpc-handlers_test.go new file mode 100644 index 000000000..21d4e95f4 --- /dev/null +++ b/cmd/s3-peer-rpc-handlers_test.go @@ -0,0 +1,109 @@ +/* + * Minio Cloud Storage, (C) 2014-2016 Minio, Inc. + * + * 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 ( + "encoding/json" + "path" + "testing" + "time" +) + +type TestRPCS3PeerSuite struct { + testServer TestServer + testAuthConf *authConfig + disks []string +} + +// Set up the suite and start the test server. +func (s *TestRPCS3PeerSuite) SetUpSuite(t *testing.T) { + s.testServer, s.disks = StartTestS3PeerRPCServer(t) + s.testAuthConf = &authConfig{ + address: s.testServer.Server.Listener.Addr().String(), + accessKey: s.testServer.AccessKey, + secretKey: s.testServer.SecretKey, + path: path.Join(reservedBucket, s3Path), + loginMethod: "S3.LoginHandler", + } +} + +func (s *TestRPCS3PeerSuite) TearDownSuite(t *testing.T) { + s.testServer.Stop() + removeRoots(s.disks) + removeAll(s.testServer.Root) +} + +func TestS3PeerRPC(t *testing.T) { + // setup + s := &TestRPCS3PeerSuite{} + s.SetUpSuite(t) + + // run test + s.testS3PeerRPC(t) + + // teardown + s.TearDownSuite(t) +} + +// Test S3 RPC handlers +func (s *TestRPCS3PeerSuite) testS3PeerRPC(t *testing.T) { + // Validate for invalid token. + args := GenericArgs{Token: "garbage", Timestamp: time.Now().UTC()} + rclient := newClient(s.testAuthConf.address, s.testAuthConf.path, false) + defer rclient.Close() + err := rclient.Call("S3.SetBucketNotificationPeer", &args, &GenericReply{}) + if err != nil { + if err.Error() != errInvalidToken.Error() { + t.Fatal(err) + } + } + + // Check bucket notification call works. + BNPArgs := SetBNPArgs{Bucket: "bucket", NCfg: ¬ificationConfig{}} + client := newAuthClient(s.testAuthConf) + defer client.Close() + err = client.Call("S3.SetBucketNotificationPeer", &BNPArgs, &GenericReply{}) + if err != nil { + t.Fatal(err) + } + + // Check bucket listener update call works. + BLPArgs := SetBLPArgs{Bucket: "bucket", LCfg: nil} + err = client.Call("S3.SetBucketListenerPeer", &BLPArgs, &GenericReply{}) + if err != nil { + t.Fatal(err) + } + + // Check bucket policy update call works. + pCh := policyChange{IsRemove: true} + pChBytes, err := json.Marshal(pCh) + if err != nil { + t.Fatal(err) + } + BPPArgs := SetBPPArgs{Bucket: "bucket", PChBytes: pChBytes} + err = client.Call("S3.SetBucketPolicyPeer", &BPPArgs, &GenericReply{}) + if err != nil { + t.Fatal(err) + } + + // Check event send event call works. + evArgs := EventArgs{Event: nil, Arn: "localhost:9000"} + err = client.Call("S3.Event", &evArgs, &GenericReply{}) + if err != nil { + t.Fatal(err) + } +} diff --git a/cmd/test-utils_test.go b/cmd/test-utils_test.go index e68ac8c81..16bb8f359 100644 --- a/cmd/test-utils_test.go +++ b/cmd/test-utils_test.go @@ -2092,5 +2092,43 @@ func StartTestBrowserPeerRPCServer(t TestErrHandler, instanceType string) TestSe // Initialize and run the TestServer. testRPCServer.Server = httptest.NewServer(initTestBrowserPeerRPCEndPoint()) return testRPCServer - +} + +func StartTestS3PeerRPCServer(t TestErrHandler) (TestServer, []string) { + root, err := newTestConfig("us-east-1") + if err != nil { + t.Fatalf("%s", err) + } + + // Create an instance of TestServer. + testRPCServer := TestServer{} + + // Fetch credentials for the test server. + credentials := serverConfig.GetCredential() + + testRPCServer.Root = root + testRPCServer.AccessKey = credentials.AccessKeyID + testRPCServer.SecretKey = credentials.SecretAccessKey + + // init disks + objLayer, fsDirs, err := prepareXL() + if err != nil { + t.Fatalf("%s", err) + } + // set object layer + testRPCServer.Obj = objLayer + globalObjLayerMutex.Lock() + globalObjectAPI = objLayer + globalObjLayerMutex.Unlock() + + // Register router on a new mux + muxRouter := router.NewRouter() + err = registerS3PeerRPCRouter(muxRouter) + if err != nil { + t.Fatalf("%s", err) + } + + // Initialize and run the TestServer. + testRPCServer.Server = httptest.NewServer(muxRouter) + return testRPCServer, fsDirs }