diff --git a/build.sbt b/build.sbt index 533d355..b0c14ee 100644 --- a/build.sbt +++ b/build.sbt @@ -42,7 +42,7 @@ lazy val utils = (project in file("utils")). ) lazy val publish = (project in file("publish")). - dependsOn(utils). + dependsOn(utils % "test->test;compile->compile"). settings(commonSettings: _*). settings( name := "s3publish", @@ -68,7 +68,7 @@ lazy val material = (project in file("material")). ) lazy val fetch = (project in file("fetch")). - dependsOn(utils). + dependsOn(utils % "test->test;compile->compile"). settings(commonSettings: _*). settings( name := "s3fetch", diff --git a/fetch/src/main/java/com/indix/gocd/s3fetch/FetchExecutor.java b/fetch/src/main/java/com/indix/gocd/s3fetch/FetchExecutor.java index 57b87f8..4057c28 100644 --- a/fetch/src/main/java/com/indix/gocd/s3fetch/FetchExecutor.java +++ b/fetch/src/main/java/com/indix/gocd/s3fetch/FetchExecutor.java @@ -29,7 +29,7 @@ public ExecutionResult execute(TaskConfig config, final TaskExecutionContext con if (env.isAbsent(GO_ARTIFACTS_S3_BUCKET)) return envNotFound(GO_ARTIFACTS_S3_BUCKET); final String bucket = env.get(GO_ARTIFACTS_S3_BUCKET); - final S3ArtifactStore store = new S3ArtifactStore(s3Client(env), bucket); + final S3ArtifactStore store = s3ArtifactStore(env, bucket); String artifactPathOnS3 = getS3ArtifactPath(config, env); context.console().printLine(String.format("Getting artifacts from %s", store.pathString(artifactPathOnS3))); @@ -39,7 +39,7 @@ public ExecutionResult execute(TaskConfig config, final TaskExecutionContext con try { store.getPrefix(artifactPathOnS3, destination); } catch (Exception e) { - ExecutionResult.failure("Failure while downloading artifacts", e); + return ExecutionResult.failure("Failure while downloading artifacts", e); } return ExecutionResult.success("Fetched all artifacts"); @@ -72,6 +72,10 @@ private void setupDestinationDirectory(String destination) { } } + public S3ArtifactStore s3ArtifactStore(GoEnvironment env, String bucket) { + return new S3ArtifactStore(s3Client(env), bucket); + } + public AmazonS3Client s3Client(GoEnvironment env) { String accessKey = env.get(AWS_ACCESS_KEY_ID); String secretKey = env.get(AWS_SECRET_ACCESS_KEY); diff --git a/fetch/src/test/java/com/indix/gocd/s3fetch/FetchExecutorTest.java b/fetch/src/test/java/com/indix/gocd/s3fetch/FetchExecutorTest.java new file mode 100644 index 0000000..1c42926 --- /dev/null +++ b/fetch/src/test/java/com/indix/gocd/s3fetch/FetchExecutorTest.java @@ -0,0 +1,113 @@ +package com.indix.gocd.s3fetch; + +import com.amazonaws.AmazonClientException; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.ListObjectsRequest; +import com.indix.gocd.utils.GoEnvironment; +import com.indix.gocd.utils.mocks.MockTaskExecutionContext; +import com.indix.gocd.utils.store.S3ArtifactStore; +import com.indix.gocd.utils.utils.Maps; +import com.thoughtworks.go.plugin.api.response.execution.ExecutionResult; +import com.thoughtworks.go.plugin.api.task.TaskConfig; +import com.thoughtworks.go.plugin.api.task.TaskExecutionContext; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import static com.indix.gocd.utils.Constants.*; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + + +public class FetchExecutorTest { + private final String destination = "artifacts"; + private final String bucket = "gocd"; + Maps.MapBuilder mockEnvironmentVariables; + private FetchExecutor fetchExecutor; + private TaskConfig config; + + @Before + public void setUp() throws Exception { + config = mock(TaskConfig.class); + when(config.getValue(FetchTask.REPO)).thenReturn(bucket); + when(config.getValue(FetchTask.PACKAGE)).thenReturn("TestPublishS3Artifacts"); + when(config.getValue(FetchTask.DESTINATION)).thenReturn(destination); + mockEnvironmentVariables = Maps.builder() + .with(AWS_SECRET_ACCESS_KEY, "secretKey") + .with(AWS_ACCESS_KEY_ID, "accessId") + .with(GO_ARTIFACTS_S3_BUCKET, bucket) + .with(GO_SERVER_DASHBOARD_URL, "http://go.server:8153") + .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"); + + fetchExecutor = spy(new FetchExecutor()); + } + + @Test + public void shouldThrowIfAWS_ACCESS_KEY_IDNotPresent() { + Map mockVariables = mockEnvironmentVariables.remove(AWS_ACCESS_KEY_ID).build(); + + ExecutionResult executionResult = fetchExecutor.execute(config, mockContext(mockVariables)); + assertFalse(executionResult.isSuccessful()); + assertThat(executionResult.getMessagesForDisplay(), is("AWS_ACCESS_KEY_ID environment variable not present")); + } + + @Test + public void shouldThrowIfAWS_SECRET_ACCESS_KEYNotPresent() { + Map mockVariables = mockEnvironmentVariables.remove(AWS_SECRET_ACCESS_KEY).build(); + + ExecutionResult executionResult = fetchExecutor.execute(config, mockContext(mockVariables)); + assertFalse(executionResult.isSuccessful()); + assertThat(executionResult.getMessagesForDisplay(), is("AWS_SECRET_ACCESS_KEY environment variable not present")); + } + + @Test + public void shouldThrowIfGO_ARTIFACTS_S3_BUCKETNotPresent() { + Map mockVariables = mockEnvironmentVariables.remove(GO_ARTIFACTS_S3_BUCKET).build(); + + ExecutionResult executionResult = fetchExecutor.execute(config, mockContext(mockVariables)); + assertFalse(executionResult.isSuccessful()); + assertThat(executionResult.getMessagesForDisplay(), is("GO_ARTIFACTS_S3_BUCKET environment variable not present")); + } + + @Test + public void shouldBeFailureIfUnableToFetchArtifacts() { + Map mockVariables = mockEnvironmentVariables.build(); + AmazonS3Client mockClient = mockClient(); + doReturn(mockClient).when(fetchExecutor).s3Client(any(GoEnvironment.class)); + doThrow(new AmazonClientException("Exception")).when(mockClient).listObjects(any(ListObjectsRequest.class)); + + ExecutionResult executionResult = fetchExecutor.execute(config, mockContext(mockVariables)); + + assertFalse(executionResult.isSuccessful()); + assertThat(executionResult.getMessagesForDisplay(), is("Failure while downloading artifacts")); + } + + @Test + public void shouldBeSuccessResultOnSuccessfulFetch() { + Map mockVariables = mockEnvironmentVariables.build(); + S3ArtifactStore mockStore = mockStore(); + doReturn(mockStore).when(fetchExecutor).s3ArtifactStore(any(GoEnvironment.class), eq(bucket)); + + ExecutionResult executionResult = fetchExecutor.execute(config, mockContext(mockVariables)); + + assertTrue(executionResult.isSuccessful()); + assertThat(executionResult.getMessagesForDisplay(), is("Fetched all artifacts")); + verify(mockStore, times(1)).getPrefix("TestPublish/defaultStage/defaultJob/20.1", "./artifacts"); + + } + + private TaskExecutionContext mockContext(final Map environmentMap) { + return new MockTaskExecutionContext(environmentMap); + } + + private S3ArtifactStore mockStore() { return mock(S3ArtifactStore.class); } + + private AmazonS3Client mockClient() { return mock(AmazonS3Client.class); } +} \ No newline at end of file diff --git a/publish/src/test/java/com/indix/gocd/s3publish/PublishExecutorTest.java b/publish/src/test/java/com/indix/gocd/s3publish/PublishExecutorTest.java index 1185291..614a95c 100644 --- a/publish/src/test/java/com/indix/gocd/s3publish/PublishExecutorTest.java +++ b/publish/src/test/java/com/indix/gocd/s3publish/PublishExecutorTest.java @@ -2,8 +2,8 @@ import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.PutObjectRequest; -import com.indix.gocd.s3publish.mocks.MockTaskExecutionContext; import com.indix.gocd.utils.GoEnvironment; +import com.indix.gocd.utils.mocks.MockTaskExecutionContext; import com.indix.gocd.utils.utils.Maps; import com.thoughtworks.go.plugin.api.response.execution.ExecutionResult; import com.thoughtworks.go.plugin.api.task.TaskConfig; diff --git a/publish/src/test/java/com/indix/gocd/s3publish/mocks/MockTaskExecutionContext.java b/utils/src/test/java/com/indix/gocd/utils/mocks/MockTaskExecutionContext.java similarity index 94% rename from publish/src/test/java/com/indix/gocd/s3publish/mocks/MockTaskExecutionContext.java rename to utils/src/test/java/com/indix/gocd/utils/mocks/MockTaskExecutionContext.java index 18e99fe..8e24bdb 100644 --- a/publish/src/test/java/com/indix/gocd/s3publish/mocks/MockTaskExecutionContext.java +++ b/utils/src/test/java/com/indix/gocd/utils/mocks/MockTaskExecutionContext.java @@ -1,8 +1,9 @@ -package com.indix.gocd.s3publish.mocks; +package com.indix.gocd.utils.mocks; import com.thoughtworks.go.plugin.api.task.Console; import com.thoughtworks.go.plugin.api.task.EnvironmentVariables; import com.thoughtworks.go.plugin.api.task.TaskExecutionContext; +import org.apache.commons.lang3.StringUtils; import java.io.InputStream; import java.util.Map; @@ -61,6 +62,6 @@ public void printEnvironment(Map stringStringMap, SecureEnvVarSp @Override public String workingDir() { - return null; + return "."; } }