ticket48956_test.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import pytest
  7. import subprocess
  8. from lib389 import DirSrv, Entry, tools, tasks
  9. from lib389.tools import DirSrvTools
  10. from lib389._constants import *
  11. from lib389.properties import *
  12. from lib389.tasks import *
  13. from lib389.utils import *
  14. DEBUGGING = False
  15. RDN_LONG_SUFFIX = 'this'
  16. LONG_SUFFIX = "dc=%s,dc=is,dc=a,dc=very,dc=long,dc=suffix,dc=so,dc=long,dc=suffix,dc=extremely,dc=long,dc=suffix" % RDN_LONG_SUFFIX
  17. LONG_SUFFIX_BE = 'ticket48956'
  18. ACCT_POLICY_PLUGIN_DN = 'cn=%s,cn=plugins,cn=config' % PLUGIN_ACCT_POLICY
  19. ACCT_POLICY_CONFIG_DN = 'cn=config,%s' % ACCT_POLICY_PLUGIN_DN
  20. INACTIVITY_LIMIT = '9'
  21. SEARCHFILTER = '(objectclass=*)'
  22. TEST_USER = 'ticket48956user'
  23. TEST_USER_PW = '%s' % TEST_USER
  24. if DEBUGGING:
  25. logging.getLogger(__name__).setLevel(logging.DEBUG)
  26. else:
  27. logging.getLogger(__name__).setLevel(logging.INFO)
  28. log = logging.getLogger(__name__)
  29. class TopologyStandalone(object):
  30. """The DS Topology Class"""
  31. def __init__(self, standalone):
  32. """Init"""
  33. standalone.open()
  34. self.standalone = standalone
  35. @pytest.fixture(scope="module")
  36. def topology(request):
  37. """Create DS Deployment"""
  38. # Creating standalone instance ...
  39. if DEBUGGING:
  40. standalone = DirSrv(verbose=True)
  41. else:
  42. standalone = DirSrv(verbose=False)
  43. args_instance[SER_HOST] = HOST_STANDALONE
  44. args_instance[SER_PORT] = PORT_STANDALONE
  45. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  46. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  47. args_standalone = args_instance.copy()
  48. standalone.allocate(args_standalone)
  49. instance_standalone = standalone.exists()
  50. if instance_standalone:
  51. standalone.delete()
  52. standalone.create()
  53. standalone.open()
  54. def fin():
  55. """If we are debugging just stop the instances, otherwise remove them
  56. """
  57. if DEBUGGING:
  58. standalone.stop()
  59. else:
  60. standalone.delete()
  61. request.addfinalizer(fin)
  62. return TopologyStandalone(standalone)
  63. def _check_status(topology, user, expected):
  64. nsaccountstatus = '%s/sbin/ns-accountstatus.pl' % topology.standalone.prefix
  65. proc = subprocess.Popen([nsaccountstatus, '-Z', 'standalone', '-D', DN_DM, '-w', PASSWORD, '-p', str(topology.standalone.port), '-I', user], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  66. found = False
  67. while True:
  68. l = proc.stdout.readline()
  69. log.info("output: %s" % l)
  70. if l == "":
  71. break
  72. if expected in l:
  73. found = True
  74. break
  75. return found
  76. def _check_inactivity(topology, mysuffix):
  77. ACCT_POLICY_DN = 'cn=Account Inactivation Policy,%s' % mysuffix
  78. log.info("\n######################### Adding Account Policy entry: %s ######################\n" % ACCT_POLICY_DN)
  79. topology.standalone.add_s(Entry((ACCT_POLICY_DN, {'objectclass': "top ldapsubentry extensibleObject accountpolicy".split(),
  80. 'accountInactivityLimit': INACTIVITY_LIMIT})))
  81. TEST_USER_DN = 'uid=%s,%s' % (TEST_USER, mysuffix)
  82. log.info("\n######################### Adding Test User entry: %s ######################\n" % TEST_USER_DN)
  83. topology.standalone.add_s(Entry((TEST_USER_DN, {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
  84. 'cn': TEST_USER,
  85. 'sn': TEST_USER,
  86. 'givenname': TEST_USER,
  87. 'userPassword': TEST_USER_PW,
  88. 'acctPolicySubentry': ACCT_POLICY_DN})))
  89. # Setting the lastLoginTime
  90. try:
  91. topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
  92. except ldap.CONSTRAINT_VIOLATION as e:
  93. log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
  94. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  95. assert(_check_status(topology, TEST_USER_DN, '- activated'))
  96. time.sleep(int(INACTIVITY_LIMIT) + 5)
  97. assert(_check_status(topology, TEST_USER_DN, '- inactivated (inactivity limit exceeded'))
  98. def test_ticket48956(topology):
  99. """Write your testcase here...
  100. Also, if you need any testcase initialization,
  101. please, write additional fixture for that(include finalizer).
  102. """
  103. topology.standalone.modify_s(ACCT_POLICY_PLUGIN_DN, [(ldap.MOD_REPLACE, 'nsslapd-pluginarg0', ACCT_POLICY_CONFIG_DN)])
  104. topology.standalone.modify_s(ACCT_POLICY_CONFIG_DN, [(ldap.MOD_REPLACE, 'alwaysrecordlogin', 'yes'),
  105. (ldap.MOD_REPLACE, 'stateattrname', 'lastLoginTime'),
  106. (ldap.MOD_REPLACE, 'altstateattrname', 'createTimestamp'),
  107. (ldap.MOD_REPLACE, 'specattrname', 'acctPolicySubentry'),
  108. (ldap.MOD_REPLACE, 'limitattrname', 'accountInactivityLimit')])
  109. # Enable the plugins
  110. topology.standalone.plugins.enable(name=PLUGIN_ACCT_POLICY)
  111. topology.standalone.restart(timeout=10)
  112. # Check inactivity on standard suffix (short)
  113. _check_inactivity(topology, SUFFIX)
  114. # Check inactivity on a long suffix
  115. topology.standalone.backend.create(LONG_SUFFIX, {BACKEND_NAME: LONG_SUFFIX_BE})
  116. topology.standalone.mappingtree.create(LONG_SUFFIX, bename=LONG_SUFFIX_BE)
  117. topology.standalone.add_s(Entry((LONG_SUFFIX, {
  118. 'objectclass': "top domain".split(),
  119. 'dc': RDN_LONG_SUFFIX})))
  120. _check_inactivity(topology, LONG_SUFFIX)
  121. if DEBUGGING:
  122. # Add debugging steps(if any)...
  123. pass
  124. log.info('Test PASSED')
  125. if __name__ == '__main__':
  126. # Run isolated
  127. # -s for DEBUG mode
  128. CURRENT_FILE = os.path.realpath(__file__)
  129. pytest.main("-s %s" % CURRENT_FILE)