view src/org/thermostat/qa2/tests/ShellCommandsTest.java @ 186:7bc828291ea4

Added support for packed thermostat (non-webstorage tests) Changes: Makefile: - introduced new makefile variables THERMOSTAT_HOME_DIR and THERMOSTAT_OTHER_HOME_DIR - by setting them manually it is possible to test packed thermostat src/org/thermostat/qa2/framework/ThermostatQAConfig.java: - single user is now used for client, agent when mongo storage is configured (that configured with thermostat-setup) src/org/thermostat/qa2/framework/utils/ThermostatUtilities.java: - added support for thermostat-setup (in addition to thermostat-devsetup which is not present in packed thermostat) - removed creating new mongo users, which are not needed now storageconfig/keyrings/Default.keyring - updated config for gnome keyring src/org/thermostat/qa2/tests/ShellCommandsTest.java - test is now using method to get client login instead of field as required now src/org/thermostat/qa2/tests/ServicesSmokeTest.java - added delay after stoping service, before checking port (not related)
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 22 May 2015 01:23:01 +0200
parents 3ec1dcfecb16
children a74aa91ab44c
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.ThermostatQAConfig.Login;
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(false));
        Login login = ThermostatQAConfig.getClientLogin();
        shell.writeln(login.getUsername());
        shell.writeln(login.getPassword());
    }

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

}