Search in sources :

Example 1 with JenkinsController

use of org.jenkinsci.test.acceptance.controller.JenkinsController in project acceptance-test-harness by jenkinsci.

the class TSRJenkinsAcceptanceTestRule method apply.

@Override
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
    final Description description = Description.createTestDescription(method.getMethod().getDeclaringClass(), method.getName(), method.getAnnotations());
    return new Statement() {

        @Inject
        JenkinsController controller;

        @Inject
        Injector injector;

        @Override
        public void evaluate() throws Throwable {
            World world = World.get();
            Injector injector = world.getInjector();
            world.startTestScope(description.getDisplayName());
            injector.injectMembers(target);
            injector.injectMembers(this);
            System.out.println("=== Starting " + description.getDisplayName());
            try {
                decorateWithRules(base).evaluate();
            } catch (AssumptionViolatedException e) {
                throw e;
            } catch (Exception | AssertionError e) {
                // Errors and failures
                controller.diagnose(e);
                throw e;
            } finally {
                world.endTestScope();
            }
        }

        /**
         * Look for annotations on a test and honor {@link RuleAnnotation}s in them.
         */
        private Statement decorateWithRules(Statement body) {
            Set<Class<? extends Annotation>> annotations = new HashSet<>();
            collectAnnotationTypes(method.getMethod(), annotations);
            collectAnnotationTypes(target.getClass(), annotations);
            Description testDescription = Description.createTestDescription(target.getClass(), method.getName(), method.getAnnotations());
            for (Class<? extends Annotation> a : annotations) {
                RuleAnnotation r = a.getAnnotation(RuleAnnotation.class);
                if (r != null) {
                    TestRule tr = injector.getInstance(r.value());
                    body = tr.apply(body, testDescription);
                }
            }
            return body;
        }

        private void collectAnnotationTypes(AnnotatedElement e, Collection<Class<? extends Annotation>> types) {
            for (Annotation a : e.getAnnotations()) {
                types.add(a.annotationType());
            }
        }
    };
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) AnnotatedElement(java.lang.reflect.AnnotatedElement) World(org.jenkinsci.test.acceptance.guice.World) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Annotation(java.lang.annotation.Annotation) TestRule(org.junit.rules.TestRule) JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) Injector(com.google.inject.Injector) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 2 with JenkinsController

use of org.jenkinsci.test.acceptance.controller.JenkinsController in project acceptance-test-harness by jenkinsci.

the class FallbackConfig method createController.

/**
 * Instantiates a controller through the "TYPE" attribute and {@link JenkinsControllerFactory}.
 */
@Provides
@TestScope
public JenkinsController createController(Injector injector, ExtensionList<JenkinsControllerFactory> factories) throws IOException {
    // this is lower case for backward compatibility
    String type = System.getenv("type");
    if (type == null)
        type = System.getenv("TYPE");
    if (type == null) {
        File socket = getSocket();
        if (socket.exists() && !JenkinsControllerPoolProcess.MAIN) {
            LOGGER.info("Found pooled jenkins controller listening on socket " + socket.getAbsolutePath());
            return new PooledJenkinsController(injector, socket);
        } else {
            LOGGER.warning("No pooled jenkins controller listening on socket " + socket.getAbsolutePath());
            type = "winstone";
        }
    }
    for (JenkinsControllerFactory f : factories) {
        if (f.getId().equalsIgnoreCase(type)) {
            final JenkinsController c = f.create();
            c.postConstruct(injector);
            return c;
        }
    }
    throw new AssertionError("Invalid controller type: " + type);
}
Also used : JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) PooledJenkinsController(org.jenkinsci.test.acceptance.server.PooledJenkinsController) JenkinsControllerFactory(org.jenkinsci.test.acceptance.controller.JenkinsControllerFactory) File(java.io.File) PooledJenkinsController(org.jenkinsci.test.acceptance.server.PooledJenkinsController) TestScope(org.jenkinsci.test.acceptance.guice.TestScope) Provides(com.google.inject.Provides)

Example 3 with JenkinsController

use of org.jenkinsci.test.acceptance.controller.JenkinsController in project acceptance-test-harness by jenkinsci.

the class RemoteJenkinsProvider method get.

