-
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.
- Loading branch information
Showing
5 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
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
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
113 changes: 113 additions & 0 deletions
113
fetch/src/test/java/com/indix/gocd/s3fetch/FetchExecutorTest.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,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); } | ||
} |
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
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