Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace special characters in package version #2683

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/core/plugin_sm/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,24 @@ pub fn deserialize_module_info(
Ok(software_list)
}

fn sm_path(name: &str, version: &Option<String>, target_dir_path: impl AsRef<Path>) -> PathBuf {
pub fn sm_path(name: &str, version: &Option<String>, target_dir_path: impl AsRef<Path>) -> PathBuf {
let mut filename = name.to_string();
if let Some(version) = version {
filename.push('_');
filename.push_str(version.as_str());
}

target_dir_path.as_ref().join(filename)
target_dir_path.as_ref().join(sanitize_filename(&filename))
}

fn sanitize_filename(filename: &str) -> String {
let mut result = String::new();
filename.chars().for_each(|c| {
if matches!(c as u8, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' | b'-' | b'.' | b'_' | b'~') {
result.push(c)
} else {
result.push_str(&format!("%{:x?}", c as u8))
reubenmiller marked this conversation as resolved.
Show resolved Hide resolved
}
});
result
}
14 changes: 14 additions & 0 deletions crates/core/plugin_sm/tests/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
mod tests {

use plugin_sm::plugin::deserialize_module_info;
use plugin_sm::plugin::sm_path;
use plugin_sm::plugin::ExternalPluginCommand;
use serial_test::serial;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use tedge_api::SoftwareError;
Expand Down Expand Up @@ -147,6 +149,18 @@ mod tests {
assert_eq!(res, Ok(()));
}

#[test_case("abc", &Some("1:2.3.4567-8~1234".to_string()), "/tmp", PathBuf::from("/tmp/abc_1%3a2.3.4567-8~1234") ; "with special character")]
fn handle_special_characters_in_module_version(
name: &str,
version: &Option<String>,
target_dir_path: impl AsRef<Path>,
expected_path: PathBuf,
) {
let res = sm_path(name, version, target_dir_path);

assert_eq!(res, expected_path);
}

fn get_dummy_plugin_path() -> PathBuf {
// Return a path to a dummy plugin in target directory.
let package_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
Expand Down
8 changes: 6 additions & 2 deletions tests/RobotFramework/tests/plugin_apt/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env bash
set -e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
nfpm package -f "$SCRIPT_DIR/nfpm-version1.yaml" --target "$SCRIPT_DIR/" --packager deb
nfpm package -f "$SCRIPT_DIR/nfpm-version2.yaml" --target "$SCRIPT_DIR/" --packager deb
pushd "$SCRIPT_DIR" 2>/dev/null ||:
nfpm package -f "nfpm-version1.yaml" --target "./" --packager deb
nfpm package -f "nfpm-version2.yaml" --target "./" --packager deb
nfpm package -f "nfpm-package-with-epoch.yaml" --target "./" --packager deb
popd 2>/dev/null ||:
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Test Tags theme:software theme:plugins
*** Test Cases ***
Apply name filter
${packages_list}= Execute Command
... sudo /etc/tedge/sm-plugins/apt list --name tedge
... sudo /etc/tedge/sm-plugins/apt list --name tedge --maintainer ""
... exp_exit_code=0
Should Match Regexp ${packages_list} ^tedge\\s+${VERSION}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Install packages overwrite config files
${output}= Execute Command cat /etc/sampledeb.cfg
Should Contain ${output} conf 2.0


*** Keywords ***
Custom Setup
${DEVICE_SN}= Setup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*** Settings ***
Resource ../../resources/common.resource
Library ThinEdgeIO
Library Cumulocity

Test Setup Custom Setup
Test Teardown Get Logs

Test Tags theme:software theme:plugins

*** Test Cases ***
Install package with Epoch in version #2666
${FILE_URL}= Cumulocity.Create Inventory Binary package-with-epoch package file=${CURDIR}/package-with-epoch_1.2.3_all.deb
${OPERATION}= Install Software {"name": "package-with-epoch", "version": "2:1.2.3", "softwareType": "apt", "url": "${FILE_URL}"}
${OPERATION}= Operation Should Be SUCCESSFUL ${OPERATION} timeout=60
Device Should Have Installed Software {"name": "package-with-epoch", "version": "2:1.2.3", "softwareType": "apt"}

*** Keywords ***
Custom Setup
${DEVICE_SN}= Setup
Device Should Exist ${DEVICE_SN}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# yaml-language-server: $schema=https://nfpm.goreleaser.com/static/schema.json
---
name: package-with-epoch
arch: all
platform: linux
version: 1.2.3
section: default
priority: optional
maintainer: thin-edge.io <thin-edge@thin-edge.io>
description: Example package which uses the epoch field
homepage: https://thin-edge.io/
license: MIT
epoch: "2"

contents:
# reuse an existing file for convenience
- src: ./sampledeb_1.cfg
dst: /etc/package-with-epoch/
type: config
2 changes: 1 addition & 1 deletion tests/RobotFramework/tests/plugin_apt/nfpm-version1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ platform: linux
version: 1.0.0
section: default
priority: optional
maintainer: Your Name <your.email@example.com>
maintainer: thin-edge.io <thin-edge@thin-edge.io>
description: My Sample App Debian Package
homepage: https://example.com/myapp
license: MIT
Expand Down
2 changes: 1 addition & 1 deletion tests/RobotFramework/tests/plugin_apt/nfpm-version2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ platform: linux
version: 2.0.0
section: default
priority: optional
maintainer: Your Name <your.email@example.com>
maintainer: thin-edge.io <thin-edge@thin-edge.io>
description: My Sample App Debian Package
homepage: https://example.com/myapp
license: MIT
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions tests/images/debian-systemd/files/tedge.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

[apt]
name = "(tedge|c8y|python|wget|vim|curl|apt|mosquitto|ssh|sudo|rolldice).*"
maintainer = ".*(thin-edge.io).*"