1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/*
* Copyright 2009- ECMWF.
*
* This software is licensed under the terms of the Apache Licence version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/
#ifndef ecflow_udp_test_TestSupport_HPP
#define ecflow_udp_test_TestSupport_HPP
#include <memory>
#include <thread>
#include <boost/test/unit_test.hpp>
#include "Process.hpp"
#include "ecflow/attribute/NodeAttr.hpp"
#include "ecflow/client/ClientInvoker.hpp"
#include "ecflow/core/EcfPortLock.hpp"
#include "ecflow/core/File.hpp"
#include "ecflow/core/Filesystem.hpp"
#include "ecflow/core/Host.hpp"
#include "ecflow/core/Str.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Node.hpp"
#include "ecflow/test/scaffold/Naming.hpp"
#include "ecflow/udp/UDPClient.hpp"
namespace ecf::test {
template <typename SERVER>
class BaseMockServer {
public:
using hostname_t = std::string;
using port_t = uint16_t;
template <typename... Args>
BaseMockServer(hostname_t host, port_t port, Args... args) : host_{std::move(host)},
port_{port},
server_{} {
BOOST_REQUIRE_MESSAGE(!host_.empty(), "determiner host name");
BOOST_REQUIRE_MESSAGE(port_ > 0, "port is be larger than 0");
server_ = std::move(SERVER::launch(host_, port_, std::forward<Args>(args)...));
ECF_TEST_DBG(<< " MOCK: " << SERVER::designation << " has been started!");
}
BaseMockServer(const BaseMockServer&) = delete;
BaseMockServer(BaseMockServer&&) = delete;
~BaseMockServer() {
server_.terminate();
SERVER::cleanup(host_, port_);
ECF_TEST_DBG(<< " MOCK: " << SERVER::designation << " has been terminated!");
}
const hostname_t& host() const { return host_; }
uint16_t port() const { return port_; }
private:
hostname_t host_;
uint16_t port_;
Process server_;
};
/**
* A "mock" ecFlow server, launches the server on a separate process, and enables checking changes in Defs state
*/
class MockServer : public BaseMockServer<MockServer> {
public:
explicit MockServer(port_t port) : BaseMockServer<MockServer>(ecf::Host{}.name(), port) {}
void load_definition(const std::string& defs) const {
ClientInvoker client(ecf::Str::LOCALHOST(), port());
try {
BOOST_REQUIRE_MESSAGE(fs::exists(defs), "definitions file doesn't exist at: " + defs);
auto error = client.loadDefs(defs);
BOOST_REQUIRE_MESSAGE(!error, "load definitions, without error ");
}
catch (std::exception& e) {
BOOST_REQUIRE_MESSAGE(
false, "load definitions, without throwing exception (but threw: " + std::string(e.what()) + ")");
}
ECF_TEST_DBG(<< " MOCK: reference ecFlow suite has been loaded");
}
Meter get_meter(const std::string& path, const std::string& name) const {
node_ptr node = get_node_at(path);
return get_attribute_by_name(node->meters(), name);
}
Label get_label(const std::string& path, const std::string& name) const {
node_ptr node = get_node_at(path);
return get_attribute_by_name(node->labels(), name);
}
Event get_event(const std::string& path, const std::string& name) const {
node_ptr node = get_node_at(path);
return get_attribute_by_name(node->events(), name);
}
private:
node_ptr get_node_at(const std::string& path) const {
ECF_TEST_DBG(<< " Creating ecflow_client connected to " << host() << ":" << port());
ClientInvoker client(ecf::Str::LOCALHOST(), port());
// load all definitions
std::shared_ptr<Defs> defs = nullptr;
client.sync(defs);
// select node at intended path
node_ptr node = defs->findAbsNode(path);
BOOST_REQUIRE_MESSAGE(node, "unable to find node");
return node;
}
template <typename ATTRIBUTE>
static ATTRIBUTE get_attribute_by_name(const std::vector<ATTRIBUTE>& attribs, const std::string& name) {
auto found = std::find_if(
std::begin(attribs), std::end(attribs), [&name](const ATTRIBUTE& attrib) { return attrib.name() == name; });
BOOST_REQUIRE_MESSAGE(found != std::end(attribs), "unable to find attribute");
return *found;
}
public:
static constexpr const char* designation = "ecFlow server";
static Process launch(const hostname_t& host, port_t port) {
// Just for precaution, in case a previous run didn't clean up...
cleanup(host, port);
std::string invoke_command = ecf::File::find_ecf_server_path();
BOOST_REQUIRE_MESSAGE(!invoke_command.empty(), "The server program could not be found");
BOOST_REQUIRE_MESSAGE(fs::exists(invoke_command), "Server exe does not exist at:" << invoke_command);
ECF_TEST_DBG(<< "Launching ecflow_server @" << host << ":" << port << ", with: " << invoke_command);
auto server = Process(invoke_command, {"--port", std::to_string(port), "-d"});
ClientInvoker client(ecf::Str::LOCALHOST(), port);
if (!client.wait_for_server_reply(5)) {
BOOST_REQUIRE_MESSAGE(false, "could not launch ecflow server");
}
return server;
}
static void cleanup(const hostname_t& host, port_t port) {
// Clean up temporary files created by the server instance
std::string temporaries[]{host + '.' + std::to_string(port) + ".ecf.check",
host + '.' + std::to_string(port) + ".ecf.check.b",
host + '.' + std::to_string(port) + ".ecf.log"};
for (const auto& t : temporaries) {
fs::remove(t);
}
}
};
/**
* A "mock" ecFlow UDP server, launches the server on a separate process, and enables triggering operations
*/
class MockUDPServer : public BaseMockServer<MockUDPServer> {
public:
explicit MockUDPServer(port_t port, port_t ecflow_port)
: BaseMockServer<MockUDPServer>("localhost", port, ecflow_port) {}
void update_label(const std::string& path, const std::string& name, const std::string& value) {
auto request = format_request(path, "alter_label", name, value);
send(request);
}
void update_meter(const std::string& path, const std::string& name, int value) {
auto request = format_request(path, "alter_meter", name, value);
send(request);
}
void clear_event(const std::string& path, const std::string& name) {
auto request = format_request(path, "alter_event", name, "0");
send(request);
}
void set_event(const std::string& path, const std::string& name) {
auto request = format_request(path, "alter_event", name, "1");
send(request);
}
void send(const std::string& request) {
ECF_TEST_DBG(<< " MOCK: UDP Client sending request: " << request);
sendRequest(port(), request);
}
private:
template <typename V>
static std::string
format_request(const std::string& path, const std::string& command, const std::string& name, V value) {
std::ostringstream oss;
// clang-format off
oss << R"({)"
<< R"("method":"put",)"
<< R"("payload":)"
<< R"({)"
<< R"("command":")" << command << R"(",)"
<< R"("path":")" << path << R"(",)"
<< R"("name":")" << name << R"(",)"
<< R"("value":")"<< value << R"(")"
<< R"(})"
<< R"(})";
// clang-format on
return oss.str();
}
static void sendRequest(uint16_t port, const std::string& request) {
const std::string host = "localhost";
ECF_TEST_DBG(<< " Creating ecflow_udp client connected to " << host << ":" << port);
ecf::UDPClient client(host, std::to_string(port));
client.send(request);
// Wait for request to flow...
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
public:
static constexpr const char* designation = "ecFlow UDP";
static Process launch(const hostname_t& host, port_t port, port_t ecflow_port) {
std::string invoke_command = ecf::File::root_build_dir() + "/bin/ecflow_udp";
ECF_TEST_DBG(<< " Launching ecflow_udp @" << host << ":" << port << ", with: " << invoke_command);
auto server =
Process(invoke_command,
{"--port", std::to_string(port), "--ecflow_port", std::to_string(ecflow_port), "--verbose"});
// Wait for server to start...
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
return server;
}
static void cleanup(const hostname_t& host, port_t port) {
// No clean up needed for ecFlow UDP
}
};
/**
* This fixture provisions the tests with both an ecFlow server and an ecFlow UDP server.
*/
struct EnableServersFixture
{
EnableServersFixture() : EnableServersFixture(get_ecflow_server_port(), get_ecflow_udp_port()) {}
~EnableServersFixture() = default;
ecf::test::MockServer ecflow_server;
ecf::test::MockUDPServer ecflow_udp;
private:
static MockServer::port_t get_ecflow_server_port() {
MockServer::port_t selected_port = 3199;
ECF_TEST_DBG(<< " Attempting to use port: " << selected_port);
while (!EcfPortLock::is_free(selected_port)) {
ECF_TEST_DBG(<< " Selected port: " << selected_port << " is not available.");
++selected_port;
ECF_TEST_DBG(<< " Attempting to use port: " << selected_port);
}
ECF_TEST_DBG(<< " Found free port: " << selected_port);
return selected_port;
}
static MockServer::port_t get_ecflow_udp_port() { return 3198; }
EnableServersFixture(MockServer::port_t ecflow_server_port, MockServer::port_t ecflow_udp_port)
: ecflow_server(ecflow_server_port),
ecflow_udp(ecflow_udp_port, ecflow_server_port) {
// Load 'reference' suite for tests...
ecflow_server.load_definition(fs::absolute("data/reference.def").string());
}
};
} // namespace ecf::test
#endif /* ecflow_udp_test_TestSupport_HPP */
|