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

karanmehta93 pushed a commit to branch 4.x-HBase-1.4
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.x-HBase-1.4 by this push:
     new d0cf340  PHOENIX-5172: Harden the PQS canary synth test tool with 
retry mechanism and more logging
d0cf340 is described below

commit d0cf34075e58511625db48a942cea77adc6e114f
Author: Swaroopa Kadam <swaroopa.kada...@gmail.com>
AuthorDate: Tue Mar 19 13:39:45 2019 -0700

    PHOENIX-5172: Harden the PQS canary synth test tool with retry mechanism 
and more logging
---
 .../org/apache/phoenix/tool/PhoenixCanaryTool.java | 212 ++++++----------
 .../tool/ParameterizedPhoenixCanaryToolIT.java     | 280 +++++++++++++++++++++
 .../apache/phoenix/tool/PhoenixCanaryToolTest.java |  53 +---
 .../resources/phoenix-canary-file-sink.properties  |  17 ++
 4 files changed, 378 insertions(+), 184 deletions(-)

diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/tool/PhoenixCanaryTool.java 
b/phoenix-core/src/main/java/org/apache/phoenix/tool/PhoenixCanaryTool.java
index 405f54f..865d210 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/tool/PhoenixCanaryTool.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/tool/PhoenixCanaryTool.java
@@ -28,18 +28,20 @@ import 
net.sourceforge.argparse4j.inf.ArgumentParserException;
 import net.sourceforge.argparse4j.inf.Namespace;
 import org.apache.hadoop.conf.Configured;
 import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.RetryCounter;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.util.ToolRunner;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import java.io.File;
 import java.io.InputStream;
 import java.sql.Connection;
