view src/org/thermostat/qa2/framework/NativeProcess.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 0d34a379de82
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 org.thermostat.qa2.framework.streamprocessors.StreamProcessor;
import org.thermostat.qa2.framework.streamprocessors.BufferingStreamProcessor;
import org.thermostat.qa2.framework.streamprocessors.LoggingStreamProcessor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.thermostat.qa2.framework.utils.ProcessUtilities;

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

    public static final int MODE_NONE = 0;
    public static final int MODE_CLOSE = 1;
    public static final int MODE_DISCARD = 2;
    public static final int MODE_LOG = 4;
    public static final int MODE_BUFFER = 8;
    public static final int MODE_LOG_AND_BUFFER = 4 | 8;

    ProcessBuilder processBuilder;
    public java.lang.Process process;
    String[] commands;

    public Thread stdoutProcessingThread = null;
    public int stdoutMode = MODE_LOG;
    public StreamProcessor stdoutStreamProcessor;

    public Thread stderrProcessingThread = null;
    public int stderrMode = MODE_LOG;
    public StreamProcessor stderrStreamProcessor;

    public PrintStream output = System.out;
    SimpleLogger logger = new SimpleLogger(output);

    String label = null;

    public NativeProcess(String command) {
        commands = new String[]{command};
        processBuilder = new ProcessBuilder(command);
    }

    public NativeProcess(String... command) {
        commands = command;
        processBuilder = new ProcessBuilder(command);
    }

    public void setWorkingDirectory(String s) {
        processBuilder.directory(new File(s));
    }

    public void setWorkingDirectory(File f) {
        processBuilder.directory(f);
        if (label != null) {
            logger.setPrefix(label + " (workdir setup)");
            logger.log(f.getPath());
        }
    }

    public void setEnvironmentVariable(String name, String value) {
        processBuilder.environment().put(name, value);
        if (label != null) {
            logger.setPrefix(label + " (environment add)");
            logger.log(name + "=" + value);
        }
    }

    public void setLabel(String label) {
        this.label = label;

    }

    public void start() throws IOException {
        if (label == null) {
            setLabel(getName());
        } else {
            logger.setPrefix(label);
        }
        logger.log(ProcessUtilities.getArgsString(commands));
        process = processBuilder.start();
        startLogging();
    }

    public void destroy() {
        try {
            process.getOutputStream().close();
//            process.getInputStream().close();
//            process.getErrorStream().close();
        } catch (IOException ex) {
            Logger.getLogger(NativeProcess.class.getName()).log(Level.SEVERE, null, ex);
        }
        process.destroy();
    }

    public String getName() {
        return commands[0];
    }

    private StreamProcessor initStreamProcessor(InputStream is, PrintStream out, String postfix, int mode, InputStream comparator) throws IOException {
        StreamProcessor sp;
        switch (mode) {
            case MODE_NONE:
                sp = null;
                break;
            case MODE_CLOSE:
                sp = null;
                is.close();
                break;
            case MODE_DISCARD:
                sp = new StreamProcessor(is);
                break;
            case MODE_LOG:
                sp = new LoggingStreamProcessor(is, new SimpleLogger(out, label + postfix));
                break;
            case MODE_BUFFER:
                sp = new BufferingStreamProcessor(is, null, false);
                break;
            case MODE_LOG_AND_BUFFER:
                sp = new BufferingStreamProcessor(is, new SimpleLogger(out, label + postfix), true);
                break;
            default:
                throw new IllegalArgumentException("Unknown stream processing mode");
        }
        return sp;
    }

    private void startLogging() throws IOException {

        stdoutStreamProcessor = initStreamProcessor(process.getInputStream(), System.out, " (stdout)", stdoutMode, null);
        if (stdoutStreamProcessor != null) {
            stdoutProcessingThread = new Thread(stdoutStreamProcessor);
            stdoutProcessingThread.setDaemon(true);
            stdoutProcessingThread.start();
        }

        stderrStreamProcessor = initStreamProcessor(process.getErrorStream(), System.out, " (stderr)", stderrMode, null);
        if (stderrStreamProcessor != null) {
            stderrProcessingThread = new Thread(stderrStreamProcessor);
            stderrProcessingThread.setDaemon(true);
            stderrProcessingThread.start();
        }

    }

    public void setStdOutMode(int mode) {
        stdoutMode = mode;
    }

    public void setStdErrMode(int mode) {
        stderrMode = mode;
    }

    public StreamProcessor getStdoutProcessor() {
        return stdoutStreamProcessor;
    }

    public List<String> getStdoutLines() throws IOException {
        return ((BufferingStreamProcessor) getStdoutProcessor()).getLineList();
    }

    public StreamProcessor getStderrProcessor() {
        return stderrStreamProcessor;
    }

    public List<String> getStderrLines() throws IOException {
        return ((BufferingStreamProcessor) getStderrProcessor()).getLineList();
    }

    public int waitForRaw() throws InterruptedException {
        int exitCode;
        for (;;) {
            try {
                exitCode = process.waitFor();
            } catch (InterruptedException e) {
                continue;
            }
            break;
        }
        if (stdoutProcessingThread != null) {
            stdoutProcessingThread.join();
        }
        if (stderrProcessingThread != null) {
            stderrProcessingThread.join();
        }
        logger.setPrefix(label + " (exit code)");
        logger.log(exitCode);
        return exitCode;
    }

    public void waitFor() throws Exception {
        int exitCode = waitForRaw();
        if (exitCode != 0) {
            throw new Exception("non zero exit code: " + exitCode);
        }
    }

}