You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by dc...@apache.org on 2020/09/18 22:10:04 UTC

[cassandra-in-jvm-dtest-api] branch master updated: Update in-jvm dtest to expose stdout and stderr for nodetool

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

dcapwell pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cassandra-in-jvm-dtest-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 5b26885  Update in-jvm dtest to expose stdout and stderr for nodetool
5b26885 is described below

commit 5b26885187d0aa8bc09cd1b1f4b6be81aebb40c7
Author: Yifan Cai <yi...@apple.com>
AuthorDate: Fri Sep 18 15:09:02 2020 -0700

    Update in-jvm dtest to expose stdout and stderr for nodetool
    
    Patch by Yifan Cai; reviewed by Alex Petrov, David Capwell for CASSANDRA-16057
---
 .../cassandra/distributed/api/NodeToolResult.java  | 64 +++++++++++++++++++
 .../distributed/api/NodeToolOutputTest.java        | 74 ++++++++++++++++++++++
 2 files changed, 138 insertions(+)

diff --git a/src/main/java/org/apache/cassandra/distributed/api/NodeToolResult.java b/src/main/java/org/apache/cassandra/distributed/api/NodeToolResult.java
index bdd75b5..6cfe4e5 100644
--- a/src/main/java/org/apache/cassandra/distributed/api/NodeToolResult.java
+++ b/src/main/java/org/apache/cassandra/distributed/api/NodeToolResult.java
@@ -36,13 +36,22 @@ public class NodeToolResult
     private final int rc;
     private final List<Notification> notifications;
     private final Throwable error;
+    private final String stdout;
+    private final String stderr;
 
     public NodeToolResult(String[] commandAndArgs, int rc, List<Notification> notifications, Throwable error)
     {
+        this(commandAndArgs, rc, notifications, error, null, null);
+    }
+
+    public NodeToolResult(String[] commandAndArgs, int rc, List<Notification> notifications, Throwable error, String stdout, String stderr)
+    {
         this.commandAndArgs = commandAndArgs;
         this.rc = rc;
         this.notifications = notifications;
         this.error = error;
+        this.stdout = stdout;
+        this.stderr = stderr;
     }
 
     public String[] getCommandAndArgs()
@@ -65,6 +74,16 @@ public class NodeToolResult
         return error;
     }
 
+    public String getStdout()
+    {
+        return stdout;
+    }
+
+    public String getStderr()
+    {
+        return stderr;
+    }
+
     public Asserts asserts()
     {
         return new Asserts();
@@ -134,6 +153,51 @@ public class NodeToolResult
             return this; // unreachable
         }
 
+        public Asserts stdoutContains(String substring)
+        {
+            return assertConsoleOutput(substring, true, true);
+        }
+
+        public Asserts stderrContains(String substring)
+        {
+            return assertConsoleOutput(substring, false, true);
+        }
+
+        public Asserts stdoutNotContains(String substring)
+        {
+            return assertConsoleOutput(substring, true, false);
+        }
+
+        public Asserts stderrNotContains(String substring)
+        {
+            return assertConsoleOutput(substring, false, false);
+        }
+
+        private Asserts assertConsoleOutput(String substring, boolean isStdout, boolean assertContains)
+        {
+            String name = isStdout ? "stdout" : "stderr";
+            String output = isStdout ? stdout : stderr;
+            AssertUtils.assertNotNull(name + " not defined", output);
+            if (assertContains)
+            {
+                if (output.contains(substring))
+                {
+                    return this;
+                }
+                fail("Unable to locate substring '" + substring + "' in " + name + ": " + output);
+            }
+            else
+            {
+                if (!output.contains(substring))
+                {
+                    return this;
+                }
+                fail("Found unexpected substring '" + substring + "' in " + name + ": " + output);
+            }
+
+            return this; // unreachable
+        }
+
         private void fail(String message)
         {
             StringBuilder sb = new StringBuilder();
diff --git a/src/test/java/org/apache/cassandra/distributed/api/NodeToolOutputTest.java b/src/test/java/org/apache/cassandra/distributed/api/NodeToolOutputTest.java
new file mode 100644
index 0000000..4420485
--- /dev/null
+++ b/src/test/java/org/apache/cassandra/distributed/api/NodeToolOutputTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.cassandra.distributed.api;
+
+import java.util.Collections;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchThrowableOfType;
+
+public class NodeToolOutputTest
+{
+    @Test
+    public void testAssertOutputNoThrow()
+    {
+        NodeToolResult result = create("stdout", "stderr");
+        result.asserts().stdoutContains("out");
+        result.asserts().stderrContains("err");
+        result.asserts().stdoutNotContains("hello");
+        result.asserts().stderrNotContains("world");
+    }
+
+    @Test
+    public void testAssertContainsWithNoOutput()
+    {
+        NodeToolResult result = create(null, null);
+        Throwable exception = catchThrowableOfType(() -> {
+            result.asserts().stdoutContains("foo");
+        }, AssertionError.class);
+        assertThat(exception.getMessage()).isEqualTo("stdout not defined");
+
+        exception = catchThrowableOfType(() -> {
+            result.asserts().stdoutNotContains("foo");
+        }, AssertionError.class);
+        assertThat(exception.getMessage()).isEqualTo("stdout not defined");
+    }
+
+    @Test
+    public void testAssertContainsFails()
+    {
+        NodeToolResult result = create("stdout", "stderr");
+        Throwable exception = catchThrowableOfType(() -> {
+           result.asserts().stdoutContains("foo");
+        }, AssertionError.class);
+        assertThat(exception.getMessage()).contains("Unable to locate substring");
+
+        exception = catchThrowableOfType(() -> {
+            result.asserts().stdoutNotContains("out");
+        }, AssertionError.class);
+        assertThat(exception.getMessage()).contains("Found unexpected substring");
+    }
+
+    private NodeToolResult create(String stdout, String stderr)
+    {
+        return new NodeToolResult(null, 0, Collections.emptyList(), null, stdout, stderr);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org