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

fix: graph deserialization #174

Merged
merged 2 commits into from
Oct 18, 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
4 changes: 2 additions & 2 deletions core/src/ten_manager/src/cmd/cmd_check/cmd_check_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Refer to the "LICENSE" file in the root directory for more information.
//

use std::{collections::HashMap, fs, path};
use std::{collections::HashMap, fs, path, str::FromStr};

use anyhow::{Context, Result};
use clap::{Arg, ArgMatches, Command};
Expand Down Expand Up @@ -132,7 +132,7 @@ fn get_graphs_to_be_checked(command: &CheckGraphCommand) -> Result<Vec<Graph>> {
let mut graphs_to_be_checked: Vec<Graph> = Vec::new();

if let Some(graph_str) = &command.graph {
let graph: Graph = serde_json::from_str(graph_str)
let graph: Graph = Graph::from_str(graph_str)
.with_context(|| "The graph json string is invalid")?;
graphs_to_be_checked.push(graph);
} else {
Expand Down
48 changes: 48 additions & 0 deletions core/src/ten_manager/tests/cmd_check_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ async fn test_cmd_check_predefined_graph_success() {
assert!(result.is_ok());
}

#[actix_rt::test]
async fn test_cmd_check_start_graph_cmd() {
let tman_config = TmanConfig::default();
let command = CheckGraphCommand {
app: vec!["tests/test_data/cmd_check_start_graph_cmd".to_string()],
graph: Some(
include_str!(
"test_data/cmd_check_start_graph_cmd/start_graph.json"
)
.to_string(),
),
predefined_graph_name: None,
};

let result = ten_manager::cmd::cmd_check::cmd_check_graph::execute_cmd(
&tman_config,
command,
)
.await;

assert!(result.is_ok());
}

#[actix_rt::test]
async fn test_cmd_check_start_graph_multi_apps() {
let tman_config = TmanConfig::default();
Expand Down Expand Up @@ -131,3 +154,28 @@ async fn test_cmd_check_predefined_graph_check_all() {
let msg = result.err().unwrap().to_string();
assert!(msg.contains("1/2 graphs failed"));
}

#[actix_rt::test]
async fn test_cmd_check_unique_extension_in_connections() {
let tman_config = TmanConfig::default();
let command = CheckGraphCommand {
app: vec!["tests/test_data/cmd_check_start_graph_cmd".to_string()],
graph: Some(
include_str!(
"test_data/cmd_check_start_graph_cmd/cmd_check_unique_extension_in_connections.json"
)
.to_string(),
),
predefined_graph_name: None,
};

let result = ten_manager::cmd::cmd_check::cmd_check_graph::execute_cmd(
&tman_config,
command,
)
.await;

// The same extension is defined in two different connections.
assert!(result.is_err());
eprintln!("{:?}", result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "app",
"name": "cmd_check_single_app",
"version": "0.1.0",
"dependencies": [
{
"type": "extension",
"name": "addon_a",
"version": "0.1.0"
},
{
"type": "extension",
"name": "addon_b",
"version": "0.1.0"
}
],
"package": {
"include": [
"**"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"type": "start_graph",
"nodes": [
{
"type": "extension",
"name": "ext_a",
"addon": "addon_a",
"extension_group": "some_group"
},
{
"type": "extension",
"name": "ext_b",
"addon": "addon_b",
"extension_group": "some_group"
}
],
"connections": [
{
"extension_group": "some_group",
"extension": "ext_a",
"cmd": [
{
"name": "cmd_1",
"dest": [
{
"extension_group": "some_group",
"extension": "ext_b"
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "extension",
"name": "addon_a",
"version": "0.1.0",
"dependencies": [
{
"type": "system",
"name": "ten_runtime_go",
"version": "0.1.0"
}
],
"package": {
"include": [
"**"
]
},
"api": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "extension",
"name": "addon_b",
"version": "0.1.0",
"dependencies": [
{
"type": "system",
"name": "ten_runtime_go",
"version": "0.1.0"
}
],
"package": {
"include": [
"**"
]
},
"api": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"nodes": [
{
"type": "extension",
"name": "some_extension",
"addon": "addon_a",
"extension_group": "some_group"
},
{
"type": "extension",
"name": "another_ext",
"addon": "addon_b",
"extension_group": "some_group"
}
],
"connections": [
{
"extension": "some_extension",
"extension_group": "some_group",
"cmd": [
{
"name": "hello",
"dest": [
{
"extension_group": "some_group",
"extension": "another_ext"
}
]
}
]
},
{
"extension": "some_extension",
"extension_group": "some_group",
"cmd": [
{
"name": "hello_2",
"dest": [
{
"extension_group": "some_group",
"extension": "another_ext"
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"type": "app",
"name": "cmd_check_start_graph_cmd",
"version": "0.1.0",
"dependencies": [
{
"type": "extension",
"name": "addon_a",
"version": "0.1.0"
},
{
"type": "extension",
"name": "addon_b",
"version": "0.1.0"
}
],
"package": {
"include": [
"**"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"type": "start_graph",
"nodes": [
{
"type": "extension",
"name": "ext_a",
"addon": "addon_a",
"extension_group": "some_group"
},
{
"type": "extension",
"name": "ext_b",
"addon": "addon_b",
"extension_group": "some_group"
}
],
"connections": [
{
"extension_group": "some_group",
"extension": "ext_a",
"cmd": [
{
"name": "cmd_1",
"dest": [
{
"extension_group": "some_group",
"extension": "ext_b"
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "extension",
"name": "addon_a",
"version": "0.1.0",
"dependencies": [
{
"type": "system",
"name": "ten_runtime_go",
"version": "0.1.0"
}
],
"package": {
"include": [
"**"
]
},
"api": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "extension",
"name": "addon_b",
"version": "0.1.0",
"dependencies": [
{
"type": "system",
"name": "ten_runtime_go",
"version": "0.1.0"
}
],
"package": {
"include": [
"**"
]
},
"api": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ impl Graph {
// Check if the app to which the graph node belongs has been
// specified.
if !existed_pkgs_of_all_apps.contains_key(node_app_uri) {
if skip_if_app_not_exist {
continue;
} else {
if !skip_if_app_not_exist {
not_installed_pkgs.push((
node_app_uri.to_string(),
node.node_type.clone(),
node.addon.clone(),
));
}

continue;
}

let existed_pkgs_of_app =
Expand Down
29 changes: 27 additions & 2 deletions core/src/ten_rust/tests/graph_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use ten_rust::pkg_info::{
};

#[test]
fn test_graph_check_extension_not_installed() {
let app_dir = "tests/test_data/graph_check_extension_not_installed";
fn test_graph_check_extension_not_installed_1() {
let app_dir = "tests/test_data/graph_check_extension_not_installed_1";
let pkg_infos =
get_all_existed_pkgs_info_of_app(Path::new(app_dir)).unwrap();
assert!(!pkg_infos.is_empty());
Expand All @@ -37,6 +37,31 @@ fn test_graph_check_extension_not_installed() {
println!("Error: {:?}", result.err().unwrap());
}

#[test]
fn test_graph_check_extension_not_installed_2() {
let app_dir = "tests/test_data/graph_check_extension_not_installed_2";
let pkg_infos =
get_all_existed_pkgs_info_of_app(Path::new(app_dir)).unwrap();
assert!(!pkg_infos.is_empty());

let app_pkg_info = pkg_infos
.iter()
.filter(|pkg| pkg.pkg_identity.pkg_type == PkgType::App)
.last();
let app_pkg = app_pkg_info.unwrap();
let predefined_graphs = app_pkg.get_predefined_graphs().unwrap();
let pkg_graph = predefined_graphs.first().unwrap();
let predefined_graph: PropertyPredefinedGraph = pkg_graph.clone();
let graph = &predefined_graph.graph;

let mut pkg_info_map: HashMap<String, Vec<PkgInfo>> = HashMap::new();
pkg_info_map.insert("http://localhost:8001".to_string(), pkg_infos);

let result = graph.check_if_nodes_have_installed(&pkg_info_map, false);
assert!(result.is_err());
println!("Error: {:?}", result.err().unwrap());
}

#[test]
fn test_graph_check_predefined_graph_success() {
let app_dir = "tests/test_data/graph_check_predefined_graph_success";
Expand Down
Loading
Loading