~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/core/src/test/java/com/eucalyptus/upgrade/TestHarness.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.eucalyptus.upgrade;
 
2
 
 
3
import java.io.File;
 
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;
 
33
 
 
34
public class TestHarness
 
35
{
 
36
        public static Integer LINE_BYTES = 0;
 
37
        static {
 
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
 
45
                                        ? defaultLevel
 
46
                                        : "INFO");
 
47
                } else {
 
48
                        LINE_BYTES = 85;
 
49
                        System.setProperty("test.output.console", "INFO");
 
50
                }
 
51
        }
 
52
 
 
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,
 
57
                                        Parameters.class);
 
58
 
 
59
        public static void main(String[] args)
 
60
        {
 
61
                boolean anyFail = false;
 
62
                try {
 
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);
 
67
 
 
68
                        try {
 
69
                                anyFail = Iterables.any(Lists.newArrayList(Opts.values()),
 
70
                                                new Predicate<Opts>()
 
71
                                                {
 
72
                                                        public boolean apply(Opts o)
 
73
                                                        {
 
74
                                                                return !o.run(cmd, opts);
 
75
                                                        }
 
76
                                                });
 
77
                        } catch (Exception e) {
 
78
                                e.printStackTrace();
 
79
                        }
 
80
                } catch (Exception e) {
 
81
                        e.printStackTrace();
 
82
                }
 
83
                System.exit(0);  //This is necessary now; some shutdown process is hanging
 
84
                //return (anyFail) ? 1 : 0;
 
85
        }
 
86
 
 
87
        enum Opts
 
88
        {
 
89
                help {
 
90
                        @Override
 
91
                        public boolean run(CommandLine cmd, Options opts)
 
92
                        {
 
93
                                if (cmd.hasOption(Opts.help.toString())
 
94
                                                || !cmd.getArgList().isEmpty()
 
95
                                                || cmd.getOptionValues("test") == null) {
 
96
                                        printHelp(opts);
 
97
                                        System.exit(0);
 
98
                                }
 
99
                                return true;
 
100
                        }
 
101
                },
 
102
                runTest {
 
103
                        @Override
 
104
                        public boolean run(CommandLine cmd, Options opts)
 
105
                        {
 
106
                                try {
 
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;
 
131
 
 
132
                                        if ((StandalonePersistence.eucaDest = System
 
133
                                                        .getProperty("euca.upgrade.destination")) == null) {
 
134
                                                throw new RuntimeException(
 
135
                                                                "Failed to find required 'euca.upgrade.destination' property");
 
136
                                        }
 
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);
 
146
                                }
 
147
 
 
148
                                runMethods(cmd, opts);
 
149
                                return true;
 
150
                        }
 
151
                };
 
152
 
 
153
                public abstract boolean run(CommandLine cmd, Options opts);
 
154
        }
 
155
 
 
156
        @SuppressWarnings("static-access")
 
157
        private static Options getCliOptions()
 
158
        {
 
159
                final Options opts = new Options();
 
160
                opts.addOption(OptionBuilder
 
161
                                .withLongOpt("test")
 
162
                                .hasArgs()
 
163
                                .withDescription(
 
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"));
 
168
                return opts;
 
169
        }
 
170
 
 
171
        @SuppressWarnings({"static-access"})
 
172
        private static void printHelp(Options opts)
 
173
        {
 
174
                try {
 
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, "",
 
180
                                        true);
 
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())
 
196
                                                        .create());
 
197
                                }
 
198
                        }
 
199
                        help.printHelp(out, LINE_BYTES, " ", "Tests:", testOptions, 0, 2,
 
200
                                        "", false);
 
201
                        out.flush();
 
202
                } catch (Exception e) {
 
203
                        System.out.println(e);
 
204
                        System.exit(1);
 
205
                }
 
206
        }
 
207
 
 
208
        @SuppressWarnings("unchecked")
 
209
        private static String getDescription(Object o)
 
210
        {
 
211
                Class c = null;
 
212
                Method m = null;
 
213
                if (o instanceof Class
 
214
                                && ((c = (Class) o).getAnnotation(TestDescription.class)) != null) {
 
215
                        return ((TestDescription) c.getAnnotation(TestDescription.class))
 
216
                                        .value();
 
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)
 
223
                                        sb.append("  @")
 
224
                                                        .append(String.format("%-9.9s", a.getSimpleName()))
 
225
                                                        .append(" ");
 
226
                        }
 
227
                        return sb
 
228
                                        .append(" ")
 
229
                                        .append(((TestDescription) m
 
230
                                                        .getAnnotation(TestDescription.class)).value())
 
231
                                        .toString();
 
232
                }
 
233
                return "";
 
234
        }
 
