1
package com.eucalyptus.upgrade;
4
import java.io.PrintWriter;
5
import java.lang.annotation.Annotation;
6
import java.lang.reflect.Method;
7
import java.util.Arrays;
8
import java.util.Collections;
9
import java.util.Enumeration;
10
import java.util.List;
11
import java.util.jar.JarEntry;
12
import java.util.jar.JarFile;
13
import org.apache.commons.cli.CommandLine;
14
import org.apache.commons.cli.GnuParser;
15
import org.apache.commons.cli.HelpFormatter;
16
import org.apache.commons.cli.OptionBuilder;
17
import org.apache.commons.cli.Options;
18
import org.apache.log4j.Logger;
19
import org.junit.After;
20
import org.junit.Before;
21
import org.junit.Ignore;
22
import org.junit.Test;
23
import org.junit.runner.JUnitCore;
24
import org.junit.runner.Result;
25
import org.junit.runners.Parameterized.Parameters;
26
import com.eucalyptus.bootstrap.ServiceJarDiscovery;
27
import com.eucalyptus.component.ComponentDiscovery;
28
import com.google.common.base.Predicate;
29
import com.google.common.collect.ArrayListMultimap;
30
import com.google.common.collect.Iterables;
31
import com.google.common.collect.Lists;
32
import com.google.common.collect.Multimap;
34
public class TestHarness
36
public static Integer LINE_BYTES = 0;
38
String defaultLevel = System.getProperty("verbose");
39
String columns = System.getenv("COLUMNS");
40
System.setProperty("test.output.console", "INFO");
41
System.setProperty("test.output.terminal", "INFO");
42
if (columns != null && !"".equals(columns)) {
43
LINE_BYTES = Integer.parseInt(System.getenv("COLUMNS"));
44
System.setProperty("test.output.terminal", defaultLevel != null
49
System.setProperty("test.output.console", "INFO");
53
public static int count = 1;
54
private static Logger LOG = Logger.getLogger(TestHarness.class);
55
private static List<Class<? extends Annotation>> testAnnotations = Lists
56
.newArrayList(Before.class, After.class, Test.class,
59
public static void main(String[] args)
61
boolean anyFail = false;
63
System.out.println(Arrays.asList(args));
64
final Options opts = getCliOptions();
65
final GnuParser cliParser = new GnuParser();
66
final CommandLine cmd = cliParser.parse(opts, args);
69
anyFail = Iterables.any(Lists.newArrayList(Opts.values()),
72
public boolean apply(Opts o)
74
return !o.run(cmd, opts);
77
} catch (Exception e) {
80
} catch (Exception e) {
83
System.exit(0); //This is necessary now; some shutdown process is hanging
84
//return (anyFail) ? 1 : 0;
91
public boolean run(CommandLine cmd, Options opts)
93
if (cmd.hasOption(Opts.help.toString())
94
|| !cmd.getArgList().isEmpty()
95
|| cmd.getOptionValues("test") == null) {
104
public boolean run(CommandLine cmd, Options opts)
107
System.setProperty("euca.log.level", "TRACE");
108
System.setProperty("euca.log.appender", "console");
109
System.setProperty("euca.log.exhaustive.cc", "FATAL");
110
System.setProperty("euca.log.exhaustive.db", "FATAL");
111
System.setProperty("euca.log.exhaustive.external", "FATAL");
112
System.setProperty("euca.log.exhaustive.user", "FATAL");
113
System.setProperty("euca.var.dir",
114
System.getProperty("euca.home")
115
+ "/var/lib/eucalyptus/");
116
System.setProperty("euca.conf.dir",
117
System.getProperty("euca.home")
118
+ "/etc/eucalyptus/cloud.d/");
119
System.setProperty("euca.log.dir",
120
System.getProperty("euca.home")
121
+ "/var/log/eucalyptus/");
122
System.setProperty("euca.lib.dir",
123
System.getProperty("euca.home")
124
+ "/usr/share/eucalyptus/");
125
boolean doTrace = "TRACE".equals(System
126
.getProperty("euca.log.level"));
127
boolean doDebug = "DEBUG".equals(System
128
.getProperty("euca.log.level")) || doTrace;
129
// Logs.DEBUG = doDebug;
130
// Logs.TRACE = doDebug;
132
if ((StandalonePersistence.eucaDest = System
133
.getProperty("euca.upgrade.destination")) == null) {
134
throw new RuntimeException(
135
"Failed to find required 'euca.upgrade.destination' property");
137
ServiceJarDiscovery.processLibraries( );
138
ServiceJarDiscovery.runDiscovery( new ComponentDiscovery( ) );
139
StandalonePersistence.setupInitProviders();
140
StandalonePersistence.runSetupDiscovery();
141
StandalonePersistence.setupProviders();
142
StandalonePersistence.setupNewDatabase();
143
} catch (Exception e) {
144
throw new RuntimeException(
145
"Standalone persistence setup failed", e);
148
runMethods(cmd, opts);
153
public abstract boolean run(CommandLine cmd, Options opts);
156
@SuppressWarnings("static-access")
157
private static Options getCliOptions()
159
final Options opts = new Options();
160
opts.addOption(OptionBuilder
164
"Run the specified test; this option can appear multiple times. Parameters can be set as key=value pairs. e.g. ConcurrencyTest:threads=16")
165
.withArgName("TEST_CLASS[:PARAM=VALUE]*").create("t"));
166
opts.addOption(OptionBuilder.withLongOpt(Opts.help.name())
167
.withDescription("Show this help information.").create("h"));
171
@SuppressWarnings({"static-access"})
172
private static void printHelp(Options opts)
175
PrintWriter out = new PrintWriter(System.out);
176
HelpFormatter help = new HelpFormatter();
177
help.setArgName("TESTS");
178
help.printHelp(out, LINE_BYTES, "java -jar test.jar",
179
"Options controlling the test harness.", opts, 2, 4, "",
181
Multimap<Class, Method> testMethods = getTestMethods();
182
help = new HelpFormatter();
183
help.setLongOptPrefix("");
184
help.setOptPrefix("");
185
help.setSyntaxPrefix("");
186
help.setLeftPadding(0);
187
Options testOptions = new Options();
188
for (Class c : testMethods.keySet()) {
189
testOptions.addOption(OptionBuilder
190
.withDescription(getDescription(c))
191
.withLongOpt(c.getSimpleName()).create());
192
for (Method m : testMethods.get(c)) {
193
testOptions.addOption(OptionBuilder
194
.withDescription(getDescription(m))
195
.withLongOpt(c.getSimpleName() + "." + m.getName())
199
help.printHelp(out, LINE_BYTES, " ", "Tests:", testOptions, 0, 2,
202
} catch (Exception e) {
203
System.out.println(e);
208
@SuppressWarnings("unchecked")
209
private static String getDescription(Object o)
213
if (o instanceof Class
214
&& ((c = (Class) o).getAnnotation(TestDescription.class)) != null) {
215
return ((TestDescription) c.getAnnotation(TestDescription.class))
217
} else if (o instanceof Method
218
&& ((m = (Method) o).getAnnotation(TestDescription.class)) != null) {
219
StringBuffer sb = new StringBuffer();
220
for (Class a : Lists.newArrayList(Before.class, After.class,
221
Test.class, Ignore.class, Parameters.class)) {
222
if (m.getAnnotation(a) != null)
224
.append(String.format("%-9.9s", a.getSimpleName()))
229
.append(((TestDescription) m
230
.getAnnotation(TestDescription.class)).value())
236
@SuppressWarnings("unchecked")
237
private static Multimap<Class, Method> getTestMethods() throws Exception
239
final Multimap<Class, Method> testMethods = ArrayListMultimap.create( );
240
List<Class> classList = Lists.newArrayList();
241
for (File f : new File(System.getProperty("euca.home")
242
+ "/usr/share/eucalyptus").listFiles()) {
243
if (f.getName().startsWith("eucalyptus")
244
&& f.getName().endsWith(".jar")
245
&& !f.getName().matches(".*-ext-.*")) {
247
JarFile jar = new JarFile(f);
248
Enumeration<JarEntry> jarList = jar.entries();
249
for( JarEntry j : Collections.list( jar.entries() ) ) {
250
if (j.getName().matches(".*\\.class.{0,1}")) {
251
String classGuess = j.getName()
252
.replaceAll("/", ".")
253
.replaceAll("\\.class.{0,1}", "");
255
Class candidate = ClassLoader
256
.getSystemClassLoader().loadClass(
258
for (final Method m : candidate
259
.getDeclaredMethods()) {
261
.any(testAnnotations,
262
new Predicate<Class<? extends Annotation>>()
264
public boolean apply(
265
Class<? extends Annotation> arg0)
267
return m.getAnnotation(arg0) != null;
270
System.out.println("Added test class: "
271
+ candidate.getCanonicalName());
272
testMethods.put(candidate, m);
275
} catch (ClassNotFoundException e) {
280
} catch (Exception e) {
281
System.out.println(e.getMessage());
289
public static boolean runTests(List<Class> tests)
291
System.out.println("TEST LIST SIZE:" + tests.size());
292
for (Class testClass : tests) {
293
System.out.println("Test:" + testClass);
295
JUnitCore core = new JUnitCore();
296
// core.run( Cleanup.class );
297
core.addListener(new TestListener());
299
for (Class clazz : tests) {
300
Result res = core.run(clazz);
301
if (res == null || !res.wasSuccessful()) {
306
// tests = Lists.newArrayList( tests );
307
// tests.add( Cleanup.class );
308
// Result res = core.run( ( Class<?>[] ) tests.toArray( new Class[] {})
310
// return res != null ? res.wasSuccessful( ) : false;
313
@SuppressWarnings("unchecked")
314
private static void runMethods(final CommandLine cmd, final Options opts)
316
String[] optVals = cmd.getOptionValues("test");
317
for (int i = 0; i < optVals.length; i++) {
318
String[] argParts = optVals[i].split(":");
319
String className = argParts[0];
320
String methodName = argParts[1];
321
String[] methodArgsArray = new String[0];
322
if (argParts.length > 2) {
323
methodArgsArray = argParts[2].split(",");
324
System.out.printf("Executing class:%s method:%s args:%s\n", className,
325
methodName, argParts[2]);
327
System.out.printf("Executing class:%s method:%s\n", className,
330
Class[] params = new Class[methodArgsArray.length];
331
for (int j=0; j < params.length; j++) {
332
params[j] = String.class;
336
Class clazz = Class.forName(className);
337
clazz.getDeclaredMethod(methodName, params).invoke(null, (Object[])methodArgsArray);
338
} catch (Exception ex) {
339
ex.printStackTrace();
341
System.out.println("Executed method");
343
// List<Class> testList = Lists.transform( Lists.newArrayList(
344
// cmd.getOptionValues( "test" ) ), new Function<String, Class>( ) {
345
// public Class apply( String arg ) {
346
// String[] argParts = arg.split(":");
347
// String className = argParts[0];
348
// System.out.println("CLASS NAME:" + className);
349
// Class targetClass = null;
351
// targetClass = Class.forName( className );
352
// } catch ( Exception e ) {
354
// targetClass = Class.forName( className + "Test" );
355
// } catch ( Exception e1 ) {
358
// if( targetClass == null ) {
359
// printHelp( opts );
362
// for( int i = 1; i < argParts.length; i++ ) {
363
// String property = argParts[i].replaceAll("=.*","");
364
// String value = argParts[i].replaceAll(".*=","");
366
// targetClass.getDeclaredMethod( property, String.class ).invoke( null,
368
// } catch ( Exception e ) {
369
// System.out.println( e );
374
// return targetClass;