view src/org/thermostat/qa2/framework/utils/ThermostatUtilities.java @ 170:0d34a379de82

- Added new framework (package org.thermosta.qa2.framework) - Removed hardcoded display in org.thermostat.qa.framework.GuiRobot class
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 10 Mar 2015 13:38:36 +0100
parents
children e37e710e45df
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.util.List;
import java.util.Map;
import java.util.Properties;
import org.thermostat.qa2.framework.NativeProcess;
import org.thermostat.qa2.framework.Shell;
import org.thermostat.qa2.framework.ThermostatQAConfig;
import org.thermostat.qa2.framework.services.ThermostatStorage;

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

    public static void setupDefaultMongoUsers(String target) throws Exception {
        ThermostatStorage storage = new ThermostatStorage(target);
        storage.start();
        Map map = new Properties();
        ThermostatQAConfig.Login agentLogin = ThermostatQAConfig.getAgentLogin();
        ThermostatQAConfig.Login clientLogin = ThermostatQAConfig.getClientLogin();
        map.put(agentLogin.getUsername(), agentLogin.getPassword());
        map.put(clientLogin.getUsername(), clientLogin.getPassword());
        setupMongoUsers("127.0.0.1:" + ThermostatQAConfig.mongoPort, map);
        storage.stop();
    }

    public static void setupMongoUsers(String address, Map<String, String> 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");
        mongo.writeln("db.auth(\"mongodevuser\", \"mongodevpassword\")");
        for (Map.Entry<String, String> entry : users.entrySet()) {
            mongo.writeln("db.addUser(\"" + entry.getKey() + "\", \"" + entry.getValue() + "\")");
        }
        mongo.writeln("quit()");
        mongo.waitFor();
    }

    public static void runThermostatSetup(String name) throws Exception {
        runThermostatSetup(ThermostatQAConfig.getThermostatHome(name), ThermostatQAConfig.getThermostatUserHome(name));
    }

    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();
    }

    public static void copyThermostatConfigFiles(String setupTarget, boolean web, boolean badAgentLogin, boolean badClientLogin) throws Exception {
        CommonUtilities.printHeading("Copying thermostat config files ...");
        String thermostatHome = ThermostatQAConfig.getThermostatHome(setupTarget);
        String thermostatUserHome = ThermostatQAConfig.getThermostatUserHome(setupTarget);
        String[] confFiles = ThermostatQAConfig.getThermostatUserConfigFiles(setupTarget, web, badAgentLogin, badClientLogin);
        String[] confFilesNames = ThermostatQAConfig.getThermostatUserConfigFilesNames();
        for (int i = 0; i < confFiles.length; ++i) {
            String file = confFiles[i];
            String name = confFilesNames[i];
            FileUtilities.copyFile(file, thermostatUserHome + File.separator + "etc" + File.separator + name);
        }
        confFiles = ThermostatQAConfig.getThermostatConfigFiles(setupTarget, web);
        for (String file : confFiles) {
            FileUtilities.copyFile(file, thermostatHome + File.separator + "etc" + File.separator + file.substring(file.lastIndexOf(File.separator) + 1));
        }
    }

    public static List<String> getListVmsOutput() throws Exception {
        ThermostatQAConfig.Login login = ThermostatQAConfig.getClientLogin();
        return getListVmsOutput("tested", login.getUsername(), login.getPassword());
    }

    public static List<String> getListVmsOutput(String target, String username, String password) throws Exception {
        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("list-vms");
        thermostat.writeln(username);
        thermostat.writeln(password);
        thermostat.writeln("exit");
        thermostat.waitFor();
        return thermostat.getStdoutLines();
    }

}