This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 778fd74  Base class for S3 testing (#1759)
778fd74 is described below

commit 778fd749894befa64847a108ec97fc7cbc8b5ad4
Author: Ivan Kelly <iv...@apache.org>
AuthorDate: Thu May 10 20:00:29 2018 +0200

    Base class for S3 testing (#1759)
    
    Moves the setting up of the mock and s3 client into a base class so
    that it can be shared by multiple tests.
    
    Includes a switch, that can be flipped with the system property
    testRealAWS(i.e. -DtestRealAWS=true) to run the test against real AWS,
    rather than the mock. This is disabled by default, but allows us to
    validate that the mock is behaving the same way as S3 would. It
    requires that credentials and a default region are specified in
    ~/.aws.
    
    Master Issue: #1511
---
 pom.xml                                            |  5 ++
 .../s3offload/S3ManagedLedgerOffloaderTest.java    | 38 ++-----------
 .../apache/pulsar/broker/s3offload/S3TestBase.java | 63 ++++++++++++++++++++++
 3 files changed, 71 insertions(+), 35 deletions(-)

diff --git a/pom.xml b/pom.xml
index 08acb3a..4875aba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -116,6 +116,7 @@ flexible messaging model and an intuitive client 
API.</description>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <redirectTestOutputToFile>true</redirectTestOutputToFile>
+    <testRealAWS>false</testRealAWS>
 
     <bookkeeper.version>4.7.0</bookkeeper.version>
     <zookeeper.version>3.4.10</zookeeper.version>
@@ -840,6 +841,10 @@ flexible messaging model and an intuitive client 
API.</description>
           <trimStackTrace>false</trimStackTrace>
           <properties>
             <property>
+              <name>testRealAWS</name>
+              <value>${testRealAWS}</value>
+            </property>
+            <property>
               <name>listener</name>
               
<value>org.apache.pulsar.tests.PulsarTestListener,org.apache.pulsar.tests.AnnotationListener</value>
             </property>
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3ManagedLedgerOffloaderTest.java
 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3ManagedLedgerOffloaderTest.java
index deeacd6..059db65 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3ManagedLedgerOffloaderTest.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3ManagedLedgerOffloaderTest.java
@@ -18,11 +18,7 @@
  */
 package org.apache.pulsar.broker.s3offload;
 
-import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
-import com.amazonaws.services.s3.AmazonS3;
-import com.amazonaws.services.s3.AmazonS3ClientBuilder;
 
-import io.findify.s3mock.S3Mock;
 import io.netty.util.concurrent.DefaultThreadFactory;
 
 import java.util.HashMap;
@@ -42,44 +38,16 @@ import org.apache.pulsar.broker.PulsarServerException;
 import org.apache.pulsar.broker.ServiceConfiguration;
 import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
 import org.testng.Assert;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-class S3ManagedLedgerOffloaderTest {
+class S3ManagedLedgerOffloaderTest extends S3TestBase {
 
     final ScheduledExecutorService scheduler;
     final MockBookKeeper bk;
-    S3Mock s3mock = null;
-    AmazonS3 s3client = null;
-    String s3endpoint = null;
-
-    final static String REGION = "foobar";
-    final static String BUCKET = "foobar";
 
     S3ManagedLedgerOffloaderTest() throws Exception {
         scheduler = Executors.newScheduledThreadPool(1, new 
DefaultThreadFactory("offloader-"));
         bk = new 
MockBookKeeper(MockedPulsarServiceBaseTest.createMockZooKeeper());
-     }
-
-    @BeforeMethod
-    public void start() throws Exception {
-        s3mock = new 
S3Mock.Builder().withPort(0).withInMemoryBackend().build();
-        int port = s3mock.start().localAddress().getPort();
-        s3endpoint = "http://localhost:"; + port;
-
-        s3client = AmazonS3ClientBuilder.standard()
-            .withRegion(REGION)
-            .withEndpointConfiguration(new EndpointConfiguration(s3endpoint, 
REGION))
-            .withPathStyleAccessEnabled(true).build();
-        s3client.createBucket(BUCKET);
-    }
-
-    @AfterMethod
-    public void stop() throws Exception {
-        if (s3mock != null) {
-            s3mock.shutdown();
-        }
     }
 
     private ReadHandle buildReadHandle() throws Exception {
@@ -105,7 +73,7 @@ class S3ManagedLedgerOffloaderTest {
         
conf.setManagedLedgerOffloadDriver(S3ManagedLedgerOffloader.DRIVER_NAME);
         conf.setS3ManagedLedgerOffloadBucket("no-bucket");
         conf.setS3ManagedLedgerOffloadServiceEndpoint(s3endpoint);
-        conf.setS3ManagedLedgerOffloadRegion(REGION);
+        conf.setS3ManagedLedgerOffloadRegion("eu-west-1");
         LedgerOffloader offloader = S3ManagedLedgerOffloader.create(conf, 
scheduler);
 
         try {
@@ -134,7 +102,7 @@ class S3ManagedLedgerOffloaderTest {
     public void testNoBucketConfigured() throws Exception {
         ServiceConfiguration conf = new ServiceConfiguration();
         
conf.setManagedLedgerOffloadDriver(S3ManagedLedgerOffloader.DRIVER_NAME);
-        conf.setS3ManagedLedgerOffloadRegion(REGION);
+        conf.setS3ManagedLedgerOffloadRegion("eu-west-1");
 
         try {
             S3ManagedLedgerOffloader.create(conf, scheduler);
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3TestBase.java
 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3TestBase.java
new file mode 100644
index 0000000..e3b994b
--- /dev/null
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/s3offload/S3TestBase.java
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.broker.s3offload;
+
+import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.AmazonS3ClientBuilder;
+
+import io.findify.s3mock.S3Mock;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.AfterMethod;
+
+public class S3TestBase {
+    final static String BUCKET = "pulsar-unittest";
+
+    S3Mock s3mock = null;
+    protected AmazonS3 s3client = null;
+    protected String s3endpoint = null;
+
+    @BeforeMethod
+    public void start() throws Exception {
+        s3mock = new 
S3Mock.Builder().withPort(0).withInMemoryBackend().build();
+        int port = s3mock.start().localAddress().getPort();
+        s3endpoint = "http://localhost:"; + port;
+
+        if (Boolean.parseBoolean(System.getProperty("testRealAWS", "false"))) {
+            // To use this, ~/.aws must be configured with credentials and a 
default region
+            s3client = AmazonS3ClientBuilder.standard().build();
+        } else {
+            s3client = AmazonS3ClientBuilder.standard()
+                .withEndpointConfiguration(new 
EndpointConfiguration(s3endpoint, "foobar"))
+                .withPathStyleAccessEnabled(true).build();
+        }
+
+        if (!s3client.doesBucketExistV2(BUCKET)) {
+            s3client.createBucket(BUCKET);
+        }
+    }
+
+    @AfterMethod
+    public void stop() throws Exception {
+        if (s3mock != null) {
+            s3mock.shutdown();
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
mme...@apache.org.

Reply via email to