ticket47653MMR_test.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. '''
  2. Created on Nov 7, 2013
  3. @author: tbordaz
  4. '''
  5. import os
  6. import sys
  7. import time
  8. import ldap
  9. import logging
  10. import pytest
  11. from lib389 import DirSrv, Entry, tools
  12. from lib389.tools import DirSrvTools
  13. from lib389._constants import *
  14. from lib389.properties import *
  15. logging.getLogger(__name__).setLevel(logging.DEBUG)
  16. log = logging.getLogger(__name__)
  17. #
  18. # important part. We can deploy Master1 and Master2 on different versions
  19. #
  20. installation1_prefix = None
  21. installation2_prefix = None
  22. TEST_REPL_DN = "cn=test_repl, %s" % SUFFIX
  23. OC_NAME = 'OCticket47653'
  24. MUST = "(postalAddress $ postalCode)"
  25. MAY = "(member $ street)"
  26. OTHER_NAME = 'other_entry'
  27. MAX_OTHERS = 10
  28. BIND_NAME = 'bind_entry'
  29. BIND_DN = 'cn=%s, %s' % (BIND_NAME, SUFFIX)
  30. BIND_PW = 'password'
  31. ENTRY_NAME = 'test_entry'
  32. ENTRY_DN = 'cn=%s, %s' % (ENTRY_NAME, SUFFIX)
  33. ENTRY_OC = "top person %s" % OC_NAME
  34. def _oc_definition(oid_ext, name, must=None, may=None):
  35. oid = "1.2.3.4.5.6.7.8.9.10.%d" % oid_ext
  36. desc = 'To test ticket 47490'
  37. sup = 'person'
  38. if not must:
  39. must = MUST
  40. if not may:
  41. may = MAY
  42. new_oc = "( %s NAME '%s' DESC '%s' SUP %s AUXILIARY MUST %s MAY %s )" % (oid, name, desc, sup, must, may)
  43. return new_oc
  44. class TopologyMaster1Master2(object):
  45. def __init__(self, master1, master2):
  46. master1.open()
  47. self.master1 = master1
  48. master2.open()
  49. self.master2 = master2
  50. @pytest.fixture(scope="module")
  51. def topology(request):
  52. '''
  53. This fixture is used to create a replicated topology for the 'module'.
  54. The replicated topology is MASTER1 <-> Master2.
  55. '''
  56. global installation1_prefix
  57. global installation2_prefix
  58. # allocate master1 on a given deployement
  59. master1 = DirSrv(verbose=False)
  60. if installation1_prefix:
  61. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  62. # Args for the master1 instance
  63. args_instance[SER_HOST] = HOST_MASTER_1
  64. args_instance[SER_PORT] = PORT_MASTER_1
  65. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_1
  66. args_master = args_instance.copy()
  67. master1.allocate(args_master)
  68. # allocate master1 on a given deployement
  69. master2 = DirSrv(verbose=False)
  70. if installation2_prefix:
  71. args_instance[SER_DEPLOYED_DIR] = installation2_prefix
  72. # Args for the consumer instance
  73. args_instance[SER_HOST] = HOST_MASTER_2
  74. args_instance[SER_PORT] = PORT_MASTER_2
  75. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_2
  76. args_master = args_instance.copy()
  77. master2.allocate(args_master)
  78. # Get the status of the instance and restart it if it exists
  79. instance_master1 = master1.exists()
  80. instance_master2 = master2.exists()
  81. # Remove all the instances
  82. if instance_master1:
  83. master1.delete()
  84. if instance_master2:
  85. master2.delete()
  86. # Create the instances
  87. master1.create()
  88. master1.open()
  89. master2.create()
  90. master2.open()
  91. #
  92. # Now prepare the Master-Consumer topology
  93. #
  94. # First Enable replication
  95. master1.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_1)
  96. master2.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_2)
  97. # Initialize the supplier->consumer
  98. properties = {RA_NAME: r'meTo_$host:$port',
  99. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  100. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  101. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  102. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  103. repl_agreement = master1.agreement.create(suffix=SUFFIX, host=master2.host, port=master2.port, properties=properties)
  104. if not repl_agreement:
  105. log.fatal("Fail to create a replica agreement")
  106. sys.exit(1)
  107. log.debug("%s created" % repl_agreement)
  108. properties = {RA_NAME: r'meTo_$host:$port',
  109. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  110. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  111. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  112. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  113. master2.agreement.create(suffix=SUFFIX, host=master1.host, port=master1.port, properties=properties)
  114. master1.agreement.init(SUFFIX, HOST_MASTER_2, PORT_MASTER_2)
  115. master1.waitForReplInit(repl_agreement)
  116. # Check replication is working fine
  117. if master1.testReplication(DEFAULT_SUFFIX, master2):
  118. log.info('Replication is working.')
  119. else:
  120. log.fatal('Replication is not working.')
  121. assert False
  122. # clear the tmp directory
  123. master1.clearTmpDir(__file__)
  124. # Here we have two instances master and consumer
  125. # with replication working.
  126. return TopologyMaster1Master2(master1, master2)
  127. def test_ticket47653_init(topology):
  128. """
  129. It adds
  130. - Objectclass with MAY 'member'
  131. - an entry ('bind_entry') with which we bind to test the 'SELFDN' operation
  132. It deletes the anonymous aci
  133. """
  134. topology.master1.log.info("Add %s that allows 'member' attribute" % OC_NAME)
  135. new_oc = _oc_definition(2, OC_NAME, must=MUST, may=MAY)
  136. topology.master1.schema.add_schema('objectClasses', new_oc)
  137. # entry used to bind with
  138. topology.master1.log.info("Add %s" % BIND_DN)
  139. topology.master1.add_s(Entry((BIND_DN, {
  140. 'objectclass': "top person".split(),
  141. 'sn': BIND_NAME,
  142. 'cn': BIND_NAME,
  143. 'userpassword': BIND_PW})))
  144. # enable acl error logging
  145. mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', str(128 + 8192))] # ACL + REPL
  146. topology.master1.modify_s(DN_CONFIG, mod)
  147. topology.master2.modify_s(DN_CONFIG, mod)
  148. # get read of anonymous ACI for use 'read-search' aci in SEARCH test
  149. ACI_ANONYMOUS = "(targetattr!=\"userPassword\")(version 3.0; acl \"Enable anonymous access\"; allow (read, search, compare) userdn=\"ldap:///anyone\";)"
  150. mod = [(ldap.MOD_DELETE, 'aci', ACI_ANONYMOUS)]
  151. topology.master1.modify_s(SUFFIX, mod)
  152. topology.master2.modify_s(SUFFIX, mod)
  153. # add dummy entries
  154. for cpt in range(MAX_OTHERS):
  155. name = "%s%d" % (OTHER_NAME, cpt)
  156. topology.master1.add_s(Entry(("cn=%s,%s" % (name, SUFFIX), {
  157. 'objectclass': "top person".split(),
  158. 'sn': name,
  159. 'cn': name})))
  160. def test_ticket47653_add(topology):
  161. '''
  162. This test ADD an entry on MASTER1 where 47653 is fixed. Then it checks that entry is replicated
  163. on MASTER2 (even if on MASTER2 47653 is NOT fixed). Then update on MASTER2 and check the update on MASTER1
  164. It checks that, bound as bind_entry,
  165. - we can not ADD an entry without the proper SELFDN aci.
  166. - with the proper ACI we can not ADD with 'member' attribute
  167. - with the proper ACI and 'member' it succeeds to ADD
  168. '''
  169. topology.master1.log.info("\n\n######################### ADD ######################\n")
  170. # bind as bind_entry
  171. topology.master1.log.info("Bind as %s" % BIND_DN)
  172. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  173. # Prepare the entry with multivalued members
  174. entry_with_members = Entry(ENTRY_DN)
  175. entry_with_members.setValues('objectclass', 'top', 'person', 'OCticket47653')
  176. entry_with_members.setValues('sn', ENTRY_NAME)
  177. entry_with_members.setValues('cn', ENTRY_NAME)
  178. entry_with_members.setValues('postalAddress', 'here')
  179. entry_with_members.setValues('postalCode', '1234')
  180. members = []
  181. for cpt in range(MAX_OTHERS):
  182. name = "%s%d" % (OTHER_NAME, cpt)
  183. members.append("cn=%s,%s" % (name, SUFFIX))
  184. members.append(BIND_DN)
  185. entry_with_members.setValues('member', members)
  186. # Prepare the entry with only one member value
  187. entry_with_member = Entry(ENTRY_DN)
  188. entry_with_member.setValues('objectclass', 'top', 'person', 'OCticket47653')
  189. entry_with_member.setValues('sn', ENTRY_NAME)
  190. entry_with_member.setValues('cn', ENTRY_NAME)
  191. entry_with_member.setValues('postalAddress', 'here')
  192. entry_with_member.setValues('postalCode', '1234')
  193. member = []
  194. member.append(BIND_DN)
  195. entry_with_member.setValues('member', member)
  196. # entry to add WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
  197. try:
  198. topology.master1.log.info("Try to add Add %s (aci is missing): %r" % (ENTRY_DN, entry_with_member))
  199. topology.master1.add_s(entry_with_member)
  200. except Exception as e:
  201. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  202. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  203. # Ok Now add the proper ACI
  204. topology.master1.log.info("Bind as %s and add the ADD SELFDN aci" % DN_DM)
  205. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  206. ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
  207. ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
  208. ACI_ALLOW = "(version 3.0; acl \"SelfDN add\"; allow (add)"
  209. ACI_SUBJECT = " userattr = \"member#selfDN\";)"
  210. ACI_BODY = ACI_TARGET + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
  211. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  212. topology.master1.modify_s(SUFFIX, mod)
  213. time.sleep(1)
  214. # bind as bind_entry
  215. topology.master1.log.info("Bind as %s" % BIND_DN)
  216. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  217. # entry to add WITHOUT member and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
  218. try:
  219. topology.master1.log.info("Try to add Add %s (member is missing)" % ENTRY_DN)
  220. topology.master1.add_s(Entry((ENTRY_DN, {
  221. 'objectclass': ENTRY_OC.split(),
  222. 'sn': ENTRY_NAME,
  223. 'cn': ENTRY_NAME,
  224. 'postalAddress': 'here',
  225. 'postalCode': '1234'})))
  226. except Exception as e:
  227. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  228. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  229. # entry to add WITH memberS and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
  230. # member should contain only one value
  231. try:
  232. topology.master1.log.info("Try to add Add %s (with several member values)" % ENTRY_DN)
  233. topology.master1.add_s(entry_with_members)
  234. except Exception as e:
  235. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  236. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  237. topology.master1.log.info("Try to add Add %s should be successful" % ENTRY_DN)
  238. try:
  239. topology.master1.add_s(entry_with_member)
  240. except ldap.LDAPError, e:
  241. topology.master1.log.info("Failed to add entry, error: " + e.message['desc'])
  242. assert False
  243. #
  244. # Now check the entry as been replicated
  245. #
  246. topology.master2.simple_bind_s(DN_DM, PASSWORD)
  247. topology.master1.log.info("Try to retrieve %s from Master2" % ENTRY_DN)
  248. loop = 0
  249. while loop <= 10:
  250. try:
  251. ent = topology.master2.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  252. break
  253. except ldap.NO_SUCH_OBJECT:
  254. time.sleep(1)
  255. loop += 1
  256. assert loop <= 10
  257. # Now update the entry on Master2 (as DM because 47653 is possibly not fixed on M2)
  258. topology.master1.log.info("Update %s on M2" % ENTRY_DN)
  259. mod = [(ldap.MOD_REPLACE, 'description', 'test_add')]
  260. topology.master2.modify_s(ENTRY_DN, mod)
  261. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  262. loop = 0
  263. while loop <= 10:
  264. try:
  265. ent = topology.master1.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  266. if ent.hasAttr('description') and (ent.getValue('description') == 'test_add'):
  267. break
  268. except ldap.NO_SUCH_OBJECT:
  269. time.sleep(1)
  270. loop += 1
  271. assert ent.getValue('description') == 'test_add'
  272. def test_ticket47653_modify(topology):
  273. '''
  274. This test MOD an entry on MASTER1 where 47653 is fixed. Then it checks that update is replicated
  275. on MASTER2 (even if on MASTER2 47653 is NOT fixed). Then update on MASTER2 (bound as BIND_DN).
  276. This update may fail whether or not 47653 is fixed on MASTER2
  277. It checks that, bound as bind_entry,
  278. - we can not modify an entry without the proper SELFDN aci.
  279. - adding the ACI, we can modify the entry
  280. '''
  281. # bind as bind_entry
  282. topology.master1.log.info("Bind as %s" % BIND_DN)
  283. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  284. topology.master1.log.info("\n\n######################### MODIFY ######################\n")
  285. # entry to modify WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
  286. try:
  287. topology.master1.log.info("Try to modify %s (aci is missing)" % ENTRY_DN)
  288. mod = [(ldap.MOD_REPLACE, 'postalCode', '9876')]
  289. topology.master1.modify_s(ENTRY_DN, mod)
  290. except Exception as e:
  291. topology.master1.log.info("Exception (expected): %s" % type(e).__name__)
  292. assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
  293. # Ok Now add the proper ACI
  294. topology.master1.log.info("Bind as %s and add the WRITE SELFDN aci" % DN_DM)
  295. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  296. ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
  297. ACI_TARGETATTR = "(targetattr = *)"
  298. ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
  299. ACI_ALLOW = "(version 3.0; acl \"SelfDN write\"; allow (write)"
  300. ACI_SUBJECT = " userattr = \"member#selfDN\";)"
  301. ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
  302. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  303. topology.master1.modify_s(SUFFIX, mod)
  304. time.sleep(1)
  305. # bind as bind_entry
  306. topology.master1.log.info("M1: Bind as %s" % BIND_DN)
  307. topology.master1.simple_bind_s(BIND_DN, BIND_PW)
  308. # modify the entry and checks the value
  309. topology.master1.log.info("M1: Try to modify %s. It should succeeds" % ENTRY_DN)
  310. mod = [(ldap.MOD_REPLACE, 'postalCode', '1928')]
  311. topology.master1.modify_s(ENTRY_DN, mod)
  312. topology.master1.log.info("M1: Bind as %s" % DN_DM)
  313. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  314. topology.master1.log.info("M1: Check the update of %s" % ENTRY_DN)
  315. ents = topology.master1.search_s(ENTRY_DN, ldap.SCOPE_BASE, 'objectclass=*')
  316. assert len(ents) == 1
  317. assert ents[0].postalCode == '1928'
  318. # Now check the update has been replicated on M2
  319. topology.master1.log.info("M2: Bind as %s" % DN_DM)
  320. topology.master2.simple_bind_s(DN_DM, PASSWORD)
  321. topology.master1.log.info("M2: Try to retrieve %s" % ENTRY_DN)
  322. loop = 0
  323. while loop <= 10:
  324. try:
  325. ent = topology.master2.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  326. if ent.hasAttr('postalCode') and (ent.getValue('postalCode') == '1928'):
  327. break
  328. except ldap.NO_SUCH_OBJECT:
  329. time.sleep(1)
  330. loop += 1
  331. assert loop <= 10
  332. assert ent.getValue('postalCode') == '1928'
  333. # Now update the entry on Master2 bound as BIND_DN (update may fail if 47653 is not fixed on M2)
  334. topology.master1.log.info("M2: Update %s (bound as %s)" % (ENTRY_DN, BIND_DN))
  335. topology.master2.simple_bind_s(BIND_DN, PASSWORD)
  336. fail = False
  337. try:
  338. mod = [(ldap.MOD_REPLACE, 'postalCode', '1929')]
  339. topology.master2.modify_s(ENTRY_DN, mod)
  340. fail = False
  341. except ldap.INSUFFICIENT_ACCESS:
  342. topology.master1.log.info("M2: Exception (INSUFFICIENT_ACCESS): that is fine the bug is possibly not fixed on M2")
  343. fail = True
  344. except Exception as e:
  345. topology.master1.log.info("M2: Exception (not expected): %s" % type(e).__name__)
  346. assert 0
  347. if not fail:
  348. # Check the update has been replicaed on M1
  349. topology.master1.log.info("M1: Bind as %s" % DN_DM)
  350. topology.master1.simple_bind_s(DN_DM, PASSWORD)
  351. topology.master1.log.info("M1: Check %s.postalCode=1929)" % (ENTRY_DN))
  352. loop = 0
  353. while loop <= 10:
  354. try:
  355. ent = topology.master1.getEntry(ENTRY_DN, ldap.SCOPE_BASE, "(objectclass=*)")
  356. if ent.hasAttr('postalCode') and (ent.getValue('postalCode') == '1929'):
  357. break
  358. except ldap.NO_SUCH_OBJECT:
  359. time.sleep(1)
  360. loop += 1
  361. assert ent.getValue('postalCode') == '1929'
  362. def test_ticket47653_final(topology):
  363. topology.master1.delete()
  364. topology.master2.delete()
  365. log.info('Testcase PASSED')
  366. def run_isolated():
  367. '''
  368. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  369. To run isolated without py.test, you need to
  370. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  371. - set the installation prefix
  372. - run this program
  373. '''
  374. global installation1_prefix
  375. global installation2_prefix
  376. installation1_prefix = None
  377. installation2_prefix = None
  378. topo = topology(True)
  379. test_ticket47653_init(topo)
  380. test_ticket47653_add(topo)
  381. test_ticket47653_modify(topo)
  382. test_ticket47653_final(topo)
  383. if __name__ == '__main__':
  384. run_isolated()