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());
}
}
};
}
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);
}
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;
}
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();
}
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();
}
}
}
Aggregations