changeset 818:eaf1fc6abd3d

Introduce integration tests for the cli These tests exercise parts of the thermostat command line to verify that things are working as expected. These initial tests exercise the help command and the shell. The integration tests have to run after the "package" phase of maven, since that's when the dependencies and scripts are copied over to distribution/target. distribution/pom.xml now runs its tests in "integration-test" phase of the build rather than the "test" phase. The default build phase is now "integration-test" to ensure that the integration tests are run. Reviewed-by: ebaron, jerboaa, vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-October/003996.html
author Omair Majid <omajid@redhat.com>
date Tue, 04 Dec 2012 20:14:25 -0500
parents 7b9365602c9f
children c563aedf9710 6cd46e3563ce
files Makefile distribution/pom.xml distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java pom.xml
diffstat 4 files changed, 232 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Dec 04 20:06:50 2012 -0500
+++ b/Makefile	Tue Dec 04 20:14:25 2012 -0500
@@ -11,7 +11,7 @@
 # Do not change anything below
 #
 REPO_FLAG       = -Dmaven.repo.local=$(REPO_LOC)
-GOAL            = package
+GOAL            = integration-test
 POM             = pom.xml
 
 ifeq ($(SKIP_TESTS),true)
--- a/distribution/pom.xml	Tue Dec 04 20:06:50 2012 -0500
+++ b/distribution/pom.xml	Tue Dec 04 20:14:25 2012 -0500
@@ -57,6 +57,26 @@
 
   <build>
     <plugins>
+      <!-- skip unit test run, tests to be executed during integration-test -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <skip>true</skip>
+        </configuration>
+        <executions>
+          <execution>
+            <id>run-integration-tests</id>
+            <phase>integration-test</phase>
+            <goals>
+              <goal>test</goal>
+            </goals>
+            <configuration>
+              <skip>false</skip>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
       <plugin>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.4</version>
@@ -271,12 +291,21 @@
     </pluginManagement>
   </build>
   <dependencies>
+
+    <!-- integration tests -->
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>net.sourceforge.expectj</groupId>
+      <artifactId>expectj</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <!-- thermostat parts -->
+    <dependency>
       <groupId>com.redhat.thermostat</groupId>
       <artifactId>thermostat-main</artifactId>
       <version>${project.version}</version>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/distribution/src/test/java/com/redhat/thermostat/distribution/CliTest.java	Tue Dec 04 20:14:25 2012 -0500
@@ -0,0 +1,196 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.distribution;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import expectj.ExpectJ;
+import expectj.Spawn;
+
+public class CliTest {
+
+    private static final long TIMEOUT_IN_SECONDS = 2;
+
+    private ExpectJ expect;
+
+    @Before
+    public void setUp() throws IOException {
+        expect = new ExpectJ(TIMEOUT_IN_SECONDS);
+    }
+
+    @Test
+    public void testExpectIsSane() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable());
+
+        try {
+            shell.expect("some-random-text-that-is-not-really-possible");
+            fail("should never match");
+        } catch (IOException endOfStream) {
+            assertTrue(endOfStream.getMessage().contains("End of stream reached, no match found"));
+        }
+        shell.expectClose();
+    }
+
+    @Test
+    public void testSimpleInvocationPrintsHelp() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable());
+        shell.expectClose();
+
+        String stdOut = shell.getCurrentStandardOutContents();
+
+        assertMatchesHelpCommandList(stdOut);
+
+        String stdErr = shell.getCurrentStandardErrContents();
+        assertEquals(stdErr, "");
+    }
+
+    @Test
+    public void testHelpCommandInvocation() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " help");
+        shell.expectClose();
+
+        String stdOut = shell.getCurrentStandardOutContents();
+        String stdErr = shell.getCurrentStandardErrContents();
+
+        assertMatchesHelpCommandList(stdOut);
+        assertEquals(stdErr, "");
+    }
+
+    @Ignore("this is currently broken; help's usage includes stuff about usernames and passwords")
+    @Test
+    public void testHelpOnHelp() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " help help");
+        shell.expectClose();
+
+        String stdOut = shell.getCurrentStandardOutContents();
+        String stdErr = shell.getCurrentStandardErrContents();
+
+        String[] lines = stdOut.split("\n");
+        String usage = lines[0];
+        assertEquals("usage: help [command-name]", usage);
+
+        assertEquals(stdErr, "");
+    }
+
+    @Test
+    public void testVersionArgument() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " --version");
+        shell.expectClose();
+
+        String stdOut = shell.getCurrentStandardOutContents();
+        String stdErr = shell.getCurrentStandardErrContents();
+
+        assertTrue(stdOut.matches("Thermostat version \\d+\\.\\d+\\.\\d+\n"));
+        assertEquals(stdErr, "");
+    }
+
+    @Test
+    public void testShell() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " shell");
+
+        shell.expect("Thermostat >");
+        shell.send("help\n");
+
+        shell.expect("Thermostat >");
+
+        assertMatchesHelpCommandList(shell.getCurrentStandardOutContents());
+
+        shell.send("exit\n");
+
+        shell.expectClose();
+    }
+
+    @Test
+    public void testShellHelp() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " help shell");
+        shell.expectClose();
+
+        String stdOut = shell.getCurrentStandardOutContents();
+
+        String[] lines = stdOut.split("\n");
+        String usage = lines[0];
+        assertTrue(usage.matches("^usage: shell \\[.*\\]$"));
+        String description = lines[1];
+
+        for (int i = 2; i < lines.length; i++) {
+            String argLine = lines[i];
+            assertTrue(argLine.matches("^\\s+--\\w+\\s.*$"));
+        }
+    }
+
+    @Test
+    public void testShellUnrecognizedArgument() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " shell --foo");
+        shell.expectErr("Unrecognized option: --foo");
+        shell.expectClose();
+    }
+
+    @Test
+    public void testInvalidCommand() throws Exception {
+        Spawn shell = expect.spawn(getThermostatExecutable() + " foobar baz");
+
+        // TODO should this be stderr?
+        shell.expect("unknown command 'foobar'");
+        shell.expectClose();
+
+        String stdOut = shell.getCurrentStandardOutContents();
+
+        assertMatchesHelpCommandList(stdOut);
+    }
+
+    public String getThermostatExecutable() {
+        return "target/bin/thermostat";
+    }
+
+    private static void assertMatchesHelpCommandList(String actual) {
+        assertTrue(actual.contains("list of commands"));
+        assertTrue(actual.contains("help"));
+        assertTrue(actual.contains("agent"));
+        assertTrue(actual.contains("gui"));
+        assertTrue(actual.contains("ping"));
+        assertTrue(actual.contains("shell"));
+    }
+
+}
--- a/pom.xml	Tue Dec 04 20:06:50 2012 -0500
+++ b/pom.xml	Tue Dec 04 20:14:25 2012 -0500
@@ -66,6 +66,7 @@
     <fest.version>1.2.1</fest.version>
     <powermock.version>1.4.11</powermock.version>
     <easymock.version>3.1</easymock.version>
+    <expectj.version>2.0.7</expectj.version>
 
     <jdktools.version>1.7.0</jdktools.version>
     <jfreechart.version>1.0.14</jfreechart.version>
@@ -287,6 +288,11 @@
         <version>1.2</version>
       </dependency>
       <dependency>
+        <groupId>net.sourceforge.expectj</groupId>
+        <artifactId>expectj</artifactId>
+        <version>${expectj.version}</version>
+      </dependency>
+      <dependency>
         <groupId>org.jboss.netty</groupId>
         <artifactId>netty</artifactId>
         <version>${netty.version}</version>