File: TestReplace.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 (1077 lines) | stat: -rw-r--r-- 39,972 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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
/*
 * 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 <boost/test/unit_test.hpp>

#include "ecflow/core/Ecf.hpp"
#include "ecflow/core/Environment.hpp"
#include "ecflow/core/File.hpp"
#include "ecflow/core/Str.hpp"
#include "ecflow/node/Defs.hpp"
#include "ecflow/node/Family.hpp"
#include "ecflow/node/Jobs.hpp"
#include "ecflow/node/JobsParam.hpp"
#include "ecflow/node/Suite.hpp"
#include "ecflow/node/Task.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace std;
using namespace ecf;

class ExpectStateChange {
public:
    ExpectStateChange() : state_change_no_(Ecf::state_change_no()) { Ecf::set_server(true); }
    ~ExpectStateChange() {
        BOOST_CHECK_MESSAGE(state_change_no_ != Ecf::state_change_no(), "Expected state change");
        Ecf::set_server(false);
    }

private:
    unsigned int state_change_no_;
};

class ExpectModifyChange {
public:
    ExpectModifyChange() : modify_change_no_(Ecf::modify_change_no()) { Ecf::set_server(true); }
    ~ExpectModifyChange() {
        BOOST_CHECK_MESSAGE(modify_change_no_ != Ecf::modify_change_no(), "Expected Modify change");
        Ecf::set_server(false);
    }

private:
    unsigned int modify_change_no_;
};

class ExpectNoChange {
public:
    ExpectNoChange() : state_change_no_(Ecf::state_change_no()), modify_change_no_(Ecf::modify_change_no()) {
        Ecf::set_server(true);
    }
    ~ExpectNoChange() {
        BOOST_CHECK_MESSAGE(state_change_no_ == Ecf::state_change_no() && modify_change_no_ == Ecf::modify_change_no(),
                            "Expected no change");
        Ecf::set_server(false);
    }

private:
    unsigned int state_change_no_;
    unsigned int modify_change_no_;
};

BOOST_AUTO_TEST_SUITE(U_Node)

BOOST_AUTO_TEST_SUITE(T_Replace)

BOOST_AUTO_TEST_CASE(test_replace_add_task) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t2");
        clientDef->addSuite(suite);
    }

    // add Child t2 to the server defs
    Defs serverDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
        serverDefs.addSuite(suite);
    }

    Defs expectedDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t2"); // notice we preserve client position, and not server position
        fam->add_task("t1");
        expectedDefs.addSuite(suite);
    }

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild(
            "/suite1/family/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_add_suite) {
    ECF_NAME_THIS_TEST();

    // In this test the server defs is *EMPTY* hence we should copy/move over the whole suite
    // provided the a/ Path to node exists in the client def, b/ create nodes as needed is TRUE
    Defs expectedDefs;
    {
        suite_ptr suite = expectedDefs.add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
        fam->add_task("t2");
    }

    {
        // The whole suite should get *MOVED* from clientDef to the *EMPTY* server def. i.e add
        defs_ptr clientDef = Defs::create();
        {
            suite_ptr suite = clientDef->add_suite("suite1");
            family_ptr fam  = suite->add_family("family");
            fam->add_task("t1");
            fam->add_task("t2");
        }

        ExpectModifyChange expect_state_change;
        Defs serverDefs; // Server defs is empty

        std::string errorMsg;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild(
                "/suite1/family/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }

    {
        // The whole suite should get *MOVED* from clientDef to the *EMPTY* server def. i.e add
        defs_ptr clientDef = Defs::create();
        {
            suite_ptr suite = clientDef->add_suite("suite1");
            family_ptr fam  = suite->add_family("family");
            fam->add_task("t1");
            fam->add_task("t2");
        }

        ExpectModifyChange expect_state_change;
        Defs serverDefs; // Server defs is empty

        std::string errorMsg;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild(
                "/suite1/family", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }

    {
        // The whole suite should get *MOVED* from clientDef to the *EMPTY* server def. i.e add
        defs_ptr clientDef = Defs::create();
        {
            suite_ptr suite = clientDef->add_suite("suite1");
            family_ptr fam  = suite->add_family("family");
            fam->add_task("t1");
            fam->add_task("t2");
        }

        ExpectModifyChange expect_state_change;
        Defs serverDefs; // Server defs is empty

        std::string errorMsg;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild("/suite1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
}

BOOST_AUTO_TEST_CASE(test_replace_child) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        clientDef->addSuite(suite);
    }
    Defs comparisonDef;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        comparisonDef.addSuite(suite);
    }
    BOOST_CHECK_MESSAGE(comparisonDef == *clientDef, "client and comparisonDef should be the same");

    ExpectModifyChange expect_state_change;
    std::string errorMsg;
    Defs serverDefs;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild("/suite1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_CHECK_MESSAGE(comparisonDef == serverDefs, "comparisonDef and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_add_preserves_states) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
        fam->add_task("t2");
        fam->add_task("t3");
        fam->add_task("t4");
    }

    // add Child t4 to the server defs, the states on t1->t3 should be preserved
    // The abort should be progagated up the node tree
    family_ptr fam;
    suite_ptr suite;
    Defs serverDefs;
    {
        suite       = serverDefs.add_suite("suite1");
        fam         = suite->add_family("family");
        task_ptr t1 = fam->add_task("t1");
        task_ptr t2 = fam->add_task("t2");
        task_ptr t3 = fam->add_task("t3");
        serverDefs.beginAll();
        t1->set_state(NState::COMPLETE);
        t2->set_state(NState::ABORTED);
        t3->set_state(NState::ACTIVE);
    }

    // cout << serverDefs;
    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild(
            "/suite1/family/t4", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);

    /// The Nodes t1,t2,t3 may have been replaced hence we must get Nodes again
    node_ptr st1 = serverDefs.findAbsNode("/suite1/family/t1");
    node_ptr st2 = serverDefs.findAbsNode("/suite1/family/t2");
    node_ptr st3 = serverDefs.findAbsNode("/suite1/family/t3");
    node_ptr st4 = serverDefs.findAbsNode("/suite1/family/t4");
    BOOST_REQUIRE_MESSAGE(st1, "Expected to find task t1");
    BOOST_REQUIRE_MESSAGE(st2, "Expected to find task t2");
    BOOST_REQUIRE_MESSAGE(st3, "Expected to find task t3");
    BOOST_REQUIRE_MESSAGE(st4, "Expected to find task t4");
    BOOST_REQUIRE_MESSAGE(st1->state() == NState::COMPLETE, " state on task t1 not preserved after replace");
    BOOST_REQUIRE_MESSAGE(st2->state() == NState::ABORTED, " state on task t2 not preserved after replace");
    BOOST_REQUIRE_MESSAGE(st3->state() == NState::ACTIVE, " state on task t3 not preserved after replace");
    BOOST_REQUIRE_MESSAGE(st4->state() == NState::QUEUED, " state on task t4 to be queued");
    BOOST_REQUIRE_MESSAGE(fam->state() == NState::ABORTED, "Aborted should have propagated to family");
    BOOST_REQUIRE_MESSAGE(suite->state() == NState::ABORTED, "Aborted should have propagated to suite");
    BOOST_REQUIRE_MESSAGE(serverDefs.state() == NState::ABORTED, "Aborted should have propagated to Defs");
}

BOOST_AUTO_TEST_CASE(test_replace_preserves_suspend) {
    ECF_NAME_THIS_TEST(); // ECFLOW-1520

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
        fam->add_task("t2");
        fam->add_task("t3");
        fam->add_task("t4");
    }

    Defs comparisonDef;
    {
        suite_ptr suite = comparisonDef.add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        task_ptr t1     = fam->add_task("t1");
        task_ptr t2     = fam->add_task("t2");
        task_ptr t3     = fam->add_task("t3");
        task_ptr t4     = fam->add_task("t4");
        BOOST_CHECK_MESSAGE(comparisonDef == *clientDef, "client and comparisonDef should be the same");
        comparisonDef.beginAll();

        fam->suspend();
        t1->suspend();
        t2->suspend();
        t3->suspend();
        t4->suspend();
    }

    defs_ptr serverDefs = Defs::create();
    {
        suite_ptr suite = serverDefs->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        task_ptr t1     = fam->add_task("t1");
        task_ptr t2     = fam->add_task("t2");
        task_ptr t3     = fam->add_task("t3");
        task_ptr t4     = fam->add_task("t4");
        serverDefs->beginAll();
        fam->suspend();
        t1->suspend();
        t2->suspend();
        t3->suspend();
        t4->suspend();
    }

    //   PrintStyle style(PrintStyle::MIGRATE);
    //   cout << serverDefs;

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs->replaceChild("/suite1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_REQUIRE_MESSAGE(serverDefs->findSuite("suite1"), "Can't find suite1");
    BOOST_REQUIRE_MESSAGE(serverDefs->findSuite("suite1")->begun(), "Expected replaced suite to preserve begun status");
    DebugEquality debug_equality; // only has affect in DEBUG build
    BOOST_CHECK_MESSAGE(comparisonDef == *serverDefs, "comparisonDef and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_preserves_suspend2) {
    ECF_NAME_THIS_TEST(); // ECFLOW-1520

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr f1   = suite->add_family("f1");
        family_ptr fam  = f1->add_family("fam");
        fam->add_task("t1");
    }

    Defs comparisonDef;
    {
        suite_ptr suite = comparisonDef.add_suite("suite1");
        family_ptr f1   = suite->add_family("f1");
        family_ptr fam  = f1->add_family("fam");
        task_ptr t1     = fam->add_task("t1");

        BOOST_CHECK_MESSAGE(comparisonDef == *clientDef, "client and comparisonDef should be the same");
        comparisonDef.beginAll();

        f1->suspend();
        fam->suspend();
    }

    defs_ptr serverDefs = Defs::create();
    {
        suite_ptr suite = serverDefs->add_suite("suite1");
        family_ptr f1   = suite->add_family("f1");
        family_ptr fam  = f1->add_family("fam");
        serverDefs->beginAll();
        f1->suspend();
        fam->suspend();
    }

    //   PrintStyle style(PrintStyle::MIGRATE);
    //   cout << serverDefs;

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs->replaceChild(
            "/suite1/f1/fam/t1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_REQUIRE_MESSAGE(serverDefs->findSuite("suite1"), "Can't find suite1");
    BOOST_REQUIRE_MESSAGE(serverDefs->findSuite("suite1")->begun(), "Expected replaced suite to preserve begun status");
    DebugEquality debug_equality; // only has affect in DEBUG build
    BOOST_CHECK_MESSAGE(comparisonDef == *serverDefs, "comparisonDef and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_preserves_sibling_states) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t4");
        clientDef->addSuite(suite);
    }

    // add Child t4 to the server defs, the states on t1->t4 should be preserved
    task_ptr t1, t2, t3;
    Defs serverDefs;
    {
        suite_ptr suite = serverDefs.add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        t1              = fam->add_task("t1");
        t1->set_state(NState::COMPLETE);
        t2 = fam->add_task("t2");
        t2->set_state(NState::ABORTED);
        t3 = fam->add_task("t3");
        t3->set_state(NState::ACTIVE);
        fam->add_task("t4");
    }

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild(
            "/suite1/family/t4", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_REQUIRE_MESSAGE(t1->state() == NState::COMPLETE, " state on task t1 not preserved after replace");
    BOOST_REQUIRE_MESSAGE(t2->state() == NState::ABORTED, " state on task t2 not preserved after replace");
    BOOST_REQUIRE_MESSAGE(t3->state() == NState::ACTIVE, " state on task t3 not preserved after replace");
}

BOOST_AUTO_TEST_CASE(test_replace_preserves_begun_status) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->addTask(Task::create("t1"));
    }
    Defs comparisonDef;
    {
        suite_ptr suite = comparisonDef.add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->addTask(Task::create("t1"));
    }
    BOOST_CHECK_MESSAGE(comparisonDef == *clientDef, "client and comparisonDef should be the same");
    comparisonDef.beginAll();

    defs_ptr serverDefs = Defs::create();
    {
        suite_ptr suite = serverDefs->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->addTask(Task::create("t1"));
    }
    serverDefs->beginAll();

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs->replaceChild("/suite1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_REQUIRE_MESSAGE(serverDefs->findSuite("suite1"), "Can't find suite1");
    BOOST_REQUIRE_MESSAGE(serverDefs->findSuite("suite1")->begun(), "Expected replaced suite to preserve begun status");
    DebugEquality debug_equality; // only has affect in DEBUG build
    BOOST_CHECK_MESSAGE(comparisonDef == *serverDefs, "comparisonDef and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_preserves_begun_where_client_not_begun) {
    ECF_NAME_THIS_TEST(); // ECFLOW-1612

    // In this test we will end up calling begin on the time attribute.
    // However we had called begin on the client defs side, where the calendar/suite had not been begun
    // Thus causing an assert failure. The fix is to call begin on the server side, after the replace.

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        task_ptr t2     = fam->add_task("t2");
        t2->addTime(ecf::TimeAttr(ecf::TimeSlot(0, 0), ecf::TimeSlot(10, 1), ecf::TimeSlot(0, 1), false));
        clientDef->addSuite(suite);
    }

    // add Child t2 to the server defs
    Defs serverDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
        serverDefs.addSuite(suite);
        serverDefs.beginAll();
    }

    Defs expectedDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = suite->add_family("family");
        task_ptr t2     = fam->add_task("t2"); // notice we preserve client position, and not server position
        t2->addTime(ecf::TimeAttr(ecf::TimeSlot(0, 0), ecf::TimeSlot(10, 1), ecf::TimeSlot(0, 1), false));
        fam->add_task("t1");
        expectedDefs.addSuite(suite);
        expectedDefs.beginAll();
    }

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild(
            "/suite1/family/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_add_node) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        suite->addTask(Task::create("t2"));
        clientDef->addSuite(suite);
    }
    Defs comparisonDef;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        suite->addTask(Task::create("t2"));
        comparisonDef.addSuite(suite);
    }
    BOOST_CHECK_MESSAGE(comparisonDef == *clientDef, "client and comparisonDef should be the same");

    // Here /suite1/t2 does not exist in the server. Moved from client defs to server
    Defs serverDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        serverDefs.addSuite(suite);
    }

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild("/suite1/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_CHECK_MESSAGE(comparisonDef == serverDefs, "comparisonDef and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_add_hierarchy) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fa   = Family::create("fa");
        family_ptr fb   = Family::create("fb");
        family_ptr fc   = Family::create("fc");
        family_ptr fd   = Family::create("fd");
        fa->addFamily(fb);
        fb->addFamily(fc);
        fc->addFamily(fd);
        fd->addTask(Task::create("t1"));
        suite->addFamily(fa);
        clientDef->addSuite(suite);
    }
    Defs comparisonDef;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fa   = Family::create("fa");
        family_ptr fb   = Family::create("fb");
        family_ptr fc   = Family::create("fc");
        family_ptr fd   = Family::create("fd");
        fa->addFamily(fb);
        fb->addFamily(fc);
        fc->addFamily(fd);
        fd->addTask(Task::create("t1"));
        suite->addFamily(fa);
        comparisonDef.addSuite(suite);
    }
    BOOST_CHECK_MESSAGE(comparisonDef == *clientDef, "client and comparisonDef should be the same");

    // Here /suite1/fa/fb/fc/fd/t1 does not exist in the server.  These should be created.
    // by adding family "fa" as a child of suite1
    Defs serverDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        serverDefs.addSuite(suite);
    }

    ExpectStateChange expect_state_change;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        serverDefs.replaceChild(
            "/suite1/fa/fb/fc/fd/t1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        errorMsg);
    BOOST_CHECK_MESSAGE(comparisonDef == serverDefs, "comparisonDef and servers defs should be the same");
}

BOOST_AUTO_TEST_CASE(test_replace_order_preserved_for_suite) {
    ECF_NAME_THIS_TEST();

    // Test that when we replace a suite, its order is preserved,
    // See  ECFLOW-23 - When replacing a node the order is changed.

    defs_ptr clientDef = Defs::create();
    {
        clientDef->add_suite("s1");
        clientDef->add_suite("s2");
        clientDef->add_suite("s3");
    }

    // Replace suite s1 with another suite s1 check order is preserved
    Defs serverDefs;
    {
        serverDefs.add_suite("s1");
        serverDefs.add_suite("s2");
        serverDefs.add_suite("s3");
    }

    Defs expectedDefs;
    {
        expectedDefs.add_suite("s1");
        expectedDefs.add_suite("s2");
        expectedDefs.add_suite("s3");
    }

    std::string errorMsg;
    {
        ExpectModifyChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild("/s1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
    {
        ExpectModifyChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild("/s2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
    {
        ExpectModifyChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild("/s3", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
}

BOOST_AUTO_TEST_CASE(test_replace_order_preserved_for_family) {
    ECF_NAME_THIS_TEST();

    // Test that when we replace a family, its order is preserved,
    // See  ECFLOW-23 - When replacing a node the order is changed.

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        suite->add_family("f1");
        suite->add_family("f2");
        suite->add_family("f3");
    }

    // Replace family f1 with another family f1 check order is preserved
    Defs serverDefs;
    {
        suite_ptr suite = serverDefs.add_suite("suite1");
        suite->add_family("f1");
        suite->add_family("f2");
        suite->add_family("f3");
    }

    Defs expectedDefs;
    {
        suite_ptr suite = expectedDefs.add_suite("suite1");
        suite->add_family("f1");
        suite->add_family("f2");
        suite->add_family("f3");
    }

    std::string errorMsg;
    DebugEquality debug_equality; // only as affect in DEBUG build
    {
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(serverDefs.replaceChild(
                                  "/suite1/f1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
                              errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
    {
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(serverDefs.replaceChild(
                                  "/suite1/f2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
                              errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
    {
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(serverDefs.replaceChild(
                                  "/suite1/f3", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
                              errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
}

BOOST_AUTO_TEST_CASE(test_replace_order_preserved_for_task) {
    ECF_NAME_THIS_TEST();

    // Test that when we replace a family, its order is preserved,
    // See  ECFLOW-23 - When replacing a node the order is changed.

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr f1   = suite->add_family("f1");
        f1->add_task("t1");
        f1->add_task("t2");
        f1->add_task("t3");
    }

    // Replace task t1 with another task t1, check order is preserved
    Defs serverDefs;
    {
        suite_ptr suite = serverDefs.add_suite("suite1");
        family_ptr f1   = suite->add_family("f1");
        f1->add_task("t1");
        f1->add_task("t2");
        f1->add_task("t3");
    }

    Defs expectedDefs;
    {
        suite_ptr suite = expectedDefs.add_suite("suite1");
        family_ptr f1   = suite->add_family("f1");
        f1->add_task("t1");
        f1->add_task("t2");
        f1->add_task("t3");
    }

    std::string errorMsg;
    DebugEquality debug_equality; // only as affect in DEBUG build
    {
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild(
                "/suite1/f1/t1", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
    {
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild(
                "/suite1/f1/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
    {
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild(
                "/suite1/f1/t3", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
            errorMsg);
        BOOST_CHECK_MESSAGE(expectedDefs == serverDefs, "expectedDefs and servers defs should be the same");
    }
}

BOOST_AUTO_TEST_CASE(test_replace_child_errors) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        suite->addTask(Task::create("t2"));
        clientDef->addSuite(suite);
    }

    ExpectNoChange expect_no_change;
    Defs serverDefs;
    std::string errorMsg;
    BOOST_REQUIRE_MESSAGE(
        !serverDefs.replaceChild(
            "/suite1/i/dont/exist", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
        "Expected failure");
}

BOOST_AUTO_TEST_CASE(test_replace_child_errors_2) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        suite->addTask(Task::create("t2"));
        clientDef->addSuite(suite);
    }
    Defs serverDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        serverDefs.addSuite(suite);
    }

    std::string errorMsg;
    {
        ExpectNoChange expect_no_change;
        // because createNodesAsNeeded is false, child adoption should fail since /suite1/t2
        // does not exist on the server
        BOOST_REQUIRE_MESSAGE(
            !serverDefs.replaceChild("/suite1/t2", clientDef, false /*create nodes as needed*/, false, errorMsg),
            "Expected failure");
    }
    {
        // With flag now set, we will create any missing nodes even if they dont exist in the server
        ExpectStateChange expect_state_change;
        errorMsg.clear();
        BOOST_REQUIRE_MESSAGE(serverDefs.replaceChild(
                                  "/suite1/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
                              "Expected success " << errorMsg);
    }
}

