Skip to content

Commit f4bf61a

Browse files
committed
export
0 parents  commit f4bf61a

15 files changed

+1298
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Build and publish package
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
tag-prefix:
7+
required: true
8+
type: string
9+
version-bump:
10+
type: string
11+
required: true
12+
default: 'minor'
13+
secrets:
14+
SONATYPE_TOKEN:
15+
required: true
16+
GPG_SECRET_KEY:
17+
required: true
18+
GPG_SECRET_KEY_ID:
19+
required: true
20+
21+
permissions:
22+
id-token: write # This is required for requesting the JWT
23+
contents: write # This is required for actions/checkout
24+
packages: write
25+
pull-requests: write
26+
27+
env:
28+
TAG_PREFIX: '${{ inputs.tag-prefix }}-'
29+
30+
jobs:
31+
buildAndPublishPackage:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v4
35+
with:
36+
fetch-depth: 0
37+
- uses: coursier/cache-action@v6
38+
- uses: VirtusLab/scala-cli-setup@main
39+
with:
40+
jvm: adoptium:1.21
41+
- name: Setup gpg
42+
id: gpg
43+
run: |
44+
echo "${{ secrets.GPG_SECRET_KEY }}" | base64 -d | gpg --import
45+
gpg -K
46+
- id: version
47+
name: Compute new version
48+
run: ./scripts/computeNewVersion.sc --prefix='${{ env.TAG_PREFIX }}' --bump=${{ inputs.version-bump }} >> "$GITHUB_OUTPUT"
49+
- name: Create release bundle
50+
run: './scripts/createReleaseBundle.sc --version=${{ steps.version.outputs.new_version }} --gpg-key=${{ secrets.GPG_SECRET_KEY_ID }}'
51+
- name: Upload bundle to Sonatype
52+
run: |
53+
curl --request POST \
54+
--verbose \
55+
--header 'Authorization: Bearer ${{ secrets.SONATYPE_TOKEN }}' \
56+
57+
https://central.sonatype.com/api/v1/publisher/upload?publishingType=AUTOMATIC&name=${{ env.TAG_PREFIX }}-${{ steps.version.outputs.new_version }}
58+
- name: Push tag
59+
id: tag_version
60+
uses: mathieudutour/[email protected]
61+
with:
62+
github_token: ${{ secrets.GITHUB_TOKEN }}
63+
custom_tag: ${{ steps.version.outputs.new_version }}
64+
tag_prefix: ${{ env.TAG_PREFIX }}
65+
- name: Create GitHub Release
66+
if: ${{ inputs.version-bump != 'keep' }}
67+
uses: actions/create-release@v1
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
with:
71+
tag_name: ${{ env.TAG_PREFIX }}${{ steps.version.outputs.new_version }}
72+
release_name: ${{ inputs.tag-prefix }} release ${{ steps.version.outputs.new_version }}
73+
draft: false
74+
prerelease: false
75+
- name: Move version tag
76+
if: ${{ inputs.version-bump == 'keep' }}
77+
uses: richardsimko/update-tag@v1
78+
with:
79+
tag_name: ${{ env.TAG_PREFIX }}${{ steps.version.outputs.new_version }}
80+
env:
81+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
82+
83+

.github/workflows/release.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Release new package version
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version-bump:
7+
type: choice
8+
description: 'How to bump a version?'
9+
required: true
10+
default: 'patch'
11+
options:
12+
- major
13+
- minor
14+
- patch
15+
- keep
16+
# push:
17+
# branches: [main]
18+
# paths:
19+
# - '*.scala'
20+
21+
jobs:
22+
ReleasePackage:
23+
uses: ./.github/workflows/buildAndPublishPackage.yaml
24+
secrets: inherit
25+
with:
26+
tag-prefix: 'version'
27+
version-bump: ${{ inputs.version-bump || 'patch' }}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.bsp/
2+
.scala-build/
3+
.metals/
4+
.vscode/

.scalafmt.conf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version = "3.7.15"
2+
runner.dialect = scala3
3+
maxColumn = 120

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2024 encalmo.org
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the " Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

LambdaServiceFixture.scala

