Skip to content

Commit

Permalink
Add tests for HTTP Lambda API
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea-Scuderi committed Dec 26, 2023
1 parent 291c739 commit bddcce7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Tests/SLSAdapterTests/Fixtures/serverless_webhook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ functions:
githubWebHook:
handler: github-webhook
memorySize: 256
description: '[${sls:stage}] get /github-webhook'
description: '[${sls:stage}] post /github-webhook'
package:
artifact: build/GitHubWebHook/GitHubWebHook.zip
environment:
Expand Down
61 changes: 61 additions & 0 deletions Tests/SLSAdapterTests/HttpAPILambdaParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-sprinter

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Foundation
import SLSAdapter
import Yams

public struct HttpAPILambdaParams {
public let name: String
public let handler: String
public let event: EventHTTPAPI
public let environment: YAMLContent?
public let artifact: String

public init(name: String, handler: String, event: EventHTTPAPI, environment: YAMLContent?, artifact: String) {
self.name = name
self.handler = handler
self.event = event
self.environment = environment
self.artifact = artifact
}
}

public extension Function {
static func httpAPILambda(params: HttpAPILambdaParams, memorySize: Int?) throws -> Function {
try .httpApiLambda(
handler: params.handler,
description: nil,
memorySize: memorySize,
environment: params.environment,
runtime: nil,
package: .init(patterns: nil,
individually: nil,
artifact: params.artifact),
event: params.event
)
}
}

extension Array where Element == HttpAPILambdaParams {
func buildFunctions(memorySize: Int) throws -> [String: Function] {
var functions: [String: Function] = [:]
for lambdasParam in self {
functions[lambdasParam.name] = try Function.httpAPILambda(params: lambdasParam, memorySize: memorySize)
}
return functions
}
}
8 changes: 4 additions & 4 deletions Tests/SLSAdapterTests/SLSAdapterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
return try Data(contentsOf: fixtureUrl)
}

