Skip to content

Commit

Permalink
Merge pull request #254 from groupon/validation
Browse files Browse the repository at this point in the history
split each .ci.yml section into a self validating module
  • Loading branch information
suryagaddipati authored Dec 19, 2016
2 parents c567ef7 + c1e8a84 commit 598c3fc
Show file tree
Hide file tree
Showing 13 changed files with 535 additions and 84 deletions.
22 changes: 18 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.5</version>
<relativePath />
<relativePath/>
</parent>
<groupId>com.groupon.jenkins-ci.plugins</groupId>
<artifactId>DotCi</artifactId>
Expand Down Expand Up @@ -241,11 +243,23 @@ THE SOFTWARE.
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<artifactId>mockito-core</artifactId>
<version>1.10.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,28 @@ of this software and associated documentation files (the "Software"), to deal
*/
package com.groupon.jenkins.buildtype;

import com.google.common.base.Joiner;

import java.util.Arrays;

@SuppressWarnings("serial")
public class InvalidBuildConfigurationException extends RuntimeException {

private final Iterable<String> validationErrors;

public InvalidBuildConfigurationException(Iterable<String> validationErrors) {
public InvalidBuildConfigurationException(final Iterable<String> validationErrors) {
this.validationErrors = validationErrors;
}

public InvalidBuildConfigurationException(String validationError) {
public InvalidBuildConfigurationException(final String validationError) {
this(Arrays.asList(validationError));
}

public Iterable<String> getValidationErrors() {
return validationErrors;
return this.validationErrors;
}

@Override
public String getMessage() {
return Joiner.on(",").join(this.validationErrors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,52 @@

package com.groupon.jenkins.buildtype.dockercompose;

import com.groupon.jenkins.buildtype.dockercompose.buildconfiguration.ComposeFileNameSection;
import com.groupon.jenkins.buildtype.dockercompose.buildconfiguration.GenericCommandsSection;
import com.groupon.jenkins.buildtype.dockercompose.buildconfiguration.NotificationsSection;
import com.groupon.jenkins.buildtype.dockercompose.buildconfiguration.PluginsSection;
import com.groupon.jenkins.buildtype.dockercompose.buildconfiguration.RunSection;
import com.groupon.jenkins.buildtype.dockercompose.buildconfiguration.SkipSection;
import com.groupon.jenkins.buildtype.plugins.DotCiPluginAdapter;
import com.groupon.jenkins.buildtype.util.shell.ShellCommands;
import com.groupon.jenkins.extensions.DotCiExtensionsHelper;
import com.groupon.jenkins.git.GitUrl;
import com.groupon.jenkins.notifications.PostBuildNotifier;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.Combination;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.lang.String.format;

public class BuildConfiguration {

private final Map config;
private final SkipSection skipSection;
private GenericCommandsSection afterEachSection;
private GenericCommandsSection afterRunSection;
private GenericCommandsSection beforeRunSection;
private GenericCommandsSection beforeSection;
private GenericCommandsSection beforeEachSection;
private ComposeFileNameSection composeFileNameSection;
private NotificationsSection notificationsSection;
private PluginsSection pluginsSection;
private RunSection runSection;

public BuildConfiguration(final Map config) {
this.config = config;
this.skipSection = new SkipSection(config.get(SkipSection.KEY));
if (isSkipped())
return; //don't initialize/validate other sections if skipped
this.runSection = new RunSection(config.get(RunSection.KEY));
this.pluginsSection = new PluginsSection(config.get(PluginsSection.KEY));
this.notificationsSection = new NotificationsSection(config.get(NotificationsSection.KEY));
this.composeFileNameSection = new ComposeFileNameSection(config.get(ComposeFileNameSection.KEY));
this.beforeEachSection = new GenericCommandsSection(config.get("before_each"));
this.beforeSection = new GenericCommandsSection(config.get("before"));
this.beforeRunSection = new GenericCommandsSection(config.get("before_run"));
this.afterRunSection = new GenericCommandsSection(config.get("after_run"));
this.afterEachSection = new GenericCommandsSection(config.get("after_each"));
}

public static ShellCommands getCheckoutCommands(final Map<String, Object> dotCiEnvVars) {
Expand Down Expand Up @@ -80,11 +103,11 @@ public static ShellCommands getCheckoutCommands(final Map<String, Object> dotCiE
}

public ShellCommands getBeforeRunCommandsIfPresent() {
return getShellCommands("before_run");
return new ShellCommands(this.beforeRunSection.getCommands());
}

public ShellCommands getAfterRunCommandsIfPresent() {
return getShellCommands("after_run");
return new ShellCommands(this.afterRunSection.getCommands());
}

public List<ShellCommands> getCommands(final Combination combination, final Map<String, Object> dotCiEnvVars) {
Expand All @@ -96,21 +119,17 @@ public List<ShellCommands> getCommands(final Combination combination, final Map<
final ShellCommands shellCommands = new ShellCommands();
shellCommands.add(BuildConfiguration.getCheckoutCommands(dotCiEnvVars));


appendCommands("before", shellCommands); //deprecated
appendCommands("before_each", shellCommands);
shellCommands.addAll(this.beforeEachSection.getCommands());
shellCommands.addAll(this.beforeSection.getCommands());//deprecated

shellCommands.add(String.format("docker-compose -f %s pull", fileName));
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");
}

appendCommands("after_each", shellCommands);

shellCommands.addAll(this.runSection.getCommands(dockerComposeContainerName, fileName));

shellCommands.addAll(this.afterEachSection.getCommands());
if (!isParallelized()) {
appendCommands("after_run", shellCommands);
shellCommands.addAll(this.afterRunSection.getCommands());
}
allCommands.add(shellCommands);
allCommands.add(getCleanupCommands(dockerComposeContainerName, projectName));
Expand All @@ -124,33 +143,13 @@ private ShellCommands getCleanupCommands(final String 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 {
return String.format("docker-compose -f %s run %s ", fileName, dockerComposeContainerName);
}
}

public AxisList getAxisList() {
final String dockerComposeContainerName = getOnlyRun();
AxisList axisList = new AxisList(new Axis("script", dockerComposeContainerName));
if (isParallelized()) {
final Set commandKeys = ((Map) this.config.get("run")).keySet();
axisList = new AxisList(new Axis("script", new ArrayList<>(commandKeys)));
}
return axisList;
return this.runSection.getAxisList();
}

public String getOnlyRun() {
final Map runConfig = (Map) this.config.get("run");

return (String) runConfig.keySet().iterator().next();
}

public boolean isParallelized() {
return ((Map) this.config.get("run")).size() > 1;
return this.runSection.isParallelized();
}

public ShellCommands getCopyWorkDirIntoWorkspaceCommands(final String run, final String projectName) {
Expand All @@ -164,49 +163,19 @@ public ShellCommands getCopyWorkDirIntoWorkspaceCommands(final String run, final
}

public List<DotCiPluginAdapter> getPlugins() {
final List plugins = this.config.get("plugins") != null ? (List) this.config.get("plugins") : Collections.emptyList();
return new DotCiExtensionsHelper().createPlugins(plugins);
return new DotCiExtensionsHelper().createPlugins(this.pluginsSection.getPlugins());
}

public List<PostBuildNotifier> getNotifiers() {
final List notifiers = this.config.get("notifications") != null ? (List) this.config.get("notifications") : Collections.emptyList();
return new DotCiExtensionsHelper().createNotifiers(notifiers);
return new DotCiExtensionsHelper().createNotifiers(this.notificationsSection.getNotifiers());
}

public String getDockerComposeFileName() {
return this.config.get("docker-compose-file") != null ? (String) this.config.get("docker-compose-file") : "docker-compose.yml";
return this.composeFileNameSection.getComposeFileName();
}

private void appendCommands(final String key, final ShellCommands commands) {
final ShellCommands added = getShellCommands(key);
if (added != null) {
commands.add(added);
}
}

private ShellCommands getShellCommands(final String key) {
final Object value = this.config.get(key);
if (value == null) {
return null;
}

final ShellCommands commands = new ShellCommands();
if (value instanceof String) {
commands.add((String) value);
} else if (value instanceof List) {
final List l = (List) value;

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));
}
commands.add((String) v);
}
}
return commands;
}

public boolean isSkipped() {
return this.config.containsKey("skip");
return this.skipSection.isSkipped();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package com.groupon.jenkins.buildtype.dockercompose;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.groupon.jenkins.buildtype.InvalidBuildConfigurationException;
import com.groupon.jenkins.buildtype.plugins.DotCiPluginAdapter;
import com.groupon.jenkins.buildtype.util.shell.ShellCommands;
Expand Down Expand Up @@ -77,7 +77,7 @@ public Result runBuild(final DynamicBuild build, final BuildExecutionContext bui
if (this.buildConfiguration.isParallelized()) {
result = runParallelBuild(build, buildExecutionContext, this.buildConfiguration, listener);
} else {
result = runSubBuild(new Combination(ImmutableMap.of("script", this.buildConfiguration.getOnlyRun())), buildExecutionContext, listener);
result = runSubBuild(Iterables.getOnlyElement(this.buildConfiguration.getAxisList().list()), buildExecutionContext, listener);
}
}
final Result pluginResult = runPlugins(build, this.buildConfiguration.getPlugins(), listener, launcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
The MIT License (MIT)
Copyright (c) 2016, Groupon, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.groupon.jenkins.buildtype.dockercompose.buildconfiguration;

import com.groupon.jenkins.buildtype.InvalidBuildConfigurationException;

public class ComposeFileNameSection {
public static final String KEY = "docker-compose-file";
protected static final String INVALID_CI_YML_COMPOSE_FILE_NAME = "Invalid .ci.yml. Compose file name should be a string.";
private final String commposeFileName;

public ComposeFileNameSection(final Object composeFileNameConfig) {
if (composeFileNameConfig != null && !(composeFileNameConfig instanceof String)) {
throw new InvalidBuildConfigurationException(INVALID_CI_YML_COMPOSE_FILE_NAME);
}
this.commposeFileName = (String) composeFileNameConfig;
}

public String getComposeFileName() {
return this.commposeFileName != null ? this.commposeFileName : "docker-compose.yml";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
The MIT License (MIT)
Copyright (c) 2016, Groupon, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.groupon.jenkins.buildtype.dockercompose.buildconfiguration;

import com.groupon.jenkins.buildtype.InvalidBuildConfigurationException;

import java.util.ArrayList;
import java.util.List;

public class GenericCommandsSection {
public static final String INVALID_CI_YML_COMMANDS_SHOULD_BE_STRING_OR_LIST = "Invalid .ci.yml. Commands should be a list of string or a single string.";
private final Object commandsConfig;

public GenericCommandsSection(final Object commandSectionConfig) {

if (commandSectionConfig != null && !(commandSectionConfig instanceof String) && !(commandSectionConfig instanceof List)) {
throw new InvalidBuildConfigurationException(INVALID_CI_YML_COMMANDS_SHOULD_BE_STRING_OR_LIST);
}

this.commandsConfig = commandSectionConfig;

}

public List<String> getCommands() {
if (this.commandsConfig == null) return new ArrayList<>();

final List<String> commands = new ArrayList<>();
if (this.commandsConfig instanceof String) {
commands.add((String) this.commandsConfig);
} else if (this.commandsConfig instanceof List) {
final List l = (List) this.commandsConfig;

for (final Object v : l) {
if (!(v instanceof String)) {
throw new RuntimeException(String.format("Unexpected type: %s. Expected String .", v.getClass().getName()));
}
commands.add((String) v);
}
}
return commands;
}
}
Loading

0 comments on commit 598c3fc

Please sign in to comment.