sqlite3_ds_test.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2011 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.datasrc import sqlite3_ds
  16. import os
  17. import socket
  18. import unittest
  19. import sqlite3
  20. TESTDATA_PATH = os.environ['TESTDATA_PATH'] + os.sep
  21. TESTDATA_WRITE_PATH = os.environ['TESTDATA_WRITE_PATH'] + os.sep
  22. READ_ZONE_DB_FILE = TESTDATA_PATH + "example.com.sqlite3"
  23. WRITE_ZONE_DB_FILE = TESTDATA_WRITE_PATH + "example.com.out.sqlite3"
  24. BROKEN_DB_FILE = TESTDATA_PATH + "brokendb.sqlite3"
  25. def example_reader():
  26. my_zone = [
  27. ("example.com.", "3600", "IN", "SOA", "ns.example.com. admin.example.com. 1234 3600 1800 2419200 7200"),
  28. ("example.com.", "3600", "IN", "NS", "ns.example.com."),
  29. ("ns.example.com.", "3600", "IN", "A", "192.0.2.1")
  30. ]
  31. for rr in my_zone:
  32. yield rr
  33. def example_reader_nested():
  34. # this iterator is used in the 'locked' test; it will cause
  35. # the load() method to try and write to the same database
  36. sqlite3_ds.load(WRITE_ZONE_DB_FILE,
  37. ".",
  38. example_reader)
  39. return example_reader()
  40. class TestSqlite3_ds(unittest.TestCase):
  41. def test_zone_exist(self):
  42. # The following file must be non existent and must be non
  43. # "creatable"; the sqlite3 library will try to create a new
  44. # DB file if it doesn't exist, so to test a failure case the
  45. # create operation should also fail. The "nodir", a non
  46. # existent directory, is inserted for this purpose.
  47. nodir = "/nodir/notexist"
  48. self.assertRaises(sqlite3_ds.Sqlite3DSError,
  49. sqlite3_ds.zone_exist, "example.com", nodir)
  50. # Open a broken database file
  51. self.assertRaises(sqlite3_ds.Sqlite3DSError,
  52. sqlite3_ds.zone_exist, "example.com",
  53. BROKEN_DB_FILE)
  54. self.assertTrue(sqlite3_ds.zone_exist("example.com.",
  55. READ_ZONE_DB_FILE))
  56. self.assertFalse(sqlite3_ds.zone_exist("example.org.",
  57. READ_ZONE_DB_FILE))
  58. def test_load_db(self):
  59. sqlite3_ds.load(WRITE_ZONE_DB_FILE, ".", example_reader)
  60. def test_locked_db(self):
  61. # load it first to make sure it exists
  62. sqlite3_ds.load(WRITE_ZONE_DB_FILE, ".", example_reader)
  63. # and manually create a writing session as well
  64. con = sqlite3.connect(WRITE_ZONE_DB_FILE);
  65. cur = con.cursor()
  66. cur.execute("delete from records")
  67. self.assertRaises(sqlite3_ds.Sqlite3DSError,
  68. sqlite3_ds.load, WRITE_ZONE_DB_FILE, ".",
  69. example_reader)
  70. con.rollback()
  71. # and make sure lock does not stay
  72. sqlite3_ds.load(WRITE_ZONE_DB_FILE, ".", example_reader)
  73. # force locked db by nested loads
  74. self.assertRaises(sqlite3_ds.Sqlite3DSError,
  75. sqlite3_ds.load, WRITE_ZONE_DB_FILE, ".",
  76. example_reader_nested)
  77. # and make sure lock does not stay
  78. sqlite3_ds.load(WRITE_ZONE_DB_FILE, ".", example_reader)
  79. if __name__ == '__main__':
  80. unittest.main()