mongo 1.9.2 → 1.10.0.rc0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/LICENSE +1 -1
- data/README.md +94 -334
- data/Rakefile +6 -4
- data/VERSION +1 -1
- data/bin/mongo_console +13 -6
- data/lib/mongo.rb +22 -27
- data/lib/mongo/bulk_write_collection_view.rb +352 -0
- data/lib/mongo/collection.rb +128 -188
- data/lib/mongo/collection_writer.rb +348 -0
- data/lib/mongo/connection.rb +19 -0
- data/lib/mongo/{util → connection}/node.rb +15 -1
- data/lib/mongo/{util → connection}/pool.rb +34 -19
- data/lib/mongo/{util → connection}/pool_manager.rb +8 -2
- data/lib/mongo/{util → connection}/sharding_pool_manager.rb +1 -1
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/{util → connection/socket}/socket_util.rb +5 -2
- data/lib/mongo/{util → connection/socket}/ssl_socket.rb +3 -4
- data/lib/mongo/{util → connection/socket}/tcp_socket.rb +25 -15
- data/lib/mongo/{util → connection/socket}/unix_socket.rb +6 -4
- data/lib/mongo/cursor.rb +113 -47
- data/lib/mongo/db.rb +203 -131
- data/lib/mongo/{exceptions.rb → exception.rb} +7 -1
- data/lib/mongo/functional.rb +19 -0
- data/lib/mongo/functional/authentication.rb +303 -0
- data/lib/mongo/{util → functional}/logging.rb +1 -1
- data/lib/mongo/{util → functional}/read_preference.rb +49 -1
- data/lib/mongo/{util → functional}/uri_parser.rb +81 -69
- data/lib/mongo/{util → functional}/write_concern.rb +2 -1
- data/{test/unit/pool_test.rb → lib/mongo/gridfs.rb} +5 -10
- data/lib/mongo/gridfs/grid.rb +1 -3
- data/lib/mongo/gridfs/grid_ext.rb +1 -1
- data/lib/mongo/gridfs/grid_file_system.rb +1 -1
- data/lib/mongo/gridfs/grid_io.rb +1 -1
- data/lib/mongo/legacy.rb +63 -8
- data/lib/mongo/mongo_client.rb +128 -154
- data/lib/mongo/mongo_replica_set_client.rb +17 -11
- data/lib/mongo/mongo_sharded_client.rb +2 -1
- data/lib/mongo/networking.rb +19 -10
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo/{util → utils}/conversions.rb +1 -1
- data/lib/mongo/{util → utils}/core_ext.rb +1 -1
- data/lib/mongo/{util → utils}/server_version.rb +1 -1
- data/lib/mongo/{util → utils}/support.rb +10 -57
- data/lib/mongo/{util → utils}/thread_local_variable_manager.rb +1 -1
- data/test/functional/authentication_test.rb +8 -21
- data/test/functional/bulk_write_collection_view_test.rb +782 -0
- data/test/functional/{connection_test.rb → client_test.rb} +153 -78
- data/test/functional/collection_test.rb +343 -97
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +1 -3
- data/test/functional/cursor_fail_test.rb +3 -3
- data/test/functional/cursor_message_test.rb +3 -3
- data/test/functional/cursor_test.rb +38 -3
- data/test/functional/db_api_test.rb +5 -5
- data/test/functional/db_connection_test.rb +2 -2
- data/test/functional/db_test.rb +35 -11
- data/test/functional/grid_file_system_test.rb +2 -2
- data/test/functional/grid_io_test.rb +2 -2
- data/test/functional/grid_test.rb +2 -2
- data/test/functional/pool_test.rb +2 -3
- data/test/functional/safe_test.rb +5 -5
- data/test/functional/ssl_test.rb +22 -102
- data/test/functional/support_test.rb +1 -1
- data/test/functional/timeout_test.rb +6 -22
- data/test/functional/uri_test.rb +113 -12
- data/test/functional/write_concern_test.rb +6 -6
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +309 -0
- data/test/replica_set/authentication_test.rb +8 -23
- data/test/replica_set/basic_test.rb +41 -14
- data/test/replica_set/client_test.rb +179 -117
- data/test/replica_set/complex_connect_test.rb +6 -7
- data/test/replica_set/connection_test.rb +46 -38
- data/test/replica_set/count_test.rb +2 -2
- data/test/replica_set/cursor_test.rb +8 -8
- data/test/replica_set/insert_test.rb +64 -2
- data/test/replica_set/max_values_test.rb +59 -10
- data/test/replica_set/pinning_test.rb +2 -2
- data/test/replica_set/query_test.rb +2 -2
- data/test/replica_set/read_preference_test.rb +6 -6
- data/test/replica_set/refresh_test.rb +7 -7
- data/test/replica_set/replication_ack_test.rb +5 -5
- data/test/replica_set/ssl_test.rb +24 -106
- data/test/sharded_cluster/basic_test.rb +43 -15
- data/test/shared/authentication/basic_auth_shared.rb +215 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/ssl_shared.rb +173 -0
- data/test/test_helper.rb +31 -199
- data/test/threading/basic_test.rb +29 -3
- data/test/tools/mongo_config.rb +45 -20
- data/test/tools/mongo_config_test.rb +1 -1
- data/test/unit/client_test.rb +136 -57
- data/test/unit/collection_test.rb +31 -55
- data/test/unit/connection_test.rb +135 -72
- data/test/unit/cursor_test.rb +2 -2
- data/test/unit/db_test.rb +19 -15
- data/test/unit/grid_test.rb +2 -2
- data/test/unit/mongo_sharded_client_test.rb +17 -15
- data/test/unit/node_test.rb +2 -2
- data/test/unit/pool_manager_test.rb +7 -5
- data/test/unit/read_pref_test.rb +82 -2
- data/test/unit/read_test.rb +14 -14
- data/test/unit/safe_test.rb +9 -9
- data/test/unit/sharding_pool_manager_test.rb +11 -5
- data/test/unit/write_concern_test.rb +9 -9
- metadata +71 -56
- metadata.gz.sig +0 -0
- data/test/functional/threading_test.rb +0 -109
- data/test/shared/authentication.rb +0 -121
- data/test/unit/util_test.rb +0 -69
@@ -0,0 +1,96 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module SASLPlainTests
|
16
|
+
|
17
|
+
# Tests for the PLAIN (LDAP) Authentication Mechanism.
|
18
|
+
#
|
19
|
+
# Note: These tests will be skipped automatically unless the test environment
|
20
|
+
# has been configured.
|
21
|
+
#
|
22
|
+
# In order to run these tests, set the following environment variables:
|
23
|
+
#
|
24
|
+
# export MONGODB_SASL_HOST='server.domain.com'
|
25
|
+
# export MONGODB_SASL_USER='application%2Fuser%40example.com'
|
26
|
+
# export MONGODB_SASL_PASS='password'
|
27
|
+
#
|
28
|
+
# # optional (defaults to '$external')
|
29
|
+
# export MONGODB_SASL_SOURCE='source_database'
|
30
|
+
#
|
31
|
+
if ENV.key?('MONGODB_SASL_HOST') && ENV.key?('MONGODB_SASL_USER') && ENV.key?('MONGODB_SASL_PASS')
|
32
|
+
|
33
|
+
def test_plain_authenticate
|
34
|
+
replica_set = @client.class.name == 'Mongo::MongoReplicaSetClient'
|
35
|
+
|
36
|
+
# TODO: Remove this once we have a replica set configured for SASL in CI
|
37
|
+
return if ENV.key?('CI') && replica_set
|
38
|
+
|
39
|
+
host = replica_set ? [ENV['MONGODB_SASL_HOST']] : ENV['MONGODB_SASL_HOST']
|
40
|
+
client = @client.class.new(host)
|
41
|
+
source = ENV['MONGODB_SASL_SOURCE'] || '$external'
|
42
|
+
db = client['test']
|
43
|
+
|
44
|
+
# should successfully authenticate
|
45
|
+
assert db.authenticate(ENV['MONGODB_SASL_USER'], ENV['MONGODB_SASL_PASS'], true, source, 'PLAIN')
|
46
|
+
assert client[source].logout
|
47
|
+
|
48
|
+
# should raise on missing password
|
49
|
+
ex = assert_raise Mongo::MongoArgumentError do
|
50
|
+
db.authenticate(ENV['MONGODB_SASL_USER'], nil, true, source, 'PLAIN')
|
51
|
+
end
|
52
|
+
assert_match /username and password are required/, ex.message
|
53
|
+
|
54
|
+
# should raise on invalid password
|
55
|
+
assert_raise Mongo::AuthenticationError do
|
56
|
+
db.authenticate(ENV['MONGODB_SASL_USER'], 'foo', true, source, 'PLAIN')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_plain_authenticate_from_uri
|
61
|
+
source = ENV['MONGODB_SASL_SOURCE'] || '$external'
|
62
|
+
|
63
|
+
uri = "mongodb://#{ENV['MONGODB_SASL_USER']}:#{ENV['MONGODB_SASL_PASS']}@" +
|
64
|
+
"#{ENV['MONGODB_SASL_HOST']}/some_db?authSource=#{source}" +
|
65
|
+
"&authMechanism=PLAIN"
|
66
|
+
|
67
|
+
client = @client.class.from_uri(uri)
|
68
|
+
db = client['test']
|
69
|
+
|
70
|
+
# should be able to checkout a socket (authentication gets applied)
|
71
|
+
assert socket = client.checkout_reader(:mode => :primary)
|
72
|
+
client[source].logout(:socket => socket)
|
73
|
+
client.checkin(socket)
|
74
|
+
|
75
|
+
uri = "mongodb://#{ENV['MONGODB_SASL_USER']}@#{ENV['MONGODB_SASL_HOST']}/" +
|
76
|
+
"some_db?authSource=#{source}&authMechanism=PLAIN"
|
77
|
+
|
78
|
+
# should raise for missing password
|
79
|
+
ex = assert_raise Mongo::MongoArgumentError do
|
80
|
+
client = @client.class.from_uri(uri)
|
81
|
+
end
|
82
|
+
assert_match /username and password are required/, ex.message
|
83
|
+
|
84
|
+
uri = "mongodb://#{ENV['MONGODB_SASL_USER']}:foo@#{ENV['MONGODB_SASL_HOST']}/" +
|
85
|
+
"some_db?authSource=#{source}&authMechanism=PLAIN"
|
86
|
+
|
87
|
+
# should raise for invalid password
|
88
|
+
client = @client.class.from_uri(uri)
|
89
|
+
assert_raise Mongo::AuthenticationError do
|
90
|
+
client.checkout_reader(:mode => :primary)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module SSLTests
|
16
|
+
include Mongo
|
17
|
+
|
18
|
+
CERT_PATH = "#{Dir.pwd}/test/fixtures/certificates/"
|
19
|
+
CLIENT_CERT = "#{CERT_PATH}client.pem"
|
20
|
+
CA_CERT = "#{CERT_PATH}ca.pem"
|
21
|
+
|
22
|
+
def create_client(*args)
|
23
|
+
if @client_class == MongoClient
|
24
|
+
@client_class.new(*args[0], args[1])
|
25
|
+
else
|
26
|
+
@client_class.new(args[0], args[1])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# This test doesn't connect, no server config required
|
31
|
+
def test_ssl_configuration
|
32
|
+
# raises when ssl=false and ssl opts specified
|
33
|
+
assert_raise MongoArgumentError do
|
34
|
+
create_client(@connect_info, :connect => false,
|
35
|
+
:ssl => false,
|
36
|
+
:ssl_cert => CLIENT_CERT)
|
37
|
+
end
|
38
|
+
|
39
|
+
# raises when ssl=nil and ssl opts specified
|
40
|
+
assert_raise MongoArgumentError do
|
41
|
+
create_client(@connect_info, :connect => false,
|
42
|
+
:ssl_key => CLIENT_CERT)
|
43
|
+
end
|
44
|
+
|
45
|
+
# raises when verify=true and no ca_cert
|
46
|
+
assert_raise MongoArgumentError do
|
47
|
+
create_client(@connect_info, :connect => false,
|
48
|
+
:ssl => true,
|
49
|
+
:ssl_key => CLIENT_CERT,
|
50
|
+
:ssl_cert => CLIENT_CERT,
|
51
|
+
:ssl_verify => true)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Requires MongoDB built with SSL and the follow options:
|
56
|
+
#
|
57
|
+
# mongod --dbpath /path/to/data/directory --sslOnNormalPorts \
|
58
|
+
# --sslPEMKeyFile /path/to/server.pem \
|
59
|
+
# --sslCAFile /path/to/ca.pem \
|
60
|
+
# --sslCRLFile /path/to/crl.pem \
|
61
|
+
# --sslWeakCertificateValidation
|
62
|
+
#
|
63
|
+
# Make sure you have 'server' as an alias for localhost in /etc/hosts
|
64
|
+
#
|
65
|
+
def test_ssl_basic
|
66
|
+
client = create_client(@connect_info, :connect => false, :ssl => true)
|
67
|
+
assert client.connect
|
68
|
+
end
|
69
|
+
|
70
|
+
# Requires MongoDB built with SSL and the follow options:
|
71
|
+
#
|
72
|
+
# mongod --dbpath /path/to/data/directory --sslOnNormalPorts \
|
73
|
+
# --sslPEMKeyFile /path/to/server.pem \
|
74
|
+
# --sslCAFile /path/to/ca.pem \
|
75
|
+
# --sslCRLFile /path/to/crl.pem
|
76
|
+
#
|
77
|
+
# Make sure you have 'server' as an alias for localhost in /etc/hosts
|
78
|
+
#
|
79
|
+
def test_ssl_with_cert
|
80
|
+
client = create_client(@connect_info, :connect => false,
|
81
|
+
:ssl => true,
|
82
|
+
:ssl_cert => CLIENT_CERT,
|
83
|
+
:ssl_key => CLIENT_CERT)
|
84
|
+
assert client.connect
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_ssl_with_peer_cert_validation
|
88
|
+
client = create_client(@connect_info, :connect => false,
|
89
|
+
:ssl => true,
|
90
|
+
:ssl_key => CLIENT_CERT,
|
91
|
+
:ssl_cert => CLIENT_CERT,
|
92
|
+
:ssl_verify => true,
|
93
|
+
:ssl_ca_cert => CA_CERT)
|
94
|
+
assert client.connect
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_ssl_peer_cert_validation_hostname_fail
|
98
|
+
client = create_client(@bad_connect_info, :connect => false,
|
99
|
+
:ssl => true,
|
100
|
+
:ssl_key => CLIENT_CERT,
|
101
|
+
:ssl_cert => CLIENT_CERT,
|
102
|
+
:ssl_verify => true,
|
103
|
+
:ssl_ca_cert => CA_CERT)
|
104
|
+
assert_raise ConnectionFailure do
|
105
|
+
client.connect
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Requires mongod built with SSL and the follow options:
|
110
|
+
#
|
111
|
+
# mongod --dbpath /path/to/data/directory --sslOnNormalPorts \
|
112
|
+
# --sslPEMKeyFile /path/to/server.pem \
|
113
|
+
# --sslCAFile /path/to/ca.pem \
|
114
|
+
# --sslCRLFile /path/to/crl_client_revoked.pem
|
115
|
+
#
|
116
|
+
# Make sure you have 'server' as an alias for localhost in /etc/hosts
|
117
|
+
#
|
118
|
+
def test_ssl_with_invalid_cert
|
119
|
+
assert_raise ConnectionFailure do
|
120
|
+
create_client(@connect_info, :ssl => true,
|
121
|
+
:ssl_key => CLIENT_CERT,
|
122
|
+
:ssl_cert => CLIENT_CERT,
|
123
|
+
:ssl_verify => true,
|
124
|
+
:ssl_ca_cert => CA_CERT)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# X509 Authentication Tests
|
129
|
+
#
|
130
|
+
# Requires MongoDB built with SSL and the follow options:
|
131
|
+
#
|
132
|
+
# mongod --auth --dbpath /path/to/data/directory --sslOnNormalPorts \
|
133
|
+
# --sslPEMKeyFile /path/to/server.pem \
|
134
|
+
# --sslCAFile /path/to/ca.pem \
|
135
|
+
# --sslCRLFile /path/to/crl.pem
|
136
|
+
#
|
137
|
+
if ENV.key?('MONGODB_X509_USER')
|
138
|
+
|
139
|
+
def test_x509_authentication
|
140
|
+
mechanism = 'MONGODB-X509'
|
141
|
+
client = create_client(@connect_info, :ssl => true,
|
142
|
+
:ssl_cert => CLIENT_CERT)
|
143
|
+
|
144
|
+
return unless client.server_version > '2.5.2'
|
145
|
+
|
146
|
+
user = ENV['MONGODB_X509_USER']
|
147
|
+
db = client.db('$external')
|
148
|
+
|
149
|
+
# add user for test (enable auth)
|
150
|
+
roles = [{:role => 'readWriteAnyDatabase', :db => 'admin'},
|
151
|
+
{:role => 'userAdminAnyDatabase', :db => 'admin'}]
|
152
|
+
db.add_user(user, nil, false, :roles => roles)
|
153
|
+
|
154
|
+
assert db.authenticate(user, nil, nil, nil, mechanism)
|
155
|
+
assert db.collection_names
|
156
|
+
|
157
|
+
assert db.logout
|
158
|
+
assert_raise Mongo::AuthenticationError do
|
159
|
+
db.collection_names
|
160
|
+
end
|
161
|
+
|
162
|
+
assert MongoReplicaSetClient.from_uri(
|
163
|
+
"mongodb://#{user}@#{@uri_info}/admin?authMechanism=#{mechanism}")
|
164
|
+
assert db.collection_names
|
165
|
+
|
166
|
+
# clean up and remove all users
|
167
|
+
db.command(:dropAllUsersFromDatabase => 1)
|
168
|
+
db.logout
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2013
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -12,213 +12,45 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
|
16
|
-
require 'pry-rescue'
|
17
|
-
require 'pry-nav'
|
18
|
-
rescue LoadError
|
19
|
-
# failed to load, skipping pry
|
20
|
-
end
|
21
|
-
|
22
|
-
# SimpleCov must load before our code - A coverage report summary line will print after each test suite
|
23
|
-
if RUBY_VERSION >= '1.9.0' && RUBY_ENGINE == 'ruby'
|
24
|
-
if ENV.key?('COVERAGE')
|
25
|
-
require 'simplecov'
|
26
|
-
SimpleCov.start do
|
27
|
-
add_group "Mongo", 'lib/mongo'
|
28
|
-
add_group "BSON", 'lib/bson'
|
29
|
-
add_filter "/test/"
|
30
|
-
merge_timeout 3600
|
31
|
-
command_name ENV['SIMPLECOV_COMMAND_NAME'] if ENV.has_key?('SIMPLECOV_COMMAND_NAME')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
gem 'test-unit' # Do NOT remove this line - gem version is needed for Test::Unit::TestCase.shutdown
|
36
|
-
require 'test/unit'
|
37
|
-
require 'tools/mongo_config'
|
38
|
-
|
39
|
-
class Test::Unit::TestCase
|
40
|
-
|
41
|
-
TEST_DATA = File.join(File.dirname(__FILE__), 'fixtures/data')
|
42
|
-
|
43
|
-
def ensure_cluster(kind=nil, opts={})
|
44
|
-
@@cluster ||= nil
|
45
|
-
|
46
|
-
unless @@cluster
|
47
|
-
if kind == :rs
|
48
|
-
cluster_opts = Mongo::Config::DEFAULT_REPLICA_SET.dup
|
49
|
-
else
|
50
|
-
cluster_opts = Mongo::Config::DEFAULT_SHARDED_SIMPLE.dup
|
51
|
-
end
|
52
|
-
|
53
|
-
cluster_opts.merge!(opts)
|
54
|
-
|
55
|
-
dbpath = ENV['DBPATH'] || 'data'
|
56
|
-
cluster_opts.merge!(:dbpath => dbpath)
|
15
|
+
# NOTE: on ruby <1.9 you need to run individual tests with 'bundle exec'
|
57
16
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
@@cluster = Mongo::Config::ClusterManager.new(config)
|
17
|
+
unless RUBY_VERSION < '1.9' || ENV.key?('JENKINS_CI')
|
18
|
+
require 'simplecov'
|
19
|
+
require 'coveralls'
|
62
20
|
|
63
|
-
|
64
|
-
|
21
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
22
|
+
SimpleCov::Formatter::HTMLFormatter,
|
23
|
+
Coveralls::SimpleCov::Formatter
|
24
|
+
]
|
65
25
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
@@cluster.clobber
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
26
|
+
SimpleCov.start do
|
27
|
+
add_group 'Driver', 'lib/mongo'
|
28
|
+
add_group 'BSON', 'lib/bson'
|
74
29
|
|
75
|
-
|
76
|
-
|
30
|
+
add_filter 'tasks'
|
31
|
+
add_filter 'test'
|
32
|
+
add_filter 'bin'
|
77
33
|
end
|
78
|
-
|
79
|
-
# Generic code for rescuing connection failures and retrying operations.
|
80
|
-
# This could be combined with some timeout functionality.
|
81
|
-
def rescue_connection_failure(max_retries=30)
|
82
|
-
retries = 0
|
83
|
-
begin
|
84
|
-
yield
|
85
|
-
rescue Mongo::ConnectionFailure => ex
|
86
|
-
#puts "Rescue attempt #{retries}: from #{ex}"
|
87
|
-
retries += 1
|
88
|
-
raise ex if retries > max_retries
|
89
|
-
sleep(2)
|
90
|
-
retry
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def silently
|
96
|
-
warn_level = $VERBOSE
|
97
|
-
$VERBOSE = nil
|
98
|
-
begin
|
99
|
-
result = yield
|
100
|
-
ensure
|
101
|
-
$VERBOSE = warn_level
|
102
|
-
end
|
103
|
-
result
|
104
|
-
end
|
105
|
-
|
106
|
-
begin
|
107
|
-
silently { require 'shoulda' }
|
108
|
-
silently { require 'mocha/setup' }
|
109
|
-
rescue LoadError
|
110
|
-
puts <<MSG
|
111
|
-
|
112
|
-
This test suite requires shoulda and mocha.
|
113
|
-
You can install them as follows:
|
114
|
-
gem install shoulda
|
115
|
-
gem install mocha
|
116
|
-
|
117
|
-
MSG
|
118
|
-
exit
|
119
|
-
end
|
120
|
-
|
121
|
-
unless defined? MONGO_TEST_DB
|
122
|
-
MONGO_TEST_DB = 'ruby-test-db'
|
123
|
-
end
|
124
|
-
|
125
|
-
unless defined? TEST_PORT
|
126
|
-
TEST_PORT = ENV['MONGO_RUBY_DRIVER_PORT'] ? ENV['MONGO_RUBY_DRIVER_PORT'].to_i : Mongo::MongoClient::DEFAULT_PORT
|
127
|
-
end
|
128
|
-
|
129
|
-
unless defined? TEST_HOST
|
130
|
-
TEST_HOST = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
131
34
|
end
|
132
35
|
|
133
|
-
|
134
|
-
|
135
|
-
include BSON
|
136
|
-
|
137
|
-
def self.standard_connection(options={}, legacy=false)
|
138
|
-
if legacy
|
139
|
-
Connection.new(TEST_HOST, TEST_PORT, options)
|
140
|
-
else
|
141
|
-
MongoClient.new(TEST_HOST, TEST_PORT, options)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
def standard_connection(options={}, legacy=false)
|
146
|
-
self.class.standard_connection(options, legacy)
|
147
|
-
end
|
148
|
-
|
149
|
-
def self.host_port
|
150
|
-
"#{mongo_host}:#{mongo_port}"
|
151
|
-
end
|
152
|
-
|
153
|
-
def self.mongo_host
|
154
|
-
TEST_HOST
|
155
|
-
end
|
156
|
-
|
157
|
-
def self.mongo_port
|
158
|
-
TEST_PORT
|
159
|
-
end
|
160
|
-
|
161
|
-
def host_port
|
162
|
-
self.class.host_port
|
163
|
-
end
|
164
|
-
|
165
|
-
def mongo_host
|
166
|
-
self.class.mongo_host
|
167
|
-
end
|
168
|
-
|
169
|
-
def mongo_port
|
170
|
-
self.class.mongo_port
|
171
|
-
end
|
36
|
+
# required for at_exit, at_start hooks
|
37
|
+
require 'test-unit'
|
172
38
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
end
|
177
|
-
|
178
|
-
def step_down_command
|
179
|
-
# Adding force=true to avoid 'no secondaries within 10 seconds of my optime' errors
|
180
|
-
step_down_command = BSON::OrderedHash.new
|
181
|
-
step_down_command[:replSetStepDown] = 5
|
182
|
-
step_down_command[:force] = true
|
183
|
-
step_down_command
|
184
|
-
end
|
185
|
-
|
186
|
-
def new_mock_socket(host='localhost', port=27017)
|
187
|
-
socket = Object.new
|
188
|
-
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
189
|
-
socket.stubs(:close)
|
190
|
-
socket.stubs(:closed?)
|
191
|
-
socket.stubs(:checkin)
|
192
|
-
socket.stubs(:pool)
|
193
|
-
socket
|
194
|
-
end
|
195
|
-
|
196
|
-
def new_mock_unix_socket(sockfile='/tmp/mongod.sock')
|
197
|
-
socket = Object.new
|
198
|
-
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP)
|
199
|
-
socket.stubs(:close)
|
200
|
-
socket.stubs(:closed?)
|
201
|
-
socket
|
202
|
-
end
|
39
|
+
require 'test/unit'
|
40
|
+
require 'shoulda'
|
41
|
+
require 'mocha/setup'
|
203
42
|
|
204
|
-
|
205
|
-
|
206
|
-
end
|
43
|
+
# cluster manager
|
44
|
+
require 'tools/mongo_config'
|
207
45
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
rescue => e
|
212
|
-
if klass.to_s != e.class.to_s
|
213
|
-
flunk "Expected exception class #{klass} but got #{e.class}.\n #{e.backtrace}"
|
214
|
-
end
|
46
|
+
# test helpers
|
47
|
+
require 'helpers/general'
|
48
|
+
require 'helpers/test_unit'
|
215
49
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
end
|
223
|
-
end
|
50
|
+
# optional development and debug utilities
|
51
|
+
begin
|
52
|
+
require 'pry-rescue'
|
53
|
+
require 'pry-nav'
|
54
|
+
rescue LoadError
|
55
|
+
# failed to load, skipping pry
|
224
56
|
end
|