changeset 192:5d335809cd51

Test fixes changes: - Broken CliClientAgentInfoTest is now using more reliable way to find agent-id. - Now, when pattern is not found in tomcat log(s), it is printed, so that problem can be more easily found and fixed. - Fixing problem with race condition caused by asynchronous nature of shutdown tomcat script - Making ShellService class cleaner and more robust
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 14 Jul 2015 19:00:56 +0200
parents 2650531d6d93
children 5b5131445da5
files src/org/thermostat/qa2/framework/services/AbstractService.java src/org/thermostat/qa2/framework/services/ShellService.java src/org/thermostat/qa2/framework/services/ThermostatAgent.java src/org/thermostat/qa2/framework/services/ThermostatService.java src/org/thermostat/qa2/framework/services/ThermostatStorage.java src/org/thermostat/qa2/framework/services/Tomcat.java src/org/thermostat/qa2/framework/utils/CommonUtilities.java src/org/thermostat/qa2/tests/AgentWebStorageTest.java src/org/thermostat/qa2/tests/CliClientAgentInfoTest.java
diffstat 9 files changed, 71 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/thermostat/qa2/framework/services/AbstractService.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/services/AbstractService.java	Tue Jul 14 19:00:56 2015 +0200
@@ -102,7 +102,7 @@
         }
     }
 
