schema_reload_test.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2017 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. import logging
  9. import pytest
  10. import ldap
  11. import os
  12. from lib389.topologies import topology_st as topo
  13. from lib389._constants import TASK_WAIT
  14. DEBUGGING = os.getenv("DEBUGGING", default=False)
  15. if DEBUGGING:
  16. logging.getLogger(__name__).setLevel(logging.DEBUG)
  17. else:
  18. logging.getLogger(__name__).setLevel(logging.INFO)
  19. log = logging.getLogger(__name__)
  20. INVALID_SCHEMA = 'givenName $ cn $ MoZiLLaATTRiBuTe'
  21. def test_valid_schema(topo):
  22. """Test schema-reload task with valid schema
  23. :id: 2ab304c0-3e58-4d34-b23b-a14b5997c7a8
  24. :setup: Standalone instance
  25. :steps:
  26. 1. Create schema file with valid schema
  27. 2. Run schema-reload.pl script
  28. 3. Run ldapsearch and check if schema was added
  29. :expectedresults:
  30. 1. File creation should work
  31. 2. The schema reload task should be successful
  32. 3. Searching the server should return the new schema
  33. """
  34. log.info("Test schema-reload task with valid schema")
  35. # Step 1 - Create schema file
  36. log.info("Create valid schema file (99user.ldif)...")
  37. schema_filename = (topo.standalone.schemadir + "/99user.ldif")
  38. try:
  39. with open(schema_filename, 'w') as schema_file:
  40. schema_file.write("dn: cn=schema\n")
  41. schema_file.write("attributetypes: ( 8.9.10.11.12.13.13 NAME " +
  42. "'ValidAttribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15" +
  43. " X-ORIGIN 'Mozilla Dummy Schema' )\n")
  44. schema_file.write("objectclasses: ( 1.2.3.4.5.6.7.8 NAME 'TestObject' " +
  45. "SUP top MUST ( objectclass $ cn ) MAY ( givenName $ " +
  46. "sn $ ValidAttribute ) X-ORIGIN 'user defined' )')\n")
  47. except OSError as e:
  48. log.fatal("Failed to create schema file: " +
  49. "{} Error: {}".format(schema_filename, str(e)))
  50. # Step 2 - Run the schema-reload task
  51. log.info("Run the schema-reload task...")
  52. reload_result = topo.standalone.tasks.schemaReload(args={TASK_WAIT: True})
  53. if reload_result != 0:
  54. log.fatal("The schema reload task failed")
  55. assert False
  56. else:
  57. log.info("The schema reload task worked as expected")
  58. # Step 3 - Verify valid schema was added to the server
  59. log.info("Check cn=schema to verify the valid schema was added")
  60. subschema = topo.standalone.schema.get_subschema()
  61. oc_obj = subschema.get_obj(ldap.schema.ObjectClass, 'TestObject')
  62. assert oc_obj is not None, "The new objectclass was not found on server"
  63. at_obj = subschema.get_obj(ldap.schema.AttributeType, 'ValidAttribute')
  64. assert at_obj is not None, "The new attribute was not found on server"
  65. def test_invalid_schema(topo):
  66. """Test schema-reload task with invalid schema
  67. :id: 2ab304c0-3e58-4d34-b23b-a14b5997c7a9
  68. :setup: Standalone instance
  69. :steps:
  70. 1. Create schema files with invalid schema
  71. 2. Run schema-reload.pl script
  72. 3. Run ldapsearch and check if schema was added
  73. :expectedresults:
  74. 1. File creation should work
  75. 2. The schema reload task should return an error
  76. 3. Searching the server should not return the invalid schema
  77. """
  78. log.info("Test schema-reload task with invalid schema")
  79. # Step 1 - Create schema files: one valid, one invalid
  80. log.info("Create valid schema file (98user.ldif)...")
  81. schema_filename = (topo.standalone.schemadir + "/98user.ldif")
  82. try:
  83. with open(schema_filename, 'w') as schema_file:
  84. schema_file.write("dn: cn=schema\n")
  85. schema_file.write("attributetypes: ( 8.9.10.11.12.13.14 NAME " +
  86. "'MozillaAttribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15" +
  87. " X-ORIGIN 'Mozilla Dummy Schema' )\n")
  88. schema_file.write("objectclasses: ( 1.2.3.4.5.6.7 NAME 'MoZiLLaOBJeCT' " +
  89. "SUP top MUST ( objectclass $ cn ) MAY ( givenName $ " +
  90. "sn $ MozillaAttribute ) X-ORIGIN 'user defined' )')\n")
  91. except OSError as e:
  92. log.fatal("Failed to create schema file: " +
  93. "{} Error: {}".format(schema_filename, str(e)))
  94. log.info("Create invalid schema file (99user.ldif)...")
  95. schema_filename = (topo.standalone.schemadir + "/99user.ldif")
  96. try:
  97. with open(schema_filename, 'w') as schema_file:
  98. schema_file.write("dn: cn=schema\n")
  99. # Same attribute/objclass names, but different OIDs and MAY attributes
  100. schema_file.write("attributetypes: ( 8.9.10.11.12.13.140 NAME " +
  101. "'MozillaAttribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15" +
  102. " X-ORIGIN 'Mozilla Dummy Schema' )\n")
  103. schema_file.write("objectclasses: ( 1.2.3.4.5.6.70 NAME 'MoZiLLaOBJeCT' " +
  104. "SUP top MUST ( objectclass $ cn ) MAY ( givenName $ " +
  105. "cn $ MoZiLLaATTRiBuTe ) X-ORIGIN 'user defined' )')\n")
  106. except OSError as e:
  107. log.fatal("Failed to create schema file: " +
  108. "{} Error: {}".format(schema_filename, str(e)))
  109. # Step 2 - Run the schema-reload task
  110. log.info("Run the schema-reload task, it should fail...")
  111. reload_result = topo.standalone.tasks.schemaReload(args={TASK_WAIT: True})
  112. if reload_result == 0:
  113. log.fatal("The schema reload task incorectly reported success")
  114. assert False
  115. else:
  116. log.info("The schema reload task failed as expected:" +
  117. " error {}".format(reload_result))
  118. # Step 3 - Verify invalid schema was not added to the server
  119. log.info("Check cn=schema to verify the invalid schema was not added")
  120. subschema = topo.standalone.schema.get_subschema()
  121. oc_obj = subschema.get_obj(ldap.schema.ObjectClass, 'MoZiLLaOBJeCT')
  122. if oc_obj is not None and INVALID_SCHEMA in str(oc_obj):
  123. log.fatal("The invalid schema was returned from the server: " + str(oc_obj))
  124. assert False
  125. else:
  126. log.info("The invalid schema is not present on the server")
  127. if __name__ == '__main__':
  128. # Run isolated
  129. # -s for DEBUG mode
  130. CURRENT_FILE = os.path.realpath(__file__)
  131. pytest.main("-s %s" % CURRENT_FILE)