Skip to content

Commit

Permalink
Merge pull request #2393 from albinsuresh/fix/2389/fix-child-op-direc…
Browse files Browse the repository at this point in the history
…tory-registration

Fix auto-registration of child device op dirs #2389
  • Loading branch information
reubenmiller authored Oct 31, 2023
2 parents 420901e + efac99a commit 180681b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
16 changes: 13 additions & 3 deletions crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,24 @@ impl CumulocityConverter {
continue;
}
};
let child_topic_id =
EntityTopicId::default_child_device(child_external_id.as_ref()).unwrap();

let child_name = self.default_device_name_from_external_id(&child_external_id);
let child_topic_id = match EntityTopicId::default_child_device(&child_name) {
Ok(topic_id) => topic_id,
Err(err) => {
error!(
"Child device directory: {} ignored due to {}",
&child_name, err
);
continue;
}
};
let child_device_reg_msg = EntityRegistrationMessage {
topic_id: child_topic_id,
external_id: Some(child_external_id.clone()),
r#type: EntityType::ChildDevice,
parent: None,
other: json!({ "name": child_external_id.as_ref() })
other: json!({ "name": child_name })
.as_object()
.unwrap()
.to_owned(),
Expand Down
37 changes: 37 additions & 0 deletions crates/extensions/c8y_mapper_ext/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,43 @@ async fn mapper_publishes_child_device_create_message() {
.await;
}

#[tokio::test]
async fn mapper_publishes_child_device_create_message_default_naming_scheme() {
let cfg_dir = TempTedgeDir::new();
create_thin_edge_child_devices(&cfg_dir, vec!["test-device:device:child1"]);

let (mqtt, _http, _fs, _timer, _dl) = spawn_c8y_mapper_actor(&cfg_dir, false).await;
let mut mqtt = mqtt.with_timeout(TEST_TIMEOUT_MS);
skip_init_messages(&mut mqtt).await;

mqtt.send(MqttMessage::new(
&C8yTopic::downstream_topic(),
"106,child-one",
))
.await
.expect("Send failed");

// Expect auto-registration message
assert_received_includes_json(
&mut mqtt,
[(
"te/device/child1//",
json!({"@type":"child-device", "name": "child1"}),
)],
)
.await;

// Expect smartrest message on `c8y/s/us` with expected payload "101,child1,child1,thin-edge.io-child".
assert_received_contains_str(
&mut mqtt,
[(
"c8y/s/us",
"101,test-device:device:child1,child1,thin-edge.io-child",
)],
)
.await;
}

#[tokio::test]
async fn mapper_publishes_supported_operations_for_child_device() {
// The test assures tedge-mapper checks if there is a directory for operations for child devices, then it reads and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,57 @@ Register devices using custom MQTT schema
Cumulocity.Set Device ${DEVICE_SN}
Cumulocity.Device Should Have Measurements type=gateway_stats minimum=1 maximum=1


Register tedge-agent when tedge-mapper-c8y is not running #2389
[Teardown] Start Service tedge-mapper-c8y
Device Should Exist ${DEVICE_SN}

Stop Service tedge-mapper-c8y
Execute Command cmd=timeout 5 env TEDGE_RUN_LOCK_FILES=false tedge-agent --mqtt-device-topic-id device/offlinechild1// ignore_exit_code=${True}
Start Service tedge-mapper-c8y

Should Be A Child Device Of Device ${DEVICE_SN}:device:offlinechild1
Should Have MQTT Messages te/device/offlinechild1// minimum=1

Device Should Exist ${DEVICE_SN}:device:offlinechild1
Cumulocity.Restart Device
Should Have MQTT Messages te/device/offlinechild1///cmd/restart/+


Register tedge-configuration-plugin when tedge-mapper-c8y is not running #2389
[Teardown] Start Service tedge-mapper-c8y
Device Should Exist ${DEVICE_SN}

Stop Service tedge-mapper-c8y
Execute Command cmd=timeout 5 tedge-configuration-plugin --mqtt-device-topic-id device/offlinechild2// ignore_exit_code=${True}
Start Service tedge-mapper-c8y

Should Be A Child Device Of Device ${DEVICE_SN}:device:offlinechild2
Should Have MQTT Messages te/device/offlinechild2// minimum=1

Device Should Exist ${DEVICE_SN}:device:offlinechild2
Cumulocity.Get Configuration dummy1
Should Have MQTT Messages te/device/offlinechild2///cmd/config_snapshot/+


Register tedge-log-plugin when tedge-mapper-c8y is not running #2389
[Teardown] Start Service tedge-mapper-c8y
Device Should Exist ${DEVICE_SN}

Stop Service tedge-mapper-c8y
Execute Command cmd=timeout 5 tedge-log-plugin --mqtt-device-topic-id device/offlinechild3// ignore_exit_code=${True}
Start Service tedge-mapper-c8y

Should Be A Child Device Of Device ${DEVICE_SN}:device:offlinechild3
Should Have MQTT Messages te/device/offlinechild3// minimum=1

Device Should Exist ${DEVICE_SN}:device:offlinechild3
Cumulocity.Create Operation
... description=Log file request
... fragments={"c8y_LogfileRequest":{"dateFrom":"2023-01-01T01:00:00+0000","dateTo":"2023-01-02T01:00:00+0000","logFile":"example1","searchText":"first","maximumLines":10}}
Should Have MQTT Messages te/device/offlinechild3///cmd/log_upload/+


*** Keywords ***

Check Child Device
Expand Down

1 comment on commit 180681b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
348 1 3 349 99.71 59m54.247s

Failed Tests

Name Message ⏱️ Duration Suite
Test if all c8y services are using configured service type AssertionError 245.323 s Service Monitoring

Please sign in to comment.