Optional<Field> findField(Class<?> type, String name) { try { return of(type.getDeclaredField(name)); } catch (NoSuchFieldException | SecurityException e) { LoggingUtil.INSTANCE.debug("Could not find field '{}' in type '{}'", name, type.getSimpleName(), e); return empty(); } }
/** * Remove the given container using the given retry policy. * * @param retryPolicy the retry policy in the event of failure * @param containerId the container id */ void removeContainer(String containerId, RetryPolicy retryPolicy) { Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.info( "Trying to remove Docker Container '{}'", containerId)) .onSuccess(result -> LoggingUtil.INSTANCE.info( "Docker Container '{}' removed", containerId)) .onFailure(throwable -> LoggingUtil.INSTANCE.error( "Docker Container '{}' could not be removed", containerId, throwable)) .run(() -> client.removeContainer(containerId)); }
@Override public void fireTestIgnored(Description description) { LoggingUtil.INSTANCE.warn("Test ignored"); runNotifier.fireTestIgnored(description); }
void removeContainer(String containerId, VirtualResource virtualResource) { LoggingUtil.INSTANCE.info("Stopping and Removing Docker Container {}", containerId); if (client.inspectContainerCmd(containerId).exec().getState().getRunning()) { client.stopContainerCmd(containerId).exec(); try { client.waitContainerCmd(containerId) .exec(new WaitCallback(containerId)) .awaitCompletion(); } catch (InterruptedException e) { LoggingUtil.INSTANCE.warn("wating for container interrupted", e); Thread.currentThread().interrupt(); } } RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withBackoff(virtualResource.delay(), virtualResource.maxDelay(), virtualResource.unit()) .withMaxRetries(virtualResource.maxRetries()) .withMaxDuration(8000, TimeUnit.MILLISECONDS); Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.debug("Trying to remove Docker Container {}", containerId, throwable)) .onSuccess(result -> { LoggingUtil.INSTANCE.info("Docker Container '{}' Removed", containerId); client.removeVolumeCmd(containerId).exec(); }) .onFailure(throwable -> LoggingUtil.INSTANCE.error("Docker Container '{}' could not be removed", containerId, throwable)) .run(() -> client.removeContainerCmd(containerId).exec()); }
void pullImage(VirtualResource virtualResource, String image, String imageName, String imageTag, TestContext testContext) { RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withBackoff(virtualResource.delay(), virtualResource.maxDelay(), virtualResource.unit()) .withMaxRetries(virtualResource.maxRetries()); Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.warn("Retrying pull request of image '{}'", image, throwable) ) .onFailure(throwable -> LoggingUtil.INSTANCE.error("Image image '{}' could not be pulled: ", image, throwable)) .run(() -> { try { CountDownLatch latch = new CountDownLatch(1); client.pullImageCmd(imageName) .withTag(imageTag) .exec(new PullCallback(virtualResource, latch)); ExceptionUtil.INSTANCE.raise(!latch.await(virtualResource.timeout(), virtualResource.unit()), "Could not start virtual resource '{}' for test '{}'", imageName, testContext.getName() ); } catch (InterruptedException e) { LoggingUtil.INSTANCE.warn("Image '{}' pull request interrupted", image); Thread.currentThread().interrupt(); } }); }
@Override public void onError(Throwable throwable) { LoggingUtil.INSTANCE.error("Image '{}:{}' pull failed: ", virtualResource.value(), virtualResource.version(), throwable); latch.countDown(); }
/** * Determine if the image is already pulled. * * @param image the image name * @param imageTag the image tag * @return true if the image is already pulled, false otherwise */ boolean isImagePulled(String image, String imageTag) { boolean imagePulled = false; //determine if the image has already been pulled try { client.inspectImage(image); //if the tag is not the latest then that means we can look to see if //the image has been pulled. if it is then that means we always go and //pull the latest image by setting leaving imagePulled as false if (!DEFAULT_VERSION.equals(imageTag)) { imagePulled = true; } } catch (InterruptedException | DockerException e) { LoggingUtil.INSTANCE.info("Image '{}' not found", image); } return imagePulled; }
public void prepareEmbeddedWebApplicationContext(@SuperCall Callable<Void> zuper, @AllArguments Object[] args) throws Exception { TestContextHolder.INSTANCE.command(testContext -> { LoggingUtil.INSTANCE.setTextContext(testContext); testContext.addProperty(SERVLET_CONTEXT, args[0]); }); zuper.call(); }
/** * Pull the given virtual resource. * * @param virtualResource the virtual resource * @param image the image */ void pullImage(VirtualResource virtualResource, String image) { RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withDelay(virtualResource.delay(), virtualResource.unit()) .withMaxRetries(virtualResource.maxRetries()); Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.warn( "Retrying pull request of image '{}'", image, throwable)) .onFailure(throwable -> LoggingUtil.INSTANCE.error( "Image image '{}' could not be pulled: ", image, throwable)) .run(() -> client.pull(image, new AnsiProgressHandler())); }
@Override public void fireTestAssumptionFailed(Failure failure) { Description description = failure.getDescription(); String methodName = description.getMethodName(); Throwable throwable = failure.getException(); if (methodName == null) { LoggingUtil.INSTANCE.error("Test class assumption failed", throwable); } else { LoggingUtil.INSTANCE.error("Test method assumption failed", throwable); } runNotifier.fireTestAssumptionFailed(failure); }
@Override public void onComplete() { LoggingUtil.INSTANCE.info("Image '{}:{}' pulled", virtualResource.value(), virtualResource.version()); latch.countDown(); }
void enableExtras(String methodName, ServiceLocator serviceLocator) { String className = "org.glassfish.hk2.extras.ExtrasUtilities"; try { Class<?> extrasUtilitiesClass = Class.forName(className); Method method = extrasUtilitiesClass.getMethod(methodName, ServiceLocator.class); method.invoke(null, serviceLocator); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { LoggingUtil.INSTANCE.debug("Method '{}' not found in class {}", methodName, className, e); } } }
/** * Stop the given container using the given retry policy. * * @param retryPolicy the retry policy in the event of failure * @param containerId the container id */ void stopContainer(String containerId, RetryPolicy retryPolicy) { Failsafe.with(retryPolicy) .onRetry(throwable -> LoggingUtil.INSTANCE.info( "Trying to stop Docker Container '{}'", containerId)) .onSuccess(result -> { LoggingUtil.INSTANCE .info("Docker Container '{}' stopped", containerId); removeContainer(containerId, retryPolicy); }) .onFailure(throwable -> LoggingUtil.INSTANCE.error( "Docker Container '{}' could not be stopped", containerId, throwable)) .run(() -> client.stopContainer(containerId, 8)); }
@Override public void fireTestFailure(Failure failure) { Description description = failure.getDescription(); String methodName = description.getMethodName(); Throwable throwable = failure.getException(); if (methodName == null) { LoggingUtil.INSTANCE.error("Test method '{}' failed", methodName, throwable); } else { LoggingUtil.INSTANCE.error("Test method failed", throwable); } runNotifier.fireTestFailure(failure); }
boolean isImagePulled(String image, String imageTag) { boolean imagePulled = false; //determine if the image has already been pulled try { client.inspectImageCmd(image).exec(); //if the tag is not the latest then that means we can look to see if //the image has been pulled. if it is then that means we always go and //pull the latest image by setting leaving imagePulled as false if (!DEFAULT_VERSION.equals(imageTag)) { imagePulled = true; } } catch (NotFoundException e) { LoggingUtil.INSTANCE.info("Image '{}' not found", image); } return imagePulled; }
@Override public void fireTestStarted(Description description) { LoggingUtil.INSTANCE.debug("Test started"); runNotifier.fireTestStarted(description); }
clientProvider.destroy(clientInstance); } catch (Exception e) { LoggingUtil.INSTANCE.error( "Could not destroy client provider '{}'", clientProvider.getClass().getSimpleName(), e serverProvider.stop(serverInstance); } catch (Exception e) { LoggingUtil.INSTANCE.error( "Could not destroy server provider '{}'", serverProvider.getClass().getSimpleName(), e
@Override public void onStart(Closeable closeable) { LoggingUtil.INSTANCE.info("Pulling '{}:{}' image", virtualResource.value(), virtualResource.version()); }
@Override public void fireTestFinished(Description description) { LoggingUtil.INSTANCE.debug("Test finished"); runNotifier.fireTestFinished(description); }