Skip to content

Commit

Permalink
tedge-agent: disable converter and file transfer if running as child …
Browse files Browse the repository at this point in the history
…device (#2265)

Signed-off-by: Marcel Guzik <marcel.guzik@inetum.com>
  • Loading branch information
Bravo555 authored Sep 15, 2023
1 parent ea332aa commit 179fbb9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
23 changes: 19 additions & 4 deletions crates/core/tedge_agent/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::software_manager::config::SoftwareManagerConfig;
use crate::tedge_operation_converter::builder::TedgeOperationConverterBuilder;
use crate::tedge_to_te_converter::converter::TedgetoTeConverter;
use crate::AgentOpt;
use anyhow::Context;
use camino::Utf8PathBuf;
use flockfile::check_another_instance_is_not_running;
use flockfile::Flockfile;
Expand All @@ -18,6 +19,7 @@ use tedge_actors::ConvertingActorBuilder;
use tedge_actors::MessageSink;
use tedge_actors::MessageSource;
use tedge_actors::Runtime;
use tedge_api::mqtt_topics::EntityTopicId;
use tedge_health_ext::HealthMonitorBuilder;
use tedge_mqtt_ext::MqttActorBuilder;
use tedge_mqtt_ext::MqttConfig;
Expand All @@ -40,7 +42,7 @@ pub struct AgentConfig {
pub use_lock: bool,
pub log_dir: Utf8PathBuf,
pub data_dir: Utf8PathBuf,
pub mqtt_device_topic_id: Arc<str>,
pub mqtt_device_topic_id: EntityTopicId,
pub mqtt_topic_root: Arc<str>,
}

Expand All @@ -61,7 +63,9 @@ impl AgentConfig {

let mqtt_device_topic_id = cliopts
.mqtt_device_topic_id
.unwrap_or(tedge_config.mqtt.device_topic_id.clone().into());
.unwrap_or(tedge_config.mqtt.device_topic_id.clone().into())
.parse()
.context("Could not parse the device MQTT topic")?;

let mqtt_session_name = format!("{TEDGE_AGENT}#{mqtt_topic_root}/{mqtt_device_topic_id}");

Expand Down Expand Up @@ -182,14 +186,25 @@ impl Agent {

// Spawn all
runtime.spawn(signal_actor_builder).await?;
runtime.spawn(file_transfer_server_builder).await?;
runtime.spawn(mqtt_actor_builder).await?;
runtime.spawn(restart_actor_builder).await?;
runtime.spawn(software_update_builder).await?;
runtime.spawn(converter_actor_builder).await?;
runtime.spawn(tedge_to_te_converter).await?;
runtime.spawn(health_actor).await?;

// TODO: replace with a call to entity store when we stop assuming default MQTT schema
let is_main_device =
self.config.mqtt_device_topic_id == EntityTopicId::default_main_device();
if is_main_device {
info!(
"Running as a main device, starting tedge_to_te_converter and file transfer actors"
);
runtime.spawn(tedge_to_te_converter).await?;
runtime.spawn(file_transfer_server_builder).await?;
} else {
info!("Running as a child device, tedge_to_te_converter and file transfer actors disabled");
}

runtime.run_to_completion().await?;

Ok(())
Expand Down
29 changes: 21 additions & 8 deletions tests/RobotFramework/tests/tedge_agent/tedge_agent.robot
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ ${CHILD_SN}

*** Test Cases ***

tedge-agent starts on child device
Converter and file transfer service are not running on a child device
Set Device Context ${CHILD_SN}
Execute Command dpkg -i packages/tedge_*.deb
Execute Command dpkg -i packages/tedge-agent_*.deb
Start Service tedge-agent
# delay for tedge-agent to connect to parent device MQTT broker
Sleep 3s

# check that file transfer service is disabled
Execute Command curl -X PUT -d '' http://127.0.0.1:8000/tedge/file-transfer/test-file exp_exit_code=7 # 7 - Failed to connect to host

# check that tedge-to-te-converter is not working while on a child device
Execute Command mosquitto_pub -t tedge/measurements -h ${PARENT_IP} -m ''

Set Device Context ${PARENT_SN}
# we check that tedge-agent on child device was able to connect
Execute Command grep -q "Sending CONNACK to tedge-agent#te/device/child1//" /var/log/mosquitto/mosquitto.log
# Only parent converter should convert the message
Should Have MQTT Messages te/device/main///m/ minimum=1 maximum=1


*** Keywords ***
Expand All @@ -59,3 +61,14 @@ Custom Setup
Execute Command echo '[mqtt]' >> /etc/tedge/tedge.toml
Execute Command echo 'device_topic_id \= "device/child1//"' >> /etc/tedge/tedge.toml
Execute Command echo 'client.host \= "${PARENT_IP}"' >> /etc/tedge/tedge.toml

# Install and start tedge-agent
Execute Command dpkg -i packages/tedge_*.deb
Execute Command dpkg -i packages/tedge-agent_*.deb
Start Service tedge-agent
# delay for tedge-agent to connect to parent device MQTT broker
Sleep 3s

Set Device Context ${PARENT_SN}
# we check that tedge-agent on child device was able to connect
Execute Command grep -q "Sending CONNACK to tedge-agent#te/device/child1//" /var/log/mosquitto/mosquitto.log

1 comment on commit 179fbb9

@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
273 0 5 273 100 51m35.287999999s

Please sign in to comment.