@Override
public JenkinsController get() {
    logger.info("Creating new RemoteJenkinsController...");
    JenkinsController jenkinsController = createNewJenkinsController();
    try {
        cleaner.addTask(jenkinsController);
        jenkinsController.start();
    } catch (IOException e) {
        throw new AssertionError("Failed to start Jenkins: " + e.getMessage(), e);
    }
    return jenkinsController;
}
Also used : JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) RemoteJenkinsController(org.jenkinsci.test.acceptance.controller.RemoteJenkinsController) IOException(java.io.IOException)

Example 4 with JenkinsController

use of org.jenkinsci.test.acceptance.controller.JenkinsController in project acceptance-test-harness by jenkinsci.

the class JenkinsControllerPoolProcess method run.

public void run() throws Exception {
    // so the actual length of the queue has to be n-1.
    if (n == 1)
        queue = new SynchronousQueue<>();
    else
        queue = new LinkedBlockingDeque<>(n - 1);
    World w = World.get();
    w.getInjector().injectMembers(this);
    new Thread() {

        /**
         * Just keeps on creating new controllers and put it into the queue.
         * Because queue is blocking, this will only prelaunch up to 3.
         */
        @Override
        public void run() {
            try {
                FallbackConfig f = new FallbackConfig();
                while (true) {
                    lifecycle.startTestScope();
                    JenkinsController c = f.createController(injector, factories);
                    c.start();
                    queue.put(new QueueItem(c, lifecycle.export()));
                }
            } catch (Throwable e) {
                // fail fatally
                e.printStackTrace();
                System.exit(1);
            }
        }
    }.start();
    processServerSocket();
}
Also used : FallbackConfig(org.jenkinsci.test.acceptance.FallbackConfig) JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) IJenkinsController(org.jenkinsci.test.acceptance.controller.IJenkinsController) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) SynchronousQueue(java.util.concurrent.SynchronousQueue) World(org.jenkinsci.test.acceptance.guice.World)

Example 5 with JenkinsController

use of org.jenkinsci.test.acceptance.controller.JenkinsController in project acceptance-test-harness by jenkinsci.

the class JenkinsControllerPoolProcess method processServerSocket.

/**
 * Accepts connection to Unix domain socket and hand it off to a connection handling thread.
 */
private void processServerSocket() throws IOException, InterruptedException {
    socket.deleteOnExit();
    socket.delete();
    try (UnixServerSocketChannel channel = UnixServerSocketChannel.open()) {
        channel.configureBlocking(true);
        channel.socket().bind(new UnixSocketAddress(socket));
        System.out.println("JUT Server is ready and listening at " + socket.getAbsolutePath());
        while (true) {
            final UnixSocketChannel c = channel.accept();
            System.out.println("Accepted");
            final QueueItem qi = queue.take();
            final JenkinsController j = qi.controller;
            System.out.println("Handed out " + j.getUrl());
            new Thread("Connection handling thread") {

                @Override
                public void run() {
                    lifecycle.import_(qi.testScope);
                    try {
                        processConnection(c, j);
                    } finally {
                        TestCleaner scope = injector.getInstance(TestCleaner.class);
                        if (scope != null)
                            scope.performCleanUp();
                        lifecycle.endTestScope();
                    }
                }
            }.start();
        }
    }
}
Also used : TestCleaner(org.jenkinsci.test.acceptance.guice.TestCleaner) UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) JenkinsController(org.jenkinsci.test.acceptance.controller.JenkinsController) IJenkinsController(org.jenkinsci.test.acceptance.controller.IJenkinsController) UnixServerSocketChannel(jnr.unixsocket.UnixServerSocketChannel) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress)

Aggregations

JenkinsController (org.jenkinsci.test.acceptance.controller.JenkinsController)6 World (org.jenkinsci.test.acceptance.guice.World)3 Injector (com.google.inject.Injector)2 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 IJenkinsController (org.jenkinsci.test.acceptance.controller.IJenkinsController)2 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)2 TestRule (org.junit.rules.TestRule)2 Description (org.junit.runner.Description)2 Statement (org.junit.runners.model.Statement)2 Provides (com.google.inject.Provides)1 File (java.io.File)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1