Skip to content

Commit 2b22d0c

Browse files
authored
Add more parameter overriding tests by parsing multiple parameter files (#1899)
1 parent c9a0ab9 commit 2b22d0c

4 files changed

+123
-0
lines changed

controller_manager/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ if(BUILD_TESTING)
210210
install(FILES test/test_controller_spawner_with_type.yaml
211211
DESTINATION test)
212212

213+
install(FILES test/test_controller_overriding_parameters.yaml
214+
DESTINATION test)
215+
213216
ament_add_gmock(test_hardware_management_srvs
214217
test/test_hardware_management_srvs.cpp
215218
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ctrl_with_parameters_and_type:
2+
ros__parameters:
3+
interface_name: "impedance"
4+
joint_offset: 0.2
5+
joint_names: ["joint10"]

controller_manager/test/test_controller_spawner_with_type.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ ctrl_with_parameters_and_type:
22
ros__parameters:
33
type: "controller_manager/test_controller"
44
joint_names: ["joint0"]
5+
interface_name: "position"
56

67
/**:
78
chainable_ctrl_with_parameters_and_type:

controller_manager/test/test_spawner_unspawner.cpp

+114
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ TEST_F(TestLoadController, spawner_test_with_params_file_string_parameter)
291291
ASSERT_THAT(
292292
ctrl_node->get_parameter("joint_names").as_string_array(),
293293
std::vector<std::string>({"joint0"}));
294+
295+
if (!ctrl_node->has_parameter("interface_name"))
296+
{
297+
ctrl_node->declare_parameter("interface_name", "invalid_interface");
298+
}
299+
ASSERT_EQ(ctrl_node->get_parameter("interface_name").as_string(), "position");
294300
}
295301

296302
TEST_F(TestLoadController, spawner_test_type_in_params_file)
@@ -397,6 +403,114 @@ TEST_F(TestLoadController, unload_on_kill_activate_as_group)
397403
ASSERT_EQ(cm_->get_loaded_controllers().size(), 0ul);
398404
}
399405

406+
TEST_F(TestLoadController, spawner_test_to_check_parameter_overriding)
407+
{
408+
const std::string main_test_file_path =
409+
ament_index_cpp::get_package_prefix("controller_manager") +
410+
"/test/test_controller_spawner_with_type.yaml";
411+
const std::string overriding_test_file_path =
412+
ament_index_cpp::get_package_prefix("controller_manager") +
413+
"/test/test_controller_overriding_parameters.yaml";
414+
415+
ControllerManagerRunner cm_runner(this);
416+
EXPECT_EQ(
417+
call_spawner(
418+
"ctrl_with_parameters_and_type --load-only -c "
419+
"test_controller_manager -p " +
420+
main_test_file_path + " -p " + overriding_test_file_path),
421+
0);
422+
423+
ASSERT_EQ(cm_->get_loaded_controllers().size(), 1ul);
424+
425+
auto ctrl_with_parameters_and_type = cm_->get_loaded_controllers()[0];
426+
ASSERT_EQ(ctrl_with_parameters_and_type.info.name, "ctrl_with_parameters_and_type");
427+
ASSERT_EQ(ctrl_with_parameters_and_type.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
428+
ASSERT_EQ(
429+
ctrl_with_parameters_and_type.c->get_lifecycle_state().id(),
430+
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED);
431+
ASSERT_THAT(
432+
cm_->get_parameter("ctrl_with_parameters_and_type.params_file").as_string_array(),
433+
std::vector<std::string>({main_test_file_path, overriding_test_file_path}));
434+
auto ctrl_node = ctrl_with_parameters_and_type.c->get_node();
435+
ASSERT_THAT(
436+
ctrl_with_parameters_and_type.info.parameters_files,
437+
std::vector<std::string>({main_test_file_path, overriding_test_file_path}));
438+
if (!ctrl_node->has_parameter("joint_names"))
439+
{
440+
ctrl_node->declare_parameter("joint_names", std::vector<std::string>({"random_joint"}));
441+
}
442+
ASSERT_THAT(
443+
ctrl_node->get_parameter("joint_names").as_string_array(),
444+
std::vector<std::string>({"joint10"}));
445+
446+
if (!ctrl_node->has_parameter("interface_name"))
447+
{
448+
ctrl_node->declare_parameter("interface_name", "invalid_interface");
449+
}
450+
ASSERT_EQ(ctrl_node->get_parameter("interface_name").as_string(), "impedance")
451+
<< "The parameter should be overridden";
452+
453+
if (!ctrl_node->has_parameter("joint_offset"))
454+
{
455+
ctrl_node->declare_parameter("joint_offset", -M_PI);
456+
}
457+
ASSERT_EQ(ctrl_node->get_parameter("joint_offset").as_double(), 0.2);
458+
}
459+
460+
TEST_F(TestLoadController, spawner_test_to_check_parameter_overriding_reverse)
461+
{
462+
const std::string main_test_file_path =
463+
ament_index_cpp::get_package_prefix("controller_manager") +
464+
"/test/test_controller_overriding_parameters.yaml";
465+
const std::string overriding_test_file_path =
466+
ament_index_cpp::get_package_prefix("controller_manager") +
467+
"/test/test_controller_spawner_with_type.yaml";
468+
469+
ControllerManagerRunner cm_runner(this);
470+
EXPECT_EQ(
471+
call_spawner(
472+
"ctrl_with_parameters_and_type --load-only -c "
473+
"test_controller_manager -p " +
474+
main_test_file_path + " -p " + overriding_test_file_path),
475+
0);
476+
477+
ASSERT_EQ(cm_->get_loaded_controllers().size(), 1ul);
478+
479+
auto ctrl_with_parameters_and_type = cm_->get_loaded_controllers()[0];
480+
ASSERT_EQ(ctrl_with_parameters_and_type.info.name, "ctrl_with_parameters_and_type");
481+
ASSERT_EQ(ctrl_with_parameters_and_type.info.type, test_controller::TEST_CONTROLLER_CLASS_NAME);
482+
ASSERT_EQ(
483+
ctrl_with_parameters_and_type.c->get_lifecycle_state().id(),
484+
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED);
485+
ASSERT_THAT(
486+
cm_->get_parameter("ctrl_with_parameters_and_type.params_file").as_string_array(),
487+
std::vector<std::string>({main_test_file_path, overriding_test_file_path}));
488+
auto ctrl_node = ctrl_with_parameters_and_type.c->get_node();
489+
ASSERT_THAT(
490+
ctrl_with_parameters_and_type.info.parameters_files,
491+
std::vector<std::string>({main_test_file_path, overriding_test_file_path}));
492+
if (!ctrl_node->has_parameter("joint_names"))
493+
{
494+
ctrl_node->declare_parameter("joint_names", std::vector<std::string>({"random_joint"}));
495+
}
496+
ASSERT_THAT(
497+
ctrl_node->get_parameter("joint_names").as_string_array(),
498+
std::vector<std::string>({"joint0"}));
499+
500+
if (!ctrl_node->has_parameter("interface_name"))
501+
{
502+
ctrl_node->declare_parameter("interface_name", "invalid_interface");
503+
}
504+
ASSERT_EQ(ctrl_node->get_parameter("interface_name").as_string(), "position")
505+
<< "The parameter should be overridden";
506+
507+
if (!ctrl_node->has_parameter("joint_offset"))
508+
{
509+
ctrl_node->declare_parameter("joint_offset", -M_PI);
510+
}
511+
ASSERT_EQ(ctrl_node->get_parameter("joint_offset").as_double(), 0.2);
512+
}
513+
400514
TEST_F(TestLoadController, spawner_test_fallback_controllers)
401515
{
402516
const std::string test_file_path = ament_index_cpp::get_package_prefix("controller_manager") +

0 commit comments

Comments
 (0)