view src/org/thermostat/qa2/tests/AgentWebStorageTest.java @ 192:5d335809cd51

Test fixes changes: - Broken CliClientAgentInfoTest is now using more reliable way to find agent-id. - Now, when pattern is not found in tomcat log(s), it is printed, so that problem can be more easily found and fixed. - Fixing problem with race condition caused by asynchronous nature of shutdown tomcat script - Making ShellService class cleaner and more robust
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 14 Jul 2015 19:00:56 +0200
parents 1d7ab9111fd2
children b4fc34288ffd
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.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.thermostat.qa2.framework.Assert.*;
import org.thermostat.qa2.framework.ThermostatQAConfig;
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.ThermostatStorage;
import org.thermostat.qa2.framework.services.Tomcat;
import org.thermostat.qa2.framework.utils.CommonUtilities;
import org.thermostat.qa2.framework.utils.FileUtilities;

/**
 * Test class ported from old framework
 * @author Zdeněk Žamberský
 */
@SetupStorage(type = "web")
public class AgentWebStorageTest {

    public static final String[] THERM_AGENT_WITH_TOMCAT_ONLY_THERMOSTAT_LOGS = new String[]{
        "com.mongodb.MongoException.Network:",
        //"com.mongodb.MongoException.Network: Read operation to server /127.0.0.1:27518 failed on database thermostat",
        "java.net.ConnectException: Connection refused"
    };
    public static final String[] THERM_AGENT_WITH_TOMCAT_ONLY_CATALINA_LOGS = new String[]{
        "WebStorageEndPoint: Initializing web service",
        "WebStorageEndPoint: THERMOSTAT_HOME == " + ThermostatQAConfig.getThermostatHome("tested"),
        "WARNING - MongoConnection: Failed to connect to storage",
        "com.mongodb.MongoException.Network:",
        //"com.mongodb.MongoException.Network: Read operation to server /127.0.0.1:27518 failed on database thermostat",
        "java.net.ConnectException: Connection refused"
    };

    public static final String[] THERM_AGENT_WITH_WEB_STORAGE_THERMOSTAT_LOGS = new String[]{
        "WebStorageEndPoint: Initializing web service",
        "WebStorageEndPoint: THERMOSTAT_HOME == " + ThermostatQAConfig.getThermostatHome("tested"),
        "MongoConnection: Using plain socket for mongodb://",
        "WebStorageEndPoint: (id: 0) registered non-aggreate category:" //
    //  "WebStorageEndPoint: (id: 0) registered non-aggreate category: vm-cpu-stats|com.redhat.thermostat.vm.cpu.common.model.VmCpuStat|{timeStamp=Key: timeStamp, agentId=Key: agentId, vmId=Key: vmId, cpuLoad=Key: cpuLoad}",
    //  "WebStorageEndPoint: (id: 1) registered non-aggreate category: vm-heap-info|com.redhat.thermostat.vm.heap.analysis.common.model.HeapInfo|{timeStamp=Key: timeStamp, agentId=Key: agentId, vmId=Key: vmId, heapId=Key: heapId, heapDumpId=Key: heapDumpId, histogramId=Key: histogramId}",
    //  "WebStorageEndPoint: (id: 2) registered non-aggreate category: vm-gc-stats|com.redhat.thermostat.vm.gc.common.model.VmGcStat|{runCount=Key: runCount, timeStamp=Key: timeStamp, agentId=Key: agentId, vmId=Key: vmId, wallTime=Key: wallTime, collectorName=Key: collectorName}",
    //  "WebStorageEndPoint: (id: 3) registered non-aggreate category: vm-memory-stats|com.redhat.thermostat.vm.memory.common.model.VmMemoryStat|{timeStamp=Key: timeStamp, generations=Key: generations, agentId=Key: agentId, vmId=Key: vmId}",
    //  "WebStorageEndPoint: (id: 4) registered non-aggreate category: numa-stat|com.redhat.thermostat.numa.common.NumaStat|{nodeStats=Key: nodeStats, timeStamp=Key: timeStamp, agentId=Key: agentId}",
    //  "WebStorageEndPoint: (id: 5) registered non-aggreate category: numa-host-info|com.redhat.thermostat.numa.common.NumaHostInfo|{agentId=Key: agentId, hostNumNumaNodes=Key: hostNumNumaNodes}",
    //  "WebStorageEndPoint: (id: 6) registered non-aggreate category: memory-stats|com.redhat.thermostat.host.memory.common.model.MemoryStat|{total=Key: total, cached=Key: cached, free=Key: free, timeStamp=Key: timeStamp, swapFree=Key: swapFree, swapTotal=Key: swapTotal, buffers=Key: buffers, agentId=Key: agentId, commitLimit=Key: commitLimit}"
    };

