Debian Codegraph @

dirsrvtests/tests/suites/replication/regression_m2c1_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2024 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9
10 import os
11 import time
12 import logging
13 import ldap
14 import pytest
15 from test389.topologies import topology_m2c1 as topo_m2c1
16 from lib389._constants import DEFAULT_SUFFIX, DN_USERROOT_LDBM
17 from lib389.replica import Changelog5
18 from lib389.idm.user import TEST_USER_PROPERTIES, UserAccounts
19 from lib389.utils import ensure_bytes, ds_supports_new_changelog
20
21 pytestmark = pytest.mark.tier1
22
23 CHANGELOG = 'cn=changelog,{}'.format(DN_USERROOT_LDBM)
24 MAXAGE_ATTR = 'nsslapd-changelogmaxage'
25 MAXAGE_VALUE = '5s'
26 TRIMINTERVAL = 'nsslapd-changelogtrim-interval'
27 TRIMINTERVAL_VALUE = '15'
28 MAX_USERS = 10
29
30 DEBUGGING = os.getenv("DEBUGGING", default=False)
31 if DEBUGGING:
32 logging.getLogger(__name__).setLevel(logging.DEBUG)
33 else:
34 logging.getLogger(__name__).setLevel(logging.INFO)
35 log = logging.getLogger(__name__)
36
37
38 def set_value(supplier, attr, val):
39 """
40 Helper function to add/replace attr: val and check the added value
41 """
42 try:
43 supplier.modify_s(CHANGELOG, [(ldap.MOD_REPLACE, attr, ensure_bytes(val))])
44 except ldap.LDAPError as e:
45 log.error(f'Failed to add {attr}: {val} to {CHANGELOG}: error {str(e)}')
46 assert False
47
48
49 def setup_max_age(supplier):
50 """Configure logging and changelog max age
51 """
52 if ds_supports_new_changelog():
53 set_value(supplier, MAXAGE_ATTR, MAXAGE_VALUE)
54 set_value(supplier, TRIMINTERVAL, TRIMINTERVAL_VALUE)
55 else:
56 cl = Changelog5(supplier)
57 cl.set_max_age(MAXAGE_VALUE)
58 cl.set_trim_interval(TRIMINTERVAL_VALUE)
59
60
61 def test_changelog_trimming(topo_m2c1):
62 """Test changelog trimming with replication
63
64 :id: c0b357ed-07a4-4000-bbe0-2fbbf90232c9
65 :setup: 2 Suppliers, 1 Consumer
66 :steps:
67 1. Configure changelog trimming (maxage=5 and interval=15)
68 2. Creates updates on S1 and S2 (that will be also replicated to C1)
69 3. Wait for maxage so any previous updates is older than maxage
70 4. Pause (disable) the replica agreement S1->S2
71 5. Do updates on S1, and add a TEST_ENTRY on S1
72 6. Wait until we are sure trimming thread have completed
73 7. Resume (enable) the replica agreement S1->S2
74 8. Check that TEST_ENTRY is present on S1, S2 and C1
75 :expectedresults:
76 1. Changelog trimming should be configured successfully
77 2. Updates should be created and replicated
78 3. Wait should complete successfully
79 4. Agreement should be paused successfully
80 5. Updates and new entry should be added to S1
81 6. Wait should complete successfully
82 7. Agreement should be resumed successfully
83 8. TEST_ENTRY should be present on all instances
84 """
85
86 S1 = topo_m2c1.ms["supplier1"]
87 S2 = topo_m2c1.ms["supplier2"]
88 C1 = topo_m2c1.cs["consumer1"]
89
90 log.info("Configure changelog trimming")
91 for supplier in (S1, S2):
92 setup_max_age(supplier)
93
94 log.info("Create updates on S1 and S2")
95 users_s1 = UserAccounts(S1, DEFAULT_SUFFIX)
96 users_s2 = UserAccounts(S2, DEFAULT_SUFFIX)
97
98 for idx in range(1, MAX_USERS):
99 user_properties = TEST_USER_PROPERTIES.copy()
100 user_properties.update({'uid': f'user_{idx}'})
101 users_s1.create(properties=user_properties)
102
103 time.sleep(5)
104
105 for idx in range(1, MAX_USERS):
106 user = users_s2.get(f'user_{idx}')
107 user.replace('description', 'value from S2')
108
109 log.info("Wait for maxage")
110 time.sleep(int(MAXAGE_VALUE[:-1]))
111
112 log.info("Pause replica agreement S1->S2")
113 agreement_s1_s2 = S1.agreement.list(suffix=DEFAULT_SUFFIX, consumer_host=S2.host, consumer_port=S2.port)[0]
114 S1.agreement.pause(agreement_s1_s2.dn)
115
116 log.info("Do updates on S1 and add TEST_ENTRY")
117 for idx in range(1, MAX_USERS):
118 user = users_s1.get(f'user_{idx}')
119 user.replace('description', 'value from S1')
120
121 test_user_properties = TEST_USER_PROPERTIES.copy()
122 test_user_properties.update({'uid': 'last_user'})
123 test_user = users_s1.create(properties=test_user_properties)
124
125 log.info("Wait for trimming thread to complete")
126 time.sleep(int(TRIMINTERVAL_VALUE) + 5)
127
128 log.info("Resume replica agreement S1->S2")
129 S1.agreement.resume(agreement_s1_s2.dn)
130
131 log.info("Check TEST_ENTRY on all instances")
132 for instance in (S1, S2, C1):
133 success = 0
134 for i in range(0, 5):
135 users = UserAccounts(instance, DEFAULT_SUFFIX)
136 try:
137 user = users.get('last_user')
138 except ldap.NO_SUCH_OBJECT:
139 time.sleep(1)
140 continue
141 log.info(f"User 'last_user' found on {instance.serverid}")
142 success = 1
143 break
144
145 assert success, f"User 'last_user' not found on {instance.serverid}"
146
147
148 if __name__ == '__main__':
149 # Run isolated
150 # -s for DEBUG mode
151 CURRENT_FILE = os.path.realpath(__file__)
152 pytest.main(["-s", CURRENT_FILE])