Skip to content

Commit

Permalink
Merge pull request #2117 from Ruadhri17/apt-plugin-fix
Browse files Browse the repository at this point in the history
Fix config and help command issues for tedge apt plugin
  • Loading branch information
reubenmiller authored Aug 2, 2023
2 parents abe5913 + 5b0d3c0 commit 052f9dc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions crates/common/tedge_config/src/tedge_config_cli/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,15 @@ define_tedge_config! {
#[tedge_config(rename = "type", example = "systemd", default(value = "service"))]
ty: String,
},

apt: {
/// The filtering criterion that is used to filter packages list output by name
#[tedge_config(example = "tedge.*")]
name: String,
/// The filtering criterion that is used to filter packages list output by maintainer
#[tedge_config(example = "thin-edge.io team.*")]
maintainer: String,
}
}

fn default_http_address(dto: &TEdgeConfigDto) -> IpAddr {
Expand Down Expand Up @@ -767,6 +776,8 @@ mod tests {
#[test_case::test_case("firmware.child.update.timeout")]
#[test_case::test_case("service.type")]
#[test_case::test_case("run.lock_files")]
#[test_case::test_case("apt.name")]
#[test_case::test_case("apt.maintainer")]
fn all_0_10_keys_can_be_deserialised(key: &str) {
key.parse::<ReadableKey>().unwrap();
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/tedge_apt_plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tedge-apt-plugin"
description = "Thin.edge.io plugin for software management using apt"
description = "Thin-edge.io plugin for software management using apt"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
Expand All @@ -24,7 +24,6 @@ regex = "1"
serde = { version = "1", features = ["derive"] }
tedge_config = { path = "../../crates/common/tedge_config" }
thiserror = "1.0"
toml = "0.5"

[dev-dependencies]
anyhow = "1.0"
Expand Down
67 changes: 29 additions & 38 deletions plugins/tedge_apt_plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ mod module_check;

use crate::error::InternalError;
use crate::module_check::PackageMetadata;
use clap::IntoApp;
use clap::Parser;
use log::warn;
use regex::Regex;
use serde::Deserialize;
use std::fs;
use std::io::{self};
use std::path::PathBuf;
use std::process::Command;
use std::process::ExitStatus;
use std::process::Stdio;
use tedge_config::new::TEdgeConfig;
use tedge_config::TEdgeConfigLocation;
use tedge_config::TEdgeConfigRepository;
use tedge_config::DEFAULT_TEDGE_CONFIG_PATH;

#[derive(Parser, Debug)]
#[clap(
name = clap::crate_name!(),
version = clap::crate_version!(),
about = clap::crate_description!(),
arg_required_else_help(true)
)]
struct AptCli {
#[clap(long = "config-dir", default_value = DEFAULT_TEDGE_CONFIG_PATH)]
config_dir: PathBuf,

#[clap(subcommand)]
operation: PluginOp,
}
Expand All @@ -25,9 +36,11 @@ struct AptCli {
pub enum PluginOp {
/// List all the installed modules
List {
/// Filter packages list output by name
#[clap(long = "--name")]
name: Option<String>,

/// Filter packages list output by maintainer
#[clap(long = "--maintainer")]
maintainer: Option<String>,
},
Expand Down Expand Up @@ -74,17 +87,6 @@ struct SoftwareModuleUpdate {
pub path: Option<String>,
}

#[derive(Debug, Deserialize)]
struct TedgeConfig {
pub apt: AptConfig,
}

#[derive(Debug, Deserialize)]
struct AptConfig {
pub name: Option<String>,
pub maintainer: Option<String>,
}

fn run(operation: PluginOp) -> Result<ExitStatus, InternalError> {
let status = match operation {
PluginOp::List { name, maintainer } => {
Expand Down Expand Up @@ -289,34 +291,23 @@ fn get_name_and_version(line: &str) -> (&str, &str) {
(name, version)
}

fn get_config() -> Option<TedgeConfig> {
let config_dir = TEdgeConfigLocation::default();

match fs::read_to_string(config_dir.tedge_config_file_path()) {
Ok(content) => match toml::from_str(&content) {
Ok(config) => Some(config),
Err(err) => {
warn!(
"Failed to parse {}: {}",
config_dir.tedge_config_file_path(),
err
);
None
}
},
Err(_) => None,
fn get_config(config_dir: PathBuf) -> Option<TEdgeConfig> {
let tedge_config_location = TEdgeConfigLocation::from_custom_root(config_dir);

match TEdgeConfigRepository::new(tedge_config_location).load_new() {
Ok(config) => Some(config),
Err(err) => {
warn!("Failed to load TEdgeConfig: {}", err);
None
}
}
}

fn main() {
// On usage error, the process exits with a status code of 1

let mut apt = match AptCli::try_parse() {
Ok(aptcli) => aptcli,
Err(_) => {
AptCli::command()
.print_help()
.expect("Failed to print usage help");
Err(err) => {
err.print().expect("Failed to print help message");
// re-write the clap exit_status from 2 to 1, if parse fails
std::process::exit(1)
}
Expand All @@ -327,13 +318,13 @@ fn main() {
ref mut maintainer,
} = apt.operation
{
if let Some(config) = get_config() {
if let Some(config) = get_config(apt.config_dir) {
if name.is_none() {
*name = config.apt.name;
*name = config.apt.name.or_none().cloned();
}

if maintainer.is_none() {
*maintainer = config.apt.maintainer;
*maintainer = config.apt.maintainer.or_none().cloned();
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/RobotFramework/tests/tedge/call_tedge_config_list.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Documentation Purpose of this test is to verify that the tedge config list
... Set new tmp.path and return to default value
... Set new logs.path and return to default value
... Set new run.path and return to default value
... Set new apt.name and return to default value
... Set new apt.maintainer and return to default value
Resource ../../resources/common.resource
Library ThinEdgeIO
Expand Down Expand Up @@ -373,3 +375,27 @@ set/unset software.plugin.default
... sudo tedge config unset software.plugin.default
${unset} Execute Command tedge config list
Should Not Contain ${unset} software.plugin.default=

set/unset apt.name
# Changing apt.name
Execute Command sudo tedge config set apt.name tedge.*
${set} Execute Command tedge config list
Should Contain ${set} apt.name=tedge.*

# Undo the change by using the 'unset' command, value returns to default one
Execute Command
... sudo tedge config unset apt.name
${unset} Execute Command tedge config list
Should Not Contain ${unset} apt.name=

set/unset apt.maintainer
# Changing apt.maintainer
Execute Command sudo tedge config set apt.maintainer 'thin-edge.io team.*'
${set} Execute Command tedge config list
Should Contain ${set} apt.maintainer=thin-edge.io team.*

# Undo the change by using the 'unset' command, value returns to default one
Execute Command
... sudo tedge config unset apt.maintainer
${unset} Execute Command tedge config list
Should Not Contain ${unset} apt.maintainer=

1 comment on commit 052f9dc

@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 %
241 0 5 241 100

Passed Tests

Name ⏱️ Duration Suite
Prerequisite Parent 17.633 s Child Conf Mgmt Plugin
Prerequisite Child 0.497 s Child Conf Mgmt Plugin
Child device bootstrapping 16.426 s Child Conf Mgmt Plugin
Snapshot from device 61.123 s Child Conf Mgmt Plugin
Child device config update 62.342 s Child Conf Mgmt Plugin
Configuration types should be detected on file change (without restarting service) 58.041 s Inotify Crate
Check lock file existence in default folder 1.206 s Lock File
Check PID number in lock file 1.054 s Lock File
Check PID number in lock file after restarting the services 2.007 s Lock File
Check starting same service twice 1.33 s Lock File
Switch off lock file creation 2.925 s Lock File
Set configuration when file exists 14.182 s Configuration Operation
Set configuration when file does not exist 6.904 s Configuration Operation
Set configuration with broken url 6.304 s Configuration Operation
Get configuration 6.004 s Configuration Operation
Get non existent configuration file 5.532 s Configuration Operation
Get non existent configuration type 5.388 s Configuration Operation
Update configuration plugin config via cloud 5.207 s Configuration Operation
Modify configuration plugin config via local filesystem modify inplace 3.9379999999999997 s Configuration Operation
Modify configuration plugin config via local filesystem overwrite 5.727 s Configuration Operation
Update configuration plugin config via local filesystem copy 6.774 s Configuration Operation
Update configuration plugin config via local filesystem move (different directory) 4.975 s Configuration Operation
Update configuration plugin config via local filesystem move (same directory) 4.251 s Configuration Operation
Update the custom operation dynamically 57.283 s Dynamically Reload Operation
Custom operation successful 137.798 s Custom Operation
Custom operation fails 129.613 s Custom Operation
Successful firmware operation 73.342 s Firmware Operation
Install with empty firmware name 111.893 s Firmware Operation
Prerequisite Parent 19.278 s Firmware Operation Child Device
Prerequisite Child 8.371 s Firmware Operation Child Device
Child device firmware update 5.076 s Firmware Operation Child Device
Child device firmware update with cache 6.828 s Firmware Operation Child Device
Firmware plugin supports restart via service manager #1932 5.319 s Firmware Operation Child Device Retry
Update Inventory data via inventory.json 1.5030000000000001 s Inventory Update
Inventory includes the agent fragment with version information 1.452 s Inventory Update
Retrieve a JWT tokens 48.407 s Jwt Request
Mapper recovers and processes output of ongoing software update request 16.987 s Recover And Publish Software Update Message
Check running collectd 1.484 s Monitor Device Collectd
Is collectd publishing MQTT messages? 3.105 s Monitor Device Collectd
Check thin-edge monitoring 4.119 s Monitor Device Collectd
Check grouping of measurements 8.945 s Monitor Device Collectd
Main device registration 2.212 s Device Registration
Child device registration 2.534 s Device Registration
Supports restarting the device 59.048 s Restart Device
Update tedge version from previous using Cumulocity 84.249 s Tedge Self Update
Test if all c8y services are up 111.716 s Service Monitoring
Test if all c8y services are down 41.583 s Service Monitoring
Test if all c8y services are using configured service type 187.259 s Service Monitoring
Test if all c8y services using default service type when service type configured as empty 286.017 s Service Monitoring
Check health status of tedge-mapper-c8y service on broker stop start 22.133 s Service Monitoring
Check health status of tedge-mapper-c8y service on broker restart 24.213 s Service Monitoring
Check health status of child device service 21.145 s Service Monitoring
Successful shell command with output 4.323 s Shell Operation
Check Successful shell command with literal double quotes output 3.461 s Shell Operation
Execute multiline shell command 3.351 s Shell Operation
Failed shell command 3.615 s Shell Operation
Software list should be populated during startup 42.161 s Software
Install software via Cumulocity 44.295 s Software
tedge-agent should terminate on SIGINT while downloading file 41.59 s Software
Software list should only show currently installed software and not candidates 50.671 s Software
Create and publish the tedge agent supported operations on mapper restart 39.145 s Mapper-Publishing-Agent-Supported-Ops
Agent gets the software list request once it comes up 30.639 s Mapper-Publishing-Agent-Supported-Ops
Define Child device 1 ID 0.005 s C8Y Child Alarms Rpi
Normal case when the child device does not exist on c8y cloud 2.408 s C8Y Child Alarms Rpi
Normal case when the child device already exists 0.78 s C8Y Child Alarms Rpi
Reconciliation when the new alarm message arrives, restart the mapper 1.055 s C8Y Child Alarms Rpi
Reconciliation when the alarm that is cleared 65.522 s C8Y Child Alarms Rpi
Child devices support sending simple measurements 1.767 s Child Device Telemetry
Child devices support sending custom measurements 1.3479999999999999 s Child Device Telemetry
Child devices support sending custom events 1.241 s Child Device Telemetry
Child devices support sending custom events overriding the type 1.476 s Child Device Telemetry
Child devices support sending custom alarms #1699 1.5030000000000001 s Child Device Telemetry
Child devices support sending inventory data via c8y topic 1.18 s Child Device Telemetry
Child device supports sending custom child device measurements directly to c8y 1.646 s Child Device Telemetry
Check retained alarms 216.045 s Raise Alarms
Thin-edge devices support sending simple measurements 1.5390000000000001 s Thin-Edge Device Telemetry
Thin-edge devices support sending simple measurements with custom type 3.191 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom measurements 3.3689999999999998 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom events 1.268 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom events overriding the type 1.431 s Thin-Edge Device Telemetry
Thin-edge devices support sending custom alarms #1699 3.745 s Thin-Edge Device Telemetry
Thin-edge device supports sending custom Thin-edge device measurements directly to c8y 2.198 s Thin-Edge Device Telemetry
Thin-edge device support sending inventory data via c8y topic 1.655 s Thin-Edge Device Telemetry
thin-edge components support a custom config-dir location via flags 26.953 s Config Dir
Validate updated data path used by tedge-agent 0.32 s Data Path Config
Validate updated data path used by c8y-firmware-plugin 11.127 s Data Path Config
Validate updated data path used by tedge-agent 0.526 s Log Path Config
Check existence of init directories 0.983 s Tedge Init
Tedge init and check creation of folders 1.1219999999999999 s Tedge Init
Check ownership of the folders 0.932 s Tedge Init
Change user/group and check the change 1.234 s Tedge Init
Tedge init and check if default values are restored 1.284 s Tedge Init
Install thin-edge via apt 65.008 s Install Apt
Install latest via script (from current branch) 39.694 s Install Tedge
Install specific version via script (from current branch) 28.654 s Install Tedge
Install latest tedge via script (from main branch) 28.035 s Install Tedge
Install then uninstall latest tedge via script (from main branch) 53.203 s Install Tedge
Support starting and stopping services 52.386 s Service-Control
Supports a reconnect 70.619 s Test-Commands
Supports disconnect then connect 40.89 s Test-Commands
Update unknown setting 62.902 s Test-Commands
Update known setting 40.474 s Test-Commands
It checks MQTT messages using a pattern 87.386 s Test-Mqtt
Stop c8y-configuration-plugin 0.475 s Health C8Y-Configuration-Plugin
Update the service file 0.361 s Health C8Y-Configuration-Plugin
Reload systemd files 1.025 s Health C8Y-Configuration-Plugin
Start c8y-configuration-plugin 0.209 s Health C8Y-Configuration-Plugin
Start watchdog service 10.233 s Health C8Y-Configuration-Plugin
Check PID of c8y-configuration-plugin 0.112 s Health C8Y-Configuration-Plugin
Kill the PID 0.242 s Health C8Y-Configuration-Plugin
Recheck PID of c8y-configuration-plugin 6.52 s Health C8Y-Configuration-Plugin
Compare PID change 0.003 s Health C8Y-Configuration-Plugin
Stop watchdog service 0.199 s Health C8Y-Configuration-Plugin
Remove entry from service file 0.159 s Health C8Y-Configuration-Plugin
Stop c8y-log-plugin 0.325 s Health C8Y-Log-Plugin
Update the service file 0.126 s Health C8Y-Log-Plugin
Reload systemd files 0.768 s Health C8Y-Log-Plugin
Start c8y-log-plugin 0.163 s Health C8Y-Log-Plugin
Start watchdog service 10.374 s Health C8Y-Log-Plugin
Check PID of c8y-log-plugin 0.089 s Health C8Y-Log-Plugin
Kill the PID 0.18 s Health C8Y-Log-Plugin
Recheck PID of c8y-log-plugin 6.765 s Health C8Y-Log-Plugin
Compare PID change 0.011 s Health C8Y-Log-Plugin
Stop watchdog service 0.239 s Health C8Y-Log-Plugin
Remove entry from service file 0.164 s Health C8Y-Log-Plugin
Stop tedge-mapper 0.151 s Health Tedge Mapper C8Y
Update the service file 0.134 s Health Tedge Mapper C8Y
Reload systemd files 0.367 s Health Tedge Mapper C8Y
Start tedge-mapper 0.14 s Health Tedge Mapper C8Y
Start watchdog service 10.147 s Health Tedge Mapper C8Y
Check PID of tedge-mapper 0.065 s Health Tedge Mapper C8Y
Kill the PID 0.25 s Health Tedge Mapper C8Y
Recheck PID of tedge-mapper 6.638 s Health Tedge Mapper C8Y
Compare PID change 0.007 s Health Tedge Mapper C8Y
Stop watchdog service 0.199 s Health Tedge Mapper C8Y
Remove entry from service file 0.149 s Health Tedge Mapper C8Y
Stop tedge-agent 0.241 s Health Tedge-Agent
Update the service file 0.239 s Health Tedge-Agent
Reload systemd files 0.539 s Health Tedge-Agent
Start tedge-agent 0.289 s Health Tedge-Agent
Start watchdog service 10.297 s Health Tedge-Agent
Check PID of tedge-mapper 0.164 s Health Tedge-Agent
Kill the PID 0.405 s Health Tedge-Agent
Recheck PID of tedge-agent 6.877 s Health Tedge-Agent
Compare PID change 0.001 s Health Tedge-Agent
Stop watchdog service 0.283 s Health Tedge-Agent
Remove entry from service file 0.147 s Health Tedge-Agent
Stop tedge-mapper-az 0.094 s Health Tedge-Mapper-Az
Update the service file 0.099 s Health Tedge-Mapper-Az
Reload systemd files 0.275 s Health Tedge-Mapper-Az
Start tedge-mapper-az 0.226 s Health Tedge-Mapper-Az
Start watchdog service 10.151 s Health Tedge-Mapper-Az
Check PID of tedge-mapper-az 0.116 s Health Tedge-Mapper-Az
Kill the PID 0.219 s Health Tedge-Mapper-Az
Recheck PID of tedge-agent 6.693 s Health Tedge-Mapper-Az
Compare PID change 0.001 s Health Tedge-Mapper-Az
Stop watchdog service 0.123 s Health Tedge-Mapper-Az
Remove entry from service file 0.169 s Health Tedge-Mapper-Az
Stop tedge-mapper-collectd 0.182 s Health Tedge-Mapper-Collectd
Update the service file 0.224 s Health Tedge-Mapper-Collectd
Reload systemd files 0.659 s Health Tedge-Mapper-Collectd
Start tedge-mapper-collectd 0.263 s Health Tedge-Mapper-Collectd
Start watchdog service 10.332 s Health Tedge-Mapper-Collectd
Check PID of tedge-mapper-collectd 0.207 s Health Tedge-Mapper-Collectd
Kill the PID 0.548 s Health Tedge-Mapper-Collectd
Recheck PID of tedge-mapper-collectd 6.634 s Health Tedge-Mapper-Collectd
Compare PID change 0.001 s Health Tedge-Mapper-Collectd
Stop watchdog service 0.079 s Health Tedge-Mapper-Collectd
Remove entry from service file 0.087 s Health Tedge-Mapper-Collectd
tedge-collectd-mapper health status 5.549 s Health Tedge-Mapper-Collectd
c8y-log-plugin health status 6.213 s MQTT health endpoints
c8y-configuration-plugin health status 5.962 s MQTT health endpoints
Publish on a local insecure broker 0.212 s Basic Pub Sub
Publish on a local secure broker 1.679 s Basic Pub Sub
Publish on a local secure broker with client authentication 2.096 s Basic Pub Sub
Publish events to subscribed topic 0.386 s Custom Sub Topics Tedge-Mapper-Aws
Publish measurements to unsubscribed topic 5.636 s Custom Sub Topics Tedge-Mapper-Aws
Publish measurements to subscribed topic 0.562 s Custom Sub Topics Tedge-Mapper-Az
Publish measurements to unsubscribed topic 5.463 s Custom Sub Topics Tedge-Mapper-Az
Publish events to subscribed topic 0.351 s Custom Sub Topics Tedge-Mapper-C8Y
Publish measurements to unsubscribed topic 5.422 s Custom Sub Topics Tedge-Mapper-C8Y
Check remote mqtt broker #1773 3.532 s Remote Mqtt Broker
Apply name filter 0.462 s Filter Packages List Output
Apply maintainer filter 0.279 s Filter Packages List Output
Apply both filters 0.174 s Filter Packages List Output
No filters 0.245 s Filter Packages List Output
Both filters but name filter as empty string 0.305 s Filter Packages List Output
Both filters but maintainer filter as empty string 0.22 s Filter Packages List Output
Both filters as empty string 0.34 s Filter Packages List Output
Wrong package name 0.272 s Improve Tedge Apt Plugin Error Messages
Wrong version 0.247 s Improve Tedge Apt Plugin Error Messages
Wrong type 0.359 s Improve Tedge Apt Plugin Error Messages
tedge_connect_test_positive 0.668 s Tedge Connect Test
tedge_connect_test_negative 2.648 s Tedge Connect Test
tedge_connect_test_sm_services 9.181 s Tedge Connect Test
tedge_disconnect_test_sm_services 60.505 s Tedge Connect Test
Install thin-edge.io 21.206 s Call Tedge
call tedge -V 0.27 s Call Tedge
call tedge -h 0.506 s Call Tedge
call tedge -h -V 0.427 s Call Tedge
call tedge help 0.357 s Call Tedge
tedge config list 0.074 s Call Tedge Config List
tedge config list --all 0.093 s Call Tedge Config List
set/unset device.type 0.476 s Call Tedge Config List
set/unset device.key_path 0.511 s Call Tedge Config List
set/unset device.cert_path 0.628 s Call Tedge Config List
set/unset c8y.root_cert_path 0.89 s Call Tedge Config List
set/unset c8y.smartrest.templates 0.791 s Call Tedge Config List
set/unset c8y.topics 0.343 s Call Tedge Config List
set/unset az.root_cert_path 0.338 s Call Tedge Config List
set/unset az.topics 0.496 s Call Tedge Config List
set/unset aws.topics 0.338 s Call Tedge Config List
set/unset aws.url 0.297 s Call Tedge Config List
set/unset aws.root_cert_path 0.271 s Call Tedge Config List
set/unset aws.mapper.timestamp 0.355 s Call Tedge Config List
set/unset az.mapper.timestamp 0.468 s Call Tedge Config List
set/unset mqtt.bind.address 0.416 s Call Tedge Config List
set/unset mqtt.bind.port 0.359 s Call Tedge Config List
set/unset http.bind.port 0.275 s Call Tedge Config List
set/unset tmp.path 0.297 s Call Tedge Config List
set/unset logs.path 0.253 s Call Tedge Config List
set/unset run.path 0.29 s Call Tedge Config List
set/unset firmware.child.update.timeout 0.28 s Call Tedge Config List
set/unset c8y.url 0.321 s Call Tedge Config List
set/unset az.url 0.296 s Call Tedge Config List
set/unset mqtt.external.bind.port 0.27 s Call Tedge Config List
mqtt.external.bind.address 0.296 s Call Tedge Config List
mqtt.external.bind.interface 0.279 s Call Tedge Config List
set/unset mqtt.external.ca_path 0.288 s Call Tedge Config List
set/unset mqtt.external.cert_file 0.284 s Call Tedge Config List
set/unset mqtt.external.key_file 0.316 s Call Tedge Config List
set/unset software.plugin.default 0.276 s Call Tedge Config List
set/unset apt.name 0.304 s Call Tedge Config List
set/unset apt.maintainer 0.286 s Call Tedge Config List
Get Put Delete 1.97 s Http File Transfer Api
Set keys should return value on stdout 0.107 s Tedge Config Get
Unset keys should not return anything on stdout and warnings on stderr 0.286 s Tedge Config Get
Invalid keys should not return anything on stdout and warnings on stderr 0.329 s Tedge Config Get
Set configuration via environment variables 1.267 s Tedge Config Get
Set configuration via environment variables for topics 0.556 s Tedge Config Get
Set unknown configuration via environment variables 0.119 s Tedge Config Get

Please sign in to comment.