view src/org/thermostat/qa2/framework/utils/ThermostatUtilities.java @ 181:e37e710e45df

added support for compatibility tests and some other improvements (generating thermostat config files from templates, posiblity to show @Before methods logs in report, ...) Changes: Makefile: - added targets new for compatibility testing (and report generation) src/org/thermostat/qa2/framework/utils/ThermostatUtilities.java: - all thermostat and storage configuration code moved from TestRunner to ThermostatUtilities class - webapp deployment (tomcat) is moved from makefile to testsuite (ThermostatUtilities class) - storage configuration files are now generated from templates, which are placed in templates directory - added helper methods, which make using thermostat shell simpler src/org/thermostat/qa2/framework/utils/ThermostatUtilities.java: - code needed for replacing patterns moved from reporters Generator class to CommonUtilities class (because it is now also used to generate config files from templates) src/org/thermostat/qa2/reporter/Generator.java: src/org/thermostat/qa2/reporter/LogParser.java: src/org/thermostat/qa2/reporter/result/TestClassResult.java: src/org/thermostat/qa2/framework/TestRunner.java - added possibility to show logs of methods annotaded with @Before annotation, when some of tests in that their test class failes src/org/thermostat/qa2/tests/DBCommandsHeapDumpTest.java: - code updated to use new methods from ThermostatUtilities class src/org/thermostat/qa2/framework/annotations/SetupStorage.java - it is now possible set SAVE_ON_EXIT property in agent.properties using this annotation src/org/thermostat/qa2/framework/services/ThermostatStorage.java - framework is now not waiting for timeout, when message about thermostat setup is displayed src/org/thermostat/qa2/framework/ThermostatQAConfig.java - added/updated to contain framework new configuration data, removed code not needed anymore templates/storage-config/* - added thermostat config files templates
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 15 May 2015 18:51:56 +0200
parents 0d34a379de82
children 3ec1dcfecb16
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.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.thermostat.qa2.framework.NativeProcess;
import org.thermostat.qa2.framework.Shell;
import org.thermostat.qa2.framework.ThermostatQAConfig;
import org.thermostat.qa2.framework.ThermostatQAConfig.Login;
import org.thermostat.qa2.framework.services.BackupService;
import org.thermostat.qa2.framework.services.ThermostatStorage;

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

    /* runs thermostat setup (creates thermostat user home dir (database)),
     backup service (on thermostat user home) is started  before if requested */
    public static void setupThermostat(String setupTarget, boolean backupUserHome) throws Exception {
        String thermostatHome = ThermostatQAConfig.getThermostatHome(setupTarget);
        String thermostatUserHome = ThermostatQAConfig.getThermostatUserHome(setupTarget);
        if (new File(thermostatUserHome).exists()) {
            if (backupUserHome) {
                BackupService backupService = new BackupService(thermostatUserHome);
                backupService.start();
            }
            ProcessUtilities.run("rm", "-rf", "--", thermostatUserHome);
        }
        runThermostatSetup(thermostatHome, thermostatUserHome);
    }

    public static void runThermostatSetup(String thermostatHome, String thermostatUserHome) throws Exception {
        NativeProcess process;
        CommonUtilities.printHeading("Running Thermostat setup ...");
        String setupPath1 = thermostatHome + File.separator + "bin" + File.separator + "thermostat-devsetup";
        String setupPath2 = thermostatHome + File.separator + "bin" + File.separator + "thermostat-setup-user-home";
        if (new File(setupPath1).exists()) {
            process = new NativeProcess(setupPath1);
        } else if (new File(setupPath2).exists()) {
            process = new NativeProcess(setupPath2);
        } else {
            throw new Exception("script to setup thermostat cannot be found in: " + thermostatHome + File.separator + "bin");
        }
        process.setLabel("Thermostat setup");
        process.setEnvironmentVariable("USER_THERMOSTAT_HOME", thermostatUserHome);
        process.start();
        process.waitFor();
    }

    /* does all required tasks to setup storage as requested,
     it does not modify existing storage (database),
     it can also start backup services if reqested */
    public static void setupStorage(String setupTarget, boolean web, boolean badAgentLogin, boolean badClientLogin, boolean agentSaveOnExit, boolean backupThermostatConfig, boolean backupUserHome) throws Exception {
        if (!web) {
            setupDefaultMongoUsers(setupTarget);
        } else {
            deployThermostatWebApp(setupTarget);

            String tomcatHome = ThermostatQAConfig.getTomcatHome();
            CommonUtilities.printHeading("Cleaning up tomcat logs");
            ProcessUtilities.shellRun("rm -rf -- \"" + tomcatHome + File.separator + "logs" + File.separator + "\"" + "*");
        }
        if (backupThermostatConfig) {
            BackupService backupService = new BackupService(ThermostatQAConfig.getThermostatHome(setupTarget) + File.separator + "etc");
            backupService.start();
        }
        if (backupUserHome) {
            String thermostatUserHome = ThermostatQAConfig.getThermostatUserHome(setupTarget);
            if (new File(thermostatUserHome).exists()) {
                BackupService backupService = new BackupService(thermostatUserHome);
                backupService.start();
            }
        }

        copyThermostatConfigFiles(setupTarget, web, badAgentLogin, badClientLogin, agentSaveOnExit);
    }

    public static void setupDefaultMongoUsers(String target) throws Exception {
        ThermostatStorage storage = new ThermostatStorage(target);
        storage.start();
        List<Login> users = new ArrayList();
        users.add(ThermostatQAConfig.getAgentLogin());
        users.add(ThermostatQAConfig.getClientLogin());
        setupMongoUsers("127.0.0.1:" + ThermostatQAConfig.mongoPort, users);
        storage.stop();
    }

    public static void setupMongoUsers(String address, List<Login> users) throws Exception {
        CommonUtilities.printHeading("Setting up mongo users ...");
        Shell mongo = new Shell("mongo", address);
        mongo.setLabel("Mongo users setup");
        mongo.start();
        mongo.writeln("use thermostat");
        Login login = ThermostatQAConfig.getMongoLogin();
        mongo.writeln("db.auth(\"" + login.getUsername() + "\", \"" + login.getPassword() + "\")");
        for (Login user : users) {
            mongo.writeln("db.addUser(\"" + user.getUsername() + "\", \"" + user.getPassword() + "\")");
        }
        mongo.writeln("quit()");
        mongo.waitFor();
    }

    private static boolean webStorage = false;

    public static void copyThermostatConfigFiles(String setupTarget, boolean web, boolean badAgentLogin, boolean badClientLogin, boolean saveOnExit) throws Exception {
        CommonUtilities.printHeading("Copying thermostat config files ...");
        webStorage = web;
        String thermostatUserConfigDir = ThermostatQAConfig.getThermostatUserHome(setupTarget) + File.separator + "etc";
        String thermostatConfigDir = ThermostatQAConfig.getThermostatHome(setupTarget) + File.separator + "etc";
        FileUtilities.printLineListToFile(thermostatUserConfigDir + File.separator + "agent.auth", getAgentAuth(badAgentLogin));
        FileUtilities.printLineListToFile(thermostatUserConfigDir + File.separator + "agent.properties", getAgentProperties(web, saveOnExit));
        FileUtilities.printLineListToFile(thermostatUserConfigDir + File.separator + "client.properties", getClientProperties(web, badClientLogin));

        FileUtilities.printLineListToFile(thermostatConfigDir + File.separator + "thermostat-users.properties", getThermostatUsers());
        FileUtilities.printLineListToFile(thermostatConfigDir + File.separator + "thermostat-roles.properties", getThermostatRoles());
    }

    public static final String storageTemplatesDir = "templates" + File.separator + "storage-config";
    public static final String usernamePattern = "${USERNAME}";
    public static final String passwordPattern = "${PASSWORD}";

    public static List<String> getAgentAuth(boolean badLogin) throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "agent.auth");
        Map<String, List<String>> patterns = new HashMap();

        ThermostatQAConfig.Login login = badLogin ? ThermostatQAConfig.getAgentBadLogin() : ThermostatQAConfig.getAgentLogin();
        CommonUtilities.addReplacementToMap(patterns, usernamePattern, login.getUsername());
        CommonUtilities.addReplacementToMap(patterns, passwordPattern, login.getPassword());

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String saveOnExitPattern = "${SAVE_ON_EXIT}";
    public static final String dbUrlPattern = "${DB_URL}";

    public static List<String> getAgentProperties(boolean web, boolean saveOnExit) throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "agent.properties");
        Map<String, List<String>> patterns = new HashMap();

        CommonUtilities.addReplacementToMap(patterns, saveOnExitPattern, java.lang.Boolean.toString(saveOnExit));
        CommonUtilities.addReplacementToMap(patterns, dbUrlPattern, ThermostatQAConfig.getStroageUrl(web));

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String connectionUrlPattern = "${CONNECTION_URL}";

    public static List<String> getClientProperties(boolean web, boolean badLogin) throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "client.properties");
        Map<String, List<String>> patterns = new HashMap();

        ThermostatQAConfig.Login login = badLogin ? ThermostatQAConfig.getClientBadLogin() : ThermostatQAConfig.getClientLogin();
        CommonUtilities.addReplacementToMap(patterns, usernamePattern, login.getUsername());
        CommonUtilities.addReplacementToMap(patterns, connectionUrlPattern, ThermostatQAConfig.getStroageUrl(web).replace(":", "\\:"));

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String usersPattern = "${USERS}";

    public static List<String> getThermostatUsers() throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "thermostat-users.properties");
        Map<String, List<String>> patterns = new HashMap();

        List<String> userLines = new ArrayList();
        Login agentLogin = ThermostatQAConfig.getAgentLogin();
        Login clientLogin = ThermostatQAConfig.getClientLogin();
        userLines.add(agentLogin.getUsername() + "=" + agentLogin.getPassword());
        userLines.add(clientLogin.getUsername() + "=" + clientLogin.getPassword());
        patterns.put(usersPattern, userLines);

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String agentUsernamePattern = "${AGENT_USERNAME}";
    public static final String clientUsernamePattern = "${CLIENT_USERNAME}";

    public static List<String> getThermostatRoles() throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "thermostat-roles.properties");
        Map<String, List<String>> patterns = new HashMap();

        Login agentLogin = ThermostatQAConfig.getAgentLogin();
        Login clientLogin = ThermostatQAConfig.getClientLogin();
        CommonUtilities.addReplacementToMap(patterns, agentUsernamePattern, agentLogin.getUsername());
        CommonUtilities.addReplacementToMap(patterns, clientUsernamePattern, clientLogin.getUsername());

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static boolean isWebStorageConfigured() {
        return webStorage;
    }

    public static void deployThermostatWebApp(String setupTarget) throws Exception {
        CommonUtilities.printHeading("Deploying thermostat web app ...");
        String deployDir = ThermostatQAConfig.getTomcatHome() + File.separator + "webapps";
        String deployedAppPath = deployDir + File.separator + "thermostat";
        if (new File(deployedAppPath).exists()) {
            ProcessUtilities.shellRun("rm -rf " + deployedAppPath);
        }
        String path = ThermostatQAConfig.getWebAppPath(setupTarget);
        String name = new File(path).getName();

        ProcessUtilities.shellRun("cp -r " + path + " " + deployDir);
        ProcessUtilities.shellRun("mv " + deployDir + File.separator + new File(path).getName() + " " + deployedAppPath);
    }

    /* Starts thermostat shell executes requested command in it,
     writes username/password, exits shell and returns output as line list */
    public static List<String> getShellCommandOutput(String target, String command) throws Exception {
        ThermostatQAConfig.Login login = ThermostatQAConfig.getClientLogin();
        String username = login.getUsername();
        String password = login.getPassword();

        String[] thermostatCmds = {ThermostatQAConfig.getThermostatExecutablePath(target), "shell"};
        Shell thermostat = new Shell(thermostatCmds);
        thermostat.setEnvironmentVariable("USER_THERMOSTAT_HOME", ThermostatQAConfig.getThermostatUserHome(target));
        thermostat.setStdOutMode(NativeProcess.MODE_LOG_AND_BUFFER);
        thermostat.start();
        thermostat.writeln(command);
        thermostat.writeln(username);
        thermostat.writeln(password);
        thermostat.writeln("exit");
        thermostat.waitFor();
        return thermostat.getStdoutLines();
    }

    public static List<String> getListVmsOutput() throws Exception {
        return getListVmsOutput("tested");
    }

    public static List<String> getListVmsOutput(String target) throws Exception {
        return getShellCommandOutput(target, "list-vms");
    }

    public static String getRunningVmLine() throws Exception {
        return ThermostatUtilities.getRunningVmLine("tested");
    }

    public static String getRunningVmLine(String target) throws Exception {
        List<String> vmsLines = ThermostatUtilities.getListVmsOutput(target);
        String vmLine = null;

        for (String line : vmsLines) {
            if (line.contains("RUNNING") && !line.contains("com.redhat.thermostat.main.Thermostat")) {
                vmLine = line;
                break;
            }
        }
        return vmLine;
    }

    public static List<String> getListHeapDumpsOutput(String target) throws Exception {
        return getShellCommandOutput(target, "list-heap-dumps");
    }

    public static String getHeapDumpLine(String hostId, String vmId) throws Exception {
        return getHeapDumpLine("tested", hostId, vmId);
    }

    public static String getHeapDumpLine(String target, String hostId, String vmId) throws Exception {
        List<String> heapLines = ThermostatUtilities.getListHeapDumpsOutput(target);
        String heapLine = null;

        for (String line : heapLines) {
            if (line.contains(hostId) && line.contains(vmId)) {
                heapLine = line;
                break;
            }
        }
        return heapLine;
    }

    public static void dumpHeap(String hostId, String vmId) throws Exception {
        dumpHeap("tested", hostId, vmId);
    }

    public static void dumpHeap(String target, String hostId, String vmId) throws Exception {
        getShellCommandOutput(target, "dump-heap --hostId " + hostId + " --vmId " + vmId);
    }

    public static List<String> profileVm(String target, String hostId, String vmId, String action) throws Exception {
        return getShellCommandOutput(target, "profile-vm --hostId " + hostId + " --vmId " + vmId + " " + action);
    }

}