Skip to content

fix: Add a fake arn to Context #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cmd/aws-lambda-rie/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox) {

invokeStart := time.Now()
invokePayload := &interop.Invoke{
ID: uuid.New().String(),
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
Payload: bodyBytes,
CorrelationID: "invokeCorrelationID",
ID: uuid.New().String(),
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "test_function")),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: show we move the defaults such as test_function, default memory of 3008 etc to a constants.go ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that could be an issue or not, but are we sure this account number doesn't exist?
Do we have example/dummy account numbers for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgrandis This is typically the account used as "fake" from what I have seen. I have no idea if it is a real account but we need something that looks like an account id. Do you have a better suggestion?

@sriram-mv Given this is likely not to change much at all. I am going to leave as is for now. Will keep an eye on this and refactor if this comes up again.

TraceID: r.Header.Get("X-Amzn-Trace-Id"),
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
Payload: bodyBytes,
CorrelationID: "invokeCorrelationID",
}
fmt.Println("START RequestId: " + invokePayload.ID + " Version: " + functionVersion)

Expand Down Expand Up @@ -176,7 +177,6 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.T
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_MEMORY_SIZE"] = "3008"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = "test_function"


// Forward Env Vars from the running system (container) to what the function can view. Without this, Env Vars will
// not be viewable when the function runs.
for _, env := range os.Environ() {
Expand All @@ -185,8 +185,6 @@ func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.T
additionalFunctionEnvironmentVariables[envVar[0]] = envVar[1]
}



initStart := time.Now()
// pass to rapid
sandbox.Init(&interop.Init{
Expand Down
36 changes: 29 additions & 7 deletions test/integration/local_lambda/end-to-end-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests

SLEEP_TIME = 2
DEFUALT_1P_ENTRYPOINT = "/lambda-entrypoint.sh"
DEFAULT_1P_ENTRYPOINT = "/lambda-entrypoint.sh"

class TestEndToEnd(TestCase):

Expand All @@ -36,7 +36,7 @@ def tearDownClass(cls):


def test_env_var_with_eqaul_sign(self):
cmd = f"docker run --name envvarcheck -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9003:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.check_env_var_handler"
cmd = f"docker run --name envvarcheck -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9003:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.check_env_var_handler"

Popen(cmd.split(' ')).communicate()

Expand All @@ -47,7 +47,7 @@ def test_env_var_with_eqaul_sign(self):
self.assertEqual(b'"4=4"', r.content)

def test_two_invokes(self):
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.success_handler"
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler"

Popen(cmd.split(' ')).communicate()

Expand All @@ -61,9 +61,31 @@ def test_two_invokes(self):
r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
self.assertEqual(b'"My lambda ran succesfully"', r.content)

def test_lambda_function_arn_exists(self):
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context"

Popen(cmd.split(' ')).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
self.assertEqual(b'"My lambda ran succesfully"', r.content)

def test_lambda_function_arn_exists_with_defining_custom_name(self):
cmd = f"docker run --name testing --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_lambda_arn_in_context"

Popen(cmd.split(' ')).communicate()

# sleep 1s to give enough time for the endpoint to be up to curl
time.sleep(SLEEP_TIME)

r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
self.assertEqual(b'"My lambda ran succesfully"', r.content)


def test_timeout_invoke(self):
cmd = f"docker run --name timeout -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p 9001:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.sleep_handler"
cmd = f"docker run --name timeout -d --env AWS_LAMBDA_FUNCTION_TIMEOUT=1 -v {self.path_to_binary}:/local-lambda-runtime-server -p 9001:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.sleep_handler"

Popen(cmd.split(' ')).communicate()

Expand All @@ -74,7 +96,7 @@ def test_timeout_invoke(self):
self.assertEqual(b"Task timed out after 1.00 seconds", r.content)

def test_exception_returned(self):
cmd = f"docker run --name exception -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9002:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.exception_handler"
cmd = f"docker run --name exception -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9002:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.exception_handler"

Popen(cmd.split(' ')).communicate()

Expand Down Expand Up @@ -108,7 +130,7 @@ def tearDownClass(cls):
Popen(f"docker rmi {cls.image_name}".split(' ')).communicate()

def test_invoke_with_pre_runtime_api_runtime(self):
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.success_handler"
cmd = f"docker run --name testing -d -v {self.path_to_binary}:/local-lambda-runtime-server -p 9000:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.success_handler"

Popen(cmd.split(' ')).communicate()

Expand All @@ -119,7 +141,7 @@ def test_invoke_with_pre_runtime_api_runtime(self):
self.assertEqual(b'"My lambda ran succesfully"', r.content)

def test_function_name_is_overriden(self):
cmd = f"docker run --name assert-overwritten -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p 9009:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFUALT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten"
cmd = f"docker run --name assert-overwritten -d --env AWS_LAMBDA_FUNCTION_NAME=MyCoolName -v {self.path_to_binary}:/local-lambda-runtime-server -p 9009:8080 --entrypoint /local-lambda-runtime-server/aws-lambda-rie {self.image_name} {DEFAULT_1P_ENTRYPOINT} main.assert_env_var_is_overwritten"

Popen(cmd.split(' ')).communicate()

Expand Down
6 changes: 6 additions & 0 deletions test/integration/testdata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ def assert_env_var_is_overwritten(event, context):
raise("Function name was not overwritten")
else:
return "My lambda ran succesfully"

def assert_lambda_arn_in_context(event, context):
if context.invoked_function_arn == f"arn:aws:lambda:us-east-1:012345678912:function:{os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'test_function')}":
return "My lambda ran succesfully"
else:
raise("Function Arn was not there")