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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/*
* 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.
*/
#include <string>
#include <boost/test/unit_test.hpp>
#include "InvokeServer.hpp"
#include "MyDefsFixture.hpp"
#include "SCPort.hpp"
#include "ecflow/client/ClientEnvironment.hpp"
#include "ecflow/client/ClientInvoker.hpp"
#include "ecflow/core/File.hpp"
#include "ecflow/core/Str.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"
#include "ecflow/test/scaffold/Naming.hpp"
using namespace std;
using namespace ecf;
BOOST_AUTO_TEST_SUITE(S_Client)
BOOST_AUTO_TEST_SUITE(T_CheckPtDefsCmd)
// ************************************************************************************
// Note: If you make edits to node tree, they will have no effect until the server is rebuilt
// ************************************************************************************
BOOST_AUTO_TEST_CASE(test_check_pt_defs_cmd) {
ECF_NAME_THIS_TEST();
// This will remove check pt and backup file before server start, to avoid the server from loading previous test
// data
InvokeServer invokeServer("Client:: ...test_check_pt_defs_cmd", SCPort::next());
BOOST_REQUIRE_MESSAGE(invokeServer.server_started(),
"Server failed to start on " << invokeServer.host() << ":" << invokeServer.port());
ClientInvoker theClient(invokeServer.host(), invokeServer.port());
BOOST_REQUIRE_MESSAGE(theClient.restartServer() == 0,
CtsApi::restartServer() << " should return 0 server not started, or connection refused\n"
<< theClient.errorMsg());
std::string path = File::test_data("libs/client/test/data/lifecycle.txt", "libs/client");
BOOST_REQUIRE_MESSAGE(theClient.loadDefs(path) == 0, "load defs failed \n" << theClient.errorMsg());
// First time. Should create a ecf_checkpt_file, but _no_ backup file
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs() == 0,
CtsApi::checkPtDefs() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(fs::exists(invokeServer.ecf_checkpt_file()),
CtsApi::checkPtDefs() << " failed file(" << invokeServer.ecf_checkpt_file() << ") not saved");
BOOST_REQUIRE_MESSAGE(fs::file_size(invokeServer.ecf_checkpt_file()) != 0,
"Expected check point file(" << invokeServer.ecf_checkpt_file()
<< "), to have file size > 0");
if (ClientEnvironment::hostSpecified().empty()) {
// This check only valid if server was invoked locally. Ignore for remote servers
BOOST_REQUIRE_MESSAGE(!fs::exists(invokeServer.ecf_backup_checkpt_file()),
"Backup check point file(" << invokeServer.ecf_backup_checkpt_file()
<< ")should not exist,for very first time.");
}
// Save checkpoint file again. This should create a backup file, we should have ecf_checkpt_file and
// ecf_backup_checkpt_file
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs() == 0,
CtsApi::checkPtDefs() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(fs::exists(invokeServer.ecf_checkpt_file()),
CtsApi::checkPtDefs()
<< " failed No check pt file(" << invokeServer.ecf_checkpt_file() << ") saved");
BOOST_REQUIRE_MESSAGE(fs::file_size(invokeServer.ecf_checkpt_file()) != 0,
"Expected check point file(" << invokeServer.ecf_checkpt_file()
<< ") to have file size > 0 ");
BOOST_REQUIRE_MESSAGE(fs::exists(invokeServer.ecf_backup_checkpt_file()),
"Backup check point file(" << invokeServer.ecf_backup_checkpt_file() << ") not created");
BOOST_REQUIRE_MESSAGE(fs::file_size(invokeServer.ecf_backup_checkpt_file()) != 0,
"Expected backup check point file(" << invokeServer.ecf_backup_checkpt_file()
<< "), to have file size > 0");
// Check the defaults for mode , interval and alarm time before making any changes
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ON_TIME,
" Expected default check pt mode to be ON_TIME");
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_interval_ == CheckPt::default_interval(),
" Expected default check pt interval of "
<< CheckPt::default_interval() << " but found "
<< theClient.server_reply().stats().checkpt_interval_);
if (ClientEnvironment::hostSpecified().empty()) {
// This check only valid if server was invoked locally. Ignore for remote servers
// From ecflow 4.7.0 checkpt_save_time_alarm went from 30 -> 20 seconds, hence will fail for old servers
BOOST_REQUIRE_MESSAGE(
theClient.server_reply().stats().checkpt_save_time_alarm_ == CheckPt::default_save_time_alarm(),
" Expected default check pt alarm time of " << CheckPt::default_save_time_alarm() << " but found "
<< theClient.server_reply().stats().checkpt_save_time_alarm_);
}
// Test change of check_pt interval and mode and alarm
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::NEVER) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::NEVER) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::NEVER,
" Expected check pt mode of NEVER");
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::ON_TIME) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::ON_TIME) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ON_TIME,
" Expected check pt mode of ON_TIME");
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::ALWAYS) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::ALWAYS) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ALWAYS,
" Expected check pt mode of ALWAYS");
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::ON_TIME, 30) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::ON_TIME, 30) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ON_TIME,
" Expected check pt mode of ON_TIME");
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_interval_ == 30,
" Expected check pt interval of 30 but found "
<< theClient.server_reply().stats().checkpt_interval_);
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::UNDEFINED, 56) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::UNDEFINED, 56) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ON_TIME,
" Expected check pt mode of ON_TIME");
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_interval_ == 56,
" Expected check pt interval of 56 but found "
<< theClient.server_reply().stats().checkpt_interval_);
// Mode and interval should remain unchanged only the alarm time should be changed
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::UNDEFINED, 0, 10) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::UNDEFINED, 0, 10) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ON_TIME,
" Expected check pt mode of ON_TIME");
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_interval_ == 56,
" Expected check pt interval of 56 but found "
<< theClient.server_reply().stats().checkpt_interval_);
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_save_time_alarm_ == 10,
" Expected check pt alarm time of 10 but found "
<< theClient.server_reply().stats().checkpt_save_time_alarm_);
// restore default check pointing for test that follow.
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::ON_TIME, CheckPt::default_interval()) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::ON_TIME) << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs(ecf::CheckPt::UNDEFINED, 0, CheckPt::default_save_time_alarm()) == 0,
CtsApi::checkPtDefs(ecf::CheckPt::ON_TIME) << " failed should return 0\n"
<< theClient.errorMsg());
// check defaults were set
BOOST_REQUIRE_MESSAGE(theClient.stats_server() == 0,
CtsApi::stats_server() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_mode_ == ecf::CheckPt::ON_TIME,
" Expected default check pt mode to be ON_TIME");
BOOST_REQUIRE_MESSAGE(theClient.server_reply().stats().checkpt_interval_ == CheckPt::default_interval(),
" Expected default check pt interval of "
<< CheckPt::default_interval() << " but found "
<< theClient.server_reply().stats().checkpt_interval_);
BOOST_REQUIRE_MESSAGE(
theClient.server_reply().stats().checkpt_save_time_alarm_ == CheckPt::default_save_time_alarm(),
" Expected default check pt alarm time of " << CheckPt::default_save_time_alarm() << " but found "
<< theClient.server_reply().stats().checkpt_save_time_alarm_);
}
BOOST_AUTO_TEST_CASE(test_restore_from_check_pt) {
ECF_NAME_THIS_TEST();
InvokeServer invokeServer("Client:: ...test_restore_from_check_pt", SCPort::next());
BOOST_REQUIRE_MESSAGE(invokeServer.server_started(),
"Server failed to start on " << invokeServer.host() << ":" << invokeServer.port());
defs_ptr the_defs = Defs::create();
the_defs->add_suite("s0");
the_defs->add_suite("s1");
the_defs->add_suite("s2");
the_defs->add_suite("s3");
the_defs->add_suite("s4");
ClientInvoker theClient(invokeServer.host(), invokeServer.port());
BOOST_REQUIRE_MESSAGE(theClient.load(the_defs) == 0, "load defs failed \n" << theClient.errorMsg());
size_t expected_no_of_suites = 5;
for (int i = 0; i < 5; i++) {
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs() == 0,
CtsApi::checkPtDefs() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.delete_all() == 0,
"Expected delete all nodes to succeed\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.haltServer() == 0, "Expected halt server to succeed\n" << theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.restoreDefsFromCheckPt() == 0, "Expected restoreDefsFromCheckPt succeed\n");
BOOST_REQUIRE_MESSAGE(theClient.getDefs() == 0, "Expected getDefs() to succeed, i.e expected empty defs\n");
BOOST_REQUIRE_MESSAGE(theClient.defs()->suiteVec().size() == expected_no_of_suites,
"Expected " << expected_no_of_suites << " suites, after restoreDefsFromCheckPt but found "
<< theClient.defs()->suiteVec().size() << "\n");
std::string suite = "/s" + ecf::convert_to<std::string>(i);
BOOST_REQUIRE_MESSAGE(theClient.delete_node(suite) == 0,
"Expected delete single suite to succeed\n"
<< theClient.errorMsg());
expected_no_of_suites--;
}
}
BOOST_AUTO_TEST_CASE(test_restore_from_check_pt_using_new_server) {
ECF_NAME_THIS_TEST();
// This test relies on a NEW server invocation. Hence if ECF_HOST/remote server is used
// the test will will invalid. hence ignore.
if (!ClientEnvironment::hostSpecified().empty()) {
cout << "Ignoring test when ECF_HOST specified\n";
return;
}
auto style = PrintStyle::STATE;
std::string port = SCPort::next();
MyDefsFixture theDefsFixture; // make sure generated server variable use this port.
defs_ptr defs_to_be_check_pointed = theDefsFixture.create_defs(port);
BOOST_REQUIRE_MESSAGE(defs_to_be_check_pointed->suiteVec().size() >= 2, "expected at least 2 suites");
{
// Start a new server. However make sure that on server exit, we not delete check pt files
InvokeServer invokeServer("Client:: ...test_restore_from_check_pt_using_new_server",
port,
false, /* bool disable_job_generation = false */
true, /* bool remove_checkpt_file_before_server_start = true */
false /* bool remove_checkpt_file_after_server_exit = true */
);
BOOST_REQUIRE_MESSAGE(invokeServer.server_started(),
"Server failed to start on " << invokeServer.host() << ":" << invokeServer.port());
ClientInvoker theClient(invokeServer.host(), invokeServer.port());
BOOST_REQUIRE_MESSAGE(theClient.load(defs_to_be_check_pointed) == 0,
"load defs failed \n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs() == 0,
CtsApi::checkPtDefs() << " failed should\n"
<< theClient.errorMsg());
}
// start a new server, using same port. Make sure on start, we do not delete any checkpt files on start up
// server should *LOAD* check pt file on start up
InvokeServer invokeServer("", /* for debug use -new server- as msg */
port,
false, /* bool disable_job_generation = false */
false, /* bool remove_checkpt_file_before_server_start = true */
true /* bool remove_checkpt_file_after_server_exit = true */
);
BOOST_REQUIRE_MESSAGE(invokeServer.server_started(),
"Server failed to start on " << invokeServer.host() << ":" << invokeServer.port());
ClientInvoker theClient(invokeServer.host(), invokeServer.port());
BOOST_REQUIRE_MESSAGE(theClient.sync_local() == 0, "Expected sync_local() to succeed \n");
BOOST_REQUIRE_MESSAGE(theClient.defs(), "Expected sync_local() to succeed defs is empty\n");
// To compare the defs, we need to massage theDefsFixture
// update server state to match server, and update flag, caused by restoreDefsFromCheckPt
defs_to_be_check_pointed->server_state().set_state(SState::HALTED);
defs_to_be_check_pointed->flag().set(ecf::Flag::MESSAGE);
// Specifically, ignore server variables, as the port numbers are different( and therefore checkpt,log, etc will not
// match)
DebugEquality debug_equality; // only as affect in DEBUG build
DebugEquality::set_ignore_server_variables(true);
BOOST_CHECK_MESSAGE(*theClient.defs() == *defs_to_be_check_pointed,
"expected defs to be the same.\nServer defs:\n"
<< ecf::as_string(*theClient.defs(), style) << "\nExpected defs:\n"
<< ecf::as_string(*defs_to_be_check_pointed, style));
}
BOOST_AUTO_TEST_CASE(test_check_pt_edit_history) {
ECF_NAME_THIS_TEST();
// This test relies on a NEW server invocation. Hence if ECF_HOST/remote server is used
// the test will will invalid. hence ignore.
if (!ClientEnvironment::hostSpecified().empty()) {
cout << "Ignoring test when ECF_HOST specified\n";
return;
}
// This will remove check pt and backup file before server start, to avoid the server from loading previous test
// data
InvokeServer invokeServer("Client:: ...test_check_pt_edit_history", SCPort::next());
BOOST_REQUIRE_MESSAGE(invokeServer.server_started(),
"Server failed to start on " << invokeServer.host() << ":" << invokeServer.port());
ClientInvoker theClient(invokeServer.host(), invokeServer.port());
BOOST_REQUIRE_MESSAGE(theClient.edit_history(Str::ROOT_PATH()) == 0,
CtsApi::to_string(CtsApi::edit_history(Str::ROOT_PATH())) << " should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().get_string_vec().size() == 0,
"Expected edit history of size 0 after server start, but found "
<< theClient.server_reply().get_string_vec().size());
// make 5 edits
BOOST_REQUIRE_MESSAGE(theClient.restartServer() == 0,
CtsApi::restartServer() << " should return 0 server not started, or connection refused\n"
<< theClient.errorMsg());
std::string path = File::test_data("libs/client/test/data/lifecycle.txt", "libs/client");
BOOST_REQUIRE_MESSAGE(theClient.loadDefs(path) == 0, "load defs failed \n" << theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.shutdownServer() == 0,
CtsApi::shutdownServer() << " should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.haltServer() == 0,
CtsApi::haltServer() << " should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.restartServer() == 0,
CtsApi::restartServer() << " should return 0\n"
<< theClient.errorMsg());
// make sure edit history updated
BOOST_REQUIRE_MESSAGE(theClient.edit_history(Str::ROOT_PATH()) == 0,
CtsApi::to_string(CtsApi::edit_history(Str::ROOT_PATH())) << " should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.server_reply().get_string_vec().size() == 5,
"Expected edit history of size 5, but found "
<< theClient.server_reply().get_string_vec().size());
// make sure edit history was *NOT* serialized, It is only serialized when check pointing
BOOST_REQUIRE_MESSAGE(theClient.getDefs() == 0,
CtsApi::get() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(theClient.defs()->get_edit_history(Str::ROOT_PATH()).size() == 0,
"Expected edit history of size 0, but found "
<< theClient.defs()->get_edit_history(Str::ROOT_PATH()).size());
// This should write the edit history
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs() == 0,
CtsApi::checkPtDefs() << " failed should return 0\n"
<< theClient.errorMsg());
BOOST_REQUIRE_MESSAGE(fs::exists(invokeServer.ecf_checkpt_file()),
CtsApi::checkPtDefs()
<< " failed No check pt file(" << invokeServer.ecf_checkpt_file() << ") saved");
BOOST_REQUIRE_MESSAGE(fs::file_size(invokeServer.ecf_checkpt_file()) != 0,
"Expected check point file(" << invokeServer.ecf_checkpt_file()
<< ") to have file size > 0 ");
// Create a defs file from the check pt file & check edit history
{
Defs defs;
defs.restore(invokeServer.ecf_checkpt_file()); // restore defs from checkpoint
BOOST_REQUIRE_MESSAGE(defs.get_edit_history(Str::ROOT_PATH()).size() == 5,
"Expected edit history of size 5, but found "
<< defs.get_edit_history(Str::ROOT_PATH()).size());
}
{
// clear edit history
BOOST_REQUIRE_MESSAGE(theClient.edit_history("clear") == 0,
CtsApi::to_string(CtsApi::edit_history("clear")) << " should return 0\n"
<< theClient.errorMsg());
// write checkpoint file, NOTE: theClient.checkPtDefs(ecf::CheckPt::ALWAYS) *ONLY* changes the mode.
BOOST_REQUIRE_MESSAGE(theClient.checkPtDefs() == 0,
CtsApi::checkPtDefs() << " failed should return 0\n"
<< theClient.errorMsg());
// restore defs from checkpoint file
Defs defs;
defs.restore(invokeServer.ecf_checkpt_file()); // restore defs from checkpoint
BOOST_REQUIRE_MESSAGE(defs.get_edit_history().size() == 0,
"Expected edit history of size 0, but found " << defs.get_edit_history().size());
}
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|