-    protected void waitForListeningPort(int port, int timeout) throws Exception {
+    protected void waitForListeningPort(int port, int timeout, boolean start) throws Exception {
         for (int i = 0; i < timeout; ++i) {
             NativeProcess process = new NativeProcess("sh", "-c", "netstat -tln | grep -q :" + port + " &> /dev/null");
             process.setLabel("Port checker");
@@ -110,14 +110,17 @@
             int ret = process.waitForRaw();
 
             //int ret = NativeProcess.runRaw("sh", "-c", "netstat -tln | grep -q :" + port + " &> /dev/null");
-            if (ret == 0) {
-                return;
+            if (start) {
+                if (ret == 0) {
+                    return;
+                }
+            } else {
+                if (ret != 0) {
+                    return;
+                }
             }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-            }
-            if (!isRunning()) {
+            CommonUtilities.sleep(1000);
+            if (start && !isRunning()) {
                 throw new Exception(getServiceName() + ": stopped while waiting for listening port " + port);
             }
         }
--- a/src/org/thermostat/qa2/framework/services/ShellService.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/services/ShellService.java	Tue Jul 14 19:00:56 2015 +0200
@@ -31,13 +31,13 @@
 package org.thermostat.qa2.framework.services;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import org.thermostat.qa2.framework.NativeProcess;
 import org.thermostat.qa2.framework.Shell;
+import org.thermostat.qa2.framework.utils.CommonUtilities;
 import org.thermostat.qa2.framework.utils.FileUtilities;
 
 /**
@@ -45,22 +45,22 @@
  * @author Zdeněk Žamberský
  */
 public abstract class ShellService extends AbstractProcessService {
-    
+
     String commands;
     Map<String, String> environment = new HashMap();
     String pidFile;
-    
+
     public ShellService(String... commands) {
         this.commands = "";
         for (String command : commands) {
             this.commands += command + " ";
         }
     }
-    
+
     public void addEnvVariable(String name, String value) {
         environment.put(name, value);
     }
-    
+
     @Override
     public NativeProcess startServiceProcess() throws Exception {
         pidFile = "." + File.separator + getServiceName().replace(" ", "_") + ".pid";
@@ -68,42 +68,44 @@
         Shell shell = new Shell(File.separator + "bin" + File.separator + "bash");
         shell.setLabel(getServiceName());
         shell.start();
-        shell.writeln("function onExit {");
-        shell.writeln("    if [ -z \"${EXIT_CODE}\" ] && [ -n \"$!\" ] ; then");
-        shell.writeln("        echo \"killing " + getServiceName() + " processes ...\"");
-        shell.writeln("        pkill -P $!");
-        shell.writeln("        wait $!");
-        shell.writeln("    fi");
-        shell.writeln("}");
-        shell.writeln("trap onExit EXIT");
-        shell.writeln("Interupted=0");
-        shell.writeln("echo $$ > " + pidFile);
+        // enable job controll so that service process runs in separate process group
+        shell.writeln("set -m");
         for (Entry<String, String> entry : environment.entrySet()) {
             shell.writeln("export " + entry.getKey() + "=" + entry.getValue());
         }
         shell.writeln(commands + " &");
         shell.writeln("PROCESS_PID=$!");
-//        shell.writeln("echo \"Agent PID: ${PROCESS_PID}\"");
-        shell.writeln("wait $!");
-        shell.writeln("EXIT_CODE=$?");
-        shell.writeln("exit ${EXIT_CODE}");
+        shell.writeln("echo ${PROCESS_PID} > " + pidFile);
+        shell.writeln("wait ${PROCESS_PID}");
+        shell.writeln("exit $?");
         return shell;
     }
-    
+
     @Override
     public void stopServiceImpl() throws Exception {
-        try {
-            List<String> pid = FileUtilities.getLineListFromFile(pidFile);
-            NativeProcess process = new NativeProcess("kill", pid.get(0));
-            process.setLabel(getServiceName());
-            process.start();
-            process.waitFor();
-        } catch (FileNotFoundException e) {
-            // service already stopped
+        int timeout = 30;
+        for (int i = 0; i < timeout; ++i) {
+            if (!this.isRunning()) {
+                // stopped itself sooner then can be killed
+                break;
+            } else if (new File(pidFile).exists()) { // pid file exists
+                List<String> list = FileUtilities.getLineListFromFile(pidFile);
+                String pid;
+                // pid file was written to
+                if (list.size() > 0 && (pid = list.get(0)) != null && pid.length() > 0) {
+                    // send TERM signal to process group of service process
+                    NativeProcess process = new NativeProcess("kill", "--", "-" + pid);
+                    process.setLabel(getServiceName());
+                    process.start();
+                    process.waitForRaw();
+                    break;
+                }
+            }
+            CommonUtilities.sleep(1000);
         }
         thread.join();
     }
-    
+
     @Override
     public void runAfterInWrappingThread() throws Exception {
         NativeProcess process = new NativeProcess("rm", pidFile);
@@ -111,5 +113,5 @@
         process.start();
         process.waitForRaw();
     }
-    
+
 }
--- a/src/org/thermostat/qa2/framework/services/ThermostatAgent.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/services/ThermostatAgent.java	Tue Jul 14 19:00:56 2015 +0200
@@ -67,7 +67,7 @@
     @Override
     public void startServiceImpl() throws Exception {
         super.startServiceImpl();
-        waitForListeningPort(port, timeout);
+        waitForListeningPort(port, timeout, true);
         CommonUtilities.sleep(3000); // wait 3 more seconds
     }
 
--- a/src/org/thermostat/qa2/framework/services/ThermostatService.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/services/ThermostatService.java	Tue Jul 14 19:00:56 2015 +0200
@@ -66,6 +66,6 @@
     @Override
     public void startServiceImpl() throws Exception {
         super.startServiceImpl();
-        waitForListeningPort(port, timeout);
+        waitForListeningPort(port, timeout, true);
     }
 }
--- a/src/org/thermostat/qa2/framework/services/ThermostatStorage.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/services/ThermostatStorage.java	Tue Jul 14 19:00:56 2015 +0200
@@ -82,7 +82,7 @@
         if (CommonUtilities.findInLineList(lines, "Please run 'thermostat-setup'.", false)) {
             throw new Exception("Storage requires thermostat-setup to be run.");
         }
-        waitForListeningPort(mongoPort, timeout);
+        waitForListeningPort(mongoPort, timeout, true);
     }
 
     @Override
@@ -96,6 +96,7 @@
         }
         stopProcess.start();
         stopProcess.waitFor();
+        waitForListeningPort(mongoPort, timeout, false);
     }
 
     boolean startStdoutBuffering = false;
