File: TestWhyCmd.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (554 lines) | stat: -rw-r--r-- 21,332 bytes parent folder | download
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * 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 <iostream>

#include <boost/test/unit_test.hpp>

#include "ServerTestHarness.hpp"
#include "TestFixture.hpp"
#include "ecflow/base/WhyCmd.hpp"
#include "ecflow/base/cts/ClientToServerCmd.hpp"
#include "ecflow/core/AssertTimer.hpp"
#include "ecflow/core/Chrono.hpp"
#include "ecflow/core/Converter.hpp"
#include "ecflow/core/Timer.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Family.hpp"
#include "ecflow/node/Limit.hpp"
#include "ecflow/node/Suite.hpp"
#include "ecflow/node/Task.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace ecf;

BOOST_AUTO_TEST_SUITE(S_Test)

BOOST_AUTO_TEST_SUITE(T_WhyCmd)

static unsigned int waitForWhy(const std::string& path, const std::string& why, int max_time_to_wait) {
    unsigned int updateCalendarCount = 0;
    TestFixture::client().set_throw_on_error(false);
    AssertTimer assertTimer(max_time_to_wait, false); // Bomb out after n seconds, fall back if test fail
    while (1) {

        /// Why command relies on the Suite serializing the calendar. If this is changed we need to get the full defs
        BOOST_REQUIRE_MESSAGE(TestFixture::client().sync_local() == 0,
                              "sync_local failed should return 0\n"
                                  << TestFixture::client().errorMsg());

        /// called to test that sync suite clock works
        BOOST_REQUIRE_MESSAGE(TestFixture::client().sync_local(true /*sync suite clock*/) == 0,
                              "sync_local failed should return 0\n"
                                  << TestFixture::client().errorMsg());
        defs_ptr server_defs = TestFixture::client().defs();
        updateCalendarCount  = server_defs->updateCalendarCount();

        WhyCmd whyCmd(server_defs, path);
        std::string reason = whyCmd.why();
        // std::cout << reason;

        if (reason.find(why) != std::string::npos) {
            // see the reason why job is not running
            std::cout << reason;
            break;
        }

        BOOST_REQUIRE_MESSAGE(assertTimer.duration() < assertTimer.timeConstraint(),
                              "waitForWhy Test wait " << assertTimer.duration()
                                                      << " taking longer than time constraint of "
                                                      << assertTimer.timeConstraint() << " aborting\n"
                                                      << ecf::as_string(*server_defs, PrintStyle::DEFS));
        sleep(1);
    }
    return updateCalendarCount;
}

