PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/interface/cluster_config.py

https://gitlab.com/asimpletune/rethinkdb
Python | 102 lines | 68 code | 30 blank | 4 comment | 4 complexity | 18b6df765f8b28545d5c3b5851840d3e MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright 2014-2015 RethinkDB, all rights reserved.
  3. """The `interface.cluster_config` test checks that the special `rethinkdb.cluster_config` table behaves as expected."""
  4. import os, sys, time
  5. startTime = time.time()
  6. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'common')))
  7. import driver, scenario_common, utils, vcoptparse
  8. r = utils.import_python_driver()
  9. op = vcoptparse.OptParser()
  10. scenario_common.prepare_option_parser_mode_flags(op)
  11. _, command_prefix, serve_options = scenario_common.parse_mode_flags(op.parse(sys.argv))
  12. print("Starting a server (%.2fs)" % (time.time() - startTime))
  13. with driver.Cluster(initial_servers=1, output_folder='.', wait_until_ready=True, command_prefix=command_prefix, extra_options=serve_options) as cluster:
  14. print("Establishing ReQL connection (%.2fs)" % (time.time() - startTime))
  15. server = cluster[0]
  16. conn = r.connect(server.host, server.driver_port)
  17. print("Setting AuthKey (%.2fs)" % (time.time() - startTime))
  18. assert list(r.db("rethinkdb").table("cluster_config").run(conn)) == [
  19. {"id": "auth", "auth_key": None},
  20. {"id": "heartbeat", "heartbeat_timeout_secs": 10}
  21. ]
  22. res = r.db("rethinkdb").table("cluster_config").get("auth").update({"auth_key": "hunter2"}).run(conn)
  23. assert res["errors"] == 0
  24. conn.close()
  25. print("Verifying AuthKey prevents unauthenticated connections (%.2fs)" % (time.time() - startTime))
  26. try:
  27. r.connect(server.host, server.driver_port)
  28. except r.ReqlDriverError:
  29. pass
  30. else:
  31. assert False, "the change to the auth key doesn't seem to have worked"
  32. print("Checking connection with the AuthKey (%.2fs)" % (time.time() - startTime))
  33. conn = r.connect(server.host, server.driver_port, auth_key="hunter2")
  34. rows = list(r.db("rethinkdb").table("cluster_config").filter({"id": "auth"}).run(conn))
  35. assert rows == [{"id": "auth", "auth_key": {"hidden": True}}]
  36. print("Removing the AuthKey (%.2fs)" % (time.time() - startTime))
  37. res = r.db("rethinkdb").table("cluster_config").get("auth").update({"auth_key": None}).run(conn)
  38. assert res["errors"] == 0
  39. assert list(r.db("rethinkdb").table("cluster_config").filter({"id": "auth"}).run(conn)) == [{"id": "auth", "auth_key": None}]
  40. conn.close()
  41. print("Verifying connection without an AuthKey (%.2fs)" % (time.time() - startTime))
  42. conn = r.connect(server.host, server.driver_port)
  43. assert list(r.db("rethinkdb").table("cluster_config").filter({"id": "auth"}).run(conn)) == [{"id": "auth", "auth_key": None}]
  44. # This is mostly to make sure the server doesn't crash in this case
  45. res = r.db("rethinkdb").table("cluster_config").get("auth").update({"auth_key": {"hidden": True}}).run(conn)
  46. assert res["errors"] == 1
  47. print("Inserting nonsense to make sure it's not accepted (%.2fs)" % (time.time() - startTime))
  48. res = r.db("rethinkdb").table("cluster_config").insert({"id": "foobar"}).run(conn)
  49. assert res["errors"] == 1, res
  50. res = r.db("rethinkdb").table("cluster_config").get("auth").delete().run(conn)
  51. assert res["errors"] == 1, res
  52. res = r.db("rethinkdb").table("cluster_config").get("auth") \
  53. .update({"foo": "bar"}).run(conn)
  54. assert res["errors"] == 1, res
  55. res = r.db("rethinkdb").table("cluster_config").get("auth") \
  56. .update({"auth_key": {"is_this_nonsense": "yes"}}).run(conn)
  57. assert res["errors"] == 1, res
  58. print("Verifying heartbeat timeout interface")
  59. res = r.db("rethinkdb").table("cluster_config").get("heartbeat").update({"heartbeat_timeout_secs": -1.0}).run(conn)
  60. assert res["errors"] == 1, res
  61. res = r.db("rethinkdb").table("cluster_config").get("heartbeat").update({"heartbeat_timeout_secs": 1.0}).run(conn)
  62. assert res["errors"] == 1, res
  63. res = r.db("rethinkdb").table("cluster_config").get("heartbeat").update({"heartbeat_timeout_secs": 2.0}).run(conn)
  64. assert res["replaced"] == 1, res
  65. res = r.db("rethinkdb").table("cluster_config").get("heartbeat").run(conn)
  66. assert res["heartbeat_timeout_secs"] == 2
  67. res = r.db("rethinkdb").table("cluster_config").get("heartbeat").update({"heartbeat_timeout_secs": 2.5}).run(conn)
  68. assert res["replaced"] == 1, res
  69. res = r.db("rethinkdb").table("cluster_config").get("heartbeat").run(conn)
  70. assert res["heartbeat_timeout_secs"] == 2.5
  71. print("Cleaning up (%.2fs)" % (time.time() - startTime))
  72. print("Done. (%.2fs)" % (time.time() - startTime))