Skip to content

Commit

Permalink
Adding unit tests for fetch plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
manojlds committed Mar 1, 2015
1 parent 3899883 commit d57702a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions fetch/src/main/java/com/indix/gocd/s3fetch/FetchExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
113 changes: 113 additions & 0 deletions fetch/src/test/java/com/indix/gocd/s3fetch/FetchExecutorTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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.<String, String>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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> environmentMap) {
return new MockTaskExecutionContext(environmentMap);
}

private S3ArtifactStore mockStore() { return mock(S3ArtifactStore.class); }

private AmazonS3Client mockClient() { return mock(AmazonS3Client.class); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -61,6 +62,6 @@ public void printEnvironment(Map<String, String> stringStringMap, SecureEnvVarSp

@Override
public String workingDir() {
return null;
return ".";
}
}

0 comments on commit d57702a

Please sign in to comment.