235
 
 
236
        @SuppressWarnings("unchecked")
 
237
        private static Multimap<Class, Method> getTestMethods() throws Exception
 
238
        {
 
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-.*")) {
 
246
                                try {
 
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}", "");
 
254
                                                        try {
 
255
                                                                Class candidate = ClassLoader
 
256
                                                                                .getSystemClassLoader().loadClass(
 
257
                                                                                                classGuess);
 
258
                                                                for (final Method m : candidate
 
259
                                                                                .getDeclaredMethods()) {
 
260
                                                                        if (Iterables
 
261
                                                                                        .any(testAnnotations,
 
262
                                                                                                        new Predicate<Class<? extends Annotation>>()
 
263
                                                                                                        {
 
264
                                                                                                                public boolean apply(
 
265
                                                                                                                                Class<? extends Annotation> arg0)
 
266
                                                                                                                {
 
267
                                                                                                                        return m.getAnnotation(arg0) != null;
 
268
                                                                                                                }
 
269
                                                                                                        })) {
 
270
                                                                                System.out.println("Added test class: "
 
271
                                                                                                + candidate.getCanonicalName());
 
272
                                                                                testMethods.put(candidate, m);
 
273
                                                                        }
 
274
                                                                }
 
275
                                                        } catch (ClassNotFoundException e) {
 
276
                                                        }
 
277
                                                }
 
278
                                        }
 
279
                                        jar.close();
 
280
                                } catch (Exception e) {
 
281
                                        System.out.println(e.getMessage());
 
282
                                        continue;
 
283
                                }
 
284
                        }
 
285
                }
 
286
                return testMethods;
 
287
        }
 
288
 
 
289
        public static boolean runTests(List<Class> tests)
 
290
        {
 
291
                System.out.println("TEST LIST SIZE:" + tests.size());
 
292
                for (Class testClass : tests) {
 
293
                        System.out.println("Test:" + testClass);
 
294
                }
 
295
                JUnitCore core = new JUnitCore();
 
296
                // core.run( Cleanup.class );
 
297
                core.addListener(new TestListener());
 
298
                boolean rv = true;
 
299
                for (Class clazz : tests) {
 
300
                        Result res = core.run(clazz);
 
301
                        if (res == null || !res.wasSuccessful()) {
 
302
                                rv = false;
 
303
                        }
 
304
                }
 
305
                return rv;
 
306
                // tests = Lists.newArrayList( tests );
 
307
                // tests.add( Cleanup.class );
 
308
                // Result res = core.run( ( Class<?>[] ) tests.toArray( new Class[] {})
 
309
                // );
 
310
                // return res != null ? res.wasSuccessful( ) : false;
 
311
        }
 
312
 
 
313
        @SuppressWarnings("unchecked")
 
314
        private static void runMethods(final CommandLine cmd, final Options opts)
 
315
        {
 
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]);
 
326
                        } else {
 
327
                                System.out.printf("Executing class:%s method:%s\n", className,
 
328
                                                methodName);
 
329
                        }
 
330
                        Class[] params = new Class[methodArgsArray.length];
 
331
                        for (int j=0; j < params.length; j++) {
 
332
                                params[j] = String.class;
 
333
                        }
 
334
                        
 
335
                        try {
 
336
                                Class clazz = Class.forName(className);
 
337
                                clazz.getDeclaredMethod(methodName, params).invoke(null, (Object[])methodArgsArray);
 
338
                        } catch (Exception ex) {
 
339
                                ex.printStackTrace();
 
340
                        }
 
341
                        System.out.println("Executed method");
 
342
                }
 
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;
 
350
                // try {
 
351
                // targetClass = Class.forName( className );
 
352
                // } catch ( Exception e ) {
 
353
                // try {
 
354
                // targetClass = Class.forName( className + "Test" );
 
355
                // } catch ( Exception e1 ) {
 
356
                // }
 
357
                // }
 
358
                // if( targetClass == null ) {
 
359
                // printHelp( opts );
 
360
                // System.exit( 1 );
 
361
                // } else {
 
362
                // for( int i = 1; i < argParts.length; i++ ) {
 
363
                // String property = argParts[i].replaceAll("=.*","");
 
364
                // String value = argParts[i].replaceAll(".*=","");
 
365
                // try {
 
366
                // targetClass.getDeclaredMethod( property, String.class ).invoke( null,
 
367
                // value );
 
368
                // } catch ( Exception e ) {
 
369
                // System.out.println( e );
 
370
                // System.exit( 1 );
 
371
                // }
 
372
                // }
 
373
                // }
 
374
                // return targetClass;
 
375
                // }
 
376
                // } );
 
377
                // return testList;
 
378
        }
 
379
}