Skip to content

Commit

Permalink
JENKINS-28441
Browse files Browse the repository at this point in the history
Tested LambdaInvoker and LambdaUploader
  • Loading branch information
cast committed May 25, 2015
1 parent 76072b6 commit b30a779
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 6 deletions.
@@ -0,0 +1,7 @@
package com.xti.jenkins.plugin.awslambda.exception;

public class AWSLambdaPluginException extends Exception {
public AWSLambdaPluginException(String message) {
super(message);
}
}
@@ -1,6 +1,6 @@
package com.xti.jenkins.plugin.awslambda.exception;

public class LambdaInvokeException extends RuntimeException {
public class LambdaInvokeException extends AWSLambdaPluginException {
public LambdaInvokeException(String message) {
super(message);
}
Expand Down
Expand Up @@ -26,11 +26,13 @@
* #L%
*/

import com.xti.jenkins.plugin.awslambda.exception.LambdaInvokeException;
import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.JsonPathParser;
import com.xti.jenkins.plugin.awslambda.service.LambdaInvokeService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class LambdaInvoker {
Expand All @@ -46,9 +48,16 @@ public LambdaInvoker(LambdaInvokeService lambda, JenkinsLogger logger) {
public LambdaInvocationResult invoke(InvokeConfig config) throws IOException, InterruptedException {
JsonPathParser jsonPathParser = new JsonPathParser(config.getJsonParameters(), logger);
logger.log("%nStarting lambda invocation.");
String output = lambda.invokeLambdaFunction(config);
Map<String, String> injectables = jsonPathParser.parse(output);
String output = null;
try {
output = lambda.invokeLambdaFunction(config);
Map<String, String> injectables = jsonPathParser.parse(output);

return new LambdaInvocationResult(true, injectables);
} catch (LambdaInvokeException e) {
logger.log(e.getMessage());
return new LambdaInvocationResult(false, new HashMap<String, String>());
}

return new LambdaInvocationResult(true, injectables);
}
}
Expand Up @@ -27,7 +27,7 @@ public LambdaInvokeService(AWSLambdaClient client, JenkinsLogger logger) {
* @param invokeConfig AWS Lambda invocation configuration
* @return response payload
*/
public String invokeLambdaFunction(InvokeConfig invokeConfig){
public String invokeLambdaFunction(InvokeConfig invokeConfig) throws LambdaInvokeException {
InvokeRequest invokeRequest = new InvokeRequest()
.withFunctionName(invokeConfig.getFunctionName())
.withPayload(invokeConfig.getPayload());
Expand Down
Expand Up @@ -45,7 +45,8 @@ public LambdaUploader(LambdaDeployService lambda, WorkSpaceZipper zipper, Jenkin
}

public Boolean upload(DeployConfig config) throws IOException, InterruptedException {
logger.log("%nStarting lambda upload procedure");
logger.log("%nStarting lambda deployment procedure");

File zipFile = zipper.getZip(config.getArtifactLocation());
return lambda.deployLambda(config, zipFile, UpdateModeValue.fromString(config.getUpdateMode()));
}
Expand Down
@@ -0,0 +1,58 @@
package com.xti.jenkins.plugin.awslambda.invoke;

import com.xti.jenkins.plugin.awslambda.exception.LambdaInvokeException;
import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.LambdaInvokeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class LambdaInvokerTest {

@Mock
private LambdaInvokeService service;

@Mock
private JenkinsLogger logger;

@Test
public void testInvokeSuccess() throws Exception {
when(service.invokeLambdaFunction(any(InvokeConfig.class))).thenReturn("{\"key2\":\"value2\"}");

LambdaInvoker invoker = new LambdaInvoker(service, logger);

InvokeConfig invokeConfig = new InvokeConfig("function", "{\"key\":\"value\"}", true, Collections.singletonList(new JsonParameter("LAMBDA_ENV_VAR", "$.key2")));

LambdaInvocationResult result = invoker.invoke(invokeConfig);

assertTrue(result.isSuccess());
assertEquals("value2", result.getInjectables().get("LAMBDA_ENV_VAR"));
}

@Test
public void testInvokeFailure() throws Exception {
when(service.invokeLambdaFunction(any(InvokeConfig.class))).thenThrow(new LambdaInvokeException("Function returned error of type: Handled"));

LambdaInvoker invoker = new LambdaInvoker(service, logger);

InvokeConfig invokeConfig = new InvokeConfig("function", "{\"key\":\"value\"}", true, new ArrayList<JsonParameter>());

LambdaInvocationResult result = invoker.invoke(invokeConfig);

assertFalse(result.isSuccess());
assertEquals(0, result.getInjectables().size());

verify(logger, times(1)).log("%nStarting lambda invocation.");
}
}
@@ -0,0 +1,80 @@
package com.xti.jenkins.plugin.awslambda.upload;

import com.xti.jenkins.plugin.awslambda.service.JenkinsLogger;
import com.xti.jenkins.plugin.awslambda.service.LambdaDeployService;
import com.xti.jenkins.plugin.awslambda.service.WorkSpaceZipper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.File;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class LambdaUploaderTest {

@Mock
private LambdaDeployService service;

@Mock
private WorkSpaceZipper zipper;

@Mock
private JenkinsLogger logger;


@Test
public void testUploadSuccess() throws Exception {
DeployConfig deployConfig = new DeployConfig("location", "description", "function", "handler", 1024, "role", "runtime", 30, "full");
File file = new File("path");

when(zipper.getZip(any(String.class))).thenReturn(file);
when(service.deployLambda(any(DeployConfig.class), any(File.class), any(UpdateModeValue.class)))
.thenReturn(true);

LambdaUploader uploader = new LambdaUploader(service, zipper, logger);
Boolean uploaded = uploader.upload(deployConfig);

verify(logger, times(1)).log("%nStarting lambda deployment procedure");
verify(service, times(1)).deployLambda(deployConfig, file, UpdateModeValue.Full);
assertTrue(uploaded);
}

@Test
public void testUploadFailure() throws Exception {
DeployConfig deployConfig = new DeployConfig("location", "description", "function", "handler", 1024, "role", "runtime", 30, "full");
File file = new File("path");

when(zipper.getZip(any(String.class))).thenReturn(file);
when(service.deployLambda(any(DeployConfig.class), any(File.class), any(UpdateModeValue.class)))
.thenReturn(false);

LambdaUploader uploader = new LambdaUploader(service, zipper, logger);
Boolean uploaded = uploader.upload(deployConfig);

verify(logger, times(1)).log("%nStarting lambda deployment procedure");
verify(service, times(1)).deployLambda(deployConfig, file, UpdateModeValue.Full);
assertFalse(uploaded);
}

@Test
public void testUploadNoArtifact() throws Exception {
DeployConfig deployConfig = new DeployConfig("location", "description", "function", "handler", 1024, "role", "runtime", 30, "full");

when(zipper.getZip(any(String.class))).thenReturn(null);
when(service.deployLambda(any(DeployConfig.class), any(File.class), any(UpdateModeValue.class)))
.thenReturn(true);

LambdaUploader uploader = new LambdaUploader(service, zipper, logger);
Boolean uploaded = uploader.upload(deployConfig);

verify(logger, times(1)).log("%nStarting lambda deployment procedure");
verify(service, times(1)).deployLambda(deployConfig, null, UpdateModeValue.Full);
assertTrue(uploaded);
}
}

0 comments on commit b30a779

Please sign in to comment.