icub-test
Loading...
Searching...
No Matches
SystemStatus.cpp
1/*
2 * iCub Robot Unit Tests (Robot Testing Framework)
3 *
4 * Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <robottestingframework/dll/Plugin.h>
22#include <yarp/os/Port.h>
23#include <yarp/os/SystemInfoSerializer.h>
24#include "SystemStatus.h"
25
26using namespace std;
27using namespace robottestingframework;
28using namespace yarp::os;
29
30#define CONNECTION_TIMEOUT 5.0 //seconds
31
32// prepare the plugin
33ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(SystemStatus)
34
35
36SystemStatus::SystemStatus() : yarp::robottestingframework::TestCase("SystemStatus") {
37}
38
39SystemStatus::~SystemStatus() { }
40
41bool SystemStatus::setup(yarp::os::Property &property) {
42
43 //updating the test name
44 if(property.check("name"))
45 setName(property.find("name").asString());
46
47 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(property.check("hosts"),
48 "A list of hosts name must be given using 'hosts' param");
49 yarp::os::Bottle portsSet = property.findGroup("hosts").tail();
50 for(unsigned int i=0; i<portsSet.size(); i++) {
51 yarp::os::Bottle* btport = portsSet.get(i).asList();
52 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(btport && btport->size()>=2, "Hosts must be given as lists of <host name> <max cpu load>");
53 HostInfo info;
54 info.name = btport->get(0).asString();
55 info.maxCpuLoad = btport->get(1).asInt32();
56 hosts.push_back(info);
57 }
58
59 return true;
60}
61
62void SystemStatus::tearDown() {
63 // finalization goes her ...
64}
65
66void SystemStatus::run() {
67 for(unsigned int i=0; i<hosts.size(); i++) {
68 SystemInfoSerializer info;
69 ROBOTTESTINGFRAMEWORK_TEST_REPORT("");
70 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Checking host %s ...", hosts[i].name.c_str()));
71 bool ret = getSystemInfo(hosts[i].name, info);
72 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(ret, Asserter::format("Failed to get the system status of host %s. Is the yarprun running on %s?",
73 hosts[i].name.c_str(), hosts[i].name.c_str()));
74 if(ret) {
75 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Total memory %d MB, free memory %d MB.",
76 info.memory.totalSpace, info.memory.freeSpace));
77 unsigned int cpuLoad1 = (int)(info.load.cpuLoad1*100);
78 unsigned int cpuLoad5 = (int)(info.load.cpuLoad5*100);
79 unsigned int cpuLoad15 = (int)(info.load.cpuLoad15*100);
80 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format("Cpu load during last minute %d\%, last 5 minutes %d\%, last 15 minutes %d\%",
81 cpuLoad1, cpuLoad5, cpuLoad15));
82 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(cpuLoad1 < hosts[i].maxCpuLoad,
83 Asserter::format("Cpu load (last minute) is higher than desired [%d\%]", hosts[i].maxCpuLoad));
84 }
85 }
86}
87
88bool SystemStatus::getSystemInfo(std::string remote,
89 SystemInfoSerializer& info) {
90
91 yarp::os::Port port;
92
93 // opening the port
94 port.setTimeout(CONNECTION_TIMEOUT);
95 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(port.open("..."), "Cannot open the yarp port");
96
97 yarp::os::Bottle msg, grp;
98 grp.clear();
99 grp.addString("sysinfo");
100 msg.addList() = grp;
101
102 ContactStyle style;
103 style.quiet = true;
104 style.timeout = CONNECTION_TIMEOUT;
105
106 bool connected = yarp::os::NetworkBase::connect(port.getName(), remote.c_str(), style);
107 if(!connected) {
108 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(connected, string("Cannot connect to ") + remote);
109 port.close();
110 return false;
111 }
112
113 bool ret = port.write(msg, info);
114 NetworkBase::disconnect(port.getName().c_str(), remote.c_str());
115 if(!ret) {
116 port.close();
117 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(ret, remote + string(" does not respond"));
118 return false;
119 }
120
121 port.close();
122 return true;
123}
124