view src/org/thermostat/qa2/tests/ShellCommandsTest.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 7bc828291ea4
children 381a7ea97aac
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.tests;

import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.thermostat.qa2.framework.Assert.*;
import org.thermostat.qa2.framework.NativeProcess;
import org.thermostat.qa2.framework.Shell;
import org.thermostat.qa2.framework.ThermostatQAConfig;
import org.thermostat.qa2.framework.annotations.RunAgent;
import org.thermostat.qa2.framework.annotations.RunGnomeKeyring;
import org.thermostat.qa2.framework.annotations.RunStorage;
import org.thermostat.qa2.framework.annotations.SetupStorage;
import org.thermostat.qa2.framework.annotations.Test;
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ý
 */
@SetupStorage(type = "mongo")
@RunStorage
@RunAgent
@RunGnomeKeyring
public class ShellCommandsTest {

    public static String[] findTestSuiteVm() throws Exception {
        String line = ThermostatUtilities.getRunningVmLine(testRunnerClassName);
        assertNotNull(line, "there should be testsuite (" + testRunnerClassName + ") vm in list");
        String[] lineParts = line.split(" ");
        return lineParts;
    }

    public static String dumpTestSuiteHeap() throws Exception {
        String[] lineParts = findTestSuiteVm();
        String hostId = lineParts[0];
        String vmId = lineParts[2];
        ThermostatUtilities.dumpHeap(hostId, vmId);

        String line = ThermostatUtilities.findLastHeapDump(hostId, vmId);
        assertNotNull(line, "there should be heap dump in db");
        lineParts = line.split(" ");
        String heapId = lineParts[2];
        return heapId;
    }

    public static String getObjectId(String heapId, String pattern) throws Exception {
        List<String> lines = ThermostatUtilities.findObjects(heapId, pattern);
        String line = null;
        int start = 0;
        for (String currentLine : lines) {
            if (currentLine.contains("ID") && currentLine.contains("TYPE")) {
                break;
            }
            ++start;
        }
        for (; start < lines.size(); ++start) {
            String currentLine = lines.get(start);
            if (currentLine.contains(pattern)) {
                line = currentLine;
                break;
            }
        }
        if (line == null) {
            return null;
        }
        String[] lineParts = line.split(" ");
        String objectId = lineParts[0];
        return objectId;
    }

    public static void connect(Shell shell) throws IOException {
        shell.writeln("connect -d " + ThermostatQAConfig.getStroageUrl(ThermostatUtilities.isWebStorageConfigured()));
    }

    public static final String testRunnerClassName = "org.thermostat.qa2.framework.TestRunner";
    public static final String thisClass = "org.thermostat.qa2.tests.ShellCommandsTest";
    public static final String infiniteLoopClass = thisClass + "$InfiniteLoop";
    public static final String searchedClass1Name = thisClass + "$SearchedClass1";
    public static final String searchedClass2Name = thisClass + "$SearchedClass2";

    @Test
    public void connect() throws Exception {
        Shell shell = ProcessUtilities.createThermostatShell();
        shell.setStdOutMode(NativeProcess.MODE_LOG_AND_BUFFER);

        shell.start();
        connect(shell);
        shell.writeln("exit");
        shell.waitFor();

        List<String> lines = shell.getStdoutLines();
        assertContainsPattern(lines, "Thermostat + > exit");
    }

    @Test
    public void disconnect() throws Exception {
        Shell shell = ProcessUtilities.createThermostatShell();
        shell.setStdOutMode(NativeProcess.MODE_LOG_AND_BUFFER);

        shell.start();
        connect(shell);
        shell.writeln("disconnect");
        shell.writeln("exit");
        shell.waitFor();

        List<String> lines = shell.getStdoutLines();
        assertContainsPattern(lines, "Thermostat - > exit");
    }

