/** * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem. * * @param path the path to the resource * @return a {@link MountableFile} that may be used to obtain a mountable path */ public static MountableFile forHostPath(final Path path) { return forHostPath(path, null); }
/** * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem. * * @param path the path to the resource * @return a {@link MountableFile} that may be used to obtain a mountable path */ public static MountableFile forHostPath(@NotNull final String path) { return forHostPath(path, null); }
/** * Adds file with given mode to tarball copied into container. * @param path in tarball * @param filePath in host filesystem * @param mode octal value of posix file mode (000..777) * @return self */ default SELF withFileFromPath(String path, Path filePath, Integer mode) { final MountableFile mountableFile = MountableFile.forHostPath(filePath, mode); return ((SELF) this).withFileFromTransferable(path, mountableFile); } }
/** * {@inheritDoc} */ @Override public void addFileSystemBind(final String hostPath, final String containerPath, final BindMode mode, final SelinuxContext selinuxContext) { final MountableFile mountableFile = MountableFile.forHostPath(hostPath); binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode, selinuxContext.selContext)); }
public ContainerisedDockerCompose(List<File> composeFiles, String identifier) { super(TestcontainersConfiguration.getInstance().getDockerComposeContainerImage()); validateFileList(composeFiles); addEnv(ENV_PROJECT_NAME, identifier); // Map the docker compose file into the container final File dockerComposeBaseFile = composeFiles.get(0); final String pwd = dockerComposeBaseFile.getAbsoluteFile().getParentFile().getAbsolutePath(); final String containerPwd = MountableFile.forHostPath(pwd).getFilesystemPath(); final List<String> absoluteDockerComposeFiles = composeFiles.stream() .map(File::getAbsolutePath) .map(MountableFile::forHostPath) .map(MountableFile::getFilesystemPath) .collect(toList()); final String composeFileEnvVariableValue = Joiner.on(UNIX_PATH_SEPERATOR).join(absoluteDockerComposeFiles); // we always need the UNIX path separator logger().debug("Set env COMPOSE_FILE={}", composeFileEnvVariableValue); addEnv(ENV_COMPOSE_FILE, composeFileEnvVariableValue); addFileSystemBind(pwd, containerPwd, READ_ONLY); // Ensure that compose can access docker. Since the container is assumed to be running on the same machine // as the docker daemon, just mapping the docker control socket is OK. // As there seems to be a problem with mapping to the /var/run directory in certain environments (e.g. CircleCI) // we map the socket file outside of /var/run, as just /docker.sock addFileSystemBind(getDockerSocketHostPath(), "/docker.sock", READ_WRITE); addEnv("DOCKER_HOST", "unix:///docker.sock"); setStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy()); setWorkingDirectory(containerPwd); String dockerConfigPath = determineDockerConfigPath(); if (dockerConfigPath != null && !dockerConfigPath.isEmpty()) { addFileSystemBind(dockerConfigPath, DOCKER_CONFIG_FILE, READ_ONLY); } }
@Test public void forHostPath() throws Exception { final Path file = createTempFile("somepath"); final MountableFile mountableFile = MountableFile.forHostPath(file.toString()); performChecks(mountableFile); }
@Test public void forHostPathWithPlus() throws Exception { final Path file = createTempFile("some+path"); final MountableFile mountableFile = MountableFile.forHostPath(file.toString()); performChecks(mountableFile); assertTrue("The resolved path contains the original space", mountableFile.getResolvedPath().contains("+")); assertFalse("The resolved path does not contain an escaped space", mountableFile.getResolvedPath().contains(" ")); }
@Test public void forHostPathWithSpaces() throws Exception { final Path file = createTempFile("some path"); final MountableFile mountableFile = MountableFile.forHostPath(file.toString()); performChecks(mountableFile); assertTrue("The resolved path contains the original space", mountableFile.getResolvedPath().contains(" ")); assertFalse("The resolved path does not contain an escaped space", mountableFile.getResolvedPath().contains("\\ ")); }
@Test public void forHostDirPathWithPermission() throws Exception { final Path dir = createTempDir(); final MountableFile mountableFile = MountableFile.forHostPath(dir.toString(), TEST_FILE_MODE); performChecks(mountableFile); assertEquals("Valid dir mode.", BASE_DIR_MODE | TEST_FILE_MODE, mountableFile.getFileMode()); }
@Test public void forHostFilePathWithPermission() throws Exception { final Path file = createTempFile("somepath"); final MountableFile mountableFile = MountableFile.forHostPath(file.toString(), TEST_FILE_MODE); performChecks(mountableFile); assertEquals("Valid file mode.", BASE_FILE_MODE | TEST_FILE_MODE, mountableFile.getFileMode()); }
/** * Obtains a {@link MountableFile} corresponding to a file on the docker host filesystem. * * @param path the path to the resource * @return a {@link MountableFile} that may be used to obtain a mountable path */ public static MountableFile forHostPath(final Path path) { return forHostPath(path, null); }
/** * Adds file with given mode to tarball copied into container. * @param path in tarball * @param filePath in host filesystem * @param mode octal value of posix file mode (000..777) * @return self */ default SELF withFileFromPath(String path, Path filePath, Integer mode) { final MountableFile mountableFile = MountableFile.forHostPath(filePath, mode); return ((SELF) this).withFileFromTransferable(path, mountableFile); } }
/** * {@inheritDoc} */ @Override public void addFileSystemBind(final String hostPath, final String containerPath, final BindMode mode, final SelinuxContext selinuxContext) { final MountableFile mountableFile = MountableFile.forHostPath(hostPath); binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode, selinuxContext.selContext)); }
public ContainerisedDockerCompose(List<File> composeFiles, String identifier) { super(TestcontainersConfiguration.getInstance().getDockerComposeContainerImage()); validateFileList(composeFiles); addEnv(ENV_PROJECT_NAME, identifier); // Map the docker compose file into the container final File dockerComposeBaseFile = composeFiles.get(0); final String pwd = dockerComposeBaseFile.getAbsoluteFile().getParentFile().getAbsolutePath(); final String containerPwd = MountableFile.forHostPath(pwd).getFilesystemPath(); final List<String> absoluteDockerComposeFiles = composeFiles.stream().map(File::getAbsolutePath).map(MountableFile::forHostPath).map(MountableFile::getFilesystemPath).collect(toList()); final String composeFileEnvVariableValue = Joiner.on(UNIX_PATH_SEPERATOR).join(absoluteDockerComposeFiles); // we always need the UNIX path separator logger().debug("Set env COMPOSE_FILE={}", composeFileEnvVariableValue); addEnv(ENV_COMPOSE_FILE, composeFileEnvVariableValue); addFileSystemBind(pwd, containerPwd, READ_ONLY); // Ensure that compose can access docker. Since the container is assumed to be running on the same machine // as the docker daemon, just mapping the docker control socket is OK. // As there seems to be a problem with mapping to the /var/run directory in certain environments (e.g. CircleCI) // we map the socket file outside of /var/run, as just /docker.sock addFileSystemBind(getDockerSocketHostPath(), "/docker.sock", READ_WRITE); addEnv("DOCKER_HOST", "unix:///docker.sock"); setStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy()); setWorkingDirectory(containerPwd); String dockerConfigPath = determineDockerConfigPath(); if (dockerConfigPath != null && !dockerConfigPath.isEmpty()) { addFileSystemBind(dockerConfigPath, DOCKER_CONFIG_FILE, READ_ONLY); } }