|
| 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 | + |
| 18 | +"""Tests for the MongoDB shell. |
| 19 | +
|
| 20 | +Right now these mostly just test that the shell handles command line arguments |
| 21 | +appropriately. |
| 22 | +""" |
| 23 | + |
| 24 | +import unittest |
| 25 | +import sys |
| 26 | +import subprocess |
| 27 | +import os |
| 28 | + |
| 29 | +"""Exit codes for MongoDB.""" |
| 30 | +BADOPTS = 2 |
| 31 | +BADHOST = 14 |
| 32 | +NOCONNECT = 255 |
| 33 | + |
| 34 | +"""Path to the mongo shell executable to be tested.""" |
| 35 | +mongo_path = None |
| 36 | + |
| 37 | +class TestShell(unittest.TestCase): |
| 38 | + |
| 39 | + def open_mongo(self, args=[]): |
| 40 | + """Get a subprocess.Popen instance of the shell with the given args. |
| 41 | + """ |
| 42 | + return subprocess.Popen([mongo_path] + args, |
| 43 | + stdin=subprocess.PIPE, |
| 44 | + stdout=subprocess.PIPE, |
| 45 | + stderr = subprocess.PIPE) |
| 46 | + |
| 47 | + def setUp(self): |
| 48 | + assert mongo_path |
| 49 | + |
| 50 | + def test_help(self): |
| 51 | + mongo_h = self.open_mongo(["-h"]) |
| 52 | + mongo_help = self.open_mongo(["--help"]) |
| 53 | + |
| 54 | + out = mongo_h.communicate() |
| 55 | + self.assertEqual(out, mongo_help.communicate()) |
| 56 | + self.assert_(out[0].startswith("usage:")) |
| 57 | + |
| 58 | + self.assertEqual(0, mongo_h.returncode) |
| 59 | + self.assertEqual(0, mongo_help.returncode) |
| 60 | + |
| 61 | + def test_nodb(self): |
| 62 | + mongo = self.open_mongo([]) |
| 63 | + mongo_nodb = self.open_mongo(["--nodb"]) |
| 64 | + |
| 65 | + out = mongo_nodb.communicate() |
| 66 | + self.assert_("MongoDB shell version" in out[0]) |
| 67 | + self.assert_("bye" in out[0]) |
| 68 | + self.assert_("couldn't connect" not in out[0]) |
| 69 | + self.assertEqual(0, mongo_nodb.returncode) |
| 70 | + |
| 71 | + out = mongo.communicate() |
| 72 | + self.assert_("MongoDB shell version" in out[0]) |
| 73 | + self.assert_("bye" not in out[0]) |
| 74 | + self.assert_("couldn't connect" in out[0]) |
| 75 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 76 | + |
| 77 | + def test_eval(self): |
| 78 | + mongo = self.open_mongo(["--nodb", "--eval", "print('hello world');"]) |
| 79 | + out = mongo.communicate() |
| 80 | + self.assert_("hello world" in out[0]) |
| 81 | + self.assert_("bye" not in out[0]) |
| 82 | + self.assertEqual(0, mongo.returncode) |
| 83 | + |
| 84 | + mongo = self.open_mongo(["--eval"]) |
| 85 | + out = mongo.communicate() |
| 86 | + self.assert_("required parameter is missing" in out[0]) |
| 87 | + self.assertEqual(BADOPTS, mongo.returncode) |
| 88 | + |
| 89 | + def test_shell(self): |
| 90 | + mongo = self.open_mongo(["--nodb", "--shell", "--eval", "print('hello world');"]) |
| 91 | + out = mongo.communicate() |
| 92 | + self.assert_("hello world" in out[0]) |
| 93 | + self.assert_("bye" in out[0]) # the shell started and immediately exited because stdin was empty |
| 94 | + self.assertEqual(0, mongo.returncode) |
| 95 | + |
| 96 | + def test_host_port(self): |
| 97 | + mongo = self.open_mongo([]) |
| 98 | + out = mongo.communicate() |
| 99 | + self.assert_("url: test" in out[0]) |
| 100 | + self.assert_("connecting to: test" in out[0]) |
| 101 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 102 | + |
| 103 | + mongo = self.open_mongo(["--host", "localhost"]) |
| 104 | + out = mongo.communicate() |
| 105 | + self.assert_("url: test" in out[0]) |
| 106 | + self.assert_("connecting to: localhost/test" in out[0]) |
| 107 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 108 | + |
| 109 | + mongo = self.open_mongo(["--port", "27018"]) |
| 110 | + out = mongo.communicate() |
| 111 | + self.assert_("url: test" in out[0]) |
| 112 | + self.assert_("connecting to: 127.0.0.1:27018" in out[0]) |
| 113 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 114 | + |
| 115 | + mongo = self.open_mongo(["--host", "localhost", "--port", "27018"]) |
| 116 | + out = mongo.communicate() |
| 117 | + self.assert_("url: test" in out[0]) |
| 118 | + self.assert_("connecting to: localhost:27018/test" in out[0]) |
| 119 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 120 | + |
| 121 | + mongo = self.open_mongo(["--host"]) |
| 122 | + out = mongo.communicate() |
| 123 | + self.assert_("required parameter is missing" in out[0]) |
| 124 | + self.assertEqual(BADOPTS, mongo.returncode) |
| 125 | + |
| 126 | + mongo = self.open_mongo(["--port"]) |
| 127 | + out = mongo.communicate() |
| 128 | + self.assert_("required parameter is missing" in out[0]) |
| 129 | + self.assertEqual(BADOPTS, mongo.returncode) |
| 130 | + |
| 131 | + def test_positionals(self): |
| 132 | + dirname = os.path.dirname(__file__) |
| 133 | + test_js = os.path.join(dirname, "testdata/test.js") |
| 134 | + test_txt = os.path.join(dirname, "testdata/test.txt") |
| 135 | + test = os.path.join(dirname, "testdata/test") |
| 136 | + non_exist_js = os.path.join(dirname, "testdata/nonexist.js") |
| 137 | + non_exist_txt = os.path.join(dirname, "testdata/nonexist.txt") |
| 138 | + |
| 139 | + mongo = self.open_mongo(["--nodb", test_js]) |
| 140 | + out = mongo.communicate() |
| 141 | + self.assert_("hello world" in out[0]) |
| 142 | + self.assert_("bye" not in out[0]) |
| 143 | + self.assertEqual(0, mongo.returncode) |
| 144 | + |
| 145 | + mongo = self.open_mongo(["--nodb", test_txt]) |
| 146 | + out = mongo.communicate() |
| 147 | + self.assert_("foobar" in out[0]) |
| 148 | + self.assert_("bye" not in out[0]) |
| 149 | + self.assertEqual(0, mongo.returncode) |
| 150 | + |
| 151 | + mongo = self.open_mongo([test_js, test, test_txt]) |
| 152 | + out = mongo.communicate() |
| 153 | + self.assert_("url: test" in out[0]) |
| 154 | + self.assert_("connecting to: test" in out[0]) |
| 155 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 156 | + |
| 157 | + mongo = self.open_mongo([test_txt, test, test_js]) |
| 158 | + out = mongo.communicate() |
| 159 | + self.assert_("url: test" in out[0]) |
| 160 | + self.assert_("connecting to: test" in out[0]) |
| 161 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 162 | + |
| 163 | + mongo = self.open_mongo([test, test_js, test_txt]) |
| 164 | + out = mongo.communicate() |
| 165 | + self.assert_("url: " + test in out[0]) |
| 166 | + self.assert_("connecting to: " + test in out[0]) |
| 167 | + self.assertEqual(BADHOST, mongo.returncode) |
| 168 | + |
| 169 | + mongo = self.open_mongo([non_exist_js, test, test_txt]) |
| 170 | + out = mongo.communicate() |
| 171 | + self.assert_("url: test" in out[0]) |
| 172 | + self.assert_("connecting to: test" in out[0]) |
| 173 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 174 | + |
| 175 | + mongo = self.open_mongo([non_exist_txt, test_js, test_txt]) |
| 176 | + out = mongo.communicate() |
| 177 | + self.assert_("url: " + non_exist_txt in out[0]) |
| 178 | + self.assert_("connecting to: " + non_exist_txt in out[0]) |
| 179 | + self.assertEqual(BADHOST, mongo.returncode) |
| 180 | + |
| 181 | + def test_multiple_files(self): |
| 182 | + dirname = os.path.dirname(__file__) |
| 183 | + test_js = os.path.join(dirname, "testdata/test.js") |
| 184 | + test_txt = os.path.join(dirname, "testdata/test.txt") |
| 185 | + |
| 186 | + mongo = self.open_mongo(["--nodb", test_js, test_txt]) |
| 187 | + out = mongo.communicate() |
| 188 | + self.assert_("hello world" in out[0]) |
| 189 | + self.assert_("foobar" in out[0]) |
| 190 | + self.assert_("bye" not in out[0]) |
| 191 | + self.assertEqual(0, mongo.returncode) |
| 192 | + |
| 193 | + mongo = self.open_mongo(["--shell", "--nodb", test_js, test_txt]) |
| 194 | + out = mongo.communicate() |
| 195 | + self.assert_("hello world" in out[0]) |
| 196 | + self.assert_("foobar" in out[0]) |
| 197 | + self.assert_("bye" in out[0]) |
| 198 | + self.assertEqual(0, mongo.returncode) |
| 199 | + |
| 200 | + # just testing that they don't blow up |
| 201 | + def test_username_and_password(self): |
| 202 | + mongo = self.open_mongo(["--username", "mike"]) |
| 203 | + out = mongo.communicate() |
| 204 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 205 | + |
| 206 | + mongo = self.open_mongo(["-u", "mike"]) |
| 207 | + out = mongo.communicate() |
| 208 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 209 | + |
| 210 | + mongo = self.open_mongo(["--password", "mike"]) |
| 211 | + out = mongo.communicate() |
| 212 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 213 | + |
| 214 | + mongo = self.open_mongo(["-p", "mike"]) |
| 215 | + out = mongo.communicate() |
| 216 | + self.assertEqual(NOCONNECT, mongo.returncode) |
| 217 | + |
| 218 | + mongo = self.open_mongo(["--username"]) |
| 219 | + out = mongo.communicate() |
| 220 | + self.assert_("required parameter is missing" in out[0]) |
| 221 | + self.assertEqual(BADOPTS, mongo.returncode) |
| 222 | + |
| 223 | + mongo = self.open_mongo(["--password"]) |
| 224 | + out = mongo.communicate() |
| 225 | + self.assert_("required parameter is missing" in out[0]) |
| 226 | + self.assertEqual(BADOPTS, mongo.returncode) |
| 227 | + |
| 228 | + |
| 229 | +if __name__ == "__main__": |
| 230 | + if len(sys.argv) != 2: |
| 231 | + print "must give the path to shell executable to be tested" |
| 232 | + sys.exit() |
| 233 | + |
| 234 | + mongo_path = sys.argv[1] |
| 235 | + |
| 236 | + suite = unittest.TestLoader().loadTestsFromTestCase(TestShell) |
| 237 | + unittest.TextTestRunner(verbosity=1).run(suite) |
0 commit comments