--- a/src/org/thermostat/qa2/framework/services/Tomcat.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/services/Tomcat.java	Tue Jul 14 19:00:56 2015 +0200
@@ -34,6 +34,7 @@
 import java.io.IOException;
 import org.thermostat.qa2.framework.NativeProcess;
 import org.thermostat.qa2.framework.ThermostatQAConfig;
+import org.thermostat.qa2.framework.utils.CommonUtilities;
 
 /**
  *
@@ -85,8 +86,8 @@
         process.setLabel(getServiceName());
         process.start();
         process.waitFor();
-        waitForListeningPort(webPort, timeout);
-        waitForListeningPort(shutdownPort, timeout);
+        waitForListeningPort(webPort, timeout, true);
+        waitForListeningPort(shutdownPort, timeout, true);
     }
 
     @Override
@@ -101,5 +102,8 @@
         process.setLabel(getServiceName());
         process.start();
         process.waitFor();
+        waitForListeningPort(webPort, timeout, false);
+        waitForListeningPort(shutdownPort, timeout, false);
+        CommonUtilities.sleep(2000);
     }
 }
--- a/src/org/thermostat/qa2/framework/utils/CommonUtilities.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/framework/utils/CommonUtilities.java	Tue Jul 14 19:00:56 2015 +0200
@@ -42,19 +42,25 @@
  */
 public class CommonUtilities {
 
-    public static boolean findInLineList(List<String> lines, String pattern, boolean exact) {
+    public static int getLineIndex(List<String> lines, String pattern, boolean exact) {
+        int i = 0;
         for (String line : lines) {
             if (exact) {
                 if (line.equals(pattern)) {
-                    return true;
+                    return i;
                 }
             } else {
                 if (line.contains(pattern)) {
-                    return true;
+                    return i;
                 }
             }
+            ++i;
         }
-        return false;
+        return -1;
+    }
+
+    public static boolean findInLineList(List<String> lines, String pattern, boolean exact) {
+        return getLineIndex(lines, pattern, exact) >= 0;
     }
 
     public static List<String> addLinesToList(List<String> list, BufferedReader br) throws IOException {
--- a/src/org/thermostat/qa2/tests/AgentWebStorageTest.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/tests/AgentWebStorageTest.java	Tue Jul 14 19:00:56 2015 +0200
@@ -124,6 +124,11 @@
 //        }
         for (String pattern : patterns) {
             boolean b = CommonUtilities.findInLineList(list, pattern, false);
+            if (!b) {
+                for (String s : list) {
+                    System.out.println("INFO: " + s);
+                }
+            }
             assertTrue(b, "pattern not found in log files: " + pattern);
         }
     }
--- a/src/org/thermostat/qa2/tests/CliClientAgentInfoTest.java	Tue Jul 14 18:53:20 2015 +0200
+++ b/src/org/thermostat/qa2/tests/CliClientAgentInfoTest.java	Tue Jul 14 19:00:56 2015 +0200
@@ -30,7 +30,6 @@
  */
 package org.thermostat.qa2.tests;
 
-import java.io.File;
 import java.util.List;
 
 import org.thermostat.qa2.framework.Assert;
@@ -42,6 +41,7 @@
 import org.thermostat.qa2.framework.annotations.RunStorage;
 import org.thermostat.qa2.framework.annotations.Test;
 import org.thermostat.qa2.framework.annotations.RunAgent;
+import org.thermostat.qa2.framework.utils.CommonUtilities;
 import org.thermostat.qa2.framework.utils.ProcessUtilities;
 
 /**
@@ -115,7 +115,8 @@
         Assert.assertContainsPatterns(output, patternListAgents);
         
         //preparate agent id
-        return output.get(5).split(" ")[0];
+        int index = CommonUtilities.getLineIndex(output, "Agent ID", false);
+        return output.get(index + 1).split(" ")[0];
     }
     
     public void checkAgentInfo(String agentId) throws Exception {