    @Test
    public void killVm() throws Exception {
        String line = ThermostatUtilities.getRunningVmLine(thisClass);
        assertNull(line, infiniteLoopClass + " should not be in vm list");

        NativeProcess process = new NativeProcess("java", "-cp", "bin/classes", infiniteLoopClass);
        try {
            process.start();

            CommonUtilities.sleep(2000);

            line = ThermostatUtilities.getRunningVmLine(infiniteLoopClass);
            assertNotNull(line, infiniteLoopClass + " should be in vm list");

            String[] lineParts = line.split(" ");
            String hostId = lineParts[0];
            String vmId = lineParts[2];
            ThermostatUtilities.killVm(hostId, vmId);

            CommonUtilities.sleep(2000);

            line = ThermostatUtilities.getRunningVmLine(infiniteLoopClass);
            assertNull(line, infiniteLoopClass + " should not be in vm list");

        } finally {
            process.destroy();
            process.waitForRaw();
        }
    }

    @Test
    public void saveHeapDumpToFile() throws Exception {
        String file = FileUtilities.getUniqueTempFile("heapDump");
        String heapId = dumpTestSuiteHeap();

        ThermostatUtilities.saveHeapDumpToFile(heapId, file);
        assertTrue(new File(file).exists(), "heap dump file (" + file + ") should exist");
        FileUtilities.removeFile(file);
    }

    @Test
    public void listVms() throws Exception {
        findTestSuiteVm();
    }

    @Test
    public void dumpHeap() throws Exception {
        dumpTestSuiteHeap();
    }

    @Test
    public void findObjects() throws Exception {
        String heapId = dumpTestSuiteHeap();
        String line = getObjectId(heapId, searchedClass1Name);
        assertNull(line, "Instance of class " + searchedClass1Name + " should not be in heap");

        SearchedClass1 toSearch = new SearchedClass1();

        heapId = dumpTestSuiteHeap();
        line = getObjectId(heapId, searchedClass1Name);
        assertNotNull(line, "Instance of class " + searchedClass1Name + " should be in heap");
    }

    @Test
    public void showHistogram() throws Exception {
        String heapId = dumpTestSuiteHeap();
        List<String> lines = ThermostatUtilities.showHistogram(heapId);
        assertNotContainsPattern(lines, searchedClass1Name);

        SearchedClass1 toSearch = new SearchedClass1();

        heapId = dumpTestSuiteHeap();
        lines = ThermostatUtilities.showHistogram(heapId);
        assertContainsPattern(lines, searchedClass1Name);
    }

    @Test
    public void findRoot() throws Exception {
        SearchedClass1 toSearch = new SearchedClass1();
        toSearch.ref = new SearchedClass2();

        String heapId = dumpTestSuiteHeap();

        List<String> lines = ThermostatUtilities.showHistogram(heapId);
        assertContainsPattern(lines, searchedClass1Name);
        assertContainsPattern(lines, searchedClass2Name);

        String line = getObjectId(heapId, searchedClass1Name);
        assertNotNull(line, "Instance of class " + searchedClass1Name + " should be in heap");
        String[] lineParts = line.split(" ");
        String objectId1 = lineParts[0];

        line = getObjectId(heapId, searchedClass2Name);
        assertNotNull(line, "Instance of class " + searchedClass2Name + " should be in heap");
        lineParts = line.split(" ");
        String objectId2 = lineParts[0];

        lines = ThermostatUtilities.findRoot(heapId, objectId2);
        assertContainsPattern(lines, "field ref in " + searchedClass1Name + "@" + objectId1 + " -> " + searchedClass2Name + "@" + objectId2);
    }

    @Test
    public void gc() throws Exception {
        SearchedClass1 toSearch = new SearchedClass1();
        String heapId = dumpTestSuiteHeap();

        // find object instead?
        List<String> lines = ThermostatUtilities.showHistogram(heapId);
        assertContainsPattern(lines, searchedClass1Name);

        toSearch = null;

        String[] lineParts = findTestSuiteVm();
        String vmId = lineParts[2];

        lines = ThermostatUtilities.gc(vmId);
        assertContainsPattern(lines, "GC Successful");

        heapId = dumpTestSuiteHeap();
        lines = ThermostatUtilities.showHistogram(heapId);
        assertNotContainsPattern(lines, searchedClass1Name);
    }

    public static class SearchedClass1 {

        SearchedClass2 ref;
    }

    public static class SearchedClass2 {
    }

    public static class InfiniteLoop {

        public static void main(String[] args) {
            for (;;) {
            }
        }
    }

}