How to use DockerRegistryContainer method of org.testcontainers.DockerRegistryContainer class

Best Testcontainers-java code snippet using org.testcontainers.DockerRegistryContainer.DockerRegistryContainer

Source:DockerClientTest.java Github

copy

Full Screen

1package org.keycloak.testsuite.docker;2import org.junit.Assert;3import org.junit.BeforeClass;4import org.junit.Test;5import org.keycloak.common.Profile;6import org.keycloak.representations.idm.KeysMetadataRepresentation;7import org.keycloak.representations.idm.RealmRepresentation;8import org.keycloak.testsuite.AbstractKeycloakTest;9import org.keycloak.testsuite.ProfileAssume;10import org.slf4j.LoggerFactory;11import org.testcontainers.containers.BindMode;12import org.testcontainers.containers.Container;13import org.testcontainers.containers.GenericContainer;14import org.testcontainers.containers.output.Slf4jLogConsumer;15import sun.security.provider.X509Factory;16import java.io.File;17import java.io.PrintWriter;18import java.util.HashMap;19import java.util.List;20import java.util.Map;21import java.util.Optional;22import static org.hamcrest.MatcherAssert.assertThat;23import static org.hamcrest.Matchers.containsString;24import static org.junit.Assume.assumeTrue;25import static org.keycloak.testsuite.util.WaitUtils.pause;26public class DockerClientTest extends AbstractKeycloakTest {27 public static final String REALM_ID = "docker-test-realm";28 public static final String CLIENT_ID = "docker-test-client";29 public static final String DOCKER_USER = "docker-user";30 public static final String DOCKER_USER_PASSWORD = "password";31 public static final String REGISTRY_HOSTNAME = "localhost";32 public static final Integer REGISTRY_PORT = 5000;33 public static final String MINIMUM_DOCKER_VERSION = "1.8.0";34 private GenericContainer dockerRegistryContainer = null;35 private GenericContainer dockerClientContainer = null;36 private static String hostIp;37 private static String authServerPort;38 @BeforeClass39 public static void verifyEnvironment() {40 ProfileAssume.assumeFeatureEnabled(Profile.Feature.DOCKER);41 final Optional<DockerVersion> dockerVersion = new DockerHostVersionSupplier().get();42 assumeTrue("Could not determine docker version for host machine. It either is not present or accessible to the JVM running the test harness.", dockerVersion.isPresent());43 assumeTrue("Docker client on host machine is not a supported version. Please upgrade and try again.", DockerVersion.COMPARATOR.compare(dockerVersion.get(), DockerVersion.parseVersionString(MINIMUM_DOCKER_VERSION)) >= 0);44 hostIp = System.getProperty("host.ip");45 if (hostIp == null) {46 final Optional<String> foundHostIp = new DockerHostIpSupplier().get();47 if (foundHostIp.isPresent()) {48 hostIp = foundHostIp.get();49 }50 }51 Assert.assertNotNull("Could not resolve host machine's IP address for docker adapter, and 'host.ip' system poperty not set. Client will not be able to authenticate against the keycloak server!", hostIp);52 authServerPort = AUTH_SERVER_SSL_REQUIRED ? System.getProperty("auth.server.https.port") : System.getProperty("auth.server.http.port");53 }54 @Override55 public void addTestRealms(final List<RealmRepresentation> testRealms) {56 final RealmRepresentation dockerRealm = DockerTestRealmSetup.createRealm(REALM_ID);57 DockerTestRealmSetup.configureDockerRegistryClient(dockerRealm, CLIENT_ID);58 DockerTestRealmSetup.configureUser(dockerRealm, DOCKER_USER, DOCKER_USER_PASSWORD);59 testRealms.add(dockerRealm);60 }61 @Override62 public void beforeAbstractKeycloakTest() throws Exception {63 super.beforeAbstractKeycloakTest();64 // find the realm cert65 String realmCert = null;66 List<KeysMetadataRepresentation.KeyMetadataRepresentation> realmKeys = adminClient.realm(REALM_ID).keys().getKeyMetadata().getKeys();67 for (KeysMetadataRepresentation.KeyMetadataRepresentation key : realmKeys) {68 if (key.getType().equals("RSA")) {69 realmCert = key.getCertificate();70 }71 }72 if (realmCert == null) {73 throw new IllegalStateException("Cannot find public realm cert");74 }75 // save the cert to a file76 File tmpCertFile = File.createTempFile("keycloak-docker-realm-cert-", ".pem");77 tmpCertFile.deleteOnExit();78 PrintWriter tmpCertWriter = new PrintWriter(tmpCertFile);79 tmpCertWriter.println(X509Factory.BEGIN_CERT);80 tmpCertWriter.println(realmCert);81 tmpCertWriter.println(X509Factory.END_CERT);82 tmpCertWriter.close();83 final Map<String, String> environment = new HashMap<>();84 environment.put("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp");85 environment.put("REGISTRY_AUTH_TOKEN_REALM", "http://" + hostIp + ":" + authServerPort + "/auth/realms/" + REALM_ID + "/protocol/docker-v2/auth");86 environment.put("REGISTRY_AUTH_TOKEN_SERVICE", CLIENT_ID);87 environment.put("REGISTRY_AUTH_TOKEN_ISSUER", "http://" + hostIp + ":" + authServerPort + "/auth/realms/" + REALM_ID);88 environment.put("REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE", "/opt/kc-certs/" + tmpCertFile.getCanonicalFile().getName());89 environment.put("INSECURE_REGISTRY", "--insecure-registry " + REGISTRY_HOSTNAME + ":" + REGISTRY_PORT);90 String dockerioPrefix = Boolean.parseBoolean(System.getProperty("docker.io-prefix-explicit")) ? "docker.io/" : "";91 dockerRegistryContainer = new GenericContainer(dockerioPrefix + "registry:2")92 .withFileSystemBind(tmpCertFile.getCanonicalPath(), "/opt/kc-certs/" + tmpCertFile.getCanonicalFile().getName(), BindMode.READ_ONLY)93 .withEnv(environment)94 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("dockerRegistryContainer")))95 .withNetworkMode("host")96 .withPrivilegedMode(true);97 dockerRegistryContainer.start();98 dockerClientContainer = new GenericContainer(dockerioPrefix + "docker:dind")99 .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("dockerClientContainer")))100 .withNetworkMode("host")101 .withPrivilegedMode(true);102 dockerClientContainer.start();103 }104 @Override105 public void afterAbstractKeycloakTest() {106 super.afterAbstractKeycloakTest();107 pause(5000); // wait for the container logs108 dockerClientContainer.close();109 dockerRegistryContainer.close();110 }111 @Test112 public void shouldPerformDockerAuthAgainstRegistry() throws Exception {113 log.info("Starting the attempt for login...");114 Container.ExecResult dockerLoginResult = dockerClientContainer.execInContainer("docker", "login", "-u", DOCKER_USER, "-p", DOCKER_USER_PASSWORD, REGISTRY_HOSTNAME + ":" + REGISTRY_PORT);115 printCommandResult(dockerLoginResult);116 assertThat(dockerLoginResult.getStdout(), containsString("Login Succeeded"));117 }118 private void printCommandResult(Container.ExecResult result) {119 log.infof("Command executed. Output follows:\nSTDOUT: %s\n---\nSTDERR: %s", result.getStdout(), result.getStderr());120 }121}...

Full Screen

Full Screen

DockerRegistryContainer

Using AI Code Generation

copy

Full Screen

1DockerRegistryContainer registry = new DockerRegistryContainer();2registry.start();3String registryUrl = registry.getContainerIpAddress() + ":" + registry.getMappedPort(5000);4DockerClient dockerClient = DockerClientFactory.INSTANCE.client();5DockerClientConfigUtils.DockerClientConfigBuilder configBuilder = new DockerClientConfigUtils.DockerClientConfigBuilder();6configBuilder.withDockerHost(dockerClientConfig.getDockerHost());7configBuilder.withDockerTlsVerify(dockerClientConfig.isDockerTlsVerify());8configBuilder.withDockerCertPath(dockerClientConfig.getDockerCertPath());9configBuilder.withApiVersion(dockerClientConfig.getApiVersion());10configBuilder.withRegistryUrl(registryUrl);11configBuilder.withRegistryUsername(dockerClientConfig.getRegistryUsername());12configBuilder.withRegistryPassword(dockerClientConfig.getRegistryPassword());13configBuilder.withRegistryEmail(dockerClientConfig.getRegistryEmail());14configBuilder.withRegistryUrl(dockerClientConfig.getRegistryUrl());15configBuilder.withRegistryUsername(dockerClientConfig.getRegistryUsername());16configBuilder.withRegistryPassword(dockerClientConfig.getRegistryPassword());17configBuilder.withRegistryEmail(dockerClientConfig.getRegistryEmail());18configBuilder.withMaxPerRouteConnections(dockerClientConfig.getMaxPerRouteConnections());19configBuilder.withMaxTotalConnections(dockerClientConfig.getMaxTotalConnections());20configBuilder.withConnectionTimeout(dockerClientConfig.getConnectionTimeout());21configBuilder.withConnectionRequestTimeout(dockerClientConfig.getConnectionRequestTimeout());22configBuilder.withSocketTimeout(dockerClientConfig.getSocketTimeout());23configBuilder.withRetryCount(dockerClientConfig.getRetryCount());24configBuilder.withRetryInterval(dockerClientConfig.getRetryInterval());25configBuilder.withCredentialsProvider(dockerClientConfig.getCredentialsProvider());26configBuilder.withServerAddress(dockerClientConfig.getServerAddress());27configBuilder.withCustomSslContext(dockerClientConfig.getCustomSslContext());28configBuilder.withDockerConfig(dockerClientConfig.getDockerConfig());29configBuilder.withDockerHost(dockerClientConfig.getDockerHost());30configBuilder.withDockerTlsVerify(dockerClientConfig.isDockerTlsVerify());31configBuilder.withDockerCertPath(dockerClientConfig.getDockerCertPath());32configBuilder.withApiVersion(dockerClientConfig.getApiVersion());33configBuilder.withRegistryUrl(registryUrl);

Full Screen

Full Screen

DockerRegistryContainer

Using AI Code Generation

copy

Full Screen

1DockerRegistryContainer registry = new DockerRegistryContainer()2registry.start()3String registryUrl = registry.getRegistryUrl()4int registryPort = registry.getMappedPort(5000)5String registryAddress = registry.getContainerIpAddress()6String registryContainerId = registry.getContainerId()7ContainerInfo registryContainerInfo = registry.getContainerInfo()8String registryContainerName = registry.getContainerName()9HostConfig registryHostConfig = registry.getHostConfig()10String registryContainerImage = registry.getDockerImageName()11String registryContainerLabel = registry.getLabel()12Set<Integer> registryContainerExposedPorts = registry.getExposedPorts()13Set<Integer> registryContainerMappedPorts = registry.getMappedPorts()14Consumer<OutputFrame> registryContainerLogConsumer = registry.getLogger()15Network registryContainerNetwork = registry.getNetwork()16Set<String> registryContainerNetworkAliases = registry.getNetworkAliases()17String registryContainerNetworkId = registry.getNetworkId()18String registryContainerNetworkMode = registry.getNetworkMode()19String registryContainerNetworkName = registry.getNetworkName()20String registryContainerNetworkShortId = registry.getNetworkShortId()21String registryContainerNetworkId = registry.getNetworkId()22ContainerState registryContainerState = registry.getContainerState()

Full Screen

Full Screen

DockerRegistryContainer

Using AI Code Generation

copy

Full Screen

1 def registry = new DockerRegistryContainer()2 registry.start()3 def dockerImage = DockerImageName.parse("alpine:3.12.0").asCompatibleSubstituteFor("alpine")4 def dockerClient = DockerClientFactory.instance().client()5 def image = dockerClient.pullImageCmd(dockerImage.getUnversionedPart())6 .withTag(dockerImage.getVersionPart())7 .exec(new PullImageResultCallback())8 .awaitSuccess()9 dockerClient.tagImageCmd(image.getId(), registry.getRegistryUrl(), "alpine")10 .withTag("3.12.0")11 .exec()12 dockerClient.pushImageCmd(registry.getRegistryUrl())13 .withImageId(image.getId())14 .withTag("alpine")15 .exec(new PushImageResultCallback())16 .awaitSuccess()17 registry.stop()18 def dockerClient2 = DockerClientFactory.instance().client()19 def image2 = dockerClient2.pullImageCmd(registry.getRegistryUrl())20 .withTag("alpine")21 .exec(new PullImageResultCallback())22 .awaitSuccess()23 println image2.getId()24 dockerClient2.removeImageCmd(image2.getId()).exec()25 dockerClient.removeImageCmd(image.getId()).exec()26 registry.close()27 def registry = new DockerRegistryContainer()28 registry.start()29 def dockerImage = DockerImageName.parse("alpine:3.12.0").asCompatibleSubstituteFor("alpine")30 def dockerClient = DockerClientFactory.instance().client()31 def image = dockerClient.pullImageCmd(dockerImage.getUnversionedPart())32 .withTag(dockerImage.getVersionPart())33 .exec(new PullImageResultCallback())34 .awaitSuccess()35 dockerClient.tagImageCmd(image.getId(), registry.getRegistryUrl(), "alpine")36 .withTag("3.12.0")37 .exec()38 dockerClient.pushImageCmd(registry.getRegistryUrl())39 .withImageId(image.getId

Full Screen

Full Screen

DockerRegistryContainer

Using AI Code Generation

copy

Full Screen

1DockerRegistryContainer registry = new DockerRegistryContainer();2registry.start();3DockerClient dockerClient = DockerClientFactory.instance().client();4dockerClient.pushImageCmd("image").withTag("tag").withRegistry(registry.getRegistryUrl()).exec(new PushImageResultCallback()).awaitSuccess();5dockerClient.pullImageCmd("image").withTag("tag").withRegistry(registry.getRegistryUrl()).exec(new PullImageResultCallback()).awaitSuccess();6InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd("image").withTag("tag").withRegistry(registry.getRegistryUrl()).exec();7dockerClient.removeImageCmd("image").withTag("tag").withRegistry(registry.getRegistryUrl()).exec();8dockerClient.tagImageCmd("image", "tag", "newImage").withTag("newTag").withRegistry(registry.getRegistryUrl()).exec();9dockerClient.tagImageCmd("image", "tag", "newImage").withTag("newTag").withRegistry(registry.getRegistryUrl()).exec();10dockerClient.tagImageCmd("image", "tag", "newImage").withTag("

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in DockerRegistryContainer

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful