diff --git a/src/main/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfiguration.java b/src/main/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfiguration.java index aa878490..ddd83d84 100644 --- a/src/main/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfiguration.java +++ b/src/main/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfiguration.java @@ -43,12 +43,42 @@ public class BuildConfiguration { - private Map config; + private final Map config; - public BuildConfiguration(Map config) { + public BuildConfiguration(final Map config) { this.config = config; } + public static ShellCommands getCheckoutCommands(final Map dotCiEnvVars) { + final String gitCloneUrl = (String) dotCiEnvVars.get("DOTCI_DOCKER_COMPOSE_GIT_CLONE_URL"); + final GitUrl gitRepoUrl = new GitUrl(gitCloneUrl); + final boolean isPrivateRepo = Boolean.parseBoolean((String) dotCiEnvVars.get("DOTCI_IS_PRIVATE_REPO")); + final String gitUrl = isPrivateRepo ? gitRepoUrl.getGitUrl() : gitCloneUrl; + final ShellCommands shellCommands = new ShellCommands(); + shellCommands.add("chmod -R u+w . ; find . ! -path \"./deploykey_rsa.pub\" ! -path \"./deploykey_rsa\" -delete"); + shellCommands.add("git init"); + shellCommands.add(format("git remote add origin %s", gitUrl)); + + if (dotCiEnvVars.get("DOTCI_PULL_REQUEST") != null) { + if (isPrivateRepo) { + + shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin '+refs/pull/%s/merge:' \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_PULL_REQUEST"))); + } else { + shellCommands.add(format("git fetch origin \"+refs/pull/%s/merge:\"", dotCiEnvVars.get("DOTCI_PULL_REQUEST"))); + } + shellCommands.add("git reset --hard FETCH_HEAD"); + } else { + if (isPrivateRepo) { + + shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin %s \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_BRANCH"))); + } else { + shellCommands.add(format("git fetch origin %s", dotCiEnvVars.get("DOTCI_BRANCH"))); + } + shellCommands.add(format("git reset --hard %s", dotCiEnvVars.get("SHA"))); + } + return shellCommands; + } + public ShellCommands getBeforeRunCommandsIfPresent() { return getShellCommands("before_run"); } @@ -57,12 +87,13 @@ public ShellCommands getAfterRunCommandsIfPresent() { return getShellCommands("after_run"); } - public ShellCommands getCommands(Combination combination, Map dotCiEnvVars) { - String dockerComposeContainerName = combination.get("script"); - String projectName = (String) dotCiEnvVars.get("COMPOSE_PROJECT_NAME"); - String fileName = getDockerComposeFileName(); + public List getCommands(final Combination combination, final Map dotCiEnvVars) { + final List allCommands = new ArrayList<>(); + final String dockerComposeContainerName = combination.get("script"); + final String projectName = (String) dotCiEnvVars.get("COMPOSE_PROJECT_NAME"); + final String fileName = getDockerComposeFileName(); - ShellCommands shellCommands = new ShellCommands(); + final ShellCommands shellCommands = new ShellCommands(); shellCommands.add(BuildConfiguration.getCheckoutCommands(dotCiEnvVars)); shellCommands.add(String.format("trap \"docker-compose -f %s down; exit\" PIPE QUIT INT HUP EXIT TERM", fileName)); @@ -71,23 +102,24 @@ public ShellCommands getCommands(Combination combination, Map do appendCommands("before_each", shellCommands); shellCommands.add(String.format("docker-compose -f %s pull", fileName)); - if (config.get("run") != null) { - Map runConfig = (Map) config.get("run"); - String dockerComposeRunCommand = getDockerComposeRunCommand(dockerComposeContainerName, fileName, runConfig); + if (this.config.get("run") != null) { + final Map runConfig = (Map) this.config.get("run"); + final String dockerComposeRunCommand = getDockerComposeRunCommand(dockerComposeContainerName, fileName, runConfig); shellCommands.add(format("export COMPOSE_CMD='%s'", dockerComposeRunCommand)); shellCommands.add(" set +e && hash unbuffer >/dev/null 2>&1 ; if [ $? = 0 ]; then set -e && unbuffer $COMPOSE_CMD ;else set -e && $COMPOSE_CMD ;fi"); } - extractWorkingDirIntoWorkSpace(dockerComposeContainerName, projectName, shellCommands); appendCommands("after_each", shellCommands); if (!isParallelized()) { appendCommands("after_run", shellCommands); } - return shellCommands; + allCommands.add(shellCommands); + allCommands.add(getCopyWorkDirIntoWorkspaceCommands(dockerComposeContainerName, projectName)); + return allCommands; } - private String getDockerComposeRunCommand(String dockerComposeContainerName, String fileName, Map runConfig) { - Object dockerComposeCommand = runConfig.get(dockerComposeContainerName); + private String getDockerComposeRunCommand(final String dockerComposeContainerName, final String fileName, final Map runConfig) { + final Object dockerComposeCommand = runConfig.get(dockerComposeContainerName); if (dockerComposeCommand != null) { return String.format("docker-compose -f %s run -T %s %s", fileName, dockerComposeContainerName, dockerComposeCommand); } else { @@ -95,34 +127,28 @@ private String getDockerComposeRunCommand(String dockerComposeContainerName, Str } } - private void extractWorkingDirIntoWorkSpace(String dockerComposeContainerName, String projectName, ShellCommands shellCommands) { - if (config.get("plugins") != null) { - shellCommands.add(getCopyWorkDirIntoWorkspaceCommands(dockerComposeContainerName, projectName)); - } - } - public AxisList getAxisList() { - String dockerComposeContainerName = getOnlyRun(); + final String dockerComposeContainerName = getOnlyRun(); AxisList axisList = new AxisList(new Axis("script", dockerComposeContainerName)); if (isParallelized()) { - Set commandKeys = ((Map) config.get("run")).keySet(); - axisList = new AxisList(new Axis("script", new ArrayList(commandKeys))); + final Set commandKeys = ((Map) this.config.get("run")).keySet(); + axisList = new AxisList(new Axis("script", new ArrayList<>(commandKeys))); } return axisList; } public String getOnlyRun() { - Map runConfig = (Map) config.get("run"); + final Map runConfig = (Map) this.config.get("run"); return (String) runConfig.keySet().iterator().next(); } public boolean isParallelized() { - return ((Map) config.get("run")).size() > 1; + return ((Map) this.config.get("run")).size() > 1; } - public ShellCommands getCopyWorkDirIntoWorkspaceCommands(String run, String projectName) { - ShellCommands copyCommands = new ShellCommands(); + public ShellCommands getCopyWorkDirIntoWorkspaceCommands(final String run, final String projectName) { + final ShellCommands copyCommands = new ShellCommands(false); copyCommands.add(String.format("if docker inspect %s_%s_1 &>/dev/null ; then containerName=%s_%s_1 ; else containerName=%s_%s_run_1 ; fi ; export containerName", projectName, run, projectName, run, projectName, run)); copyCommands.add("export workingDir=`docker inspect -f '{{ .Config.WorkingDir }}' $containerName | sed -e 's|^/||g'`"); copyCommands.add("stripComponents=0 ; if [ ! \"x\" == \"x$workingDir\" ]; then set +e ; (( stripComponents+=1 )) ; set -e ; fi ; export stripComponents"); @@ -132,69 +158,39 @@ public ShellCommands getCopyWorkDirIntoWorkspaceCommands(String run, String proj } public List getPlugins() { - List plugins = config.get("plugins") != null ? (List) config.get("plugins") : Collections.emptyList(); + final List plugins = this.config.get("plugins") != null ? (List) this.config.get("plugins") : Collections.emptyList(); return new DotCiExtensionsHelper().createPlugins(plugins); } public List getNotifiers() { - List notifiers = config.get("notifications") != null ? (List) config.get("notifications") : Collections.emptyList(); + final List notifiers = this.config.get("notifications") != null ? (List) this.config.get("notifications") : Collections.emptyList(); return new DotCiExtensionsHelper().createNotifiers(notifiers); } public String getDockerComposeFileName() { - return config.get("docker-compose-file") != null ? (String) config.get("docker-compose-file") : "docker-compose.yml"; - } - - public static ShellCommands getCheckoutCommands(Map dotCiEnvVars) { - String gitCloneUrl = (String) dotCiEnvVars.get("DOTCI_DOCKER_COMPOSE_GIT_CLONE_URL"); - GitUrl gitRepoUrl = new GitUrl(gitCloneUrl); - boolean isPrivateRepo = Boolean.parseBoolean((String) dotCiEnvVars.get("DOTCI_IS_PRIVATE_REPO")); - String gitUrl = isPrivateRepo ? gitRepoUrl.getGitUrl() : gitCloneUrl; - ShellCommands shellCommands = new ShellCommands(); - shellCommands.add("chmod -R u+w . ; find . ! -path \"./deploykey_rsa.pub\" ! -path \"./deploykey_rsa\" -delete"); - shellCommands.add("git init"); - shellCommands.add(format("git remote add origin %s", gitUrl)); - - if (dotCiEnvVars.get("DOTCI_PULL_REQUEST") != null) { - if (isPrivateRepo) { - - shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin '+refs/pull/%s/merge:' \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_PULL_REQUEST"))); - } else { - shellCommands.add(format("git fetch origin \"+refs/pull/%s/merge:\"", dotCiEnvVars.get("DOTCI_PULL_REQUEST"))); - } - shellCommands.add("git reset --hard FETCH_HEAD"); - } else { - if (isPrivateRepo) { - - shellCommands.add(format("ssh-agent bash -c \"ssh-add -D && ssh-add \\%s/deploykey_rsa && git fetch origin %s \"", dotCiEnvVars.get("WORKSPACE"), dotCiEnvVars.get("DOTCI_BRANCH"))); - } else { - shellCommands.add(format("git fetch origin %s", dotCiEnvVars.get("DOTCI_BRANCH"))); - } - shellCommands.add(format("git reset --hard %s", dotCiEnvVars.get("SHA"))); - } - return shellCommands; + return this.config.get("docker-compose-file") != null ? (String) this.config.get("docker-compose-file") : "docker-compose.yml"; } - private void appendCommands(String key, ShellCommands commands) { - ShellCommands added = getShellCommands(key); + private void appendCommands(final String key, final ShellCommands commands) { + final ShellCommands added = getShellCommands(key); if (added != null) { commands.add(added); } } - private ShellCommands getShellCommands(String key) { - Object value = config.get(key); + private ShellCommands getShellCommands(final String key) { + final Object value = this.config.get(key); if (value == null) { return null; } - ShellCommands commands = new ShellCommands(); + final ShellCommands commands = new ShellCommands(); if (value instanceof String) { commands.add((String) value); } else if (value instanceof List) { - List l = (List) value; + final List l = (List) value; - for (Object v : l) { + for (final Object v : l) { if (!(v instanceof String)) { throw new RuntimeException(String.format("Unexpected type: %s. Expected String for key: %s", v.getClass().getName(), key)); } @@ -205,6 +201,6 @@ private ShellCommands getShellCommands(String key) { } public boolean isSkipped() { - return config.containsKey("skip"); + return this.config.containsKey("skip"); } } diff --git a/src/main/java/com/groupon/jenkins/buildtype/dockercompose/DockerComposeBuild.java b/src/main/java/com/groupon/jenkins/buildtype/dockercompose/DockerComposeBuild.java index a31cbb63..18b92ffa 100644 --- a/src/main/java/com/groupon/jenkins/buildtype/dockercompose/DockerComposeBuild.java +++ b/src/main/java/com/groupon/jenkins/buildtype/dockercompose/DockerComposeBuild.java @@ -87,15 +87,24 @@ public Result runBuild(final DynamicBuild build, final BuildExecutionContext bui @Override public Result runSubBuild(final Combination combination, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException { - final ShellCommands commands = this.buildConfiguration.getCommands(combination, buildExecutionContext.getBuildEnvironmentVariables()); + final List commands = this.buildConfiguration.getCommands(combination, buildExecutionContext.getBuildEnvironmentVariables()); return runCommands(commands, buildExecutionContext, listener); } + private Result doCheckout(final Map buildEnvironment, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException { final ShellCommands commands = BuildConfiguration.getCheckoutCommands(buildEnvironment); return runCommands(commands, buildExecutionContext, listener); } + private Result runCommands(final List commandList, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException { + final Result result = Result.SUCCESS; + for (final ShellCommands commands : commandList) { + result.combine(runCommands(commands, buildExecutionContext, listener)); + } + return result; + } + private Result runCommands(final ShellCommands commands, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException { if (commands == null) { return Result.SUCCESS; diff --git a/src/main/java/com/groupon/jenkins/buildtype/util/shell/ShellCommands.java b/src/main/java/com/groupon/jenkins/buildtype/util/shell/ShellCommands.java index 00a137bc..4afda274 100644 --- a/src/main/java/com/groupon/jenkins/buildtype/util/shell/ShellCommands.java +++ b/src/main/java/com/groupon/jenkins/buildtype/util/shell/ShellCommands.java @@ -37,26 +37,50 @@ of this software and associated documentation files (the "Software"), to deal import java.util.regex.Pattern; public class ShellCommands { - private final List commands; public static final ShellCommands NOOP = new ShellCommands("NOOP", ""); + public static final Escaper SHELL_ESCAPE; + + static { + final Escapers.Builder builder = Escapers.builder(); + builder.addEscape('\'', "'\"'\"'"); + SHELL_ESCAPE = builder.build(); + } + + private final List commands; + private boolean echoCommands; - public ShellCommands(String... commands) { + public ShellCommands(final boolean echoCommands, final String... commands) { + this.echoCommands = echoCommands; this.commands = Lists.newArrayList(commands); } - public ShellCommands(List commands) { + public ShellCommands(final String... commands) { + this(true, commands); + } + + public ShellCommands(final List commands) { this.commands = commands; } - public String get(int index) { - return commands.get(index); + public static ShellCommands combine(final List subPhases) { + final ShellCommands combinedShellCommands = new ShellCommands(new LinkedList<>()); + for (final ShellCommands shellCommands : subPhases) { + if (shellCommands != null && ShellCommands.NOOP != shellCommands) { + combinedShellCommands.add(shellCommands); + } + } + return combinedShellCommands; } - public void add(String command) { - commands.add(command); + public String get(final int index) { + return this.commands.get(index); } - protected String concat(String... lines) { + public void add(final String command) { + this.commands.add(command); + } + + protected String concat(final String... lines) { return Joiner.on("\n").join(lines); } @@ -65,32 +89,27 @@ public String toShellScript() { } private String[] formatCommandsForConsole() { - List formattedCommands = new ArrayList(2 * commands.size()); - for (String command : commands) { - formattedCommands.add(echoCommand(command)); + final List formattedCommands = new ArrayList<>(2 * this.commands.size()); + for (final String command : this.commands) { + if (this.echoCommands) { + formattedCommands.add(echoCommand(command)); + } formattedCommands.add(command); } return formattedCommands.toArray(new String[formattedCommands.size()]); } - private String echoCommand(String command) { + private String echoCommand(final String command) { return "echo $ ' " + escapeForShell(command) + "'"; } - public static final Escaper SHELL_ESCAPE; - - static { - final Escapers.Builder builder = Escapers.builder(); - builder.addEscape('\'', "'\"'\"'"); - SHELL_ESCAPE = builder.build(); - } - - private String escapeForShell(String command) { + private String escapeForShell(final String command) { return SHELL_ESCAPE.escape((String) command); } public Pattern regexp(String re) { - int n = re.length() - 1, flags = Pattern.UNIX_LINES | Pattern.MULTILINE; + final int n = re.length() - 1; + final int flags = Pattern.UNIX_LINES | Pattern.MULTILINE; if (re.charAt(0) != '^') { re = ".*" + re; } @@ -100,42 +119,32 @@ public Pattern regexp(String re) { return Pattern.compile(re, flags); } - public ShellCommands addAll(Stack commands) { + public ShellCommands addAll(final Stack commands) { while (!commands.empty()) { this.commands.add(commands.pop()); } return this; } - public ShellCommands addAll(List commands) { + public ShellCommands addAll(final List commands) { this.commands.addAll(commands); return this; } - public ShellCommands add(ShellCommands otherCommands) { - commands.addAll(otherCommands.getCommands()); + public ShellCommands add(final ShellCommands otherCommands) { + this.commands.addAll(otherCommands.getCommands()); return this; } public Collection getCommands() { - return commands; + return this.commands; } public String toSingleShellCommand() { - return Joiner.on(" && ").join(commands); - } - - public static ShellCommands combine(List subPhases) { - ShellCommands combinedShellCommands = new ShellCommands(new LinkedList()); - for (ShellCommands shellCommands : subPhases) { - if (shellCommands != null && ShellCommands.NOOP != shellCommands) { - combinedShellCommands.add(shellCommands); - } - } - return combinedShellCommands; + return Joiner.on(" && ").join(this.commands); } public int size() { - return commands.size(); + return this.commands.size(); } } diff --git a/src/test/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfigurationTest.java b/src/test/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfigurationTest.java index 977166e8..d7dadd94 100644 --- a/src/test/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfigurationTest.java +++ b/src/test/java/com/groupon/jenkins/buildtype/dockercompose/BuildConfigurationTest.java @@ -30,157 +30,160 @@ import com.groupon.jenkins.buildtype.util.shell.ShellCommands; import hudson.matrix.Combination; import org.junit.Assert; -import static com.google.common.collect.ImmutableMap.of; import org.junit.Test; -import java.util.*; +import java.util.Map; + +import static com.google.common.collect.ImmutableMap.of; public class BuildConfigurationTest { - @Test - public void should_parallelize_on_run(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit","command","integration","integration"))); - Iterable axisList = buildConfiguration.getAxisList().list(); - Assert.assertEquals(2,Iterables.size(axisList)); - Assert.assertEquals("script=unit", Iterables.get(axisList,0).toString()); - Assert.assertEquals("script=integration", Iterables.get(axisList,1).toString()); - } - - @Test - public void should_cleanup_after_test_run(){ - ShellCommands commands = getRunCommands(); - Assert.assertEquals("trap \"docker-compose -f docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM",commands.get(5)); - } - - @Test - public void should_pull_latest_image_from_registry(){ - ShellCommands commands = getRunCommands(); - Assert.assertEquals("docker-compose -f docker-compose.yml pull",commands.get(6)); - } - - @Test - public void should_run_cmd_from_ci_yml(){ - ShellCommands commands = getRunCommands(); - Assert.assertEquals("export COMPOSE_CMD='docker-compose -f docker-compose.yml run -T unit command'",commands.get(7)); - Assert.assertEquals(" set +e && hash unbuffer >/dev/null 2>&1 ; if [ $? = 0 ]; then set -e && unbuffer $COMPOSE_CMD ;else set -e && $COMPOSE_CMD ;fi",commands.get(8)); - } - - @Test - public void should_run_before_each_command_string(){ - ShellCommands commands = getRunCommands(ImmutableMap.of("before_each", "before_each cmd", "run", of("unit", "command", "integration", "integration"))); - Assert.assertEquals("trap \"docker-compose -f docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM",commands.get(5)); - Assert.assertEquals("before_each cmd", commands.get(6)); - } - - @Test - public void should_run_before_each_command_list(){ - ShellCommands commands = getRunCommands(ImmutableMap.of("before_each", ImmutableList.of("cmd 1", "cmd 2"), "run", of("unit", "command", "integration", "integration"))); - Assert.assertEquals("trap \"docker-compose -f docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM",commands.get(5)); - Assert.assertEquals("cmd 1", commands.get(6)); - Assert.assertEquals("cmd 2", commands.get(7)); - } - - @Test - public void should_run_before_run_command_string(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", "before_run cmd", "run", of("unit", "command", "integration", "integration"))); - ShellCommands commands = buildConfiguration.getBeforeRunCommandsIfPresent(); - Assert.assertEquals(1, commands.size()); - Assert.assertEquals("before_run cmd", commands.get(0)); - } - - @Test - public void should_run_before_run_command_list(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", ImmutableList.of("cmd 1", "cmd 2"), "run", of("unit", "command", "integration", "integration"))); - ShellCommands commands = buildConfiguration.getBeforeRunCommandsIfPresent(); - Assert.assertEquals(2, commands.size()); - Assert.assertEquals("cmd 1", commands.get(0)); - Assert.assertEquals("cmd 2", commands.get(1)); - } - - @Test - public void should_not_run_before_run_for_parallel_subbuild(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", "before_run cmd", "run", of("unit", "command", "integration", "integration"))); - ShellCommands commands = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - for (String command : commands.getCommands()) { - Assert.assertNotEquals("before_run cmd", command); - } - } - - @Test - public void should_accept_alternative_docker_compose_file(){ - ShellCommands commands = getRunCommands(ImmutableMap.of("docker-compose-file", "./jenkins/docker-compose.yml", "run", of("unit", "command"))); - Assert.assertEquals("trap \"docker-compose -f ./jenkins/docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM",commands.get(5)); - Assert.assertEquals("docker-compose -f ./jenkins/docker-compose.yml pull",commands.get(6)); - Assert.assertEquals("export COMPOSE_CMD='docker-compose -f ./jenkins/docker-compose.yml run -T unit command'",commands.get(7)); - Assert.assertEquals(" set +e && hash unbuffer >/dev/null 2>&1 ; if [ $? = 0 ]; then set -e && unbuffer $COMPOSE_CMD ;else set -e && $COMPOSE_CMD ;fi",commands.get(8)); - } - @Test - public void should_be_skipped_if_skip_specified(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("skip",true)); - Assert.assertTrue(buildConfiguration.isSkipped()); - } - @Test - public void should_not_be_skipped_if_not_specified(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", "before_run cmd", "run", of("unit", "command", "integration", "integration"))); - Assert.assertFalse(buildConfiguration.isSkipped()); - } - - @Test - public void should_run_after_each_string(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"), "after_each", "after_each cmd")); - ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - Assert.assertEquals("after_each cmd",Iterables.getLast(commandList.getCommands())); - } - - @Test - public void should_run_after_each_list(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"), "after_each", ImmutableList.of("cmd 1", "cmd 2"))); - ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - Assert.assertEquals("cmd 1", commandList.get(commandList.size()-2)); - Assert.assertEquals("cmd 2", commandList.get(commandList.size()-1)); - } - - @Test - public void should_not_run_after_run_after_parallized_builds_are_finished(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"), "after_run", "after_run cmd")); - ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - Assert.assertNotEquals("after_run cmd",Iterables.getLast(commandList.getCommands())); - } - - @Test - public void should_run_after_run_for_non_parallel(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command"), "after_run", "after_run cmd")); - ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - Assert.assertEquals("after_run cmd",Iterables.getLast(commandList.getCommands())); - } - - @Test - public void should_run_after_run_command_string(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("after_run", "after_run cmd", "run", of("unit", "command", "integration", "integration"))); - ShellCommands commands = buildConfiguration.getAfterRunCommandsIfPresent(); - Assert.assertEquals(1, commands.size()); - Assert.assertEquals("after_run cmd", commands.get(0)); - } - - @Test - public void should_run_after_run_command_list(){ - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("after_run", ImmutableList.of("cmd 1", "cmd 2"), "run", of("unit", "command", "integration", "integration"))); - ShellCommands commands = buildConfiguration.getAfterRunCommandsIfPresent(); - Assert.assertEquals(2, commands.size()); - Assert.assertEquals("cmd 1", commands.get(0)); - Assert.assertEquals("cmd 2", commands.get(1)); - } - - private ShellCommands getRunCommands(Map ciConfig) { - BuildConfiguration buildConfiguration = new BuildConfiguration(ciConfig); - return buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - } - - private ShellCommands getRunCommands() { - BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"))); - return buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()); - } - - private Map getEnvVars() { - return ImmutableMap.of("DOTCI_DOCKER_COMPOSE_GIT_CLONE_URL","git@github.com:groupon/DotCi.git", "DOTCI_BRANCH", "master", "SHA", "abc123"); - } + @Test + public void should_parallelize_on_run() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"))); + final Iterable axisList = buildConfiguration.getAxisList().list(); + Assert.assertEquals(2, Iterables.size(axisList)); + Assert.assertEquals("script=unit", Iterables.get(axisList, 0).toString()); + Assert.assertEquals("script=integration", Iterables.get(axisList, 1).toString()); + } + + @Test + public void should_cleanup_after_test_run() { + final ShellCommands commands = getRunCommands(); + Assert.assertEquals("trap \"docker-compose -f docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM", commands.get(5)); + } + + @Test + public void should_pull_latest_image_from_registry() { + final ShellCommands commands = getRunCommands(); + Assert.assertEquals("docker-compose -f docker-compose.yml pull", commands.get(6)); + } + + @Test + public void should_run_cmd_from_ci_yml() { + final ShellCommands commands = getRunCommands(); + Assert.assertEquals("export COMPOSE_CMD='docker-compose -f docker-compose.yml run -T unit command'", commands.get(7)); + Assert.assertEquals(" set +e && hash unbuffer >/dev/null 2>&1 ; if [ $? = 0 ]; then set -e && unbuffer $COMPOSE_CMD ;else set -e && $COMPOSE_CMD ;fi", commands.get(8)); + } + + @Test + public void should_run_before_each_command_string() { + final ShellCommands commands = getRunCommands(ImmutableMap.of("before_each", "before_each cmd", "run", of("unit", "command", "integration", "integration"))); + Assert.assertEquals("trap \"docker-compose -f docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM", commands.get(5)); + Assert.assertEquals("before_each cmd", commands.get(6)); + } + + @Test + public void should_run_before_each_command_list() { + final ShellCommands commands = getRunCommands(ImmutableMap.of("before_each", ImmutableList.of("cmd 1", "cmd 2"), "run", of("unit", "command", "integration", "integration"))); + Assert.assertEquals("trap \"docker-compose -f docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM", commands.get(5)); + Assert.assertEquals("cmd 1", commands.get(6)); + Assert.assertEquals("cmd 2", commands.get(7)); + } + + @Test + public void should_run_before_run_command_string() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", "before_run cmd", "run", of("unit", "command", "integration", "integration"))); + final ShellCommands commands = buildConfiguration.getBeforeRunCommandsIfPresent(); + Assert.assertEquals(1, commands.size()); + Assert.assertEquals("before_run cmd", commands.get(0)); + } + + @Test + public void should_run_before_run_command_list() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", ImmutableList.of("cmd 1", "cmd 2"), "run", of("unit", "command", "integration", "integration"))); + final ShellCommands commands = buildConfiguration.getBeforeRunCommandsIfPresent(); + Assert.assertEquals(2, commands.size()); + Assert.assertEquals("cmd 1", commands.get(0)); + Assert.assertEquals("cmd 2", commands.get(1)); + } + + @Test + public void should_not_run_before_run_for_parallel_subbuild() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", "before_run cmd", "run", of("unit", "command", "integration", "integration"))); + final ShellCommands commands = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + for (final String command : commands.getCommands()) { + Assert.assertNotEquals("before_run cmd", command); + } + } + + @Test + public void should_accept_alternative_docker_compose_file() { + final ShellCommands commands = getRunCommands(ImmutableMap.of("docker-compose-file", "./jenkins/docker-compose.yml", "run", of("unit", "command"))); + Assert.assertEquals("trap \"docker-compose -f ./jenkins/docker-compose.yml down; exit\" PIPE QUIT INT HUP EXIT TERM", commands.get(5)); + Assert.assertEquals("docker-compose -f ./jenkins/docker-compose.yml pull", commands.get(6)); + Assert.assertEquals("export COMPOSE_CMD='docker-compose -f ./jenkins/docker-compose.yml run -T unit command'", commands.get(7)); + Assert.assertEquals(" set +e && hash unbuffer >/dev/null 2>&1 ; if [ $? = 0 ]; then set -e && unbuffer $COMPOSE_CMD ;else set -e && $COMPOSE_CMD ;fi", commands.get(8)); + } + + @Test + public void should_be_skipped_if_skip_specified() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("skip", true)); + Assert.assertTrue(buildConfiguration.isSkipped()); + } + + @Test + public void should_not_be_skipped_if_not_specified() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("before_run", "before_run cmd", "run", of("unit", "command", "integration", "integration"))); + Assert.assertFalse(buildConfiguration.isSkipped()); + } + + @Test + public void should_run_after_each_string() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"), "after_each", "after_each cmd")); + final ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + Assert.assertEquals("after_each cmd", Iterables.getLast(commandList.getCommands())); + } + + @Test + public void should_run_after_each_list() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"), "after_each", ImmutableList.of("cmd 1", "cmd 2"))); + final ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + Assert.assertEquals("cmd 1", commandList.get(commandList.size() - 2)); + Assert.assertEquals("cmd 2", commandList.get(commandList.size() - 1)); + } + + @Test + public void should_not_run_after_run_after_parallized_builds_are_finished() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"), "after_run", "after_run cmd")); + final ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + Assert.assertNotEquals("after_run cmd", Iterables.getLast(commandList.getCommands())); + } + + @Test + public void should_run_after_run_for_non_parallel() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command"), "after_run", "after_run cmd")); + final ShellCommands commandList = buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + Assert.assertEquals("after_run cmd", Iterables.getLast(commandList.getCommands())); + } + + @Test + public void should_run_after_run_command_string() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("after_run", "after_run cmd", "run", of("unit", "command", "integration", "integration"))); + final ShellCommands commands = buildConfiguration.getAfterRunCommandsIfPresent(); + Assert.assertEquals(1, commands.size()); + Assert.assertEquals("after_run cmd", commands.get(0)); + } + + @Test + public void should_run_after_run_command_list() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("after_run", ImmutableList.of("cmd 1", "cmd 2"), "run", of("unit", "command", "integration", "integration"))); + final ShellCommands commands = buildConfiguration.getAfterRunCommandsIfPresent(); + Assert.assertEquals(2, commands.size()); + Assert.assertEquals("cmd 1", commands.get(0)); + Assert.assertEquals("cmd 2", commands.get(1)); + } + + private ShellCommands getRunCommands(final Map ciConfig) { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ciConfig); + return buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + } + + private ShellCommands getRunCommands() { + final BuildConfiguration buildConfiguration = new BuildConfiguration(ImmutableMap.of("run", of("unit", "command", "integration", "integration"))); + return buildConfiguration.getCommands(Combination.fromString("script=unit"), getEnvVars()).get(0); + } + + private Map getEnvVars() { + return ImmutableMap.of("DOTCI_DOCKER_COMPOSE_GIT_CLONE_URL", "git@github.com:groupon/DotCi.git", "DOTCI_BRANCH", "master", "SHA", "abc123"); + } } diff --git a/src/test/java/com/groupon/jenkins/buildtype/util/shell/ShellCommandsTest.java b/src/test/java/com/groupon/jenkins/buildtype/util/shell/ShellCommandsTest.java index 5deacd92..345f1963 100644 --- a/src/test/java/com/groupon/jenkins/buildtype/util/shell/ShellCommandsTest.java +++ b/src/test/java/com/groupon/jenkins/buildtype/util/shell/ShellCommandsTest.java @@ -23,14 +23,22 @@ of this software and associated documentation files (the "Software"), to deal */ package com.groupon.jenkins.buildtype.util.shell; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; public class ShellCommandsTest { @Test public void should_escape_quotes_in_echo() { - ShellCommands executionPhase = new ShellCommands("echo \"hello\"", "echo world"); - String[] commands = executionPhase.toShellScript().split("\\r?\\n"); + final ShellCommands executionPhase = new ShellCommands("echo \"hello\"", "echo world"); + final String[] commands = executionPhase.toShellScript().split("\\r?\\n"); Assert.assertEquals("echo $ \' echo \"hello\"\'", commands[0]); } + + @Test + public void should_not_echo_commands_if_echo_turned_off() { + final ShellCommands executionPhase = new ShellCommands(false, "echo meow"); + final String[] commands = executionPhase.toShellScript().split("\\r?\\n"); + Assert.assertEquals(1, commands.length); + Assert.assertEquals("echo meow", commands[0]); + } }