-import java.sql.DatabaseMetaData;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
-import java.sql.Statement;
+import java.sql.SQLException;
+import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -49,16 +51,23 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 
 /**
- * A Canary Tool to perform synthetic tests for Query Server
+ * A Canary Tool to perform synthetic tests for Phoenix
+ * It assumes that TEST.PQSTEST or the schema.table passed in the argument
+ * is already present as following command
+ * CREATE TABLE IF NOT EXISTS TEST.PQSTEST (mykey INTEGER NOT NULL
+ * PRIMARY KEY, mycolumn VARCHAR, insert_date TIMESTAMP);
+ *
  */
 public class PhoenixCanaryTool extends Configured implements Tool {
 
     private static String TEST_SCHEMA_NAME = "TEST";
     private static String TEST_TABLE_NAME = "PQSTEST";
     private static String FQ_TABLE_NAME = "TEST.PQSTEST";
-    private boolean USE_NAMESPACE = true;
-
+    private static Timestamp timestamp;
+    private static final int MAX_CONNECTION_ATTEMPTS = 5;
+    private final int FIRST_TIME_RETRY_TIMEOUT = 5000;
     private Sink sink = new StdOutSink();
+    public static final String propFileName = 
"phoenix-canary-file-sink.properties";
 
     /**
      * Base class for a Canary Test
@@ -97,84 +106,38 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
         }
     }
 
-    /**
-     * Test which prepares environment before other tests run
-     */
-    static class PrepareTest extends CanaryTest {
-        void onExecute() throws Exception {
-            result.setTestName("prepare");
-            Statement statement = connection.createStatement();
-            DatabaseMetaData dbm = connection.getMetaData();
-            ResultSet tables = dbm.getTables(null, TEST_SCHEMA_NAME, 
TEST_TABLE_NAME, null);
-            if (tables.next()) {
-                // Drop test Table if exists
-                statement.executeUpdate("DROP TABLE IF EXISTS " + 
FQ_TABLE_NAME);
-            }
-
-            // Drop test schema if exists
-            if (TEST_SCHEMA_NAME != null) {
-                statement = connection.createStatement();
-                statement.executeUpdate("DROP SCHEMA IF EXISTS " + 
TEST_SCHEMA_NAME);
-            }
-        }
-    }
-
-    /**
-     * Create Schema Test
-     */
-    static class CreateSchemaTest extends CanaryTest {
-        void onExecute() throws Exception {
-            result.setTestName("createSchema");
-            Statement statement = connection.createStatement();
-            statement.executeUpdate("CREATE SCHEMA IF NOT EXISTS " + 
TEST_SCHEMA_NAME);
-        }
-    }
-
-    /**
-     * Create Table Test
-     */
-    static class CreateTableTest extends CanaryTest {
-        void onExecute() throws Exception {
-            result.setTestName("createTable");
-            Statement statement = connection.createStatement();
-            // Create Table
-            statement.executeUpdate("CREATE TABLE IF NOT EXISTS" + 
FQ_TABLE_NAME + " (mykey " + "INTEGER "
-                    + "NOT " + "NULL PRIMARY KEY, " + "mycolumn VARCHAR)");
-        }
-    }
-
-    /**
-     * Upsert Data into Table Test
-     */
     static class UpsertTableTest extends CanaryTest {
         void onExecute() throws Exception {
             result.setTestName("upsertTable");
             // Insert data
-            Statement statement = connection.createStatement();
-            statement.executeUpdate("UPSERT INTO " + FQ_TABLE_NAME + " VALUES 
(1, " +
-                    "'Hello" + " World')");
+            timestamp = new Timestamp(System.currentTimeMillis());
+            String stmt = "UPSERT INTO " + FQ_TABLE_NAME
+                    + "(mykey, mycolumn, insert_date) VALUES (?, ?, ?)";
+            PreparedStatement ps = connection.prepareStatement(stmt);
+            ps.setInt(1, 1);
+            ps.setString(2, "Hello World");
+            ps.setTimestamp(3, timestamp);
+            ps.executeUpdate();
             connection.commit();
         }
     }
 
-    /**
-     * Read data from Table Test
-     */
     static class ReadTableTest extends CanaryTest {
         void onExecute() throws Exception {
             result.setTestName("readTable");
-            // Query for table
-            PreparedStatement ps = connection.prepareStatement("SELECT * FROM 
" + FQ_TABLE_NAME);
+            PreparedStatement ps = connection.prepareStatement("SELECT * FROM "
+                    + FQ_TABLE_NAME+" WHERE INSERT_DATE = ?");
+            ps.setTimestamp(1,timestamp);
             ResultSet rs = ps.executeQuery();
 
-            // Check correctness
             int totalRows = 0;
             while (rs.next()) {
                 totalRows += 1;
                 Integer myKey = rs.getInt(1);
                 String myColumn = rs.getString(2);
                 if (myKey != 1 || !myColumn.equals("Hello World")) {
-                    throw new Exception("Retrieved values do not match the 
inserted " + "values");
+                    throw new Exception("Retrieved values do not " +
+                            "match the inserted values");
                 }
             }
             if (totalRows != 1) {
@@ -186,35 +149,6 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
     }
 
     /**
-     * Delete test table Test
-     */
-    static class DeleteTableTest extends CanaryTest {
-        void onExecute() throws Exception {
-            result.setTestName("deleteTable");
-            Statement statement = connection.createStatement();
-            statement.executeUpdate("DROP TABLE IF EXISTS" + FQ_TABLE_NAME);
-
-            // Check if table dropped
-            DatabaseMetaData dbm = connection.getMetaData();
-            ResultSet tables = dbm.getTables(null, TEST_SCHEMA_NAME, 
TEST_TABLE_NAME, null);
-            if (tables.next()) {
-                throw new Exception("Test Table could not be dropped");
-            }
-        }
-    }
-
-    /**
-     * Delete test Schema Test
-     */
-    static class DeleteSchemaTest extends CanaryTest {
-        void onExecute() throws Exception {
-            result.setTestName("deleteSchema");
-            Statement statement = connection.createStatement();
-            statement.executeUpdate("DROP SCHEMA IF EXISTS " + 
TEST_SCHEMA_NAME);
-        }
-    }
-
-    /**
      * Sink interface used by the canary to output information
      */
     public interface Sink {
@@ -227,9 +161,6 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
         void clearResults();
     }
 
-    /**
-     * Implementation of Std Out Sink
-     */
     public static class StdOutSink implements Sink {
         private List<CanaryTestResult> results = new ArrayList<>();
 
@@ -244,7 +175,7 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
         }
 
         @Override
-        public void publishResults() throws Exception {
+        public void publishResults() {
 
             Gson gson = new GsonBuilder().setPrettyPrinting().create();
             String resultJson = gson.toJson(results);
@@ -264,7 +195,6 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
         private List<CanaryTestResult> results = new ArrayList<>();
         File dir;
         String logfileName;
-        String propFileName = "phoenix-canary-file-sink.properties";
 
         public FileOutSink() throws Exception {
             Properties prop = new Properties();
@@ -338,7 +268,7 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
                 TEST_TABLE_NAME);
 
         parser.addArgument("--logsinkclass", 
"-lsc").type(String.class).nargs("?").setDefault
-                ("PhoenixCanaryTool$StdOutSink").help
+                ("org.apache.phoenix.tool.PhoenixCanaryTool$StdOutSink").help
                 ("Path to a Custom implementation for log sink class. default: 
stdout");
 
         Namespace res = null;
@@ -393,18 +323,10 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
             appInfo.setTestName("appInfo");
             appInfo.setMiscellaneous(connectionURL);
 
-            Properties connProps = new Properties();
-            connProps.setProperty("phoenix.schema.mapSystemTablesToNamespace", 
"true");
-            connProps.setProperty("phoenix.schema.isNamespaceMappingEnabled", 
"true");
+            connection = getConnectionWithRetry(connectionURL);
 
-            try {
-                connection = DriverManager.getConnection(connectionURL, 
connProps);
-            } catch (Exception e) {
-                LOG.info("Namespace mapping cannot be set. Using default 
schema");
-                USE_NAMESPACE = false;
-                connection = DriverManager.getConnection(connectionURL);
-                TEST_SCHEMA_NAME = null;
-                FQ_TABLE_NAME = TEST_TABLE_NAME;
+            if (connection == null) {
+                LOG.error("Failed to get connection after multiple retries; 
the connection is null");
             }
 
             SimpleTimeLimiter limiter = new SimpleTimeLimiter();
@@ -416,32 +338,13 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
                     sink.clearResults();
 
                     // Execute tests
-
-                    LOG.info("Starting PrepareTest");
-                    sink.updateResults(new PrepareTest().runTest(connection));
-
-                    if (USE_NAMESPACE) {
-                        LOG.info("Starting CreateSchemaTest");
-                        sink.updateResults(new 
CreateSchemaTest().runTest(connection));
-                    }
-
-                    LOG.info("Starting CreateTableTest");
-                    sink.updateResults(new 
CreateTableTest().runTest(connection));
-
                     LOG.info("Starting UpsertTableTest");
                     sink.updateResults(new 
UpsertTableTest().runTest(connection));
 
                     LOG.info("Starting ReadTableTest");
                     sink.updateResults(new 
ReadTableTest().runTest(connection));
-
-                    LOG.info("Starting DeleteTableTest");
-                    sink.updateResults(new 
DeleteTableTest().runTest(connection));
-
-                    if (USE_NAMESPACE) {
-                        LOG.info("Starting DeleteSchemaTest");
-                        sink.updateResults(new 
DeleteSchemaTest().runTest(connection));
-                    }
                     return null;
+
                 }
             }, timeoutVal, TimeUnit.SECONDS, true);
 
@@ -464,11 +367,56 @@ public class PhoenixCanaryTool extends Configured 
implements Tool {
         return 0;
     }
 
+    private Connection getConnectionWithRetry(String connectionURL) {
+        Connection connection=null;
+        try{
+            connection = getConnectionWithRetry(connectionURL, true);
+        } catch (Exception e) {
+            LOG.info("Failed to get connection with namespace enabled", e);
+            try {
+                connection = getConnectionWithRetry(connectionURL, false);
+            } catch (Exception ex) {
+                LOG.info("Failed to get connection without namespace enabled", 
ex);
+            }
+        }
+        return connection;
+    }
+
+    private Connection getConnectionWithRetry(String connectionURL, boolean 
namespaceFlag)
+        throws Exception {
+        Properties connProps = new Properties();
+        Connection connection = null;
+
+        connProps.setProperty("phoenix.schema.mapSystemTablesToNamespace", 
String.valueOf(namespaceFlag));
+        connProps.setProperty("phoenix.schema.isNamespaceMappingEnabled", 
String.valueOf(namespaceFlag));
+
+        RetryCounter retrier = new RetryCounter(MAX_CONNECTION_ATTEMPTS,
+                FIRST_TIME_RETRY_TIMEOUT, TimeUnit.MILLISECONDS);
+        LOG.info("Trying to get the connection with "
+                + retrier.getMaxAttempts() + " attempts with "
+                + "connectionURL :" + connectionURL
+                + "connProps :" + connProps);
+        while (retrier.shouldRetry()) {
+            try {
+                connection = DriverManager.getConnection(connectionURL, 
connProps);
+            } catch (SQLException e) {
+                LOG.info("Trying to establish connection with "
+                        + retrier.getAttemptTimes() + " attempts", e);
+            }
+            if (connection != null) {
+                LOG.info("Successfully established connection within "
+                        + retrier.getAttemptTimes() + " attempts");
+                break;
+            }
+            retrier.sleepUntilNextRetry();
+        }
+        return connection;
+    }
+
     public static void main(final String[] args) {
-        int result = 0;
         try {
             LOG.info("Starting Phoenix Canary Test tool...");
-            result = ToolRunner.run(new PhoenixCanaryTool(), args);
+            ToolRunner.run(new PhoenixCanaryTool(), args);
         } catch (Exception e) {
             LOG.error("Error in running Phoenix Canary Test tool. " + e);
         }
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/tool/ParameterizedPhoenixCanaryToolIT.java
 
b/phoenix-core/src/test/java/org/apache/phoenix/tool/ParameterizedPhoenixCanaryToolIT.java
new file mode 100644
index 0000000..cefb456
--- /dev/null
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/tool/ParameterizedPhoenixCanaryToolIT.java
@@ -0,0 +1,280 @@
+/*
+* 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.phoenix.tool;
+
+import com.google.common.collect.Maps;
+import com.google.gson.Gson;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.phoenix.end2end.ChangePermissionsIT;
+import org.apache.phoenix.end2end.NeedsOwnMiniClusterTest;
+import org.apache.phoenix.query.BaseTest;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.util.ReadOnlyProps;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.apache.phoenix.tool.PhoenixCanaryTool.propFileName;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Parameterized.class)
+@Category(NeedsOwnMiniClusterTest.class)
+public class ParameterizedPhoenixCanaryToolIT extends BaseTest {
+
+       private static final Log logger = 
LogFactory.getLog(ParameterizedPhoenixCanaryToolIT.class);
+       private static final String stdOutSink
+                       = 
"org.apache.phoenix.tool.PhoenixCanaryTool$StdOutSink";
+       private static final String fileOutSink
+                       = 
"org.apache.phoenix.tool.PhoenixCanaryTool$FileOutSink";
+
+       private static Map<String, String> serverProps = 
Maps.newHashMapWithExpectedSize(2);
+       private static Map<String, String> clientProps = 
Maps.newHashMapWithExpectedSize(2);
+       private static String connString = "";
+       private static Properties canaryProp = new Properties();
+       private static Connection connection = null;
+       private boolean isNamespaceEnabled;
+       private boolean isPositiveTestType;
+       private List<String> cmd = new ArrayList<>();
+       private String resultSinkOption;
+       private ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+       public ParameterizedPhoenixCanaryToolIT(boolean isPositiveTestType,
+                       boolean isNamespaceEnabled, String resultSinkOption) {
+               this.isPositiveTestType = isPositiveTestType;
+               this.isNamespaceEnabled = isNamespaceEnabled;
+               this.resultSinkOption = resultSinkOption;
+       }
+
+       @Parameterized.Parameters(name = 
"ParameterizedPhoenixCanaryToolIT_isPositiveTestType={0}," +
+                       "isNamespaceEnabled={1},resultSinkOption={2}")
+       public static Collection parametersList() {
+               return Arrays.asList(new Object[][] {
+                       {true, true, stdOutSink},
+                       {true, true, fileOutSink},
+                       {false, true, stdOutSink},
+                       {false, true, fileOutSink},
+                       {true, false, stdOutSink},
+                       {true, false, fileOutSink},
+                       {false, false, stdOutSink},
+                       {false, false, fileOutSink}
+               });
+       }
+
+       @Before
+       public void setup() throws Exception {
+               String createSchema;
+               String createTable;
+
+               if(needsNewCluster()) {
+                       setClientSideNamespaceProperties();
+                       setServerSideNamespaceProperties();
+                       tearDownMiniClusterAsync(1);
+                       setUpTestDriver(new 
ReadOnlyProps(serverProps.entrySet().iterator()),
+                                       new 
ReadOnlyProps(clientProps.entrySet().iterator()));
+                       logger.info("New cluster is spinned up with test 
parameters " +
+                                       "isPositiveTestType" + 
this.isPositiveTestType +
+                                       "isNamespaceEnabled" + 
this.isNamespaceEnabled +
+                                       "resultSinkOption" + 
this.resultSinkOption);
+                       connString = BaseTest.getUrl();
+                       connection = getConnection();
+               }
+
+               if (this.isNamespaceEnabled) {
+                       createSchema = "CREATE SCHEMA IF NOT EXISTS TEST";
+                       connection.createStatement().execute(createSchema);
+               }
+               createTable = "CREATE TABLE IF NOT EXISTS TEST.PQSTEST " +
+                                               "(mykey INTEGER NOT NULL 
PRIMARY KEY, mycolumn VARCHAR," +
+                                               " insert_date TIMESTAMP)";
+               connection.createStatement().execute(createTable);
+               cmd.add("--constring");
+               cmd.add(connString);
+               cmd.add("--logsinkclass");
+               cmd.add(this.resultSinkOption);
+               if (this.resultSinkOption.contains(stdOutSink)) {
+                       System.setOut(new java.io.PrintStream(out));
+               } else {
+                       loadCanaryPropertiesFile(canaryProp);
+               }
+       }
+
+       private boolean needsNewCluster() {
+               if (connection == null) {
+                       return true;
+               }
+               if 
(!clientProps.get(QueryServices.IS_SYSTEM_TABLE_MAPPED_TO_NAMESPACE)
+                               
.equalsIgnoreCase(String.valueOf(this.isNamespaceEnabled))) {
+                       return true;
+               }
+               return false;
+       }
+
+       private void setClientSideNamespaceProperties() {
+
+               
clientProps.put(QueryServices.IS_SYSTEM_TABLE_MAPPED_TO_NAMESPACE,
+                               String.valueOf(this.isNamespaceEnabled));
+
+               clientProps.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED,
+                               String.valueOf(this.isNamespaceEnabled));
+       }
+
+       private Connection getConnection() throws SQLException {
+               Properties props = new Properties();
+               
props.setProperty(QueryServices.IS_SYSTEM_TABLE_MAPPED_TO_NAMESPACE,
+                               String.valueOf(this.isNamespaceEnabled));
+
+               props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED,
+                               String.valueOf(this.isNamespaceEnabled));
+               return DriverManager.getConnection(connString, props);
+       }
+
+       void setServerSideNamespaceProperties() {
+               serverProps.put(QueryServices.IS_NAMESPACE_MAPPING_ENABLED,
+                               String.valueOf(this.isNamespaceEnabled));
+               
serverProps.put(QueryServices.IS_SYSTEM_TABLE_MAPPED_TO_NAMESPACE,
+                               String.valueOf(this.isNamespaceEnabled));
+       }
+
+       /*
+       *       This test runs in the test suit with
+       *       combination of parameters provided.
+       *       It tests the tool in positive type where test expects to pass
+       *       and negative type where test expects to fail.
+       */
+       @Test
+       public void phoenixCanaryToolTest() throws SQLException, IOException {
+               if (!isPositiveTestType) {
+                       dropTestTable();
+               }
+               PhoenixCanaryTool.main(cmd.toArray(new String[cmd.size()]));
+               Boolean result = getAggregatedResult();
+               if (isPositiveTestType) {
+                       assertTrue(result);
+               } else {
+                       assertFalse(result);
+               }
+       }
+
+       private Boolean getAggregatedResult() throws IOException {
+               HashMap<String, Boolean> resultsMap;
+               Boolean result = true;
+               resultsMap = parsePublishedResults();
+               for (Boolean b : resultsMap.values()) {
+                       result = result && b;
+               }
+               return result;
+       }
+
+       private HashMap<String, Boolean> parsePublishedResults() throws 
IOException {
+               Gson parser = new Gson();
+               CanaryTestResult[] results;
+               HashMap<String, Boolean> resultsMap = new HashMap<>();
+
+               if (this.resultSinkOption.contains(fileOutSink)) {
+                       File resultFile = getTestResultsFile();
+                       results = parser.fromJson(new FileReader(resultFile),
+                                       CanaryTestResult[].class);
+               } else {
+                       String result = out.toString();
+                       results = parser.fromJson(result, 
CanaryTestResult[].class);
+               }
+               for (CanaryTestResult r : results) {
+                       resultsMap.put(r.getTestName(), r.isSuccessful());
+               }
+               return resultsMap;
+       }
+
+       private File getTestResultsFile() {
+               File[] files = getLogFileList();
+               return files[0];
+       }
+
+       @After
+       public void teardown() throws SQLException {
+               if (this.isNamespaceEnabled) {
+                       dropTestTableAndSchema();
+               } else {
+                       dropTestTable();
+               }
+               if (this.resultSinkOption.contains(fileOutSink)) {
+                       deleteResultSinkFile();
+               }
+       }
+
+       private void deleteResultSinkFile() {
+               File[] files = getLogFileList();
+               for (final File file : files) {
+                       if (!file.delete()) {
+                               System.err.println("Can't remove " + 
file.getAbsolutePath());
+                       }
+               }
+       }
+
+       private File[] getLogFileList() {
+               File dir = new File(canaryProp.getProperty("file.location"));
+               return dir.listFiles(new FilenameFilter() {
+                       @Override
+                       public boolean accept(File dir, String name) {
+                               return name.endsWith(".log");
+                       }
+               });
+       }
+
+       private void loadCanaryPropertiesFile(Properties prop) {
+               InputStream input = 
ClassLoader.getSystemResourceAsStream(propFileName);
+               try {
+                       prop.load(input);
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       private void dropTestTable() throws SQLException {
+               String dropTable = "DROP TABLE IF EXISTS TEST.PQSTEST";
+               connection.createStatement().execute(dropTable);
+       }
+
+       private void dropTestTableAndSchema() throws SQLException {
+               dropTestTable();
+               String dropSchema = "DROP SCHEMA IF EXISTS TEST";
+               connection.createStatement().execute(dropSchema);
+       }
+
+}
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/tool/PhoenixCanaryToolTest.java 
b/phoenix-core/src/test/java/org/apache/phoenix/tool/PhoenixCanaryToolTest.java
index bd2a3d1..94229c2 100644
--- 
a/phoenix-core/src/test/java/org/apache/phoenix/tool/PhoenixCanaryToolTest.java
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/tool/PhoenixCanaryToolTest.java
@@ -54,40 +54,9 @@ public class PhoenixCanaryToolTest {
     }
 
     @Test
-    public void prepareTest() throws Exception {
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeUpdate(Mockito.anyString())).thenReturn(0);
-        when(connection.getMetaData()).thenReturn(dbm);
-        when(dbm.getTables(null, "TEST", "PQSTEST", null)).thenReturn(rs);
-        when(rs.next()).thenReturn(true).thenReturn(false);
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeUpdate(Mockito.anyString())).thenReturn(0);
-        CanaryTestResult result = new 
PhoenixCanaryTool.PrepareTest().runTest(connection);
-        assertEquals(true, result.isSuccessful());
-        assertEquals("Test prepare successful", result.getMessage());
-    }
-
-    @Test
-    public void createSchemaTest() throws Exception {
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeUpdate(Mockito.anyString())).thenReturn(0);
-        CanaryTestResult result = new 
PhoenixCanaryTool.CreateSchemaTest().runTest(connection);
-        assertEquals(true, result.isSuccessful());
-        assertEquals("Test createSchema successful", result.getMessage());
-    }
-
-    @Test
-    public void createTableTest() throws Exception {
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeUpdate(Mockito.anyString())).thenReturn(0);
-        CanaryTestResult result = new 
PhoenixCanaryTool.CreateTableTest().runTest(connection);
-        assertEquals(true, result.isSuccessful());
-        assertEquals("Test createTable successful", result.getMessage());
-    }
-
-    @Test
     public void upsertTableTest() throws Exception {
         when(connection.createStatement()).thenReturn(statement);
+        when(connection.prepareStatement(Mockito.anyString())).thenReturn(ps);
         when(statement.executeUpdate(Mockito.anyString())).thenReturn(1);
         CanaryTestResult result = new 
PhoenixCanaryTool.UpsertTableTest().runTest(connection);
         assertEquals(true, result.isSuccessful());
@@ -107,26 +76,6 @@ public class PhoenixCanaryToolTest {
     }
 
     @Test
-    public void deleteTableTest() throws Exception {
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeUpdate(Mockito.anyString())).thenReturn(0);
-        when(connection.getMetaData()).thenReturn(dbm);
-        when(dbm.getTables(null, "TEST", "PQSTEST", null)).thenReturn(rs);
-        CanaryTestResult result = new 
PhoenixCanaryTool.DeleteTableTest().runTest(connection);
-        assertEquals(true, result.isSuccessful());
-        assertEquals("Test deleteTable successful", result.getMessage());
-    }
-
-    @Test
-    public void deleteSchemaTest() throws Exception {
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeUpdate(Mockito.anyString())).thenReturn(0);
-        CanaryTestResult result = new 
PhoenixCanaryTool.DeleteSchemaTest().runTest(connection);
-        assertEquals(true, result.isSuccessful());
-        assertEquals("Test deleteSchema successful", result.getMessage());
-    }
-
-    @Test
     public void failTest() throws Exception {
         when(connection.prepareStatement(Mockito.anyString())).thenReturn(ps);
         when(ps.executeQuery()).thenReturn(rs);
diff --git 
a/phoenix-core/src/test/resources/phoenix-canary-file-sink.properties 
b/phoenix-core/src/test/resources/phoenix-canary-file-sink.properties
new file mode 100644
index 0000000..951c7b1
--- /dev/null
+++ b/phoenix-core/src/test/resources/phoenix-canary-file-sink.properties
@@ -0,0 +1,17 @@
+# 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.
+file.name=phoenix-canary-result.txt
+file.location=.
\ No newline at end of file

Reply via email to