You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2016/02/26 13:12:41 UTC

[13/17] ignite git commit: IGNITE-944: additional improvements for HTTP REST log command

IGNITE-944: additional improvements for HTTP REST log command


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/53fe9937
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/53fe9937
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/53fe9937

Branch: refs/heads/ignite-1232
Commit: 53fe9937e72d3677e8dab67a8673d4d6905cfdba
Parents: 52e178f
Author: Saikat Maitra <sa...@gmail.com>
Authored: Thu Feb 25 23:39:30 2016 +0300
Committer: Denis Magda <dm...@gridgain.com>
Committed: Thu Feb 25 23:39:30 2016 +0300

----------------------------------------------------------------------
 .../handlers/log/GridLogCommandHandler.java     | 32 +++++--
 .../handlers/log/GridLogCommandHandlerTest.java | 97 +++++++++++++++-----
 .../testframework/junits/GridAbstractTest.java  |  8 ++
 .../junits/GridTestKernalContext.java           | 34 ++++---
 4 files changed, 126 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/53fe9937/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandler.java
index 603faf3..aac1fa7 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandler.java
@@ -22,7 +22,6 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.nio.file.InvalidPathException;
 import java.util.Collection;
-
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.IgniteInternalFuture;
@@ -93,7 +92,6 @@ public class GridLogCommandHandler extends GridRestCommandHandlerAdapter {
             else
                 from = DEFAULT_FROM;
 
-
             int to;
 
             if (req0.to() != -1) {
@@ -113,12 +111,26 @@ public class GridLogCommandHandler extends GridRestCommandHandlerAdapter {
             File logFile;
 
             try {
-                if (req0.path() != null)
-                    logFile = new File(req0.path());
+                if (req0.path() != null) {
+                    if (log.fileName() != null) {
+                        if (!req0.path().equals(log.fileName())) {
+                            return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED,
+                                "Request parameter 'path' must contain a path to valid log file."));
+                        }
+                        else
+                            logFile = new File(req0.path());
+                    }
+                    else if (req0.path().startsWith(ctx.config().getIgniteHome()))
+                        logFile = new File(req0.path());
+                    else {
+                        return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED,
+                            "Request parameter 'path' must contain a path to valid log file."));
+                    }
+                }
+                else if (log.fileName() == null)
+                    logFile = new File(ctx.config().getIgniteHome() + "/work/log/ignite.log");
                 else
-                    logFile = new File(log.fileName() == null ?
-                        ctx.config().getIgniteHome() + "/" + "work/log/ignite.log" :
-                        log.fileName());
+                    logFile = new File(log.fileName());
             }
             catch (InvalidPathException e) {
                 return new GridFinishedFuture<>(new GridRestResponse(GridRestResponse.STATUS_FAILED,
@@ -150,7 +162,7 @@ public class GridLogCommandHandler extends GridRestCommandHandlerAdapter {
     private String readLog(int from, int to, File logFile) throws IgniteCheckedException {
         StringBuilder content = new StringBuilder();
 
-        try (BufferedReader reader =  new BufferedReader(new FileReader(logFile))) {
+        try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
             String line;
 
             int start = 0;
@@ -161,6 +173,10 @@ public class GridLogCommandHandler extends GridRestCommandHandlerAdapter {
 
                 start++;
             }
+
+            if (content.length() == 0)
+                throw new IgniteCheckedException("Request parameter 'from' and 'to' are for lines that " +
+                    "do not exist in log file.");
         }
         catch (IOException e) {
             throw new IgniteCheckedException(e);

http://git-wip-us.apache.org/repos/asf/ignite/blob/53fe9937/modules/core/src/test/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandlerTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandlerTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandlerTest.java
index 572b267..7b0e950 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandlerTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/rest/handlers/log/GridLogCommandHandlerTest.java
@@ -24,6 +24,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
+import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.processors.rest.GridRestCommand;
 import org.apache.ignite.internal.processors.rest.GridRestResponse;
@@ -47,24 +48,20 @@ public class GridLogCommandHandlerTest extends GridCommonAbstractTest {
             "[22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Task locally undeployed: \n"
         );
 
-        Path file = Paths.get("test.log");
-        Files.write(file, lines, Charset.forName("UTF-8"));
-
-        lines = Arrays.asList("[22:01:30,329][INFO ][grid-load-test-thread-12][GridDeploymentLocalStore] ",
-            "[22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Removed undeployed class: \n",
-            "[22:01:30,329][INFO ][grid-load-test-thread-18][GridDeploymentLocalStore] Task locally undeployed: \n"
-        );
-
         Path dir = Paths.get(igniteHome + "/work/log");
         Files.createDirectories(dir);
 
-        file = Paths.get(igniteHome + "/work/log/" + "ignite.log");
+        Path file = Paths.get(igniteHome + "/work/log/" + "ignite.log");
         Files.write(file, lines, Charset.forName("UTF-8"));
+
+        file = Paths.get(igniteHome + "/work/log/" + "test.log");
+        Files.write(file, lines, Charset.forName("UTF-8"));
+
     }
 
     /** {@inheritDoc} */
     @Override protected void afterTestsStopped() throws Exception {
-        Path file = Paths.get("test.log");
+        Path file = Paths.get(igniteHome + "/work/log/" + "test.log");
         Files.delete(file);
 
         Files.delete(Paths.get(igniteHome + "/work/log/" + "ignite.log"));
@@ -102,13 +99,16 @@ public class GridLogCommandHandlerTest extends GridCommonAbstractTest {
      * @throws Exception If failed.
      */
     public void testHandleAsync() throws Exception {
-        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(newContext());
+        IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setIgniteHome(igniteHome);
+        GridTestKernalContext ctx = newContext(cfg);
+        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
         GridRestLogRequest req = new GridRestLogRequest();
 
         req.to(5);
         req.from(2);
 
-        req.path("test.log");
+        req.path(igniteHome + "/work/log/" + "test.log");
         IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
 
         assertNull(resp.result().getError());
@@ -119,12 +119,35 @@ public class GridLogCommandHandlerTest extends GridCommonAbstractTest {
     /**
      * @throws Exception If failed.
      */
-    public void testHandleAsyncFromAndToNotSet() throws Exception {
-        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(newContext());
+    public void testHandleAsyncForNonExistingLines() throws Exception {
+        IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setIgniteHome(igniteHome);
+        GridTestKernalContext ctx = newContext(cfg);
+        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
         GridRestLogRequest req = new GridRestLogRequest();
 
-        req.path("test.log");
+        req.to(50);
+        req.from(20);
+
+        req.path(igniteHome + "/work/log/" + "test.log");
+        IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
+
+        assertEquals("Request parameter 'from' and 'to' are for lines that do not exist in log file.", resp.result().getError());
+        assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus());
+        assertNull(resp.result().getResponse());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testHandleAsyncFromAndToNotSet() throws Exception {
+        IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setIgniteHome(igniteHome);
+        GridTestKernalContext ctx = newContext(cfg);
+        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
+        GridRestLogRequest req = new GridRestLogRequest();
 
+        req.path(igniteHome + "/work/log/" + "test.log");
         IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
 
         assertNull(resp.result().getError());
@@ -138,7 +161,6 @@ public class GridLogCommandHandlerTest extends GridCommonAbstractTest {
     public void testHandleAsyncPathNotSet() throws Exception {
         GridTestKernalContext ctx = newContext();
         ctx.config().setIgniteHome(igniteHome);
-
         GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
         GridRestLogRequest req = new GridRestLogRequest();
 
@@ -155,14 +177,38 @@ public class GridLogCommandHandlerTest extends GridCommonAbstractTest {
     /**
      * @throws Exception If failed.
      */
+    public void testHandleAsyncPathIsOutsideIgniteHome() throws Exception {
+        IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setIgniteHome(igniteHome);
+        GridTestKernalContext ctx = newContext(cfg);
+        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
+        GridRestLogRequest req = new GridRestLogRequest();
+
+        req.to(5);
+        req.from(2);
+        req.path("/home/users/mytest.log");
+
+        IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
+
+        assertEquals("Request parameter 'path' must contain a path to valid log file.", resp.result().getError());
+        assertEquals(GridRestResponse.STATUS_FAILED, resp.result().getSuccessStatus());
+        assertNull(resp.result().getResponse());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
     public void testHandleAsyncFromGreaterThanTo() throws Exception {
-        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(newContext());
+        IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setIgniteHome(igniteHome);
+        GridTestKernalContext ctx = newContext(cfg);
+        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
         GridRestLogRequest req = new GridRestLogRequest();
 
-        req.to(2);
-        req.from(5);
-        req.path("test.log");
+        req.to(5);
+        req.from(7);
 
+        req.path(igniteHome + "/work/log/" + "test.log");
         IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
 
         assertEquals("Request parameter 'from' must be less than 'to'.", resp.result().getError());
@@ -174,13 +220,16 @@ public class GridLogCommandHandlerTest extends GridCommonAbstractTest {
      * @throws Exception If failed.
      */
     public void testHandleAsyncFromEqualTo() throws Exception {
-        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(newContext());
+        IgniteConfiguration cfg = new IgniteConfiguration();
+        cfg.setIgniteHome(igniteHome);
+        GridTestKernalContext ctx = newContext(cfg);
+        GridLogCommandHandler cmdHandler = new GridLogCommandHandler(ctx);
         GridRestLogRequest req = new GridRestLogRequest();
 
-        req.to(2);
-        req.from(2);
-        req.path("test.log");
+        req.to(5);
+        req.from(5);
 
+        req.path(igniteHome + "/work/log/" + "test.log");
         IgniteInternalFuture<GridRestResponse> resp = cmdHandler.handleAsync(req);
 
         assertEquals("Request parameter 'from' must be less than 'to'.", resp.result().getError());

http://git-wip-us.apache.org/repos/asf/ignite/blob/53fe9937/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
index 3c83d86..614e634 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
@@ -452,6 +452,14 @@ public abstract class GridAbstractTest extends TestCase {
     }
 
     /**
+     * @param cfg Configuration to use in Test
+     * @return Test kernal context.
+     */
+    protected GridTestKernalContext newContext(IgniteConfiguration cfg) throws IgniteCheckedException {
+        return new GridTestKernalContext(log(), cfg);
+    }
+
+    /**
      * Called before execution of every test method in class.
      *
      * @throws Exception If failed. {@link #afterTest()} will be called in this case.

http://git-wip-us.apache.org/repos/asf/ignite/blob/53fe9937/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java
index f318c38..b4c31be 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridTestKernalContext.java
@@ -40,19 +40,27 @@ public class GridTestKernalContext extends GridKernalContextImpl {
      * @param log Logger to use in context config.
      */
     public GridTestKernalContext(IgniteLogger log) throws IgniteCheckedException {
+        this(log, new IgniteConfiguration());
+    }
+
+    /**
+     * @param log Logger to use in context config.
+     * @param cfg Configuration to use in Test
+     */
+    public GridTestKernalContext(IgniteLogger log, IgniteConfiguration cfg) throws IgniteCheckedException {
         super(new GridLoggerProxy(log, null, null, null),
-            new IgniteKernal(null),
-            new IgniteConfiguration(),
-            new GridKernalGatewayImpl(null),
-            null,
-            null,
-            null,
-            null,
-            null,
-            null,
-            null,
-            null,
-            U.allPluginProviders());
+                new IgniteKernal(null),
+                cfg,
+                new GridKernalGatewayImpl(null),
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                U.allPluginProviders());
 
         GridTestUtils.setFieldValue(grid(), "cfg", config());
 
@@ -108,4 +116,4 @@ public class GridTestKernalContext extends GridKernalContextImpl {
     public void setExecutorService(ExecutorService execSvc){
         this.execSvc = execSvc;
     }
-}
\ No newline at end of file
+}