Skip to content

Commit

Permalink
fixup! feat: add an option to use c8y operation ID to update its status
Browse files Browse the repository at this point in the history
Signed-off-by: Rina Fujino <[email protected]>
  • Loading branch information
rina23q committed Aug 21, 2024
1 parent b600bf7 commit 4814b7a
Show file tree
Hide file tree
Showing 6 changed files with 739 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,82 @@ mod tests {
.await;
}

#[tokio::test]
async fn handle_config_snapshot_successful_cmd_with_op_id() {
let ttd = TempTedgeDir::new();
let config = C8yMapperConfig {
smartrest_with_operation_id: SmartRestWithOperationId::Yes,
..test_mapper_config(&ttd)
};
let test_handle = spawn_c8y_mapper_actor_with_config(&ttd, config, true).await;
let TestHandle {
mqtt, http, ul, dl, ..
} = test_handle;
spawn_dummy_c8y_http_proxy(http);

let mut mqtt = mqtt.with_timeout(TEST_TIMEOUT_MS);
let mut ul = ul.with_timeout(TEST_TIMEOUT_MS);
let mut dl = dl.with_timeout(TEST_TIMEOUT_MS);
skip_init_messages(&mut mqtt).await;

// Simulate config_snapshot command with "successful" state
mqtt.send(MqttMessage::new(
&Topic::new_unchecked("te/device/main///cmd/config_snapshot/c8y-mapper-1234"),
json!({
"status": "successful",
"tedgeUrl": "http://localhost:8888/tedge/file-transfer/test-device/config_snapshot/path:type:A-c8y-mapper-1234",
"type": "path/type/A",
})
.to_string(),
))
.await
.expect("Send failed");

// Downloader gets a download request
let download_request = dl.recv().await.expect("timeout");
assert_eq!(download_request.0, "c8y-mapper-1234"); // Command ID

// simulate downloader returns result
dl.send((
download_request.0,
Ok(DownloadResponse {
url: download_request.1.url,
file_path: download_request.1.file_path,
}),
))
.await
.unwrap();

// Uploader gets a download request and assert them
let request = ul.recv().await.expect("timeout");
assert_eq!(request.0, "c8y-mapper-1234"); // Command ID
assert_eq!(
request.1.url,
"http://127.0.0.1:8001/c8y/event/events/dummy-event-id-1234/binaries"
);

// Simulate Uploader returns a result
ul.send((
request.0,
Ok(UploadResponse {
url: request.1.url,
file_path: request.1.file_path,
}),
))
.await
.unwrap();

// Expect `506` smartrest message on `c8y/s/us`.
assert_received_contains_str(
&mut mqtt,
[(
"c8y/s/us",
"506,1234,https://test.c8y.io/event/events/dummy-event-id-1234/binaries",
)],
)
.await;
}

#[tokio::test]
async fn auto_log_upload_successful_operation() {
let ttd = TempTedgeDir::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ impl OperationContext {

#[cfg(test)]
mod tests {
use crate::config::C8yMapperConfig;
use crate::tests::skip_init_messages;
use crate::tests::spawn_c8y_mapper_actor;
use crate::tests::spawn_c8y_mapper_actor_with_config;
use crate::tests::test_mapper_config;
use crate::tests::TestHandle;
use c8y_api::json_c8y_deserializer::C8yDeviceControlTopic;
use serde_json::json;
use std::time::Duration;
use tedge_actors::test_helpers::MessageReceiverExt;
use tedge_actors::Sender;
use tedge_config::SmartRestWithOperationId;
use tedge_mqtt_ext::test_helpers::assert_received_contains_str;
use tedge_mqtt_ext::test_helpers::assert_received_includes_json;
use tedge_mqtt_ext::MqttMessage;
Expand Down Expand Up @@ -295,6 +299,56 @@ mod tests {
.await;
}

#[tokio::test]
async fn handle_config_update_executing_and_failed_cmd_with_id() {
let ttd = TempTedgeDir::new();
let config = C8yMapperConfig {
smartrest_with_operation_id: SmartRestWithOperationId::Yes,
..test_mapper_config(&ttd)
};
let test_handle = spawn_c8y_mapper_actor_with_config(&ttd, config, true).await;
let TestHandle { mqtt, .. } = test_handle;
let mut mqtt = mqtt.with_timeout(TEST_TIMEOUT_MS);

skip_init_messages(&mut mqtt).await;

// Simulate config_snapshot command with "executing" state
mqtt.send(MqttMessage::new(
&Topic::new_unchecked("te/device/main///cmd/config_update/c8y-mapper-1234"),
json!({
"status": "executing",
"tedgeUrl": "http://localhost:8888/tedge/file-transfer/test-device/config_update/typeA-c8y-mapper-1234",
"remoteUrl": "http://www.my.url",
"type": "typeA",
})
.to_string(),
))
.await
.expect("Send failed");

// Expect `504` smartrest message on `c8y/s/us`.
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "504,1234")]).await;

// Simulate config_update command with "failed" state
mqtt.send(MqttMessage::new(
&Topic::new_unchecked("te/device/main///cmd/config_update/c8y-mapper-1234"),
json!({
"status": "failed",
"tedgeUrl": "http://localhost:8888/tedge/file-transfer/test-device/config_update/typeA-c8y-mapper-1234",
"remoteUrl": "http://www.my.url",
"type": "typeA",
"reason": "Something went wrong"
})
.to_string(),
))
.await
.expect("Send failed");

// Expect `505` smartrest message on `c8y/s/us`.
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "505,1234,Something went wrong")])
.await;
}

#[tokio::test]
async fn handle_config_update_successful_cmd_for_main_device() {
let ttd = TempTedgeDir::new();
Expand Down Expand Up @@ -362,4 +416,35 @@ mod tests {
)
.await;
}

#[tokio::test]
async fn handle_config_update_successful_cmd_with_id() {
let ttd = TempTedgeDir::new();
let config = C8yMapperConfig {
smartrest_with_operation_id: SmartRestWithOperationId::Yes,
..test_mapper_config(&ttd)
};
let test_handle = spawn_c8y_mapper_actor_with_config(&ttd, config, true).await;
let TestHandle { mqtt, .. } = test_handle;
let mut mqtt = mqtt.with_timeout(TEST_TIMEOUT_MS);

skip_init_messages(&mut mqtt).await;

// Simulate config_update command with "executing" state
mqtt.send(MqttMessage::new(
&Topic::new_unchecked("te/device/main///cmd/config_update/c8y-mapper-1234"),
json!({
"status": "successful",
"tedgeUrl": "http://localhost:8888/tedge/file-transfer/test-device/config_update/path:type:A-c8y-mapper-1234",
"remoteUrl": "http://www.my.url",
"type": "path/type/A",
})
.to_string(),
))
.await
.expect("Send failed");

// Expect `503` smartrest message on `c8y/s/us`.
assert_received_contains_str(&mut mqtt, [("c8y/s/us", "506,1234")]).await;
}
}
Loading

0 comments on commit 4814b7a

Please sign in to comment.