BOOST_AUTO_TEST_CASE(test_replace_child_errors_3) {
    ECF_NAME_THIS_TEST();

    // test force option

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        suite->addTask(Task::create("t2"));
        clientDef->addSuite(suite);
    }

    Defs serverDefs;
    {
        suite_ptr suite = Suite::create("suite1");
        family_ptr fam  = Family::create("family");
        fam->addTask(Task::create("t1"));
        suite->addFamily(fam);
        task_ptr t2 = Task::create("t2");
        suite->addTask(t2);
        serverDefs.addSuite(suite);

        t2->set_state(NState::ACTIVE); // Must be done after parent has been setup
    }

    std::string errorMsg;
    {
        ExpectNoChange expect_no_change;
        BOOST_REQUIRE_MESSAGE(!serverDefs.replaceChild(
                                  "/suite1/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg),
                              "Expected failure since server task t2 is active, and force not used");
    }
    {
        errorMsg.clear();
        ExpectStateChange expect_state_change;
        BOOST_REQUIRE_MESSAGE(
            serverDefs.replaceChild("/suite1/t2", clientDef, true /*create nodes as needed*/, true /*force*/, errorMsg),
            errorMsg);
    }
}

BOOST_AUTO_TEST_CASE(test_replace_add_task_with_bad_trigger) {
    ECF_NAME_THIS_TEST();

    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        task_ptr task   = fam->add_task("t2");
        task->add_trigger("txx eq complete");
    }

    // add Child t2 to the server defs
    Defs serverDefs;
    {
        suite_ptr suite = serverDefs.add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
    }

    // Expect to pass, since the replace part is ok.
    std::string errorMsg;
    node_ptr replaced_node;
    {
        ExpectStateChange expect_state_change;
        replaced_node = serverDefs.replaceChild(
            "/suite1/family/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg);
        BOOST_REQUIRE_MESSAGE(replaced_node, "Expected replaced to succeed, even though triggers are dodgy");
    }

    // Although we have change the data model, Check if the trigger expressions are still valid. Should fail
    std::string warning_msg;
    BOOST_REQUIRE_MESSAGE(!replaced_node->suite()->check(errorMsg, warning_msg), "Expected failure " << errorMsg);
}

BOOST_AUTO_TEST_CASE(test_replace_add_suite_with_bad_triggers) {
    ECF_NAME_THIS_TEST();

    // The whole suite should get *MOVED* from clientDef to the *EMPTY* server def. i.e add
    defs_ptr clientDef = Defs::create();
    {
        suite_ptr suite = clientDef->add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        fam->add_task("t1");
        fam->add_task("t2");
        task_ptr t3 = fam->add_task("t3");
        t3->add_trigger("txxxxx eq complete");
    }

    // Server defs is empty
    Defs serverDefs;

    // Expect to pass, since the replace part is ok.
    std::string errorMsg;
    node_ptr replaced_node;
    {
        ExpectModifyChange expect_state_change;
        replaced_node = serverDefs.replaceChild(
            "/suite1/family/t2", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg);
        BOOST_REQUIRE_MESSAGE(replaced_node, "Expected replaced to succeed, even though triggers are dodgy");
    }

    // Although we have changed the data model, Check if the trigger expressions are still valid. Should fail.
    std::string warning_msg;
    BOOST_REQUIRE_MESSAGE(!replaced_node->suite()->check(errorMsg, warning_msg), "Expected failure " << errorMsg);

    // reset, to avoid effecting downstream tests
    Ecf::set_state_change_no(0);
    Ecf::set_modify_change_no(0);
}

BOOST_AUTO_TEST_CASE(test_replace_task_ECFLOW_1135) {
    ECF_NAME_THIS_TEST();

    // The whole suite should get *MOVED* from clientDef to the *EMPTY* server def. i.e add
    defs_ptr clientDef = Defs::create();
    {
        clientDef->add_suite("o")
            ->add_family("main")
            ->add_family("00")
            ->add_family("an")
            ->add_family("4dvar")
            ->add_task("vardata");
    }

    // Server
    Defs serverDefs;
    { serverDefs.add_suite("o")->add_family("main")->add_family("00")->add_family("an")->add_task("4dvar"); }

    // Expect to fail *IF*  irrespective of 'create nodes as needed' flag, since path in the server is a *TASK*
    {
        std::string errorMsg;
        ExpectNoChange expect_no_change;
        node_ptr replaced_node = serverDefs.replaceChild(
            "/o/main/00/an/4dvar/vardata", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg);
        BOOST_REQUIRE_MESSAGE(!replaced_node, "Expected replace to fail");
        BOOST_REQUIRE_MESSAGE(!errorMsg.empty(), "Expected error message");
    }
    {
        std::string errorMsg;
        ExpectNoChange expect_no_change;
        node_ptr replaced_node = serverDefs.replaceChild(
            "/o/main/00/an/4dvar/vardata", clientDef, false /*create nodes as needed*/, false /*force*/, errorMsg);
        BOOST_REQUIRE_MESSAGE(!replaced_node, "Expected replace to fail");
        BOOST_REQUIRE_MESSAGE(!errorMsg.empty(), "Expected error message");
    }
}

BOOST_AUTO_TEST_CASE(test_trigger_references_during_replace) {
    ECF_NAME_THIS_TEST(); // ECFLOW-1319

    // This is used to check that the trigger references in AST, are invalidated after replace.

    Defs serverDefs;
    suite_ptr server_suite;
    {
        server_suite = serverDefs.add_suite("suite");
        server_suite->addVariable(Variable(ecf::environment::ECF_INCLUDE, "$ECF_HOME/../includes"));
        server_suite->addVariable(Variable("SLEEPTIME", "1"));
        server_suite->addVariable(Variable("ECF_CLIENT_EXE_PATH", "a/made/up/path"));
        family_ptr fam = server_suite->add_family("family");
        fam->add_task("t1")->add_trigger("/suite1/family/suite1_task1 == complete");
        fam->add_task("t2")->add_trigger("/suite1/family/suite1_task2 == complete");
        fam->add_task("t3")->add_trigger("/suite1/family/suite1_task3 == complete");
    }
    task_ptr suite1_task1, suite1_task2, suite1_task3;
    {
        suite_ptr suite = serverDefs.add_suite("suite1");
        family_ptr fam  = suite->add_family("family");
        suite1_task1    = fam->add_task("suite1_task1");
        suite1_task2    = fam->add_task("suite1_task2");
        suite1_task3    = fam->add_task("suite1_task3");
    }

    // Override ECF_HOME. ECF_HOME is need to locate to the .ecf files
    std::string ecf_home = File::test_data("libs/node/test/data/SMSHOME", "libs/node");
    serverDefs.server_state().add_or_update_user_variables(ecf::environment::ECF_HOME, ecf_home);

    /// begin , will cause creation of generated variables. The generated variables
    /// are use in client scripts and used to locate the ecf files
    serverDefs.beginAll();

    suite1_task1->set_state(NState::COMPLETE);
    suite1_task2->set_state(NState::COMPLETE);
    suite1_task3->set_state(NState::COMPLETE);

    // We need JOB generation to *FORCE* the creation of the trigger AST, and hence references
    {
        JobsParam jobsParam(true /*create jobs*/); // spawn_jobs = false
        Jobs jobs(&serverDefs);
        jobs.generate(jobsParam);
        BOOST_REQUIRE_MESSAGE(jobsParam.submitted().size() == 3,
                              "expected 3 jobs but found " << jobsParam.submitted().size() << "\n"
                                                           << jobsParam.errorMsg());
    }

    {
        // Now replace the suite1/family thereby, invalidating the trigger reference on suite/family/t1,t2,t3
        defs_ptr clientDef = Defs::create();
        suite_ptr suite    = clientDef->add_suite("suite1");
        family_ptr fam     = suite->add_family("family");
        fam->add_task("suite1_task1");
        fam->add_task("suite1_task2");
        fam->add_task("suite1_task3");
        fam->add_task("dummy");

        std::string errorMsg;
        serverDefs.replaceChild(
            "/suite1/family", clientDef, true /*create nodes as needed*/, false /*force*/, errorMsg);
        BOOST_REQUIRE_MESSAGE(errorMsg.empty(), "Expected no message " << errorMsg);
    }
    {
        std::vector<Node*> all_server_nodes;
        serverDefs.getAllNodes(all_server_nodes);

        // Now check the Trigger reference. The old reference to nodes in the trigger expressions should have been
        // removed
        std::vector<Task*> theTasks;
        server_suite->getAllTasks(theTasks);
        BOOST_REQUIRE_MESSAGE(theTasks.size() == 3, "Expected 3 tasks but found, " << theTasks.size());
        for (auto& theTask : theTasks) {

            std::set<Node*> referenced_nodes;
            theTask->getAllAstNodes(referenced_nodes);
            BOOST_REQUIRE_MESSAGE(referenced_nodes.size() == 1, " expected 1 referenced node");

            // The reference nodes must exist in the server. Otherwise replace has still kept references to old nodes in
            // the triggers
            bool found_reference = false;
            for (auto& all_server_node : all_server_nodes) {
                if (all_server_node == (*referenced_nodes.begin())) {
                    found_reference = true;
                    break;
                }
            }
            BOOST_CHECK_MESSAGE(found_reference,
                                "Could not find trigger reference " << (*referenced_nodes.begin())->absNodePath()
                                                                    << " in the server");
        }
    }

    // reset, to avoid effecting downstream tests
    Ecf::set_state_change_no(0);
    Ecf::set_modify_change_no(0);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()