Skip to content
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
1 change: 1 addition & 0 deletions nav2_map_server/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ add_definitions( -DTEST_DIRECTORY=\"${CMAKE_CURRENT_SOURCE_DIR}\")

add_subdirectory(unit)
add_subdirectory(component)
add_subdirectory(map_saver_cli)
13 changes: 13 additions & 0 deletions nav2_map_server/test/map_saver_cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
include_directories(${PROJECT_SOURCE_DIR}/test)

# map_saver CLI
ament_add_gtest(test_map_saver_cli
test_map_saver_cli.cpp
${PROJECT_SOURCE_DIR}/test/test_constants.cpp
)

ament_target_dependencies(test_map_saver_cli rclcpp nav_msgs)
target_link_libraries(test_map_saver_cli
stdc++fs
${dependencies}
)
99 changes: 99 additions & 0 deletions nav2_map_server/test/map_saver_cli/test_map_saver_cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) 2020 Samsung Research America
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>
#include <experimental/filesystem>
#include <string>
#include <memory>
#include <utility>

#include "rclcpp/rclcpp.hpp"
#include "nav_msgs/msg/occupancy_grid.hpp"

TEST(MapSaverCLI, CLITest)
{
std::string path = "/tmp/";
std::string file = "test_map";
std::string file_path = path + file;

rclcpp::init(0, nullptr);

auto node = std::make_shared<rclcpp::Node>("CLI_Test_Node");
RCLCPP_INFO(node->get_logger(), "Testing Map Saver CLI");

auto publisher = node->create_publisher<nav_msgs::msg::OccupancyGrid>(
"/map",
rclcpp::QoS(rclcpp::KeepLast(1)).transient_local().reliable());

auto msg = std::make_unique<nav_msgs::msg::OccupancyGrid>();
msg->header.frame_id = "map";
msg->header.stamp = node->now();
msg->info.map_load_time = node->now();
msg->info.resolution = 0.05;
msg->info.width = 3;
msg->info.height = 3;
msg->info.origin.position.x = 0.0;
msg->info.origin.position.y = 0.0;
msg->info.origin.orientation.w = 1.0;
msg->data.resize(9);

RCLCPP_INFO(node->get_logger(), "Publishing occupancy grid...");

publisher->publish(std::move(msg));

rclcpp::Rate(1).sleep();

// succeed on real map
RCLCPP_INFO(node->get_logger(), "Calling saver...");

EXPECT_FALSE(std::experimental::filesystem::exists(file_path + ".yaml"));

std::string command =
std::string(
"ros2 run nav2_map_server map_saver_cli -f ") + file_path;
auto return_code = system(command.c_str());
EXPECT_EQ(return_code, 0);

rclcpp::Rate(0.25).sleep();

RCLCPP_INFO(node->get_logger(), "Checking on file...");

EXPECT_TRUE(std::experimental::filesystem::exists(file_path + ".pgm"));
EXPECT_EQ(std::experimental::filesystem::file_size(file_path + ".pgm"), 20ul);

if (std::experimental::filesystem::exists(file_path + ".yaml")) {
std::experimental::filesystem::remove(file_path + ".yaml");
}
if (std::experimental::filesystem::exists(file_path + ".pgm")) {
std::experimental::filesystem::remove(file_path + ".pgm");
}

// fail on bogus map
RCLCPP_INFO(node->get_logger(), "Calling saver...");

EXPECT_FALSE(std::experimental::filesystem::exists(file_path + ".yaml"));

command =
std::string(
"ros2 run nav2_map_server map_saver_cli "
"-t map_failure --occ 100 --free 2 --mode trinary --fmt png -f ") + file_path;
return_code = system(command.c_str());
EXPECT_EQ(return_code, 256);

rclcpp::Rate(0.25).sleep();

RCLCPP_INFO(node->get_logger(), "Checking on file...");

EXPECT_FALSE(std::experimental::filesystem::exists(file_path + ".yaml"));
}