view src/org/thermostat/qa2/tests/AgentSaveOnExitTest.java @ 185:3ec1dcfecb16

Adds new test class ShellCommandsTest, which tests commands in thermostat shell.
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 15 May 2015 19:12:02 +0200
parents 98c90d0f337d
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.util.List;
import static org.thermostat.qa2.framework.Assert.assertFalse;
import static org.thermostat.qa2.framework.Assert.assertNotNull;
import static org.thermostat.qa2.framework.Assert.assertNull;
import static org.thermostat.qa2.framework.Assert.assertRuns;
import static org.thermostat.qa2.framework.Assert.assertTrue;
import org.thermostat.qa2.framework.annotations.RunGnomeKeyring;
import org.thermostat.qa2.framework.annotations.SetupStorage;
import org.thermostat.qa2.framework.annotations.Test;
import org.thermostat.qa2.framework.services.ThermostatAgent;
import org.thermostat.qa2.framework.services.ThermostatFullStorage;
import org.thermostat.qa2.framework.utils.CommonUtilities;
import org.thermostat.qa2.framework.utils.ThermostatUtilities;

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

    /* storage and agent of first thermostat (target1) are started, heap dump is performed, storage and agent are stopped
     storage of second thermostat (target2) is started, heap dump is expected or not expected to be in db, based on saveOnExit value */
    public static void heapDumpTestImpl(String target1, String target2, boolean saveOnExit) throws Exception {
        // first thermostat
        ThermostatFullStorage thermostatOtherStorage = new ThermostatFullStorage(target1);
        ThermostatAgent thermostatOtherAgent = new ThermostatAgent(target1);
        thermostatOtherStorage.start();
        thermostatOtherAgent.start();
        assertRuns(thermostatOtherAgent, "Agent should be running");

        String[] vmLineSegments = ThermostatUtilities.getRunningVmLine(target1, null).split(" ");
        String hostId = vmLineSegments[0];
        String vmId = vmLineSegments[2];

        String heapDump = ThermostatUtilities.getHeapDumpLine(target1, hostId, vmId);
        assertNull(heapDump, "there should not be any heap dump yet");

        ThermostatUtilities.dumpHeap(target1, hostId, vmId);
        heapDump = ThermostatUtilities.getHeapDumpLine(target1, hostId, vmId);
        assertNotNull(heapDump, "there should be heap dump in db");

        thermostatOtherAgent.stop();
        thermostatOtherStorage.stop();

        // second thermostat
        ThermostatFullStorage thermostatStorage = new ThermostatFullStorage(target2);
        thermostatStorage.start();

        heapDump = ThermostatUtilities.getHeapDumpLine(target2, hostId, vmId);
        if (saveOnExit) {
            assertNotNull(heapDump, "there should be heap dump in db");
        } else {
            assertNull(heapDump, "there should not be any heap dump in db");
        }
        thermostatStorage.stop();
    }

    static boolean containsProfilingData(List<String> profileOutput) {
        int counter = 0;
        int size = profileOutput.size();
        for (; counter < size; ++counter) {
            String line = profileOutput.get(counter);
            if (line.contains("% Time") && line.contains("Time (ms)") && line.contains("Method Name")) {
                return true;
            }
        }
        return false;
    }

    /* storage and agent of first thermostat (target1) are started, profiling is performed, storage and agent are stopped
     storage of second thermostat (target2) is started, profiling data are expected or not expected to be in db, based on saveOnExit value */
    public static void profileTestImpl(String target1, String target2, boolean saveOnExit) throws Exception {
        // first thermostat
        ThermostatFullStorage thermostatOtherStorage = new ThermostatFullStorage(target1);
        ThermostatAgent thermostatOtherAgent = new ThermostatAgent(target1);
        thermostatOtherStorage.start();
        thermostatOtherAgent.start();
        assertRuns(thermostatOtherAgent, "Agent should be running");

        String[] vmLineSegments = ThermostatUtilities.getRunningVmLine(target1, null).split(" ");
        String hostId = vmLineSegments[0];
        String vmId = vmLineSegments[2];

        List<String> profileOutput = ThermostatUtilities.profileVm(target1, hostId, vmId, "show");
        assertFalse(containsProfilingData(profileOutput), "there should not be any profiling data in db yet");

        ThermostatUtilities.profileVm(target1, hostId, vmId, "start");
        CommonUtilities.sleep(5000);
        ThermostatUtilities.profileVm(target1, hostId, vmId, "stop");

        profileOutput = ThermostatUtilities.profileVm(target1, hostId, vmId, "show");
        assertTrue(containsProfilingData(profileOutput), "there should be profiling data in db");

        thermostatOtherAgent.stop();
        thermostatOtherStorage.stop();

        // second thermostat
        ThermostatFullStorage thermostatStorage = new ThermostatFullStorage(target2);
        thermostatStorage.start();

        profileOutput = ThermostatUtilities.profileVm(target2, hostId, vmId, "show");
        if (saveOnExit) {
            assertTrue(containsProfilingData(profileOutput), "there should be profiling data in db");
        } else {
            assertFalse(containsProfilingData(profileOutput), "there should not be any profiling data in db");
        }
        thermostatStorage.stop();
    }

    public static String target = "tested";

    @Test
    @SetupStorage(type = "mongo", agentSaveOnExit = true)
    public void heapDumpSave() throws Exception {
        heapDumpTestImpl(target, target, true);
    }

    @Test
    @SetupStorage(type = "mongo", agentSaveOnExit = false)
    public void heapDumpNotSave() throws Exception {
        heapDumpTestImpl(target, target, false);
    }

    @Test
    @SetupStorage(type = "web", agentSaveOnExit = true)
    public void heapDumpWebSave() throws Exception {
        heapDumpTestImpl(target, target, true);
    }

    @Test
    @SetupStorage(type = "web", agentSaveOnExit = false)
    public void heapDumpWebNotSave() throws Exception {
        heapDumpTestImpl(target, target, false);
    }

    @Test
    @SetupStorage(type = "mongo", agentSaveOnExit = true)
    public void profileSave() throws Exception {
        profileTestImpl(target, target, true);
    }

    @Test
    @SetupStorage(type = "mongo", agentSaveOnExit = false)
    public void profileNotSave() throws Exception {
        profileTestImpl(target, target, false);
    }

    @Test
    @SetupStorage(type = "web", agentSaveOnExit = true)
    public void profileWebSave() throws Exception {
        profileTestImpl(target, target, true);
    }

    @Test
    @SetupStorage(type = "web", agentSaveOnExit = false)
    public void profileWebNotSave() throws Exception {
        profileTestImpl(target, target, false);
    }

}