    public static final String[] THERM_AGENT_WITH_WEB_STORAGE_CATALINA_LOGS = new String[]{
        "INFO: Deploying web application directory",
        "/webapps/thermostat",
        "WebStorageEndPoint: Initializing web service",
        "WebStorageEndPoint: THERMOSTAT_HOME == " + ThermostatQAConfig.getThermostatHome("tested"),
        "MongoConnection: Using plain socket for mongodb://",
        "WebStorageEndPoint: (id: 0) registered non-aggreate category:" //
    //  "WebStorageEndPoint: (id: 0) registered non-aggreate category: vm-cpu-stats|com.redhat.thermostat.vm.cpu.common.model.VmCpuStat|{timeStamp=Key: timeStamp, agentId=Key: agentId, vmId=Key: vmId, cpuLoad=Key: cpuLoad}",
    //  "WebStorageEndPoint: (id: 1) registered non-aggreate category: vm-heap-info|com.redhat.thermostat.vm.heap.analysis.common.model.HeapInfo|{timeStamp=Key: timeStamp, agentId=Key: agentId, vmId=Key: vmId, heapId=Key: heapId, heapDumpId=Key: heapDumpId, histogramId=Key: histogramId}",
    //  "WebStorageEndPoint: (id: 2) registered non-aggreate category: vm-gc-stats|com.redhat.thermostat.vm.gc.common.model.VmGcStat|{runCount=Key: runCount, timeStamp=Key: timeStamp, agentId=Key: agentId, vmId=Key: vmId, wallTime=Key: wallTime, collectorName=Key: collectorName}",
    //  "WebStorageEndPoint: (id: 3) registered non-aggreate category: vm-memory-stats|com.redhat.thermostat.vm.memory.common.model.VmMemoryStat|{timeStamp=Key: timeStamp, generations=Key: generations, agentId=Key: agentId, vmId=Key: vmId}",
    //  "WebStorageEndPoint: (id: 4) registered non-aggreate category: numa-stat|com.redhat.thermostat.numa.common.NumaStat|{nodeStats=Key: nodeStats, timeStamp=Key: timeStamp, agentId=Key: agentId}"
    };

    public static final String[] THERM_AGENT_BAD_LOGIN_CATALINA_LOGS = new String[]{
        "WARNING: Login exception authenticating username \"doctor-evil\"",
        "javax.security.auth.login.LoginException: User 'doctor-evil' not found"
    };

    public static final String logsPath = ThermostatQAConfig.tomcatHome + File.separator + "logs";

    public static List<String> getLineListFromLogFiles(final String namePattern) throws IOException {
        ArrayList<String> lines = new ArrayList();
        File logsDir = new File(logsPath);
        String[] files = logsDir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.contains(namePattern);
            }
        });
        for (String file : files) {
            FileUtilities.addLinesFromFileToList(lines, logsDir.getPath() + File.separator + file);
        }
        return lines;
    }

    public static void checkForPatterns(String namePattern, String[] patterns) throws IOException {
        List<String> list = getLineListFromLogFiles(namePattern);
//        for(String s:list){
//            System.out.println("INFO: " + s);
//        }
        for (String pattern : patterns) {
            boolean b = CommonUtilities.findInLineList(list, pattern, false);
            if (!b) {
                for (String s : list) {
                    System.out.println("INFO: " + s);
                }
            }
            assertTrue(b, "pattern not found in log files: " + pattern);
        }
    }

    @Test
    public void agentWithoutStorageWithoutTomcat() throws Exception {
        ThermostatAgent agent = new ThermostatAgent();
        assertFailsToStartAndRun(agent, "Agent should not run without storage and without tomcat");
    }

    @Test
    public void agentWithTomcatWithoutStorage() throws Exception {
        Tomcat tomcat = new Tomcat();
        ThermostatAgent agent = new ThermostatAgent();

        tomcat.start();
        assertFailsToStartAndRun(agent, "Agent should not run without storage");
        tomcat.stop();

        checkForPatterns("catalina", THERM_AGENT_WITH_TOMCAT_ONLY_CATALINA_LOGS);
        checkForPatterns("thermostat-web-storage.", THERM_AGENT_WITH_TOMCAT_ONLY_THERMOSTAT_LOGS);
    }

    @Test
    public void agentWithStorageWithoutTomcat() throws Exception {
        ThermostatStorage storage = new ThermostatStorage();
        ThermostatAgent agent = new ThermostatAgent();
        assertFailsToStartAndRun(agent, "Agent should not run without tomcat");
    }

    @Test
    public void agent() throws Exception {
        ThermostatStorage storage = new ThermostatStorage();
        Tomcat tomcat = new Tomcat();
        ThermostatAgent agent = new ThermostatAgent();

        storage.start();
        tomcat.start();
        agent.start();
        assertRuns(agent, "Agent should be running");
        agent.stop();
        tomcat.stop();
        storage.stop();

        checkForPatterns("catalina", THERM_AGENT_WITH_WEB_STORAGE_CATALINA_LOGS);
        checkForPatterns("thermostat-web-storage.", THERM_AGENT_WITH_WEB_STORAGE_THERMOSTAT_LOGS);
    }

    @Test
    @SetupStorage(type = "web", badAgentLogin = true)
    public void agentBadLogin() throws Exception {
        ThermostatStorage storage = new ThermostatStorage();
        Tomcat tomcat = new Tomcat();
        ThermostatAgent agent = new ThermostatAgent();

        storage.start();
        tomcat.start();
        assertFailsToStartAndRun(agent, "Agent should not run with bad login");
        tomcat.stop();
        storage.stop();

        //checkForPatterns("catalina", THERM_AGENT_BAD_LOGIN_CATALINA_LOGS);
    }
}