+242
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package org.encalmo.lambda
2+
3+
import com.sun.net.httpserver.{HttpContext, HttpExchange, HttpHandler, HttpServer}
4+
import munit.Assertions
5+
import upickle.default.*
6+
7+
import java.net.InetSocketAddress
8+
import java.util.UUID
9+
import java.util.concurrent.LinkedBlockingQueue
10+
import scala.concurrent.ExecutionContext.Implicits.given
11+
import scala.concurrent.{Future, Promise}
12+
import scala.io.Source
13+
import scala.jdk.CollectionConverters.*
14+
import scala.reflect.ClassTag
15+
import scala.util.{Failure, Success, Try}
16+
17+
case class LambdaError(
18+
success: Boolean,
19+
errorMessage: String,
20+
error: String
21+
) derives ReadWriter
22+
23+
class LambdaServiceFixture extends munit.Fixture[LambdaService]("lambdaService") {
24+
25+
private var server: HttpServer = null
26+
private var lambdaService: LambdaService = null
27+
28+
override def apply(): LambdaService =
29+
if (lambdaService != null)
30+
then lambdaService
31+
else throw new Exception("Test http server not initialized!")
32+
33+
final override def beforeAll(): Unit = {
34+
this.server = HttpServer.create(new InetSocketAddress(0), 0)
35+
val address = s"localhost:${server.getAddress().getPort()}"
36+
server.start()
37+
lambdaService = new LambdaService(server)
38+
println(
39+
s"Started test http server at port ${server.getAddress().getPort()}"
40+
)
41+
System.setProperty("AWS_LAMBDA_RUNTIME_API", address)
42+
System.setProperty("AWS_LAMBDA_FUNCTION_NAME", "TestFunction")
43+
System.setProperty("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST")
44+
System.setProperty("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128")
45+
System.setProperty(
46+
"AWS_LAMBDA_LOG_GROUP_NAME",
47+
"/aws/lambda/TestFunction"
48+
)
49+
System.setProperty(
50+
"AWS_LAMBDA_LOG_STREAM_NAME",
51+
"/aws/lambda/TestFunction/$LATEST"
52+
)
53+
}
54+
55+
final def close(): Unit = {
56+
server.stop(0)
57+
println(
58+
s"Shutdown test http server at port ${server.getAddress().getPort()}"
59+
)
60+
}
61+
62+
}
63+
64+
class LambdaService(server: HttpServer) extends Assertions {
65+
66+
private val events =
67+
new LinkedBlockingQueue[(String, String)]
68+
69+
server.createContext(
70+
"/2018-06-01/runtime/invocation/next",
71+
(exchange: HttpExchange) => {
72+
val (requestId, input) = events.take()
73+
74+
val responseHeaders = exchange.getResponseHeaders()
75+
responseHeaders.put(
76+
"Lambda-Runtime-Aws-Request-Id",
77+
Seq(requestId).asJava
78+
)
79+
responseHeaders.put(
80+
"Lambda-Runtime-Deadline-Ms",
81+
Seq("30000").asJava
82+
)
83+
responseHeaders.put(
84+
"Lambda-Runtime-Invoked-Function-Arn",
85+
Seq(
86+
"arn:aws:lambda:us-east-1:00000000:function:TestFunction"
87+
).asJava
88+
)
89+
responseHeaders.put(
90+
"Lambda-Runtime-Trace-Id",
91+
Seq(
92+
"Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419;Sampled=1"
93+
).asJava
94+
)
95+
exchange.sendResponseHeaders(200, input.length())
96+
val os = exchange.getResponseBody()
97+
os.write(input.getBytes())
98+
os.close()
99+
}
100+
)
101+
102+
server.createContext(
103+
"/2018-06-01/runtime/init/error",
104+
(exchange: HttpExchange) => {
105+
println(s"[LambdaService] Initialization error: ${Source
106+
.fromInputStream(exchange.getRequestBody())
107+
.mkString}")
108+
exchange.sendResponseHeaders(202, -1)
109+
}
110+
)
111+
112+
def mockAndAssertLambdaInvocation(
113+
input: String,
114+
expectedOutput: String
115+
): Future[String] = {
116+
// println(s"[LambdaService] Expecting: $input => $expectedOutput")
117+
val requestId = UUID.randomUUID().toString()
118+
events.offer((requestId, input))
119+
mockRuntimeInvocationResponse(requestId)
120+
.andThen {
121+
case Success(result) =>
122+
assertEquals(result, expectedOutput)
123+
println(
124+
// s"[LambdaService] Success confirmed: $result == $expectedOutput"
125+
)
126+
case Failure(exception) =>
127+
fail(
128+
"[LambdaService] Invocation didn't return any result",
129+
exception
130+
)
131+
}
132+
}
133+
134+
def mockAndAssertLambdaInvocationError[I: ReadWriter](
135+
input: I,
136+
expectedError: LambdaError
137+
): Future[LambdaError] = {
138+
println(s"[LambdaService] Expecting failure: $input => $expectedError")
139+
val requestId = UUID.randomUUID().toString()
140+
val event = ujson.write(writeJs(input))
141+
events.offer((requestId, event))
142+
mockRuntimeInvocationError(requestId)
143+
.andThen {
144+
case Success(error) =>
145+
assertEquals(error.errorMessage, expectedError.errorMessage)
146+
assertEquals(error.error, expectedError.error)
147+
println(
148+
s"[LambdaService] Error confirmed: ${error.errorMessage} == ${expectedError.errorMessage}"
149+
)
150+
case Failure(exception) =>
151+
fail(
152+
"[LambdaService] Invocation didn't return any error",
153+
exception
154+
)
155+
}
156+
}
157+
158+
def mockRuntimeInvocationResponse(
159+
requestId: String
160+
): Future[String] = {
161+
var httpContext: HttpContext = null
162+
var errorHttpContext: HttpContext = null
163+
val promise = Promise[String]
164+
165+
httpContext = server.createContext(
166+
s"/2018-06-01/runtime/invocation/$requestId/response",
167+
(exchange: HttpExchange) => {
168+
promise.complete(Try {
169+
val body = Source
170+
.fromInputStream(exchange.getRequestBody())
171+
.mkString
172+
exchange
173+
.sendResponseHeaders(202, -1)
174+
server.removeContext(httpContext)
175+
server.removeContext(errorHttpContext)
176+
body
177+
})
178+
}
179+
)
180+
errorHttpContext = server.createContext(
181+
s"/2018-06-01/runtime/invocation/$requestId/error",
182+
(exchange: HttpExchange) => {
183+
val body = Source
184+
.fromInputStream(exchange.getRequestBody())
185+
.mkString
186+
exchange.sendResponseHeaders(202, -1)
187+
server.removeContext(httpContext)
188+
server.removeContext(errorHttpContext)
189+
promise.complete(
190+
Failure(
191+
new Exception(
192+
s"Expected success but got an error:\n$body"
193+
)
194+
)
195+
)
196+
}
197+
)
198+
promise.future
199+
}
200+
201+
def mockRuntimeInvocationError(
202+
requestId: String
203+
): Future[LambdaError] = {
204+
var httpContext: HttpContext = null
205+
var errorHttpContext: HttpContext = null
206+
val promise = Promise[LambdaError]
207+
httpContext = server.createContext(
208+
s"/2018-06-01/runtime/invocation/$requestId/response",
209+
(exchange: HttpExchange) => {
210+
val body = Source
211+
.fromInputStream(exchange.getRequestBody())
212+
.mkString
213+
exchange.sendResponseHeaders(202, -1)
214+
server.removeContext(httpContext)
215+
server.removeContext(errorHttpContext)
216+
promise.complete(
217+
Failure(
218+
new Exception(
219+
s"Expected an error but got successful result:\n$body"
220+
)
221+
)
222+
)
223+
}
224+
)
225+
226+
errorHttpContext = server.createContext(
227+
s"/2018-06-01/runtime/invocation/$requestId/error",
228+
(exchange: HttpExchange) => {
229+
promise.complete(Try {
230+
val body = Source
231+
.fromInputStream(exchange.getRequestBody())
232+
.mkString
233+
exchange.sendResponseHeaders(202, -1)
234+
server.removeContext(httpContext)
235+
server.removeContext(errorHttpContext)
236+
upickle.default.read[LambdaError](body)
237+
})
238+
}
239+
)
240+
promise.future
241+
}
242+
}

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# scala-aws-lambda-testkit
2+
3+
Artefacts supporting testing of the AWS lambda functions written using Scala 3 and [scala-aws-lambda-runtime](https://github.com/encalmo/scala-aws-lambda-runtime)
4+
5+
## Dependencies
6+
7+
- Scala >= 3.6.3
8+
- [munit](https://scalameta.org/munit/)
9+
- [upickle-utils](https://github.com/encalmo/upickle-utils)
10+
11+
## Usage
12+
13+
Use with SBT
14+
15+
libraryDependencies += "org.encalmo" %% "scala-aws-lambda-testkit" % "0.9.0"
16+
17+
or with SCALA-CLI
18+
19+
//> using dep org.encalmo::scala-aws-lambda-testkit:0.9.0

0 commit comments

Comments
 (0)