Skip to content

Commit d239c18

Browse files
authored
Merge pull request #22 from fermyon/mqtt
Add mqtt tests
2 parents 5a8da6d + f9ebd85 commit d239c18

File tree

8 files changed

+114
-2
lines changed

8 files changed

+114
-2
lines changed

Cargo.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/outbound-mqtt/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "outbound-mqtt-test-component"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
anyhow = { workspace = true }
11+
helper = { workspace = true }
12+
wit-bindgen = { workspace = true }

components/outbound-mqtt/src/lib.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use anyhow::Context as _;
2+
use helper::bindings::{
3+
fermyon::spin2_0_0::mqtt::{self, Qos},
4+
wasi::http0_2_0::types::{IncomingRequest, OutgoingResponse, ResponseOutparam},
5+
};
6+
7+
struct Component;
8+
helper::gen_http_trigger_bindings!(Component);
9+
10+
impl bindings::Guest for Component {
11+
fn handle(request: IncomingRequest, response_out: ResponseOutparam) {
12+
helper::handle_result(handle(request), response_out)
13+
}
14+
}
15+
16+
const MQTT_ADDRESS: &str = "MQTT_ADDRESS";
17+
const MQTT_USERNAME: &str = "MQTT_USERNAME";
18+
const MQTT_PASSWORD: &str = "MQTT_PASSWORD";
19+
20+
fn handle(request: IncomingRequest) -> anyhow::Result<OutgoingResponse> {
21+
let address = get_header(&request, &MQTT_ADDRESS.to_owned())?;
22+
let username = get_header(&request, &MQTT_USERNAME.to_owned())?;
23+
let password = get_header(&request, &MQTT_PASSWORD.to_owned())?;
24+
25+
let connection = mqtt::Connection::open(&address, &username, &password, 30)?;
26+
27+
connection.publish("telemetry-topic", &b"Eureka!".to_vec(), Qos::AtLeastOnce)?;
28+
29+
Ok(helper::ok_response())
30+
}
31+
32+
fn get_header(request: &IncomingRequest, header_key: &String) -> anyhow::Result<String> {
33+
helper::get_header(request, header_key).with_context(|| format!("no {} header", header_key))
34+
}

components/outbound-redis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl bindings::Guest for Component {
1818
const REDIS_ADDRESS_HEADER: &str = "REDIS_ADDRESS";
1919

2020
fn handle(request: IncomingRequest) -> anyhow::Result<OutgoingResponse> {
21-
let Some(address) = helper::get_header(request, &REDIS_ADDRESS_HEADER.to_owned()) else {
21+
let Some(address) = helper::get_header(&request, &REDIS_ADDRESS_HEADER.to_owned()) else {
2222
// Otherwise, return a 400 Bad Request response.
2323
return Ok(helper::response(
2424
400,

crates/conformance-tests/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ pub enum Precondition {
176176
Sqlite,
177177
/// The test expects a Redis service to be available.
178178
Redis,
179+
/// The test expects a Redis service to be available.
180+
Mqtt,
179181
}
180182

181183
#[derive(Debug, Clone, serde::Deserialize)]

crates/helper/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn handle_result(result: anyhow::Result<OutgoingResponse>, response_out: Res
6565
}
6666

6767
/// Get the value of a header from a request.
68-
pub fn get_header(request: IncomingRequest, header_key: &String) -> Option<String> {
68+
pub fn get_header(request: &IncomingRequest, header_key: &String) -> Option<String> {
6969
request
7070
.headers()
7171
.get(header_key)

tests/outbound-mqtt/spin.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "outbound-mqtt"
5+
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
6+
version = "0.1.0"
7+
8+
[[trigger.http]]
9+
route = "/"
10+
component = "test"
11+
12+
[component.test]
13+
source = "%{source=outbound-mqtt}"
14+
allowed_outbound_hosts = ["mqtt://localhost:%{port=1883}"]
15+
environment = { MQTT_ADDRESS = "mqtt://localhost:%{port=1883}?client_id=spintest" }

tests/outbound-mqtt/test.json5

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"invocations": [
3+
{
4+
"request": {
5+
"path": "/",
6+
"headers": [
7+
{
8+
"name": "Host",
9+
"value": "example.com"
10+
},
11+
{
12+
"name": "MQTT_ADDRESS",
13+
"value": "mqtt://localhost:%{port=1883}?client_id=spintest"
14+
},
15+
{
16+
"name": "MQTT_USERNAME",
17+
"value": "user"
18+
},
19+
{
20+
"name": "MQTT_PASSWORD",
21+
"value": "password"
22+
},
23+
]
24+
},
25+
"response": {
26+
"headers": [
27+
{
28+
"name": "Content-Length",
29+
"value": "0"
30+
},
31+
{
32+
"name": "Date",
33+
"optional": true
34+
}
35+
]
36+
}
37+
}
38+
],
39+
"preconditions": [ { "kind": "mqtt" } ]
40+
}

0 commit comments

Comments
 (0)