PageRenderTime 62ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/buildscripts/test_shell.py

https://github.com/homocury/mongo
Python | 239 lines | 169 code | 44 blank | 26 comment | 2 complexity | 39c53520ded2f480389364de401f139a MD5 | raw file
  1. # Copyright 2009 10gen, Inc.
  2. #
  3. # This file is part of MongoDB.
  4. #
  5. # MongoDB is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # MongoDB is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with MongoDB. If not, see <http://www.gnu.org/licenses/>.
  17. """Tests for the MongoDB shell.
  18. Right now these mostly just test that the shell handles command line arguments
  19. appropriately.
  20. """
  21. import unittest
  22. import sys
  23. import subprocess
  24. import os
  25. """Exit codes for MongoDB."""
  26. BADOPTS = 2
  27. NOCONNECT = 255
  28. """Path to the mongo shell executable to be tested."""
  29. mongo_path = None
  30. class TestShell(unittest.TestCase):
  31. def open_mongo(self, args=[]):
  32. """Get a subprocess.Popen instance of the shell with the given args.
  33. """
  34. return subprocess.Popen([mongo_path] + args,
  35. stdin=subprocess.PIPE,
  36. stdout=subprocess.PIPE,
  37. stderr = subprocess.PIPE)
  38. def setUp(self):
  39. assert mongo_path
  40. def test_help(self):
  41. mongo_h = self.open_mongo(["-h"])
  42. mongo_help = self.open_mongo(["--help"])
  43. out = mongo_h.communicate()
  44. self.assertEqual(out, mongo_help.communicate())
  45. self.assert_("usage:" in out[0])
  46. self.assertEqual(0, mongo_h.returncode)
  47. self.assertEqual(0, mongo_help.returncode)
  48. def test_nodb(self):
  49. mongo = self.open_mongo([])
  50. mongo_nodb = self.open_mongo(["--nodb"])
  51. out = mongo_nodb.communicate()
  52. self.assert_("MongoDB shell version" in out[0])
  53. self.assert_("bye" in out[0])
  54. self.assert_("couldn't connect" not in out[0])
  55. self.assertEqual(0, mongo_nodb.returncode)
  56. out = mongo.communicate()
  57. self.assert_("MongoDB shell version" in out[0])
  58. self.assert_("bye" not in out[0])
  59. self.assert_("couldn't connect" in out[0])
  60. self.assertEqual(NOCONNECT, mongo.returncode)
  61. def test_eval(self):
  62. mongo = self.open_mongo(["--nodb", "--eval", "print('hello world');"])
  63. out = mongo.communicate()
  64. self.assert_("hello world" in out[0])
  65. self.assert_("bye" not in out[0])
  66. self.assertEqual(0, mongo.returncode)
  67. mongo = self.open_mongo(["--eval"])
  68. out = mongo.communicate()
  69. self.assert_("required parameter is missing" in out[0])
  70. self.assertEqual(BADOPTS, mongo.returncode)
  71. def test_shell(self):
  72. mongo = self.open_mongo(["--nodb", "--shell", "--eval", "print('hello world');"])
  73. out = mongo.communicate()
  74. self.assert_("hello world" in out[0])
  75. self.assert_("bye" in out[0]) # the shell started and immediately exited because stdin was empty
  76. self.assertEqual(0, mongo.returncode)
  77. def test_host_port(self):
  78. mongo = self.open_mongo([])
  79. out = mongo.communicate()
  80. self.assert_("url: test" in out[0])
  81. self.assert_("connecting to: test" in out[0])
  82. self.assertEqual(NOCONNECT, mongo.returncode)
  83. mongo = self.open_mongo(["--host", "localhost"])
  84. out = mongo.communicate()
  85. self.assert_("url: test" in out[0])
  86. self.assert_("connecting to: localhost/test" in out[0])
  87. self.assertEqual(NOCONNECT, mongo.returncode)
  88. mongo = self.open_mongo(["--port", "27018"])
  89. out = mongo.communicate()
  90. self.assert_("url: test" in out[0])
  91. self.assert_("connecting to: 127.0.0.1:27018" in out[0])
  92. self.assertEqual(NOCONNECT, mongo.returncode)
  93. mongo = self.open_mongo(["--host", "localhost", "--port", "27018"])
  94. out = mongo.communicate()
  95. self.assert_("url: test" in out[0])
  96. self.assert_("connecting to: localhost:27018/test" in out[0])
  97. self.assertEqual(NOCONNECT, mongo.returncode)
  98. mongo = self.open_mongo(["--host"])
  99. out = mongo.communicate()
  100. self.assert_("required parameter is missing" in out[0])
  101. self.assertEqual(BADOPTS, mongo.returncode)
  102. mongo = self.open_mongo(["--port"])
  103. out = mongo.communicate()
  104. self.assert_("required parameter is missing" in out[0])
  105. self.assertEqual(BADOPTS, mongo.returncode)
  106. def test_positionals(self):
  107. dirname = os.path.dirname(__file__)
  108. test_js = os.path.join(dirname, "testdata/test.js")
  109. test_txt = os.path.join(dirname, "testdata/test.txt")
  110. test = os.path.join(dirname, "testdata/test")
  111. non_exist_js = os.path.join(dirname, "testdata/nonexist.js")
  112. non_exist_txt = os.path.join(dirname, "testdata/nonexist.txt")
  113. mongo = self.open_mongo(["--nodb", test_js])
  114. out = mongo.communicate()
  115. self.assert_("hello world" in out[0])
  116. self.assert_("bye" not in out[0])
  117. self.assertEqual(0, mongo.returncode)
  118. mongo = self.open_mongo(["--nodb", test_txt])
  119. out = mongo.communicate()
  120. self.assert_("foobar" in out[0])
  121. self.assert_("bye" not in out[0])
  122. self.assertEqual(0, mongo.returncode)
  123. mongo = self.open_mongo([test_js, test, test_txt])
  124. out = mongo.communicate()
  125. self.assert_("url: test" in out[0])
  126. self.assert_("connecting to: test" in out[0])
  127. self.assertEqual(NOCONNECT, mongo.returncode)
  128. mongo = self.open_mongo([test_txt, test, test_js])
  129. out = mongo.communicate()
  130. self.assert_("url: test" in out[0])
  131. self.assert_("connecting to: test" in out[0])
  132. self.assertEqual(NOCONNECT, mongo.returncode)
  133. mongo = self.open_mongo([test, test_js, test_txt])
  134. out = mongo.communicate()
  135. self.assert_("url: " + test in out[0])
  136. self.assert_("connecting to: " + test in out[0])
  137. self.assertEqual(NOCONNECT, mongo.returncode)
  138. mongo = self.open_mongo([non_exist_js, test, test_txt])
  139. out = mongo.communicate()
  140. self.assert_("url: test" in out[0])
  141. self.assert_("connecting to: test" in out[0])
  142. self.assertEqual(NOCONNECT, mongo.returncode)
  143. mongo = self.open_mongo([non_exist_txt, test_js, test_txt])
  144. out = mongo.communicate()
  145. self.assert_("url: " + non_exist_txt in out[0])
  146. self.assert_("connecting to: " + non_exist_txt in out[0])
  147. self.assertEqual(NOCONNECT, mongo.returncode)
  148. def test_multiple_files(self):
  149. dirname = os.path.dirname(__file__)
  150. test_js = os.path.join(dirname, "testdata/test.js")
  151. test_txt = os.path.join(dirname, "testdata/test.txt")
  152. mongo = self.open_mongo(["--nodb", test_js, test_txt])
  153. out = mongo.communicate()
  154. self.assert_("hello world" in out[0])
  155. self.assert_("foobar" in out[0])
  156. self.assert_("bye" not in out[0])
  157. self.assertEqual(0, mongo.returncode)
  158. mongo = self.open_mongo(["--shell", "--nodb", test_js, test_txt])
  159. out = mongo.communicate()
  160. self.assert_("hello world" in out[0])
  161. self.assert_("foobar" in out[0])
  162. self.assert_("bye" in out[0])
  163. self.assertEqual(0, mongo.returncode)
  164. # just testing that they don't blow up
  165. def test_username_and_password(self):
  166. mongo = self.open_mongo(["--username", "mike"])
  167. out = mongo.communicate()
  168. self.assertEqual(NOCONNECT, mongo.returncode)
  169. mongo = self.open_mongo(["-u", "mike"])
  170. out = mongo.communicate()
  171. self.assertEqual(NOCONNECT, mongo.returncode)
  172. mongo = self.open_mongo(["--password", "mike"])
  173. out = mongo.communicate()
  174. self.assertEqual(NOCONNECT, mongo.returncode)
  175. mongo = self.open_mongo(["-p", "mike"])
  176. out = mongo.communicate()
  177. self.assertEqual(NOCONNECT, mongo.returncode)
  178. mongo = self.open_mongo(["--username"])
  179. out = mongo.communicate()
  180. self.assert_("required parameter is missing" in out[0])
  181. self.assertEqual(BADOPTS, mongo.returncode)
  182. mongo = self.open_mongo(["--password"])
  183. out = mongo.communicate()
  184. self.assert_("required parameter is missing" in out[0])
  185. self.assertEqual(BADOPTS, mongo.returncode)
  186. def run_tests():
  187. suite = unittest.TestLoader().loadTestsFromTestCase(TestShell)
  188. unittest.TextTestRunner(verbosity=1).run(suite)
  189. if __name__ == "__main__":
  190. if len(sys.argv) != 2:
  191. print "must give the path to shell executable to be tested"
  192. sys.exit()
  193. mongo_path = sys.argv[1]
  194. run_tests()