function it_should_create_console_command(DockerRunCommandBuilder $runCommandWithImage, DockerRunCommandBuilder $runCommandWithWorkDir, DockerRunCommandBuilder $runCommandWithRm, DockerRunCommandBuilder $runCommandWithEmptyEntrypoint, DockerRunCommandBuilder $runCommandWithEnvFirst, DockerRunCommandBuilder $runCommandWithEnvSecond, DockerRunCommandBuilder $runCommandWithEnvThird, DockerRunCommandBuilder $runCommandWithEnvFourth, DockerRunCommandBuilder $runCommandWithEnvFifth, DockerRunCommandBuilder $runCommandWithFirstVolume, DockerRunCommandBuilder $runCommandWithSecondVolume, DockerRunCommandBuilder $runCommandWithThirdVolume, DockerRunCommandBuilder $runCommandWithCmd) { $job = new Job('phpspec_php_5_6', 'registry.com/php:5.6-cli', new Stage('transfer-changes-phase'), ['phing composer-dev', 'phing phpspec'], [], []); $refName = 'develop'; $sleep = 200; $mappedVolumes = ['/artifact:/artifact_repository', '/data:/data']; $projectRoot = vfsStream::url('root/project'); $ciProjectId = md5($projectRoot); $variables = [new Variable('CI_CONFIG', 'some'), new Variable('CI_DIRECTORY', '/home')]; $this->dockerRunCommandBuilder->image('registry.com/php:5.6-cli')->willReturn($runCommandWithImage); $runCommandWithImage->workDir(ConsoleCommandFactory::CONTAINER_PROJECT_COPY)->willReturn($runCommandWithWorkDir); $runCommandWithWorkDir->rm(true)->willReturn($runCommandWithRm); $runCommandWithRm->entrypoint('/bin/sh')->willReturn($runCommandWithEmptyEntrypoint); $runCommandWithEmptyEntrypoint->environment('CI_CONFIG', 'some')->willReturn($runCommandWithEnvFirst); $runCommandWithEnvFirst->environment('CI_DIRECTORY', '/home')->willReturn($runCommandWithEnvSecond); $runCommandWithEnvSecond->environment('CI_PROJECT_ID', $ciProjectId)->willReturn($runCommandWithEnvThird); $runCommandWithEnvThird->environment('CI_PROJECT_DIR', ConsoleCommandFactory::CONTAINER_PROJECT_COPY)->willReturn($runCommandWithEnvFourth); $runCommandWithEnvFourth->environment('CI_BUILD_REF_NAME', $refName)->willReturn($runCommandWithEnvFifth); $runCommandWithEnvFifth->volume(ConsoleCommandFactory::CONTAINER_PROJECT, $projectRoot, 'ro')->willReturn($runCommandWithFirstVolume); $runCommandWithFirstVolume->volume('/artifact', '/artifact_repository')->willReturn($runCommandWithSecondVolume); $runCommandWithSecondVolume->volume('/data', '/data')->willReturn($runCommandWithThirdVolume); $runCommandWithThirdVolume->cmd("\"-c\" \"cp -R /build/base/. /build/project/ && phing composer-dev && phing phpspec && sleep 200\"")->willReturn($runCommandWithCmd); $command = 'docker run (..) registry.com/php:5.6-cli'; $runCommandWithCmd->toString()->willReturn($command); $this->createDockerRunCommand($job, $variables, $projectRoot, $refName, $sleep, $mappedVolumes)->shouldReturn($command); }
/** * @param Job $job * @param Variable[] $variables * @param string $projectRootPath * @param string|null $gitRefName * @param int|null $sleep * @param string[]|null $volumes * * @return string */ public function createDockerRunCommand(Job $job, array $variables, $projectRootPath, $gitRefName = null, $sleep = null, $volumes = null) { $projectId = md5($projectRootPath); $gitRefName = $gitRefName ? $gitRefName : 'develop'; $volumes = $volumes ? $this->getVolumesFromString($volumes) : []; $dockerRunCommandBuilder = $this->dockerRunCommandBuilder->image($job->imageName()); $dockerRunCommandBuilder = $dockerRunCommandBuilder->workDir(self::CONTAINER_PROJECT_COPY); $dockerRunCommandBuilder = $dockerRunCommandBuilder->rm(true); $dockerRunCommandBuilder = $dockerRunCommandBuilder->entrypoint('/bin/sh'); foreach ($variables as $variable) { $dockerRunCommandBuilder = $dockerRunCommandBuilder->environment($variable->key(), $variable->value()); } $dockerRunCommandBuilder = $dockerRunCommandBuilder->environment('CI_PROJECT_ID', $projectId); $dockerRunCommandBuilder = $dockerRunCommandBuilder->environment('CI_PROJECT_DIR', self::CONTAINER_PROJECT_COPY); $dockerRunCommandBuilder = $dockerRunCommandBuilder->environment('CI_BUILD_REF_NAME', $gitRefName); $dockerRunCommandBuilder = $dockerRunCommandBuilder->volume(self::CONTAINER_PROJECT, $projectRootPath, 'ro'); foreach ($volumes as $volume) { $dockerRunCommandBuilder = $dockerRunCommandBuilder->volume($volume->hostVolume(), $volume->containerVolume()); } $containerProject = self::CONTAINER_PROJECT; $containerProjectCopy = self::CONTAINER_PROJECT_COPY; $command = "\"-c\" \"cp -R {$containerProject}/. {$containerProjectCopy}/"; foreach ($job->scripts() as $script) { $command .= " && {$script}"; } $command .= $sleep ? " && sleep {$sleep}" : ''; $command .= "\""; $dockerRunCommandBuilder = $dockerRunCommandBuilder->cmd($command); return $dockerRunCommandBuilder->toString(); }