func testReadServerlessYml() throws {
func test_ReadServerlessYml() throws {
let serverlessYml = try fixture(name: "serverless", type: "yml")

let decoder = YAMLDecoder()
Expand Down Expand Up @@ -162,7 +162,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
XCTAssertEqual(productTableProperties.dictionary?["BillingMode"]?.string, "PAY_PER_REQUEST")
}

func testReadWriteServerlessYml() throws {
func test_ReadWriteServerlessYml() throws {
let serverlessYml = try fixture(name: "serverless", type: "yml")
let decoder = YAMLDecoder()
let serverlessConfig = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
Expand All @@ -179,7 +179,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
let path: String
}

func testInitServerlessYml() throws {
func test_InitServerlessYml() throws {
let decoder = YAMLDecoder()
let serverlessYml = try fixture(name: "serverless", type: "yml")
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
Expand Down Expand Up @@ -215,7 +215,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
XCTAssertEqual(serverlessConfig.resources, serverlessConfig2.resources)
}

func testInitServerlessNolLayerYml() throws {
func test_InitServerlessNolLayerYml() throws {
let decoder = YAMLDecoder()
let serverlessYml = try fixture(name: "serverless_no_layer", type: "yml")
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
Expand Down
37 changes: 6 additions & 31 deletions Tests/SLSAdapterTests/ServerlessConfig+Exensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,57 +203,32 @@ extension ServerlessConfig {

static func webhookLambdaAPI(
service: String,
httpAPIPath: String,
region: Region,
runtime: Runtime,
architecture: Architecture,
memorySize: Int,
executable: String,
artifact: String
lambdasParams: [HttpAPILambdaParams]
) throws -> ServerlessConfig {
let iam = Iam(
role: Role(
statements: [.allowLogAccess(resource: try YAMLContent(with: "*"))]
)
)
let environment = try YAMLContent(with: ["WEBHOOK_SECRET": "${ssm:/dev/swift-webhook/webhook_secret}"])
let provider = Provider(
name: .aws,
region: region,
runtime: runtime,
environment: environment,
environment: nil,
architecture: architecture,
httpAPI: .init(
payload: "2.0",
cors: false
),
httpAPI: .init(payload: "2.0", cors: false),
iam: iam
)
let endpoints = [
Endpoint(handler: "postWebHook", method: .post, path: "/webhook"),
Endpoint(handler: "getWebHook", method: .get, path: "/webhook"),
Endpoint(handler: "githubWebHook", method: .post, path: "/github-webhook")
]
var functions: [String: Function] = [:]
for endpoint in endpoints {
let function = try Function.httpApiLambda(
handler: "\(endpoint.handler)",
description: nil,
memorySize: memorySize,
environment: environment,
runtime: nil,
package: .init(patterns: nil,
individually: nil,
artifact: "build/GitHubWebHook/GitHubWebHook.zip"),
event: .init(path: endpoint.path, method: endpoint.method)
)
functions["\(endpoint.handler)\(executable)"] = function
}

let package = Package(patterns: nil, individually: true)
let functions = try lambdasParams.buildFunctions(memorySize: memorySize)
return ServerlessConfig(
service: service,
provider: provider,
package: .init(patterns: nil, individually: nil, artifact: artifact),
package: package,
custom: nil,
layers: nil,
functions: functions,
Expand Down
53 changes: 50 additions & 3 deletions Tests/SLSAdapterTests/ServerlessHttpAPILambdaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
return try Data(contentsOf: fixtureUrl)
}

func testReadServerlessWebhook() throws {
func test_ReadServerlessWebhook() throws {
let serverlessYml = try fixture(name: "serverless_webhook", type: "yml")

let decoder = YAMLDecoder()
Expand Down Expand Up @@ -97,7 +97,7 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
let githubWebHook = try XCTUnwrap(serverlessConfig.functions?["githubWebHook"])
XCTAssertEqual(githubWebHook.handler, "github-webhook")
XCTAssertEqual(githubWebHook.memorySize, 256)
XCTAssertEqual(githubWebHook.description, "[${sls:stage}] get /github-webhook")
XCTAssertEqual(githubWebHook.description, "[${sls:stage}] post /github-webhook")
let artifact3 = githubWebHook.package?.artifact
XCTAssertEqual(artifact3, "build/GitHubWebHook/GitHubWebHook.zip")
let environment = githubWebHook.environment?.dictionary
Expand All @@ -107,7 +107,7 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
XCTAssertEqual(githubWebHook.events.first?.httpAPI?.method, .post)
}

func testWriteServerlessWebook() throws {
func test_WriteServerlessWebook() throws {
let serverlessYml = try fixture(name: "serverless_webhook", type: "yml")
let decoder = YAMLDecoder()
let serverlessConfig = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
Expand All @@ -117,4 +117,51 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: data)
XCTAssertEqual(serverlessConfig, serverlessConfig2)
}

func test_InitServerlessYml() throws {
let decoder = YAMLDecoder()
let serverlessYml = try fixture(name: "serverless_webhook", type: "yml")
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: serverlessYml)

let service: String = "swift-webhook"
let region: Region = .us_east_1
let runtime: Runtime = .providedAl2
let architecture: Architecture = .arm64
let memorySize: Int = 256

let lambdasParams: [HttpAPILambdaParams] = [
HttpAPILambdaParams(
name: "postWebHook",
handler: "post-webhook",
event: .init(path: "/webhook", method: .post),
environment: nil,
artifact: "build/WebHook/WebHook.zip"
),
HttpAPILambdaParams(
name: "getWebHook",
handler: "get-webhook",
event: .init(path: "/webhook", method: .get),
environment: nil,
artifact: "build/WebHook/WebHook.zip"
),
HttpAPILambdaParams(
name: "githubWebHook",
handler: "github-webhook",
event: .init(path: "/github-webhook", method: .post),
environment: YAMLContent.dictionary(
["WEBHOOK_SECRET": .string("${ssm:/dev/swift-webhook/webhook_secret}")]
),
artifact: "build/GitHubWebHook/GitHubWebHook.zip"
)
]
let serverlessConfig = try ServerlessConfig.webhookLambdaAPI(
service: service,
region: region,
runtime: runtime,
architecture: architecture,
memorySize: memorySize,
lambdasParams: lambdasParams
)
XCTAssertEqual(serverlessConfig, serverlessConfig2)
}
}

0 comments on commit bddcce7

Please sign in to comment.