<T> T getFieldValue(Field field, Object instance) { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { try { field.setAccessible(true); return (T) field.get(instance); } catch (IllegalAccessException | IllegalArgumentException | SecurityException e) { throw ExceptionUtil.INSTANCE.propagate(e); } }); }
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 stop(TestContext testContext, VirtualResource virtualResource) { if (started.compareAndSet(true, false)) { removeContainer(containerResponse.getId(), virtualResource); try { client.close(); } catch (IOException e) { throw ExceptionUtil.INSTANCE.propagate(e); } } }
@Override public ServerInstance<EmbeddedServletContainer> start(TestContext testContext, Application application, SpringApplicationBuilder configuration) { LoggingUtil.INSTANCE.debug("Starting Spring Boot application '{}'", testContext .getName()); SpringApplication springApplication = configuration.build(); springApplication.run(); Optional<ServletContext> servletContext = testContext.findProperty(SERVLET_CONTEXT); Optional<EmbeddedServletContainer> servletContainer = testContext.findProperty(SERVER); if (servletContext.isPresent() && servletContainer.isPresent()) { EmbeddedServletContainer server = servletContainer.get(); ServletContext context = servletContext.get(); String uri = format(DEFAULT_URI_FORMAT, server.getPort(), context .getContextPath()); URI baseURI = URI.create(uri); return ServerInstanceBuilder.builder() .baseURI(baseURI) .server(server) .property(APP, springApplication) .property(APP_NAME, testContext.getName()) .property(SERVER, server) .build("springboot", application); } throw ExceptionUtil.INSTANCE.propagate( "Could not start springboot application due to missing servlet container"); }
throw ExceptionUtil.INSTANCE.propagate(e);
void addScans(InjectionManager injectionManager, TestDescriptor testDescriptor, ClassLoader classLoader) throws TestifyException { try { DynamicConfigurationService dcs = injectionManager.getInstance(DynamicConfigurationService.class); DynamicConfiguration dc = dcs.createDynamicConfiguration(); Populator populator = dcs.getPopulator(); for (Scan scan : testDescriptor.getScans()) { JerseyDescriptorPopulator descriptorPopulator = new JerseyDescriptorPopulator(classLoader, scan.value()); populator.populate(descriptorPopulator); } dc.commit(); } catch (IOException | MultiException e) { throw ExceptionUtil.INSTANCE.propagate("Could not populate service instance", e); } }
createClient(testContext, testConfigurer, clientProvider, application); } catch (Exception ex) { throw ExceptionUtil.INSTANCE.propagate(ex);
void addScans(ServiceLocator serviceLocator, TestDescriptor testDescriptor, ClassLoader classLoader) throws TestifyException { try { DynamicConfigurationService dcs = serviceLocator.getService(DynamicConfigurationService.class); DynamicConfiguration dc = dcs.createDynamicConfiguration(); Populator populator = dcs.getPopulator(); for (Scan scan : testDescriptor.getScans()) { HK2DescriptorPopulator descriptorPopulator = new HK2DescriptorPopulator(classLoader, scan.value()); populator.populate(descriptorPopulator); } dc.commit(); } catch (IOException | MultiException e) { throw ExceptionUtil.INSTANCE.propagate("Could not populate service instance", e); } }
return serverInstance; } catch (Exception e) { throw ExceptionUtil.INSTANCE.propagate("Could not start Undertow Server", e);
@Override public DefaultDockerClient.Builder configure(TestContext testContext, VirtualResource virtualResource, PropertiesReader configReader) { try { DefaultDockerClient.Builder builder = DefaultDockerClient.fromEnv(); PropertiesReader reader = configReader; if (reader.isEmpty()) { reader = testContext.getPropertiesReader(DEFAULT_CONFIG_KEY); } if (!reader.isEmpty()) { RegistryAuth registryAuth = RegistryAuth.builder() .serverAddress(reader.getProperty("uri")) .email(reader.getProperty("email")) .username(reader.getProperty("username")) .password(reader.getProperty("password")) .build(); RegistryAuthSupplier dockerHubAuthSupplier = new DockerHubRegistryAuthSupplier(registryAuth); //TODO: explore making these configuration configurable via .testify.yml file builder.registryAuthSupplier(dockerHubAuthSupplier) .connectTimeoutMillis(10000) .connectionPoolSize(16); } return builder; } catch (DockerCertificateException e) { throw ExceptionUtil.INSTANCE.propagate(e); } }