Added screenshots and an output directory argument to the tester.

This commit is contained in:
devin148 2012-08-27 15:37:27 -04:00
parent a7eb7b49ee
commit 3919f77ea8
2 changed files with 69 additions and 26 deletions

View File

@ -54,6 +54,8 @@ class Args:
self.verbose = False
self.exception = False
self.exception_string = ""
self.output = False
self.output_dir = ""
def parse(self):
sys.argv.pop(0)
@ -99,11 +101,22 @@ class Args:
try:
arg = sys.argv.pop(0)
printout("Running in exception mode: ")
printout("Printing all exceptions with the string '" + arg + "'\n")
printout("Printing all exceptions with the string '" + arg + "'.\n")
self.exception = True
self.exception_string = arg
except:
printerror("Error: No exception string given.")
elif(arg == "-o" or arg == "--output"):
try:
arg = sys.argv.pop(0)
if dir_exists(arg):
printout("Running with output directory set to " + arg + ".\n")
self.output = True
self.output_dir = arg
else:
printerror("Error: Given output directory for -o doesn't exist.")
except:
printerror("Error: No output directory given.\n")
elif arg == "-h" or arg == "--help":
printout(usage())
return False
@ -366,7 +379,7 @@ def run_test(image_file):
# Set the case to work for this test
case.image_file = image_file
case.image_name = case.get_image_name(image_file)
case.antlog_dir = make_local_path(case.output_dir, case.image_name, "antlog.txt")
case.antlog_dir = make_path(case.output_dir, case.image_name, "antlog.txt")
case.known_bad_path = make_path(case.input_dir, "notablehashes.txt-md5.idx")
case.keyword_path = make_path(case.input_dir, "notablekeywords.xml")
case.nsrl_path = make_path(case.input_dir, "nsrl.txt-md5.idx")
@ -387,7 +400,7 @@ def run_test(image_file):
rebuild()
# If NOT keeping Solr index (-k)
if not args.keep:
solr_index = make_local_path(case.output_dir, case.image_name, "AutopsyTestCase", "KeywordSearch")
solr_index = make_path(case.output_dir, case.image_name, "AutopsyTestCase", "KeywordSearch")
if clear_dir(solr_index):
print_report([], "DELETE SOLR INDEX", "Solr index deleted.")
elif args.keep:
@ -440,7 +453,7 @@ def run_ant():
case.ant.append("-Dkeyword_path=" + case.keyword_path)
case.ant.append("-Dnsrl_path=" + case.nsrl_path)
case.ant.append("-Dgold_path=" + make_local_path(case.gold))
case.ant.append("-Dout_path=" + make_local_path(case.output_dir, case.image_name))
case.ant.append("-Dout_path=" + make_path(case.output_dir, case.image_name))
case.ant.append("-Dignore_unalloc=" + "%s" % args.unallocated)
case.ant.append("-Dtest.timeout=" + str(case.timeout))
@ -487,9 +500,9 @@ def rebuild():
clear_dir(gold_dir)
# Rebuild the database
gold_from = make_local_path(case.output_dir, case.image_name,
gold_from = make_path(case.output_dir, case.image_name,
"AutopsyTestCase", "autopsy.db")
gold_to = make_local_path(case.gold, case.image_name, "standard.db")
gold_to = make_path(case.gold, case.image_name, "standard.db")
try:
copy_file(gold_from, gold_to)
except FileNotFoundException as e:
@ -499,7 +512,7 @@ def rebuild():
errors.append(str(e) + "\n")
# Rebuild the HTML report
html_path = make_local_path(case.output_dir, case.image_name,
html_path = make_path(case.output_dir, case.image_name,
"AutopsyTestCase", "Reports")
try:
html_from = get_file_in_dir(html_path, ".html")
@ -576,7 +589,7 @@ def compare_to_gold_db():
# the regression test against the gold standard html report
def compare_to_gold_html():
gold_html_file = make_local_path(case.gold, case.image_name, "standard.html")
autopsy_html_path = make_local_path(case.output_dir, case.image_name,
autopsy_html_path = make_path(case.output_dir, case.image_name,
"AutopsyTestCase", "Reports")
try:
autopsy_html_file = get_file_in_dir(autopsy_html_path, ".html")
@ -658,8 +671,8 @@ def compare_tsk_objects():
# Generate the "common log": a log of all exceptions and warnings
# from each log file generated by Autopsy
def generate_common_log():
#try:
logs_path = make_local_path(case.output_dir, case.image_name, "logs")
try:
logs_path = make_path(case.output_dir, case.image_name, "logs")
common_log = open(case.common_log, "a")
common_log.write("--------------------------------------------------\n")
common_log.write(case.image_name + "\n")
@ -677,15 +690,15 @@ def generate_common_log():
log.close()
common_log.write("\n\n")
common_log.close()
#except Exception as e:
# printerror("Error: Unable to generate the common log.")
# printerror(str(e))
except Exception as e:
printerror("Error: Unable to generate the common log.")
printerror(str(e))
# Fill in the global case's variables that require the log files
def fill_case_data():
try:
# Open autopsy.log.0
log_path = make_local_path(case.output_dir, case.image_name, "logs", "autopsy.log.0")
log_path = make_path(case.output_dir, case.image_name, "logs", "autopsy.log.0")
log = open(log_path)
# Set the case starting time based off the first line of autopsy.log.0
@ -845,7 +858,7 @@ def csv_header(csv_path):
# Returns a list of all the exceptions listed in all the autopsy logs
def get_exceptions():
exceptions = []
logs_path = make_local_path(case.output_dir, case.image_name, "logs")
logs_path = make_path(case.output_dir, case.image_name, "logs")
results = []
for file in os.listdir(logs_path):
if "autopsy" in file:
@ -881,7 +894,7 @@ def report_all_errors():
# Searched all the known logs for the given regex
# The function expects regex = re.compile(...)
def regex_search_logs(regex):
logs_path = make_local_path(case.output_dir, case.image_name, "logs")
logs_path = make_path(case.output_dir, case.image_name, "logs")
results = []
for file in os.listdir(logs_path):
log = open(make_path(logs_path, file), "r")
@ -896,7 +909,7 @@ def regex_search_logs(regex):
# Search through all the known log files for a specific string.
# Returns a list of all lines with that string
def search_logs(string):
logs_path = make_local_path(case.output_dir, case.image_name, "logs")
logs_path = make_path(case.output_dir, case.image_name, "logs")
results = []
for file in os.listdir(logs_path):
log = open(make_path(logs_path, file), "r")
@ -921,7 +934,7 @@ def search_common_log(string):
# Searches the given log for the given string
# Returns a list of all lines with that string
def search_log(log, string):
logs_path = make_local_path(case.output_dir, case.image_name, "logs", log)
logs_path = make_path(case.output_dir, case.image_name, "logs", log)
try:
results = []
log = open(logs_path, "r")
@ -938,7 +951,7 @@ def search_log(log, string):
# Search through all the the logs of the given type
# Types include autopsy, tika, and solr
def search_log_set(type, string):
logs_path = make_local_path(case.output_dir, case.image_name, "logs")
logs_path = make_path(case.output_dir, case.image_name, "logs")
results = []
for file in os.listdir(logs_path):
if type in file:
@ -1014,7 +1027,7 @@ def generate_html():
logs = "<div id='logs'>\
<h2><a name='" + case.image_name + "-logs'>Logs</a></h2>\
<hr color='#00a00f'>"
logs_path = make_local_path(case.output_dir, case.image_name, "logs")
logs_path = make_path(case.output_dir, case.image_name, "logs")
for file in os.listdir(logs_path):
logs += "<p><a href='file:\\" + make_path(logs_path, file) + "' target='_blank'>" + file + "</a></p>"
logs += "</div>"
@ -1195,7 +1208,7 @@ def wgetcwd():
def copy_logs():
try:
log_dir = os.path.join("..","build","test","qa-functional","work","userdir0","var","log")
shutil.copytree(log_dir, make_local_path(case.output_dir, case.image_name, "logs"))
shutil.copytree(log_dir, make_path(case.output_dir, case.image_name, "logs"))
except Exception as e:
printerror("Error: Failed to copy the logs.")
printerror(str(e) + "\n")
@ -1302,6 +1315,7 @@ Options:
-v Verbose mode; prints all errors to the screen.
-e ex Prints out all errors containing ex.
-l cfg Runs from configuration file cfg.
-o dir Uses dir as the output directory. Must be a full path.
"""
@ -1364,11 +1378,14 @@ def main():
pass
# Otherwise test away!
else:
case.output_dir = make_path("output", time.strftime("%Y.%m.%d-%H.%M.%S"))
if not args.output:
case.output_dir = make_local_path("output", time.strftime("%Y.%m.%d-%H.%M.%S"))
else:
case.output_dir = make_path(args.output_dir, time.strftime("%Y.%m.%d-%H.%M.%S"))
os.makedirs(case.output_dir)
case.common_log = make_local_path(case.output_dir, "AutopsyErrors.txt")
case.csv = make_local_path(case.output_dir, "CSV.txt")
case.html_log = make_local_path(case.output_dir, "AutopsyTestCase.html")
case.common_log = make_path(case.output_dir, "AutopsyErrors.txt")
case.csv = make_path(case.output_dir, "CSV.txt")
case.html_log = make_path(case.output_dir, "AutopsyTestCase.html")
# If user wants to do a single file and a list (contradictory?)
if args.single and args.list:

View File

@ -18,10 +18,18 @@
*/
package org.sleuthkit.autopsy.testing;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JTextField;
import junit.framework.Test;
@ -226,6 +234,7 @@ public class RegressionTest extends TestCase{
// consistently, making it seem like default behavior
Random rand = new Random();
new Timeout("pausing", 10000 + (rand.nextInt(15000) + 5000)).sleep();
screenshot("Finished Ingest");
}
@ -254,12 +263,29 @@ public class RegressionTest extends TestCase{
JButtonOperator jbo0 = new JButtonOperator(reportDialogOperator, "Generate Report");
jbo0.pushNoBlock();
new Timeout("pausing", 3000).sleep(); // Give it a few seconds to generate
screenshot("Finished Report");
JDialog previewDialog = JDialogOperator.waitJDialog("Report Preview", false, false);
JDialogOperator previewDialogOperator = new JDialogOperator(previewDialog);
JButtonOperator jbo1 = new JButtonOperator(previewDialogOperator, "Close");
jbo1.pushNoBlock();
new Timeout("pausing", 3000).sleep(); // Give the program a second to idle to be safe
screenshot("Done Testing");
}
public void screenshot(String name) {
logger.info("Taking screenshot.");
try {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
String outPath = System.getProperty("out_path");
ImageIO.write(capture, "png", new File(outPath + "\\" + name + ".png"));
new Timeout("pausing", 1000).sleep(); // give it a second to save
} catch (IOException ex) {
logger.info("IOException taking screenshot.");
} catch (AWTException ex) {
logger.info("AWTException taking screenshot.");
}
}
}