-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validating fetch config before trying to fetch artifacts
- Loading branch information
Showing
4 changed files
with
218 additions
and
57 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
fetch/src/main/java/com/indix/gocd/s3fetch/FetchConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.indix.gocd.s3fetch; | ||
|
||
import com.amazonaws.util.StringUtils; | ||
import com.indix.gocd.utils.GoEnvironment; | ||
import com.thoughtworks.go.plugin.api.response.validation.ValidationError; | ||
import com.thoughtworks.go.plugin.api.response.validation.ValidationResult; | ||
import com.thoughtworks.go.plugin.api.task.TaskConfig; | ||
import com.thoughtworks.go.plugin.api.task.TaskExecutionContext; | ||
|
||
import static com.indix.gocd.utils.Constants.*; | ||
|
||
public class FetchConfig { | ||
private final String materialLabel; | ||
private final String pipeline; | ||
private final String stage; | ||
private final String job; | ||
private GoEnvironment env; | ||
|
||
public FetchConfig(TaskConfig config, TaskExecutionContext context) { | ||
this.env = new GoEnvironment(); | ||
env.putAll(context.environment().asMap()); | ||
|
||
String repoName = config.getValue(FetchTask.REPO).toUpperCase(); | ||
String packageName = config.getValue(FetchTask.PACKAGE).toUpperCase(); | ||
this.materialLabel = env.get(String.format("GO_PACKAGE_%s_%s_LABEL", repoName, packageName)); | ||
this.pipeline = env.get(String.format("GO_PACKAGE_%s_%s_PIPELINE_NAME", repoName, packageName)); | ||
this.stage = env.get(String.format("GO_PACKAGE_%s_%s_STAGE_NAME", repoName, packageName)); | ||
this.job = env.get(String.format("GO_PACKAGE_%s_%s_JOB_NAME", repoName, packageName)); | ||
} | ||
|
||
public ValidationResult validate() { | ||
ValidationResult validationResult = new ValidationResult(); | ||
if (env.isAbsent(AWS_ACCESS_KEY_ID)) validationResult.addError(envNotFound(AWS_ACCESS_KEY_ID)); | ||
if (env.isAbsent(AWS_SECRET_ACCESS_KEY)) validationResult.addError(envNotFound(AWS_SECRET_ACCESS_KEY)); | ||
if (env.isAbsent(GO_ARTIFACTS_S3_BUCKET)) validationResult.addError(envNotFound(GO_ARTIFACTS_S3_BUCKET)); | ||
if (StringUtils.isNullOrEmpty(materialLabel)) | ||
validationResult.addError(new ValidationError("Please check Repository name or Package name configuration. Also ensure that the appropriate S3 material is configured for the pipeline.")); | ||
|
||
return validationResult; | ||
} | ||
|
||
public String getArtifactsLocationTemplate() { | ||
String[] counters = materialLabel.split("\\."); | ||
String pipelineCounter = counters[0]; | ||
String stageCounter = counters[1]; | ||
return env.artifactsLocationTemplate(pipeline, stage, job, pipelineCounter, stageCounter); | ||
} | ||
|
||
public String getAWSAccessKeyId() { | ||
return env.get(AWS_ACCESS_KEY_ID); | ||
} | ||
|
||
public String getAWSSecretAccessKey() { | ||
return env.get(AWS_SECRET_ACCESS_KEY); | ||
} | ||
|
||
public String getS3Bucket() { | ||
return env.get(GO_ARTIFACTS_S3_BUCKET); | ||
} | ||
|
||
private ValidationError envNotFound(String environmentVariable) { | ||
return new ValidationError(environmentVariable, String.format("%s environment variable not present", environmentVariable)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
fetch/src/test/java/com/indix/gocd/s3fetch/FetchConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.indix.gocd.s3fetch; | ||
|
||
import com.indix.gocd.utils.mocks.MockTaskExecutionContext; | ||
import com.indix.gocd.utils.utils.Maps; | ||
import com.thoughtworks.go.plugin.api.response.validation.ValidationResult; | ||
import com.thoughtworks.go.plugin.api.task.TaskConfig; | ||
import com.thoughtworks.go.plugin.api.task.TaskExecutionContext; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.indix.gocd.utils.Constants.*; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FetchConfigTest { | ||
private final String bucket = "gocd"; | ||
Maps.MapBuilder<String, String> mockEnvironmentVariables; | ||
private TaskConfig config; | ||
private FetchConfig fetchConfig; | ||
private final String secretKey = "secretKey"; | ||
private final String accessId = "accessId"; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
config = mock(TaskConfig.class); | ||
when(config.getValue(FetchTask.REPO)).thenReturn(bucket); | ||
when(config.getValue(FetchTask.PACKAGE)).thenReturn("TestPublishS3Artifacts"); | ||
mockEnvironmentVariables = Maps.<String, String>builder() | ||
.with(AWS_SECRET_ACCESS_KEY, secretKey) | ||
.with(AWS_ACCESS_KEY_ID, accessId) | ||
.with(GO_ARTIFACTS_S3_BUCKET, bucket) | ||
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_LABEL", "20.1") | ||
.with("GO_REPO_GOCD_TESTPUBLISHS3ARTIFACTS_S3_BUCKET", bucket) | ||
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_PIPELINE_NAME", "TestPublish") | ||
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_STAGE_NAME", "defaultStage") | ||
.with("GO_PACKAGE_GOCD_TESTPUBLISHS3ARTIFACTS_JOB_NAME", "defaultJob"); | ||
} | ||
|
||
@Test | ||
public void shouldGetAWSSecretAccessKey() { | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
String awsSecretAccessKey = fetchConfig.getAWSSecretAccessKey(); | ||
assertThat(awsSecretAccessKey, is(secretKey)); | ||
} | ||
|
||
@Test | ||
public void shouldGetAWSAccessKeyId() { | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
String awsSecretAccessKey = fetchConfig.getAWSAccessKeyId(); | ||
assertThat(awsSecretAccessKey, is(accessId)); | ||
} | ||
|
||
@Test | ||
public void shouldS3Bucket() { | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
String awsSecretAccessKey = fetchConfig.getS3Bucket(); | ||
assertThat(awsSecretAccessKey, is(bucket)); | ||
} | ||
|
||
@Test | ||
public void shouldGetArtifactLocation() { | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
String location = fetchConfig.getArtifactsLocationTemplate(); | ||
assertThat(location, is("TestPublish/defaultStage/defaultJob/20.1")); | ||
} | ||
|
||
@Test | ||
public void shouldBeValid() { | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
ValidationResult validationResult = fetchConfig.validate(); | ||
assertTrue(validationResult.isSuccessful()); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeValidIfAWSSecretAccessKeyNotPresent() { | ||
fetchConfig = new FetchConfig(config, mockContext( mockEnvironmentVariables.remove(AWS_SECRET_ACCESS_KEY).build())); | ||
ValidationResult validationResult = fetchConfig.validate(); | ||
assertFalse(validationResult.isSuccessful()); | ||
ArrayList<String> messages = new ArrayList<String>(); | ||
messages.add("AWS_SECRET_ACCESS_KEY environment variable not present"); | ||
assertThat(validationResult.getMessages(), Matchers.<List<String>>is(messages)); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeValidIfAWSAccessKeyIdNotPresent() { | ||
fetchConfig = new FetchConfig(config, mockContext( mockEnvironmentVariables.remove(AWS_ACCESS_KEY_ID).build())); | ||
ValidationResult validationResult = fetchConfig.validate(); | ||
assertFalse(validationResult.isSuccessful()); | ||
ArrayList<String> messages = new ArrayList<String>(); | ||
messages.add("AWS_ACCESS_KEY_ID environment variable not present"); | ||
assertThat(validationResult.getMessages(), Matchers.<List<String>>is(messages)); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeValidIfS3BucketNotPresent() { | ||
fetchConfig = new FetchConfig(config, mockContext( mockEnvironmentVariables.remove(GO_ARTIFACTS_S3_BUCKET).build())); | ||
ValidationResult validationResult = fetchConfig.validate(); | ||
assertFalse(validationResult.isSuccessful()); | ||
ArrayList<String> messages = new ArrayList<String>(); | ||
messages.add("GO_ARTIFACTS_S3_BUCKET environment variable not present"); | ||
assertThat(validationResult.getMessages(), Matchers.<List<String>>is(messages)); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeValidIfRepoConfigIsNotValid() { | ||
when(config.getValue(FetchTask.REPO)).thenReturn("Wrong"); | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
ValidationResult validationResult = fetchConfig.validate(); | ||
assertFalse(validationResult.isSuccessful()); | ||
ArrayList<String> messages = new ArrayList<String>(); | ||
messages.add("Please check Repository name or Package name configuration. Also ensure that the appropriate S3 material is configured for the pipeline."); | ||
assertThat(validationResult.getMessages(), Matchers.<List<String>>is(messages)); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeValidIfPackageConfigIsNotValid() { | ||
when(config.getValue(FetchTask.PACKAGE)).thenReturn("Wrong"); | ||
fetchConfig = new FetchConfig(config, mockContext(mockEnvironmentVariables.build())); | ||
ValidationResult validationResult = fetchConfig.validate(); | ||
assertFalse(validationResult.isSuccessful()); | ||
ArrayList<String> messages = new ArrayList<String>(); | ||
messages.add("Please check Repository name or Package name configuration. Also ensure that the appropriate S3 material is configured for the pipeline."); | ||
assertThat(validationResult.getMessages(), Matchers.<List<String>>is(messages)); | ||
} | ||
|
||
private TaskExecutionContext mockContext(final Map<String, String> environmentMap) { | ||
return new MockTaskExecutionContext(environmentMap); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters