File: ticket47823_test.py

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (965 lines) | stat: -rw-r--r-- 36,307 bytes parent folder | download | duplicates (3)
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
# --- BEGIN COPYRIGHT BLOCK ---
# Copyright (C) 2016 Red Hat, Inc.
# All rights reserved.
#
# License: GPL (version 3 or any later version).
# See LICENSE for details.
# --- END COPYRIGHT BLOCK ---
#
import logging
import re
import shutil
import subprocess
import time

import ldap
import pytest
from lib389 import Entry
from lib389._constants import *
from lib389.topologies import topology_st

log = logging.getLogger(__name__)
from lib389.utils import *

# Skip on older versions
pytestmark = [pytest.mark.tier2,
              pytest.mark.skipif(ds_is_older('1.3.3'), reason="Not implemented")]
PROVISIONING_CN = "provisioning"
PROVISIONING_DN = "cn=%s,%s" % (PROVISIONING_CN, SUFFIX)

ACTIVE_CN = "accounts"
STAGE_CN = "staged users"
DELETE_CN = "deleted users"
ACTIVE_DN = "cn=%s,%s" % (ACTIVE_CN, SUFFIX)
STAGE_DN = "cn=%s,%s" % (STAGE_CN, PROVISIONING_DN)
DELETE_DN = "cn=%s,%s" % (DELETE_CN, PROVISIONING_DN)

STAGE_USER_CN = "stage guy"
STAGE_USER_DN = "cn=%s,%s" % (STAGE_USER_CN, STAGE_DN)

ACTIVE_USER_CN = "active guy"
ACTIVE_USER_DN = "cn=%s,%s" % (ACTIVE_USER_CN, ACTIVE_DN)

ACTIVE_USER_1_CN = "test_1"
ACTIVE_USER_1_DN = "cn=%s,%s" % (ACTIVE_USER_1_CN, ACTIVE_DN)
ACTIVE_USER_2_CN = "test_2"
ACTIVE_USER_2_DN = "cn=%s,%s" % (ACTIVE_USER_2_CN, ACTIVE_DN)

STAGE_USER_1_CN = ACTIVE_USER_1_CN
STAGE_USER_1_DN = "cn=%s,%s" % (STAGE_USER_1_CN, STAGE_DN)
STAGE_USER_2_CN = ACTIVE_USER_2_CN
STAGE_USER_2_DN = "cn=%s,%s" % (STAGE_USER_2_CN, STAGE_DN)

ALL_CONFIG_ATTRS = ['nsslapd-pluginarg0', 'nsslapd-pluginarg1', 'nsslapd-pluginarg2',
                    'uniqueness-attribute-name', 'uniqueness-subtrees', 'uniqueness-across-all-subtrees']


def _header(topology_st, label):
    topology_st.standalone.log.info("\n\n###############################################")
    topology_st.standalone.log.info("#######")
    topology_st.standalone.log.info("####### %s" % label)
    topology_st.standalone.log.info("#######")
    topology_st.standalone.log.info("###############################################")


def _uniqueness_config_entry(topology_st, name=None):
    if not name:
        return None

    ent = topology_st.standalone.getEntry("cn=%s,%s" % (PLUGIN_ATTR_UNIQUENESS, DN_PLUGIN), ldap.SCOPE_BASE,
                                          "(objectclass=nsSlapdPlugin)",
                                          ['objectClass', 'cn', 'nsslapd-pluginPath', 'nsslapd-pluginInitfunc',
                                           'nsslapd-pluginType', 'nsslapd-pluginEnabled',
                                           'nsslapd-plugin-depends-on-type',
                                           'nsslapd-pluginId', 'nsslapd-pluginVersion', 'nsslapd-pluginVendor',
                                           'nsslapd-pluginDescription'])
    ent.dn = "cn=%s uniqueness,%s" % (name, DN_PLUGIN)
    return ent


def _build_config(topology_st, attr_name='cn', subtree_1=None, subtree_2=None, type_config='old',
                  across_subtrees=False):
    assert topology_st
    assert attr_name
    assert subtree_1

    if type_config == 'old':
        # enable the 'cn' uniqueness on Active
        config = _uniqueness_config_entry(topology_st, attr_name)
        config.setValue('nsslapd-pluginarg0', attr_name)
        config.setValue('nsslapd-pluginarg1', subtree_1)
        if subtree_2:
            config.setValue('nsslapd-pluginarg2', subtree_2)
    else:
        # prepare the config entry
        config = _uniqueness_config_entry(topology_st, attr_name)
        config.setValue('uniqueness-attribute-name', attr_name)
        config.setValue('uniqueness-subtrees', subtree_1)
        if subtree_2:
            config.setValue('uniqueness-subtrees', subtree_2)
        if across_subtrees:
            config.setValue('uniqueness-across-all-subtrees', 'on')
    return config


def _active_container_invalid_cfg_add(topology_st):
    '''
    Check uniqueness is not enforced with ADD (invalid config)
    '''
    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': ACTIVE_USER_1_CN})))

    topology_st.standalone.add_s(Entry((ACTIVE_USER_2_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_2_CN,
        'cn': [ACTIVE_USER_1_CN, ACTIVE_USER_2_CN]})))

    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)
    topology_st.standalone.delete_s(ACTIVE_USER_2_DN)


def _active_container_add(topology_st, type_config='old'):
    '''
    Check uniqueness in a single container (Active)
    Add an entry with a given 'cn', then check we can not add an entry with the same 'cn' value

    '''
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config=type_config,
                           across_subtrees=False)

    # remove the 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.restart(timeout=120)

    topology_st.standalone.log.info('Uniqueness not enforced: create the entries')

    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': ACTIVE_USER_1_CN})))

    topology_st.standalone.add_s(Entry((ACTIVE_USER_2_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_2_CN,
        'cn': [ACTIVE_USER_1_CN, ACTIVE_USER_2_CN]})))

    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)
    topology_st.standalone.delete_s(ACTIVE_USER_2_DN)

    topology_st.standalone.log.info('Uniqueness enforced: checks second entry is rejected')

    # enable the 'cn' uniqueness on Active
    topology_st.standalone.add_s(config)
    topology_st.standalone.restart(timeout=120)
    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': ACTIVE_USER_1_CN})))

    try:
        topology_st.standalone.add_s(Entry((ACTIVE_USER_2_DN, {
            'objectclass': "top person".split(),
            'sn': ACTIVE_USER_2_CN,
            'cn': [ACTIVE_USER_1_CN, ACTIVE_USER_2_CN]})))
    except ldap.CONSTRAINT_VIOLATION:
        # yes it is expected
        pass

    # cleanup the stuff now
    topology_st.standalone.delete_s(config.dn)
    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)


def _active_container_mod(topology_st, type_config='old'):
    '''
    Check uniqueness in a single container (active)
    Add and entry with a given 'cn', then check we can not modify an entry with the same 'cn' value

    '''

    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config=type_config,
                           across_subtrees=False)

    # enable the 'cn' uniqueness on Active
    topology_st.standalone.add_s(config)
    topology_st.standalone.restart(timeout=120)

    topology_st.standalone.log.info('Uniqueness enforced: checks MOD ADD entry is rejected')
    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': ACTIVE_USER_1_CN})))

    topology_st.standalone.add_s(Entry((ACTIVE_USER_2_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_2_CN,
        'cn': ACTIVE_USER_2_CN})))

    try:
        topology_st.standalone.modify_s(ACTIVE_USER_2_DN, [(ldap.MOD_ADD, 'cn', ensure_bytes(ACTIVE_USER_1_CN))])
    except ldap.CONSTRAINT_VIOLATION:
        # yes it is expected
        pass

    topology_st.standalone.log.info('Uniqueness enforced: checks MOD REPLACE entry is rejected')
    try:
        topology_st.standalone.modify_s(ACTIVE_USER_2_DN,
                                        [(ldap.MOD_REPLACE, 'cn', [ensure_bytes(ACTIVE_USER_1_CN), ensure_bytes(ACTIVE_USER_2_CN)])])
    except ldap.CONSTRAINT_VIOLATION:
        # yes it is expected
        pass

    # cleanup the stuff now
    topology_st.standalone.delete_s(config.dn)
    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)
    topology_st.standalone.delete_s(ACTIVE_USER_2_DN)


def _active_container_modrdn(topology_st, type_config='old'):
    '''
    Check uniqueness in a single container
    Add and entry with a given 'cn', then check we can not modrdn an entry with the same 'cn' value

    '''
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config=type_config,
                           across_subtrees=False)

    # enable the 'cn' uniqueness on Active
    topology_st.standalone.add_s(config)
    topology_st.standalone.restart(timeout=120)

    topology_st.standalone.log.info('Uniqueness enforced: checks MODRDN entry is rejected')

    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': [ACTIVE_USER_1_CN, 'dummy']})))

    topology_st.standalone.add_s(Entry((ACTIVE_USER_2_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_2_CN,
        'cn': ACTIVE_USER_2_CN})))

    try:
        topology_st.standalone.rename_s(ACTIVE_USER_2_DN, 'cn=dummy', delold=0)
    except ldap.CONSTRAINT_VIOLATION:
        # yes it is expected
        pass

    # cleanup the stuff now
    topology_st.standalone.delete_s(config.dn)
    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)
    topology_st.standalone.delete_s(ACTIVE_USER_2_DN)


def _active_stage_containers_add(topology_st, type_config='old', across_subtrees=False):
    '''
    Check uniqueness in several containers
    Add an entry on a container with a given 'cn'
    with across_subtrees=False check we CAN add an entry with the same 'cn' value on the other container
    with across_subtrees=True check we CAN NOT add an entry with the same 'cn' value on the other container

    '''
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=STAGE_DN,
                           type_config=type_config, across_subtrees=False)

    topology_st.standalone.add_s(config)
    topology_st.standalone.restart(timeout=120)
    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': ACTIVE_USER_1_CN})))
    try:

        # adding an entry on a separated contains with the same 'cn'
        topology_st.standalone.add_s(Entry((STAGE_USER_1_DN, {
            'objectclass': "top person".split(),
            'sn': STAGE_USER_1_CN,
            'cn': ACTIVE_USER_1_CN})))
    except ldap.CONSTRAINT_VIOLATION:
        assert across_subtrees

    # cleanup the stuff now
    topology_st.standalone.delete_s(config.dn)
    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)
    topology_st.standalone.delete_s(STAGE_USER_1_DN)


def _active_stage_containers_mod(topology_st, type_config='old', across_subtrees=False):
    '''
    Check uniqueness in a several containers
    Add an entry on a container with a given 'cn', then check we CAN mod an entry with the same 'cn' value on the other container

    '''
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=STAGE_DN,
                           type_config=type_config, across_subtrees=False)

    topology_st.standalone.add_s(config)
    topology_st.standalone.restart(timeout=120)
    # adding an entry on active with a different 'cn'
    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': ACTIVE_USER_2_CN})))

    # adding an entry on a stage with a different 'cn'
    topology_st.standalone.add_s(Entry((STAGE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': STAGE_USER_1_CN,
        'cn': STAGE_USER_1_CN})))

    try:

        # modify add same value
        topology_st.standalone.modify_s(STAGE_USER_1_DN, [(ldap.MOD_ADD, 'cn', [ensure_bytes(ACTIVE_USER_2_CN)])])
    except ldap.CONSTRAINT_VIOLATION:
        assert across_subtrees

    topology_st.standalone.delete_s(STAGE_USER_1_DN)
    topology_st.standalone.add_s(Entry((STAGE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': STAGE_USER_1_CN,
        'cn': STAGE_USER_2_CN})))
    try:
        # modify replace same value
        topology_st.standalone.modify_s(STAGE_USER_1_DN,
                                        [(ldap.MOD_REPLACE, 'cn', [ensure_bytes(STAGE_USER_2_CN), ensure_bytes(ACTIVE_USER_1_CN)])])
    except ldap.CONSTRAINT_VIOLATION:
        assert across_subtrees

    # cleanup the stuff now
    topology_st.standalone.delete_s(config.dn)
    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)
    topology_st.standalone.delete_s(STAGE_USER_1_DN)


def _active_stage_containers_modrdn(topology_st, type_config='old', across_subtrees=False):
    '''
    Check uniqueness in a several containers
    Add and entry with a given 'cn', then check we CAN modrdn an entry with the same 'cn' value on the other container

    '''

    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=STAGE_DN,
                           type_config=type_config, across_subtrees=False)

    # enable the 'cn' uniqueness on Active and Stage
    topology_st.standalone.add_s(config)
    topology_st.standalone.restart(timeout=120)
    topology_st.standalone.add_s(Entry((ACTIVE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': ACTIVE_USER_1_CN,
        'cn': [ACTIVE_USER_1_CN, 'dummy']})))

    topology_st.standalone.add_s(Entry((STAGE_USER_1_DN, {
        'objectclass': "top person".split(),
        'sn': STAGE_USER_1_CN,
        'cn': STAGE_USER_1_CN})))

    try:

        topology_st.standalone.rename_s(STAGE_USER_1_DN, 'cn=dummy', delold=0)

        # check stage entry has 'cn=dummy'
        stage_ent = topology_st.standalone.getEntry("cn=dummy,%s" % (STAGE_DN), ldap.SCOPE_BASE, "objectclass=*",
                                                    ['cn'])
        assert stage_ent.hasAttr('cn')
        found = False
        for value in stage_ent.getValues('cn'):
            if ensure_str(value) == 'dummy':
                found = True
        assert found

        # check active entry has 'cn=dummy'
        active_ent = topology_st.standalone.getEntry(ACTIVE_USER_1_DN, ldap.SCOPE_BASE, "objectclass=*", ['cn'])
        assert active_ent.hasAttr('cn')
        found = False
        for value in stage_ent.getValues('cn'):
            if ensure_str(value) == 'dummy':
                found = True
        assert found

        topology_st.standalone.delete_s("cn=dummy,%s" % (STAGE_DN))
    except ldap.CONSTRAINT_VIOLATION:
        assert across_subtrees
        topology_st.standalone.delete_s(STAGE_USER_1_DN)

    # cleanup the stuff now
    topology_st.standalone.delete_s(config.dn)
    topology_st.standalone.delete_s(ACTIVE_USER_1_DN)


def _config_file(topology_st, action='save'):
    dse_ldif = topology_st.standalone.confdir + '/dse.ldif'
    sav_file = topology_st.standalone.confdir + '/dse.ldif.ticket47823'
    if action == 'save':
        shutil.copy(dse_ldif, sav_file)
    else:
        shutil.copy(sav_file, dse_ldif)
    time.sleep(1)


def _pattern_errorlog(file, log_pattern):
    try:
        _pattern_errorlog.last_pos += 1
    except AttributeError:
        _pattern_errorlog.last_pos = 0

    found = None
    log.debug("_pattern_errorlog: start at offset %d" % _pattern_errorlog.last_pos)
    file.seek(_pattern_errorlog.last_pos)

    # Use a while true iteration because 'for line in file: hit a
    # python bug that break file.tell()
    while True:
        line = file.readline()
        log.debug("_pattern_errorlog: [%d] %s" % (file.tell(), line))
        found = log_pattern.search(line)
        if ((line == '') or (found)):
            break

    log.debug("_pattern_errorlog: end at offset %d" % file.tell())
    _pattern_errorlog.last_pos = file.tell()
    return found


def test_ticket47823_init(topology_st):
    """

    """

    # Enabled the plugins
    topology_st.standalone.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
    topology_st.standalone.restart(timeout=120)

    topology_st.standalone.add_s(Entry((PROVISIONING_DN, {'objectclass': "top nscontainer".split(),
                                                          'cn': PROVISIONING_CN})))
    topology_st.standalone.add_s(Entry((ACTIVE_DN, {'objectclass': "top nscontainer".split(),
                                                    'cn': ACTIVE_CN})))
    topology_st.standalone.add_s(Entry((STAGE_DN, {'objectclass': "top nscontainer".split(),
                                                   'cn': STAGE_CN})))
    topology_st.standalone.add_s(Entry((DELETE_DN, {'objectclass': "top nscontainer".split(),
                                                    'cn': DELETE_CN})))
    topology_st.standalone.errorlog_file = open(topology_st.standalone.errlog, "r")

    topology_st.standalone.stop(timeout=120)
    time.sleep(1)
    topology_st.standalone.start(timeout=120)
    time.sleep(3)


def test_ticket47823_one_container_add(topology_st):
    '''
    Check uniqueness in a single container
    Add and entry with a given 'cn', then check we can not add an entry with the same 'cn' value

    '''
    _header(topology_st, "With former config (args), check attribute uniqueness with 'cn' (ADD) ")

    _active_container_add(topology_st, type_config='old')

    _header(topology_st, "With new config (args), check attribute uniqueness with 'cn' (ADD) ")

    _active_container_add(topology_st, type_config='new')


def test_ticket47823_one_container_mod(topology_st):
    '''
    Check uniqueness in a single container
    Add and entry with a given 'cn', then check we can not modify an entry with the same 'cn' value

    '''
    _header(topology_st, "With former config (args), check attribute uniqueness with 'cn' (MOD)")

    _active_container_mod(topology_st, type_config='old')

    _header(topology_st, "With new config (args), check attribute uniqueness with 'cn' (MOD)")

    _active_container_mod(topology_st, type_config='new')


def test_ticket47823_one_container_modrdn(topology_st):
    '''
    Check uniqueness in a single container
    Add and entry with a given 'cn', then check we can not modrdn an entry with the same 'cn' value

    '''
    _header(topology_st, "With former config (args), check attribute uniqueness with 'cn' (MODRDN)")

    _active_container_modrdn(topology_st, type_config='old')

    _header(topology_st, "With former config (args), check attribute uniqueness with 'cn' (MODRDN)")

    _active_container_modrdn(topology_st, type_config='new')


def test_ticket47823_multi_containers_add(topology_st):
    '''
    Check uniqueness in a several containers
    Add and entry with a given 'cn', then check we can not add an entry with the same 'cn' value

    '''
    _header(topology_st, "With former config (args), check attribute uniqueness with 'cn' (ADD) ")

    _active_stage_containers_add(topology_st, type_config='old', across_subtrees=False)

    _header(topology_st, "With new config (args), check attribute uniqueness with 'cn' (ADD) ")

    _active_stage_containers_add(topology_st, type_config='new', across_subtrees=False)


def test_ticket47823_multi_containers_mod(topology_st):
    '''
    Check uniqueness in a several containers
    Add an entry on a container with a given 'cn', then check we CAN mod an entry with the same 'cn' value on the other container

    '''
    _header(topology_st, "With former config (args), check attribute uniqueness with 'cn' (MOD) on separated container")

    topology_st.standalone.log.info(
        'Uniqueness not enforced: if same \'cn\' modified (add/replace) on separated containers')
    _active_stage_containers_mod(topology_st, type_config='old', across_subtrees=False)

    _header(topology_st, "With new config (args), check attribute uniqueness with 'cn' (MOD) on separated container")

    topology_st.standalone.log.info(
        'Uniqueness not enforced: if same \'cn\' modified (add/replace) on separated containers')
    _active_stage_containers_mod(topology_st, type_config='new', across_subtrees=False)


def test_ticket47823_multi_containers_modrdn(topology_st):
    '''
    Check uniqueness in a several containers
    Add and entry with a given 'cn', then check we CAN modrdn an entry with the same 'cn' value on the other container

    '''
    _header(topology_st,
            "With former config (args), check attribute uniqueness with 'cn' (MODRDN) on separated containers")

    topology_st.standalone.log.info('Uniqueness not enforced: checks MODRDN entry is accepted on separated containers')
    _active_stage_containers_modrdn(topology_st, type_config='old', across_subtrees=False)

    topology_st.standalone.log.info('Uniqueness not enforced: checks MODRDN entry is accepted on separated containers')
    _active_stage_containers_modrdn(topology_st, type_config='old')


def test_ticket47823_across_multi_containers_add(topology_st):
    '''
    Check uniqueness across several containers, uniquely with the new configuration
    Add and entry with a given 'cn', then check we can not add an entry with the same 'cn' value

    '''
    _header(topology_st, "With new config (args), check attribute uniqueness with 'cn' (ADD) across several containers")

    _active_stage_containers_add(topology_st, type_config='old', across_subtrees=True)


def test_ticket47823_across_multi_containers_mod(topology_st):
    '''
    Check uniqueness across several containers, uniquely with the new configuration
    Add and entry with a given 'cn', then check we can not modifiy an entry with the same 'cn' value

    '''
    _header(topology_st, "With new config (args), check attribute uniqueness with 'cn' (MOD) across several containers")

    _active_stage_containers_mod(topology_st, type_config='old', across_subtrees=True)


def test_ticket47823_across_multi_containers_modrdn(topology_st):
    '''
    Check uniqueness across several containers, uniquely with the new configuration
    Add and entry with a given 'cn', then check we can not modrdn an entry with the same 'cn' value

    '''
    _header(topology_st,
            "With new config (args), check attribute uniqueness with 'cn' (MODRDN) across several containers")

    _active_stage_containers_modrdn(topology_st, type_config='old', across_subtrees=True)


def test_ticket47823_invalid_config_1(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using old config: arg0 is missing
    '''
    _header(topology_st, "Invalid config (old): arg0 is missing")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config='old',
                           across_subtrees=False)

    del config.data['nsslapd-pluginarg0']
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', b'65536')])
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("[U|u]nable to parse old style")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


def test_ticket47823_invalid_config_2(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using old config: arg1 is missing
    '''
    _header(topology_st, "Invalid config (old): arg1 is missing")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config='old',
                           across_subtrees=False)

    del config.data['nsslapd-pluginarg1']
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("No valid subtree is defined")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


def test_ticket47823_invalid_config_3(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using old config: arg0 is missing
    '''
    _header(topology_st, "Invalid config (old): arg0 is missing but new config attrname exists")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config='old',
                           across_subtrees=False)

    del config.data['nsslapd-pluginarg0']
    config.data['uniqueness-attribute-name'] = 'cn'
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', b'65536')])
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("[U|u]nable to parse old style")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


def test_ticket47823_invalid_config_4(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using old config: arg1 is missing
    '''
    _header(topology_st, "Invalid config (old): arg1 is missing but new config exist")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config='old',
                           across_subtrees=False)

    del config.data['nsslapd-pluginarg1']
    config.data['uniqueness-subtrees'] = ACTIVE_DN
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("No valid subtree is defined")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


def test_ticket47823_invalid_config_5(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using new config: uniqueness-attribute-name is missing
    '''
    _header(topology_st, "Invalid config (new): uniqueness-attribute-name is missing")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config='new',
                           across_subtrees=False)

    del config.data['uniqueness-attribute-name']
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("[A|a]ttribute name not defined")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


def test_ticket47823_invalid_config_6(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using new config: uniqueness-subtrees is missing
    '''
    _header(topology_st, "Invalid config (new): uniqueness-subtrees is missing")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1=ACTIVE_DN, subtree_2=None, type_config='new',
                           across_subtrees=False)

    del config.data['uniqueness-subtrees']
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("[O|o]bjectclass for subtree entries is not defined")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


def test_ticket47823_invalid_config_7(topology_st):
    '''
    Check that an invalid config is detected. No uniqueness enforced
    Using new config: uniqueness-subtrees is missing
    '''
    _header(topology_st, "Invalid config (new): uniqueness-subtrees are invalid")

    _config_file(topology_st, action='save')

    # create an invalid config without arg0
    config = _build_config(topology_st, attr_name='cn', subtree_1="this_is dummy DN", subtree_2="an other=dummy DN",
                           type_config='new', across_subtrees=False)

    topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', b'65536')])
    # replace 'cn' uniqueness entry
    try:
        topology_st.standalone.delete_s(config.dn)

    except ldap.NO_SUCH_OBJECT:
        pass
    topology_st.standalone.add_s(config)

    topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)

    # Check the server did not restart
    try:
        topology_st.standalone.restart()
        ent = topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)",
                                              ALL_CONFIG_ATTRS)
        if ent:
            # be sure to restore a valid config before assert
            _config_file(topology_st, action='restore')
        assert not ent
    except:
        pass

    # Check the expected error message
    regex = re.compile("No valid subtree is defined")
    res = _pattern_errorlog(topology_st.standalone.errorlog_file, regex)
    if not res:
        # be sure to restore a valid config before assert
        _config_file(topology_st, action='restore')
    assert res

    # Check we can restart the server
    _config_file(topology_st, action='restore')
    topology_st.standalone.start()
    try:
        topology_st.standalone.getEntry(config.dn, ldap.SCOPE_BASE, "(objectclass=nsSlapdPlugin)", ALL_CONFIG_ATTRS)
    except ldap.NO_SUCH_OBJECT:
        pass


if __name__ == '__main__':
    # Run isolated
    # -s for DEBUG mode
    CURRENT_FILE = os.path.realpath(__file__)
    pytest.main("-s %s" % CURRENT_FILE)