view src/org/thermostat/qa2/framework/TestRunner.java @ 203:a74aa91ab44c

Fixed setup/gnome-keyring/collections + added debugging changes: org.thermostat.qa2.framework.services.GnomeKeyring: - changed way how gnome-keyring is started (+ now encrypted) org.thermostat.qa2.framework.services.ShellService: - fix for arguments with spaces org.thermostat.qa2.framework.services.ThermostatAgent: org.thermostat.qa2.framework.services.ThermostatGui: org.thermostat.qa2.framework.services.ThermostatService: org.thermostat.qa2.framework.services.ThermostatStorage: - thermostat is now started with collection enabled when configured. org.thermostat.qa2.framework.utils.FileUtilities: - copyFile method now operates recursively on directories org.thermostat.qa2.framework.utils.ProcessUtilities: - new helper functions to run command witch collection enabled, handle arguments with spaces org.thermostat.qa2.framework.utils.ThermostatUtilities: - fixed method performing thermostat setup - new helper functions to get thermostat command line, passing of credentials removed from shell helper function org.thermostat.qa2.framework.NativeProcess: - logging of command which has arguments with spaces fixed org.thermostat.qa2.framework.TestRunner: - changes related to thermostat setup, gnome-keyring startup - added support for debugging org.thermostat.qa2.framework.ThermostatQAConfig: Makefile: - support of new properties required for debugging, gnome-keyring, collections support org.thermostat.qa2.framework.tests.*: - changes to tests which operate on thermostat shell, credentials no longer needed (use keyring)
author Zdenek Zambersky <zzambers@redhat.com>
date Mon, 30 Nov 2015 19:08:04 +0100
parents b82b8bc167ed
children
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA 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 ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.thermostat.qa2.framework.ThermostatQAConfig.Login;
import org.thermostat.qa2.framework.annotations.*;
import org.thermostat.qa2.framework.services.*;
import org.thermostat.qa2.framework.utils.AnnotationUtilities;
import org.thermostat.qa2.framework.utils.CommonUtilities;
import org.thermostat.qa2.framework.utils.FileUtilities;
import org.thermostat.qa2.framework.utils.ProcessUtilities;
import org.thermostat.qa2.framework.utils.ThermostatUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public class TestRunner {

    static ArrayList<Service> services = new ArrayList();

    boolean interrupted = false;
    String testClass;

    public void setTestClass(String name) {
        testClass = name;
    }

    public synchronized void interrupt() {
        interrupted = true;
    }

    public synchronized boolean isInterrupted() {
        return interrupted;
    }

    public void run() {
        try {
            Class c = Class.forName(testClass);
            runTestClass(c);
        } catch (Exception ex) {
        }

    }

    public void runTestClass(Class c) throws Exception {
        List<Method> methods = Arrays.asList(c.getDeclaredMethods());
        List<Method> testMethods = AnnotationUtilities.getAnnotatedObjects(methods, Test.class);
        int testCount = testMethods.size();
        if (testCount > 0) {
            long startTime = System.currentTimeMillis();
            int passedCount = 0;
            int failedCount = 0;
            int errorCount = 0;
            int ignoredCount = 0;

            Object o = c.newInstance();
            String className = c.getCanonicalName();

            List<Method> beforeMethods = AnnotationUtilities.getAnnotatedObjects(methods, Before.class);
            boolean beforeMethodFailed = false;
            for (Method m : beforeMethods) {
                Throwable t = null;
                try {
                    runThermostatSetup(m);
                    CommonUtilities.printHeading("Running \"before\" method: " + m.getName() + " ...");
                    m.invoke(o);
                } catch (Throwable e) {
                    t = e;
                    beforeMethodFailed = true;
                } finally {
                    runServiceCleanups();
                }
                String logNameString = "@Before: " + className + "." + m.getName();
                if (t != null) {
                    System.out.println("ERROR: " + logNameString);
                    t.printStackTrace(System.out);
                    break;
                } else {
                    System.out.println("PASSED: " + logNameString);
                }
            }
            for (Method m : testMethods) {
                TestResult result;
                boolean afterSetup = false;
                Throwable t = null;
                testrun:
                if (beforeMethodFailed || isInterrupted()) {
                    result = TestResult.ERROR;
                } else {
                    try {
                        runThermostatSetup(m);
                        afterSetup = true;
                        if (isInterrupted()) {
                            result = TestResult.ERROR;
                            break testrun;
                        }
                        CommonUtilities.printHeading("Running test method: " + m.getName() + " ...");
                        m.invoke(o);
                        result = TestResult.PASSED;
                    } catch (Throwable e) {
                        t = e;
                        //e.printStackTrace();
                        System.out.println("INFO: thrown: " + e);
                        result = afterSetup ? TestResult.FAILED : TestResult.ERROR;
                    } finally {
                        if (ThermostatQAConfig.isDebugMode()) {
                            try {
                                if (ThermostatUtilities.isWebStorageConfigured()) {
                                    // save thermostat logs
                                    String tomcatLogsDir = ThermostatQAConfig.getTomcatHome() + File.separator + "logs";
                                    if (new File(tomcatLogsDir).exists()) {
                                        debugStore(m, tomcatLogsDir, "tomcat-logs");
                                    }
                                }
                                // save keyring configuration
                                String keyringDir = ThermostatQAConfig.getGnomeKeyringConfigDir() + File.separator + "keyrings";
                                debugStore(m, keyringDir, "keyrings");
                            } catch (Exception e) {
                            }
                        }
                        CommonUtilities.printHeading("Running automatic cleanup: ...");
                        runServiceCleanups();
                    }
                }
                String testName = className + "." + m.getName();
                switch (result) {
                    case PASSED:
                        System.out.println("PASSED: " + testName);
                        ++passedCount;
                        break;
                    case FAILED:
                        System.out.println("FAILED: " + testName);
                        if (t != null) {
                            t.printStackTrace(System.out);
                        }
                        ++failedCount;
                        break;
                    case ERROR:
                        System.out.println("ERROR: " + testName);
                        if (t != null) {
                            t.printStackTrace(System.out);
                        }
                        ++errorCount;
                        break;
                    case IGNORED:
                        System.out.println("IGNORED: " + testName);
                        ++ignoredCount;
                        break;
                }
            }
            long duration = System.currentTimeMillis() - startTime;
            System.out.println("SUMMARY: " + className + "    total: " + testCount
                    + "    passed: " + passedCount + "    failed: " + failedCount
                    + "    error: " + errorCount + "    ignored: " + ignoredCount
                    + "    duration: " + duration);
        }
    }

    public static void runThermostatSetup(Method testMethod) throws Exception {
        ThermostatSetupTarget setupTargetA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, ThermostatSetupTarget.class);
        if (setupTargetA == null) {
            return;
        }
        SetupThermostat setupThermostatA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, SetupThermostat.class);
        SetupStorage setupStorageA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, SetupStorage.class);
        RunStorage startStorageA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunStorage.class);
        RunAgent startAgentA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunAgent.class);
        RunGnomeKeyring startGnomeKeyringA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunGnomeKeyring.class);
        RunGui startGuiA = AnnotationUtilities.findAnnotationWithClassInheritance(testMethod, RunGui.class);

        boolean setupThermostat = setupThermostatA != null;
        boolean setupStorage = setupStorageA != null;
        boolean startStorage = startStorageA != null;
        boolean startAgent = startAgentA != null;
        boolean startGnomeKeyring = startGnomeKeyringA != null;
        boolean startGui = startGuiA != null;

        String setupTarget = setupTargetA.value();

        // start gnome keyring
        if (startGnomeKeyring) {
            String keyringDir = ThermostatQAConfig.getGnomeKeyringConfigDir() + File.separator + "keyrings";
            FileUtilities.removeFile(keyringDir);
            ProcessUtilities.run("mkdir", "-p", keyringDir);
            GnomeKeyring keyring = new GnomeKeyring();
            keyring.start();
        }
        // setup storage
        if (setupStorage) {
            String storageType = setupStorageA.type();
            boolean web = ThermostatQAConfig.getStorageTypeAsBoolean(storageType);
            boolean badAgentLogin = setupStorageA.badAgentLogin();
            boolean badClientLogin = setupStorageA.badClientLogin();
            boolean agentSaveOnExit = setupStorageA.agentSaveOnExit();
            ThermostatUtilities.setupThermostat(setupTarget, web, badAgentLogin, badClientLogin, agentSaveOnExit, true, false);

            if (ThermostatQAConfig.isDebugMode()) {
                try {
                    // save thermostat user home and etc dirs
                    debugStore(testMethod, ThermostatQAConfig.getThermostatUserHome(), "thermostat-user-home");
                    debugStore(testMethod, ThermostatQAConfig.getThermostatHome(setupTarget) + File.separator + "etc", "etc");
                    // remove db from saved thermostat user home as it has ~160MB
                    FileUtilities.removeFile(ThermostatQAConfig.getDebugDir() + File.separator
                            + testMethod.getName() + File.separator
                            + "thermostat-user-home" + File.separator
                            + "data" + File.separator + "db");
                } catch (Exception e) {
                }
            }
        }
        // start storage
        if (startStorage) {
            String storageType = setupStorageA.type();
            boolean web = ThermostatQAConfig.getStorageTypeAsBoolean(storageType);
            ThermostatStorage storage = new ThermostatStorage(setupTarget);
            storage.start();
            if (web) {
                Tomcat tomcat = new Tomcat();
                tomcat.start();
            }
        }
        // start agent
        if (startAgent) {
            ThermostatAgent agent = new ThermostatAgent(setupTarget);
            agent.start();
        }
        // start gui
        if (startGui) {
            ThermostatGui gui = new ThermostatGui(setupTarget);
            gui.start();
        }
    }

    public static void debugStore(Method m, String src, String name) throws Exception {
        String methodName = m.getName();
        String debugDir = ThermostatQAConfig.getDebugDir() + File.separator + methodName;
        ProcessUtilities.run("mkdir", "-p", debugDir);
        FileUtilities.copyFile(src, debugDir + File.separator + name);
    }

    public static void registerServiceCleanup(Service service) {
        services.add(service);
    }

    public static void unregisterServiceCleanup(Service service) {
        services.remove(service);
    }

    public static void runServiceCleanups() {
        for (int i = services.size() - 1; i >= 0; --i) {
            try {
                services.get(i).stop();
                services.remove(i);
            } catch (Exception ex) {
            }
        }
    }

    public static void runTests(final TestRunner r) {
        final Thread testingThread = new Thread() {
            @Override
            public void run() {
                r.run();
            }
        };
        final Thread hookThread = new Thread() {
            @Override
            public void run() {
                try {
                    r.interrupt();
                    if (testingThread.isAlive()) {
                        testingThread.join();
                    }
                } catch (InterruptedException ex) {
                }
            }
        };
        testingThread.setDaemon(true);
        Runtime runtime = Runtime.getRuntime();
        runtime.addShutdownHook(hookThread);
        testingThread.start();
        try {
            testingThread.join();
        } catch (InterruptedException ex) {
        }
        runtime.removeShutdownHook(hookThread);
        System.exit(0);
    }

    public static void main(String[] args) {
        if (args.length > 0) {
            String className = args[0];
            try {
                Class c = Class.forName(className);
                try {
                    TestRunner runner = new TestRunner();
                    runner.testClass = className;
                    runTests(runner);
                } catch (Exception ex) {
                    Logger.getLogger(TestRunner.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(TestRunner.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

}