selftest_rostest.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include "ros/ros.h"
36 #include "diagnostic_msgs/SelfTest.h"
37 
38 #include <gtest/gtest.h>
39 #include <string>
40 
41 TEST(SelfTest, runSelfTest)
42 {
43  ros::NodeHandle nh;
44  ros::NodeHandle nh_private("~");
45 
46  std::string node_to_test;
47  double max_delay;
48  nh_private.param("node_to_test", node_to_test, std::string());
49  nh_private.param("max_delay", max_delay, 60.);
50  ASSERT_FALSE(node_to_test.empty()) << "selftest_rostest needs the \"node_to_test\" parameter.";
51 
52  std::string service_name = node_to_test+"/self_test";
53  ros::service::waitForService(service_name, max_delay);
54 
55  diagnostic_msgs::SelfTest srv;
56 
57  if (nh.serviceClient<diagnostic_msgs::SelfTest>(service_name).call(srv))
58  {
59  diagnostic_msgs::SelfTest::Response &res = srv.response;
60 
61  std::string passfail;
62 
63  if (res.passed)
64  passfail = "PASSED";
65  else
66  passfail = "FAILED";
67 
68  EXPECT_TRUE(res.passed) << "Overall self-test FAILED.";
69 
70  printf("Self test %s for device with id: [%s]\n", passfail.c_str(), res.id.c_str());
71 
72  for (size_t i = 0; i < res.status.size(); i++)
73  {
74  printf("%2d) %s\n", ((int) i + 1), res.status[i].name.c_str());
75  if (res.status[i].level == 0)
76  printf(" [OK]: ");
77  else if (res.status[i].level == 1)
78  printf(" [WARNING]: ");
79  else
80  printf(" [ERROR]: ");
81 
82  printf("%s\n", res.status[i].message.c_str());
83 
84  EXPECT_EQ(0, res.status[i].level) << res.status[i].name << " did not PASS: " << res.status[i].message;
85 
86  for (size_t j = 0; j < res.status[i].values.size(); j++)
87  printf(" [%s] %s\n", res.status[i].values[j].key.c_str(), res.status[i].values[j].value.c_str());
88 
89  printf("\n");
90  }
91  }
92  else
93  {
94  FAIL() << "Unable to trigger self-test.";
95  printf("Failed to call service.\n");
96  }
97 }
98 
99 int main(int argc, char **argv)
100 {
101  ros::init(argc, argv, "selftest_nodetest");
102  testing::InitGoogleTest(&argc, argv);
103  return RUN_ALL_TESTS();
104 }
105 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
::std::string string
Definition: gtest.h:1979
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void InitGoogleTest(int *argc, char **argv)
Definition: gtest-all.cc:6489
TEST(SelfTest, runSelfTest)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:19747
int main(int argc, char **argv)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:20057
#define EXPECT_TRUE(condition)
Definition: gtest.h:19327
#define FAIL()
Definition: gtest.h:19290
#define ASSERT_FALSE(condition)
Definition: gtest.h:19336
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Wed Mar 27 2019 03:04:24