BOOST_AUTO_TEST_CASE(test_why_day) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;
    TestClean clean_at_start_and_end;

    Defs theDefs;
    {
        auto today      = Calendar::second_clock_time();
        suite_ptr suite = Suite::create("test_why_day");
        family_ptr fam  = Family::create("family");
        task_ptr task   = Task::create("t1");

        // Dont use hybrid for day dependency as that will force node to complete if days is not the same
        ClockAttr clockAttr(today, false);
        suite->addClock(clockAttr);

        // ** add tomorrow days so that node stays queued **
        task->addDay(DayAttr(today.date() + boost::gregorian::date_duration(1)));

        fam->addTask(task);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_day.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // The job should not be submitted and should stay queued since the day does not match
    // running the why command, should report dependency on day
    unsigned int updateCalendarCount = waitForWhy("/test_why_day/family/t1", "day", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_date) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_date");
        family_ptr fam  = Family::create("family");
        task_ptr task   = Task::create("t1");

        // Don't use hybrid for date dependency as that will force node to complete if date is not the same
        auto today = Calendar::second_clock_time();
        ClockAttr clockAttr(today, false);
        suite->addClock(clockAttr);

        // ** add tomorrow date so that node stays queued **
        task->addDate(DateAttr(today.date() + boost::gregorian::date_duration(1)));
        fam->addTask(task);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_date.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // The job should not be submitted and should stay queued since the day does not match
    // running the why command, should report dependency on day
    unsigned int updateCalendarCount = waitForWhy("/test_why_date/family/t1", "date", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_time) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        auto theLocalTime = Calendar::second_clock_time();
        auto time1        = theLocalTime - boost::posix_time::hours(1);

        suite_ptr suite = Suite::create("test_why_time");
        family_ptr fam  = Family::create("family");
        task_ptr task   = Task::create("t1");

        ClockAttr clockAttr(theLocalTime);
        suite->addClock(clockAttr);

        task->addTime(ecf::TimeAttr(ecf::TimeSlot(time1.time_of_day())));
        fam->addTask(task);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_time.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // The job should not be submitted and should stay queued since the time does not match
    // running the why command, should report dependency on time
    unsigned int updateCalendarCount = waitForWhy("/test_why_time/family/t1", "time", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_today) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        // IMPORTANT: A Node with a today attribute is *free* to run *IF* the suite
        //            calendar time is greater than the today date.
        //            Hence if we start this test just before midnight i.e 11.20,
        //            and add hour , then the today time would be 00:20
        //            *i.e node is free to run*
        // This is not what we want for this test. i.e we depend on node being queued
        // in order to test the why command. Also the following tests, start
        // by deleting all nodes. This will fail due to active/submitted tasks
        // Hence can *NOT* use:
        //   auto theLocalTime =  Calendar::second_clock_time();
        //   auto time1 =  theLocalTime + hours(1);

        // Use a hard coded a time, to avoid failure if test is run just before midnight
        auto theLocalTime = boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::hours(1));
        auto time1        = theLocalTime + boost::posix_time::hours(1);

        suite_ptr suite = theDefs.add_suite("test_why_today");
        ClockAttr clockAttr(theLocalTime);
        suite->addClock(clockAttr);

        family_ptr fam = suite->add_family("family");
        task_ptr task  = fam->add_task("t1");
        task->addToday(ecf::TodayAttr(ecf::TimeSlot(time1.time_of_day())));
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_today.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // The job should not be submitted and should stay queued since the time does not match
    // running the why command, should report dependency on time
    unsigned int updateCalendarCount = waitForWhy("/test_why_today/family/t1", "today", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_cron) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        auto theLocalTime =
            boost::posix_time::ptime(boost::gregorian::date(2010, 6, 21), boost::posix_time::time_duration(11, 0, 0));
        auto time1      = theLocalTime - boost::posix_time::hours(3);
        auto time2      = theLocalTime - boost::posix_time::hours(1);
        auto todaysDate = theLocalTime.date();

        suite_ptr suite = Suite::create("test_why_cron");
        family_ptr fam  = Family::create("family");
        task_ptr task   = Task::create("t1");

        ClockAttr clockAttr(theLocalTime, false /*real*/);
        suite->addClock(clockAttr);

        // ** IMPORTANT **, for a date in the future we must match day of week,
        // ***************  day of month, and year, i.e the results are ANDED
        // ***************  this can lead to unexpected time in the future
        // ***************  if all 3 dependencies are present, as *below*
        ecf::CronAttr cronAttr;
        ecf::TimeSlot start(time1.time_of_day());  // in the past
        ecf::TimeSlot finish(time2.time_of_day()); // in the past
        ecf::TimeSlot incr(0, 5);
        cronAttr.addTimeSeries(start, finish, incr);

        std::vector<int> weekDays;
        weekDays.push_back(todaysDate.day_of_week().as_number());
        std::vector<int> daysOfMonth;
        daysOfMonth.push_back(todaysDate.day());
        std::vector<int> months;
        months.push_back(todaysDate.month());
        cronAttr.addWeekDays(weekDays);
        cronAttr.addDaysOfMonth(daysOfMonth);
        cronAttr.addMonths(months);

        task->addCron(cronAttr);

        fam->addTask(task);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_cron.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // The job should not be submitted and should stay queued we are past the cron time
    // running the why command, should report dependency on cron
    unsigned int updateCalendarCount = waitForWhy("/test_why_cron/family/t1", "cron", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_limit) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    // Testing for limits requires that we have least some jobs are submitted.
    // we need to kill these jobs, at the end of test.

    // suite test_why_limit
    //   limit disk 50
    //   family family
    //     inlimit /suite1:disk 50
    //     task t0
    //     task t1
    //     task t2
    //     task t3
    //     task t4
    //     task t5
    //   endfamily
    // endsuite

    Defs theDefs;
    {
        std::string suiteName   = "test_why_limit";
        std::string pathToLimit = "/" + suiteName;
        suite_ptr suite         = theDefs.add_suite(suiteName);
        suite->addVariable(Variable("ECF_TRIES", "1"));
        suite->addLimit(Limit("disk", 50));

        family_ptr fam = suite->add_family("family");
        fam->addInLimit(InLimit("disk", pathToLimit, 50));
        int taskSize = 6;
        for (int i = 0; i < taskSize; i++) {
            fam->addTask(Task::create("t" + ecf::convert_to<std::string>(i)));
        }
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_limit.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Only one job should be submitted, at a time, we will query on task t5, which
    // should still be queued because the limit is full
    // running the why command, should report dependency on limit
    waitForWhy("/test_why_limit/family/t5", "limit", 10);

    // The main check is over. Wait for jobs to complete
    int updateCalendarCount = 0;
    TestFixture::client().set_throw_on_error(false);
    {
        AssertTimer assertTimer(50, false); // Bomb out after n seconds, fall back if test fail
        while (1) {
            BOOST_REQUIRE_MESSAGE(TestFixture::client().getDefs() == 0,
                                  CtsApi::get() << " failed should return 0\n"
                                                << TestFixture::client().errorMsg());
            defs_ptr defs       = TestFixture::client().defs();
            updateCalendarCount = defs->updateCalendarCount();
            bool wait           = false;
            std::vector<Task*> tasks;
            defs->getAllTasks(tasks);
            for (Task* task : tasks) {
                if (task->state() != NState::COMPLETE) {
                    wait = true;
                }
            }
            if (!wait) {
                break;
            }
            if (assertTimer.duration() >= assertTimer.timeConstraint()) {
                std::cout << "waitFor jobs to complete, wait time of " << assertTimer.duration()
                          << " taking longer than time constraint of " << assertTimer.timeConstraint()
                          << " aborting, ......breaking out\n"
                          << ecf::as_string(*defs, PrintStyle::DEFS) << "\n";
            }
            sleep(1);
        }
    }
    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_trigger) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_trigger");
        family_ptr fam  = Family::create("family");
        task_ptr task   = Task::create("t1");
        task->add_trigger("1 == 0");
        fam->addTask(task);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_trigger.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Waiting for a trigger expression which will never evaluate
    unsigned int updateCalendarCount = waitForWhy("/test_why_trigger/family/t1", "expression", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_meter) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_meter");
        family_ptr fam  = Family::create("family");
        fam->addMeter(Meter("meter", 0, 100, 100));
        task_ptr task = Task::create("t1");
        task->add_trigger("/test_why_meter/family:meter > 20");
        fam->addTask(task);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_meter.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Waiting for a trigger expression which references a meter.
    unsigned int updateCalendarCount = waitForWhy("/test_why_meter/family/t1", "/test_why_meter/family:meter", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_event) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_event");
        family_ptr fam  = Family::create("family");
        task_ptr t1     = Task::create("t1");
        task_ptr t2     = Task::create("t2");
        t2->addEvent(Event(1, "theEvent"));
        t2->add_trigger("1 == 0"); // make sure task t2 never runs
        t1->add_trigger("/test_why_event/family/t2:theEvent == set");
        fam->addTask(t1);
        fam->addTask(t2);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_event.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Task t1, should stay queued because the event on task t2 is never set.
    unsigned int updateCalendarCount =
        waitForWhy("/test_why_event/family/t1", "/test_why_event/family/t2:theEvent", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_user_var) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_user_var");
        family_ptr fam  = Family::create("family");
        task_ptr t1     = Task::create("t1");
        suite->addVariable(Variable("user_var", "10"));
        t1->add_trigger("/test_why_user_var:user_var eq 100");
        fam->addTask(t1);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_user_var.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Task t1, should stay queued because the user variable 'user_var' value  = 10, is not 100
    unsigned int updateCalendarCount = waitForWhy("/test_why_user_var/family/t1", "/test_why_user_var:user_var", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_gen_var) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_gen_var");
        family_ptr fam  = Family::create("family");
        task_ptr t1     = Task::create("t1");
        t1->add_trigger("/test_why_gen_var/family/t1:ECF_TRYNO eq 100");
        fam->addTask(t1);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_gen_var.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Task t1, should stay queued because the generated variable 'ECF_TRYNO' value  = 0, is not 100
    unsigned int updateCalendarCount =
        waitForWhy("/test_why_gen_var/family/t1", "/test_why_gen_var/family/t1:ECF_TRYNO", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_CASE(test_why_repeat) {
    ECF_NAME_THIS_TEST();

    DurationTimer timer;

    Defs theDefs;
    {
        suite_ptr suite = Suite::create("test_why_repeat");
        family_ptr fam  = Family::create("family");
        task_ptr t1     = Task::create("t1");
        suite->addRepeat(RepeatInteger("suite_repeat_var", 0, 3, 1));
        fam->addRepeat(RepeatInteger("family_repeat_var", 0, 3, 1));
        t1->add_trigger(
            "/test_why_repeat/family:family_repeat_var eq 100  or /test_why_repeat:suite_repeat_var eq 100");
        fam->addTask(t1);
        suite->addFamily(fam);
        theDefs.addSuite(suite);
    }

    ServerTestHarness serverTestHarness;
    serverTestHarness.run(theDefs,
                          ServerTestHarness::testDataDefsLocation("test_why_repeat.def"),
                          1 /*timeout*/,
                          false /* waitForTestCompletion*/);

    // Task t1, should stay queued because the repeat variable on the suite and family do not match
    unsigned int updateCalendarCount =
        waitForWhy("/test_why_repeat/family/t1", "/test_why_repeat/family:family_repeat_var", 10);

    std::cout << " " << timer.duration() << " update-calendar-count(" << updateCalendarCount << ")\n";
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()