mongoid 8.0.9 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +18 -67
  6. data/lib/config/locales/en.yml +46 -14
  7. data/lib/mongoid/association/accessors.rb +3 -7
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/eager_loadable.rb +0 -3
  10. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  11. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  12. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  13. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  14. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  15. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  16. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
  17. data/lib/mongoid/association/macros.rb +0 -6
  18. data/lib/mongoid/association/nested/one.rb +40 -2
  19. data/lib/mongoid/association/proxy.rb +1 -1
  20. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  21. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  22. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -2
  23. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  24. data/lib/mongoid/association/reflections.rb +2 -2
  25. data/lib/mongoid/atomic.rb +7 -16
  26. data/lib/mongoid/attributes/dynamic.rb +1 -1
  27. data/lib/mongoid/attributes/nested.rb +2 -2
  28. data/lib/mongoid/attributes/processing.rb +5 -29
  29. data/lib/mongoid/attributes/projector.rb +1 -1
  30. data/lib/mongoid/attributes/readonly.rb +1 -1
  31. data/lib/mongoid/attributes.rb +8 -2
  32. data/lib/mongoid/changeable.rb +107 -5
  33. data/lib/mongoid/clients/storage_options.rb +2 -5
  34. data/lib/mongoid/clients/validators/storage.rb +1 -13
  35. data/lib/mongoid/collection_configurable.rb +58 -0
  36. data/lib/mongoid/composable.rb +2 -0
  37. data/lib/mongoid/config/defaults.rb +60 -0
  38. data/lib/mongoid/config/options.rb +0 -3
  39. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  40. data/lib/mongoid/config/validators.rb +1 -0
  41. data/lib/mongoid/config.rb +88 -27
  42. data/lib/mongoid/contextual/atomic.rb +1 -1
  43. data/lib/mongoid/contextual/memory.rb +233 -33
  44. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  45. data/lib/mongoid/contextual/mongo.rb +370 -133
  46. data/lib/mongoid/contextual/none.rb +162 -7
  47. data/lib/mongoid/contextual.rb +12 -0
  48. data/lib/mongoid/criteria/findable.rb +2 -2
  49. data/lib/mongoid/criteria/includable.rb +4 -3
  50. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -15
  51. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  52. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
  53. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  54. data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
  55. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  56. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  57. data/lib/mongoid/criteria.rb +6 -5
  58. data/lib/mongoid/deprecable.rb +1 -2
  59. data/lib/mongoid/deprecation.rb +3 -3
  60. data/lib/mongoid/document.rb +1 -8
  61. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  62. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  63. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  64. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  65. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  66. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  67. data/lib/mongoid/errors.rb +4 -1
  68. data/lib/mongoid/extensions/hash.rb +2 -24
  69. data/lib/mongoid/extensions/object.rb +2 -2
  70. data/lib/mongoid/extensions/time.rb +2 -0
  71. data/lib/mongoid/fields/localized.rb +10 -0
  72. data/lib/mongoid/fields/standard.rb +10 -0
  73. data/lib/mongoid/fields.rb +59 -35
  74. data/lib/mongoid/findable.rb +27 -3
  75. data/lib/mongoid/interceptable.rb +6 -116
  76. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  77. data/lib/mongoid/matcher/type.rb +1 -1
  78. data/lib/mongoid/persistable/creatable.rb +1 -0
  79. data/lib/mongoid/persistable/deletable.rb +1 -1
  80. data/lib/mongoid/persistable/savable.rb +13 -1
  81. data/lib/mongoid/persistable/unsettable.rb +2 -2
  82. data/lib/mongoid/persistable/updatable.rb +51 -1
  83. data/lib/mongoid/persistable/upsertable.rb +20 -1
  84. data/lib/mongoid/persistable.rb +3 -0
  85. data/lib/mongoid/query_cache.rb +5 -1
  86. data/lib/mongoid/railties/database.rake +7 -2
  87. data/lib/mongoid/reloadable.rb +5 -3
  88. data/lib/mongoid/stateful.rb +22 -1
  89. data/lib/mongoid/tasks/database.rake +12 -0
  90. data/lib/mongoid/tasks/database.rb +20 -0
  91. data/lib/mongoid/timestamps/created.rb +1 -8
  92. data/lib/mongoid/traversable.rb +0 -12
  93. data/lib/mongoid/utils.rb +22 -0
  94. data/lib/mongoid/validatable/associated.rb +17 -98
  95. data/lib/mongoid/validatable/macros.rb +5 -5
  96. data/lib/mongoid/validatable.rb +4 -9
  97. data/lib/mongoid/version.rb +1 -1
  98. data/lib/mongoid/warnings.rb +17 -1
  99. data/lib/mongoid.rb +16 -3
  100. data/spec/integration/app_spec.rb +2 -2
  101. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -40
  102. data/spec/integration/callbacks_spec.rb +99 -12
  103. data/spec/integration/discriminator_key_spec.rb +4 -5
  104. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  105. data/spec/mongoid/association/eager_spec.rb +2 -24
  106. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  107. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  108. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  109. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +0 -4
  110. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  111. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  112. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +5 -27
  113. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  114. data/spec/mongoid/association/syncable_spec.rb +1 -1
  115. data/spec/mongoid/association_spec.rb +0 -60
  116. data/spec/mongoid/attributes_spec.rb +3 -33
  117. data/spec/mongoid/changeable_spec.rb +299 -24
  118. data/spec/mongoid/clients_spec.rb +122 -13
  119. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  120. data/spec/mongoid/config/defaults_spec.rb +160 -0
  121. data/spec/mongoid/config_spec.rb +154 -27
  122. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  123. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  124. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  125. data/spec/mongoid/contextual/none_spec.rb +49 -2
  126. data/spec/mongoid/copyable_spec.rb +2 -10
  127. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  128. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  129. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  130. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  131. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  132. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  133. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  134. data/spec/mongoid/criteria_spec.rb +5 -9
  135. data/spec/mongoid/document_spec.rb +0 -27
  136. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  137. data/spec/mongoid/extensions/hash_spec.rb +3 -3
  138. data/spec/mongoid/extensions/time_spec.rb +8 -43
  139. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  140. data/spec/mongoid/fields/localized_spec.rb +46 -28
  141. data/spec/mongoid/fields_spec.rb +136 -77
  142. data/spec/mongoid/findable_spec.rb +391 -34
  143. data/spec/mongoid/indexable_spec.rb +16 -10
  144. data/spec/mongoid/interceptable_spec.rb +153 -442
  145. data/spec/mongoid/interceptable_spec_models.rb +111 -51
  146. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  147. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  148. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  149. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  150. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  151. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  152. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  153. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  154. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  155. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  156. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  157. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  158. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  159. data/spec/mongoid/persistence_context_spec.rb +7 -57
  160. data/spec/mongoid/query_cache_spec.rb +56 -61
  161. data/spec/mongoid/reloadable_spec.rb +24 -28
  162. data/spec/mongoid/scopable_spec.rb +70 -0
  163. data/spec/mongoid/serializable_spec.rb +23 -44
  164. data/spec/mongoid/stateful_spec.rb +122 -8
  165. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  166. data/spec/mongoid/tasks/database_spec.rb +127 -0
  167. data/spec/mongoid/timestamps/created_spec.rb +0 -23
  168. data/spec/mongoid/timestamps_spec.rb +9 -11
  169. data/spec/mongoid/touchable_spec.rb +277 -5
  170. data/spec/mongoid/touchable_spec_models.rb +3 -1
  171. data/spec/mongoid/traversable_spec.rb +9 -24
  172. data/spec/mongoid/validatable/associated_spec.rb +34 -27
  173. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  174. data/spec/mongoid_spec.rb +36 -10
  175. data/spec/shared/LICENSE +20 -0
  176. data/spec/shared/bin/get-mongodb-download-url +17 -0
  177. data/spec/shared/bin/s3-copy +45 -0
  178. data/spec/shared/bin/s3-upload +69 -0
  179. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  180. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  181. data/spec/shared/lib/mrss/constraints.rb +378 -0
  182. data/spec/shared/lib/mrss/docker_runner.rb +298 -0
  183. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  184. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  185. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  186. data/spec/shared/lib/mrss/server_version_registry.rb +113 -0
  187. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  188. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  189. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  190. data/spec/shared/lib/mrss/utils.rb +37 -0
  191. data/spec/shared/share/Dockerfile.erb +321 -0
  192. data/spec/shared/share/haproxy-1.conf +16 -0
  193. data/spec/shared/share/haproxy-2.conf +17 -0
  194. data/spec/shared/shlib/config.sh +27 -0
  195. data/spec/shared/shlib/distro.sh +74 -0
  196. data/spec/shared/shlib/server.sh +416 -0
  197. data/spec/shared/shlib/set_env.sh +169 -0
  198. data/spec/spec_helper.rb +5 -0
  199. data/spec/support/immutable_ids.rb +118 -0
  200. data/spec/support/macros.rb +47 -15
  201. data/spec/support/models/artist.rb +0 -1
  202. data/spec/support/models/band.rb +1 -0
  203. data/spec/support/models/book.rb +1 -0
  204. data/spec/support/models/building.rb +2 -0
  205. data/spec/support/models/cover.rb +10 -0
  206. data/spec/support/models/name.rb +0 -10
  207. data/spec/support/models/person.rb +0 -1
  208. data/spec/support/models/product.rb +1 -0
  209. data.tar.gz.sig +0 -0
  210. metadata +746 -636
  211. metadata.gz.sig +2 -0
  212. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  213. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
  214. data/spec/support/models/purse.rb +0 -9
@@ -0,0 +1,231 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ # ClusterConfig requires ClientRegistry class provided by the host project.
5
+
6
+ require 'singleton'
7
+
8
+ module Mrss
9
+ class ClusterConfig
10
+ include Singleton
11
+ include RSpec::Core::Pending
12
+
13
+ def single_server?
14
+ determine_cluster_config
15
+ @single_server
16
+ end
17
+
18
+ def sharded_ish?
19
+ determine_cluster_config
20
+ @topology == :sharded || @topology == :load_balanced
21
+ end
22
+
23
+ def replica_set_name
24
+ determine_cluster_config
25
+ @replica_set_name
26
+ end
27
+
28
+ def server_version
29
+ determine_cluster_config
30
+ @server_version
31
+ end
32
+
33
+ def enterprise?
34
+ determine_cluster_config
35
+ @enterprise
36
+ end
37
+
38
+ def short_server_version
39
+ server_version.split('.')[0..1].join('.')
40
+ end
41
+
42
+ def fcv
43
+ determine_cluster_config
44
+ @fcv
45
+ end
46
+
47
+ # Per https://jira.mongodb.org/browse/SERVER-39052, working with FCV
48
+ # in sharded topologies is annoying. Also, FCV doesn't exist in servers
49
+ # less than 3.4. This method returns FCV on 3.4+ servers when in single
50
+ # or RS topologies, and otherwise returns the major.minor server version.
51
+ def fcv_ish
52
+ if server_version.nil?
53
+ raise "Deployment server version not known - check that connection to deployment succeeded"
54
+ end
55
+
56
+ if server_version >= '3.4' && !sharded_ish?
57
+ fcv
58
+ else
59
+ if short_server_version == '4.1'
60
+ '4.2'
61
+ else
62
+ short_server_version
63
+ end
64
+ end
65
+ end
66
+
67
+ # @return [ Mongo::Address ] The address of the primary in the deployment.
68
+ def primary_address
69
+ determine_cluster_config
70
+ @primary_address
71
+ end
72
+
73
+ def primary_address_str
74
+ determine_cluster_config
75
+ @primary_address.seed
76
+ end
77
+
78
+ def primary_address_host
79
+ both = primary_address_str
80
+ both.split(':').first
81
+ end
82
+
83
+ def primary_address_port
84
+ both = primary_address_str
85
+ both.split(':')[1] || 27017
86
+ end
87
+
88
+ def primary_description
89
+ determine_cluster_config
90
+ @primary_description
91
+ end
92
+
93
+ def server_parameters
94
+ determine_cluster_config
95
+ @server_parameters
96
+ end
97
+
98
+ # Try running a command on the admin database to see if the mongod was
99
+ # started with auth.
100
+ def auth_enabled?
101
+ if @auth_enabled.nil?
102
+ @auth_enabled = begin
103
+ basic_client.use(:admin).command(getCmdLineOpts: 1).first["argv"].include?("--auth")
104
+ rescue => e
105
+ e.message =~ /(not authorized)|(unauthorized)|(no users authenticated)|(requires authentication)/
106
+ end
107
+ end
108
+ @auth_enabled
109
+ end
110
+
111
+ def topology
112
+ determine_cluster_config
113
+ @topology
114
+ end
115
+
116
+ def storage_engine
117
+ @storage_engine ||= begin
118
+ # 2.6 does not have wired tiger
119
+ if short_server_version == '2.6'
120
+ :mmapv1
121
+ else
122
+ client = ClientRegistry.instance.global_client('root_authorized')
123
+ if sharded_ish?
124
+ shards = client.use(:admin).command(listShards: 1).first
125
+ if shards['shards'].empty?
126
+ raise 'Shards are empty'
127
+ end
128
+ shard = shards['shards'].first
129
+ address_str = shard['host'].sub(/^.*\//, '').sub(/,.*/, '')
130
+ client = ClusterTools.instance.direct_client(address_str,
131
+ SpecConfig.instance.test_options.merge(SpecConfig.instance.auth_options).merge(connect: :direct))
132
+ end
133
+ rv = client.use(:admin).command(serverStatus: 1).first
134
+ rv = rv['storageEngine']['name']
135
+ rv_map = {
136
+ 'wiredTiger' => :wired_tiger,
137
+ 'mmapv1' => :mmapv1,
138
+ }
139
+ rv_map[rv] || rv
140
+ end
141
+ end
142
+ end
143
+
144
+ # This method returns an alternate address for connecting to the configured
145
+ # deployment. For example, if the replica set is configured with nodes at
146
+ # of localhost:27017 and so on, this method will return 127.0.0.:27017.
147
+ #
148
+ # Note that the "alternate" refers to replica set configuration, not the
149
+ # addresses specified in test suite configuration. If the deployment topology
150
+ # is not a replica set, "alternate" refers to test suite configuration as
151
+ # this is the only configuration available.
152
+ def alternate_address
153
+ @alternate_address ||= begin
154
+ address = primary_address_host
155
+ str = case address
156
+ when '127.0.0.1'
157
+ 'localhost'
158
+ when /^(\d+\.){3}\d+$/
159
+ skip 'This test requires a hostname or 127.0.0.1 as address'
160
+ else
161
+ # We don't know if mongod is listening on ipv4 or ipv6, in principle.
162
+ # Our tests use ipv4, so hardcode that for now.
163
+ # To support both we need to try both addresses which will make this
164
+ # test more complicated.
165
+ #
166
+ # JRuby chokes on primary_address_port as the port (e.g. 27017).
167
+ # Since the port does not actually matter, use a common port like 80.
168
+ resolved_address = Addrinfo.getaddrinfo(address, 80, Socket::PF_INET).first.ip_address
169
+ if resolved_address.include?(':')
170
+ "[#{resolved_address}]"
171
+ else
172
+ resolved_address
173
+ end
174
+ end + ":#{primary_address_port}"
175
+ Mongo::Address.new(str)
176
+ end
177
+ end
178
+
179
+ private
180
+
181
+ def determine_cluster_config
182
+ return if @primary_address
183
+
184
+ # Run all commands to figure out the cluster configuration from the same
185
+ # client. This is somewhat wasteful when running a single test, but reduces
186
+ # test runtime for the suite overall because all commands are sent on the
187
+ # same connection rather than each command connecting to the cluster by
188
+ # itself.
189
+ client = ClientRegistry.instance.global_client('root_authorized')
190
+
191
+ primary = client.cluster.next_primary
192
+ @primary_address = primary.address
193
+ @primary_description = primary.description
194
+ @replica_set_name = client.cluster.topology.replica_set_name
195
+
196
+ @topology ||= begin
197
+ topology = client.cluster.topology.class.name.sub(/.*::/, '')
198
+ topology = topology.gsub(/([A-Z])/) { |match| '_' + match.downcase }.sub(/^_/, '')
199
+ if topology =~ /^replica_set/
200
+ topology = 'replica_set'
201
+ end
202
+ topology.to_sym
203
+ end
204
+
205
+ @single_server = client.cluster.servers_list.length == 1
206
+
207
+ build_info = client.database.command(buildInfo: 1).first
208
+
209
+ @server_version = build_info['version']
210
+ @enterprise = build_info['modules'] && build_info['modules'].include?('enterprise')
211
+
212
+ @server_parameters = begin
213
+ client.use(:admin).command(getParameter: '*').first
214
+ rescue => e
215
+ STDERR.puts("WARNING: Failed to obtain server parameters: #{e.class}: #{e.message}")
216
+ {}
217
+ end
218
+
219
+ if !sharded_ish? && short_server_version >= '3.4'
220
+ rv = @server_parameters['featureCompatibilityVersion']
221
+ @fcv = rv['version'] || rv
222
+ end
223
+ end
224
+
225
+ def basic_client
226
+ # Do not cache the result here so that if the client gets closed,
227
+ # client registry reconnects it in subsequent tests
228
+ ClientRegistry.instance.global_client('basic')
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,378 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ module Mrss
5
+ module Constraints
6
+ def min_server_version(version)
7
+ parsed_version = Gem::Version.new(version)
8
+
9
+ before(:all) do
10
+ if parsed_version > Gem::Version.new(ClusterConfig.instance.server_version)
11
+ skip "Server version #{version} or higher required, we have #{ClusterConfig.instance.server_version}"
12
+ end
13
+ end
14
+ end
15
+
16
+ def max_server_version(version)
17
+ parsed_version = Gem::Version.new(version)
18
+
19
+ before(:all) do
20
+ if parsed_version < Gem::Version.new(ClusterConfig.instance.server_version)
21
+ skip "Server version #{version} or lower required, we have #{ClusterConfig.instance.server_version}"
22
+ end
23
+ end
24
+ end
25
+
26
+ def min_server_fcv(version)
27
+ parsed_version = Gem::Version.new(version)
28
+
29
+ before(:all) do
30
+ unless Gem::Version.new(ClusterConfig.instance.fcv_ish) >= parsed_version
31
+ skip "FCV #{version} or higher required, we have #{ClusterConfig.instance.fcv_ish} (server #{ClusterConfig.instance.server_version})"
32
+ end
33
+ end
34
+ end
35
+
36
+ def max_server_fcv(version)
37
+ parsed_version = Gem::Version.new(version)
38
+
39
+ before(:all) do
40
+ if parsed_version < Gem::Version.new(ClusterConfig.instance.fcv_ish)
41
+ skip "FCV #{version} or lower required, we have #{ClusterConfig.instance.fcv_ish} (server #{ClusterConfig.instance.server_version})"
42
+ end
43
+ end
44
+ end
45
+
46
+ def require_topology(*topologies)
47
+ invalid_topologies = topologies - [:single, :replica_set, :sharded, :load_balanced]
48
+
49
+ unless invalid_topologies.empty?
50
+ raise ArgumentError, "Invalid topologies requested: #{invalid_topologies.join(', ')}"
51
+ end
52
+
53
+ before(:all) do
54
+ unless topologies.include?(topology = ClusterConfig.instance.topology)
55
+ skip "Topology #{topologies.join(' or ')} required, we have #{topology}"
56
+ end
57
+ end
58
+ end
59
+
60
+ def max_example_run_time(timeout)
61
+ around do |example|
62
+ TimeoutInterrupt.timeout(timeout, TimeoutInterrupt::Error.new("Test execution terminated after #{timeout} seconds")) do
63
+ example.run
64
+ end
65
+ end
66
+ end
67
+
68
+ def require_transaction_support
69
+ before(:all) do
70
+ case ClusterConfig.instance.topology
71
+ when :single
72
+ skip 'Transactions tests require a replica set (4.0+) or a sharded cluster (4.2+)'
73
+ when :replica_set
74
+ unless ClusterConfig.instance.server_version >= '4.0'
75
+ skip 'Transactions tests in a replica set topology require server 4.0+'
76
+ end
77
+ when :sharded, :load_balanced
78
+ unless ClusterConfig.instance.server_version >= '4.2'
79
+ skip 'Transactions tests in a sharded cluster topology require server 4.2+'
80
+ end
81
+ else
82
+ raise NotImplementedError
83
+ end
84
+ end
85
+ end
86
+
87
+ # Fail command fail point was added to mongod in 4.0 and to mongos in 4.2.
88
+ def require_fail_command
89
+ require_transaction_support
90
+ end
91
+
92
+ def require_tls
93
+ before(:all) do
94
+ unless SpecConfig.instance.ssl?
95
+ skip "SSL not enabled"
96
+ end
97
+ end
98
+ end
99
+
100
+ def require_no_tls
101
+ before(:all) do
102
+ if SpecConfig.instance.ssl?
103
+ skip "SSL enabled"
104
+ end
105
+ end
106
+ end
107
+
108
+ def require_retry_writes
109
+ before(:all) do
110
+ unless SpecConfig.instance.retry_writes?
111
+ skip "Retry writes is disabled"
112
+ end
113
+ end
114
+ end
115
+
116
+ def require_no_retry_writes
117
+ before(:all) do
118
+ if SpecConfig.instance.retry_writes?
119
+ skip "Retry writes is enabled"
120
+ end
121
+ end
122
+ end
123
+
124
+ def require_compression
125
+ before(:all) do
126
+ if SpecConfig.instance.compressors.nil?
127
+ skip "Compression is not enabled"
128
+ end
129
+ end
130
+ end
131
+
132
+ def require_zlib_compression
133
+ before(:all) do
134
+ compressors = SpecConfig.instance.compressors
135
+ unless compressors && compressors.include?('zlib')
136
+ skip "Zlib compression is not enabled"
137
+ end
138
+ end
139
+ end
140
+
141
+ def require_snappy_compression
142
+ before(:all) do
143
+ compressors = SpecConfig.instance.compressors
144
+ unless compressors && compressors.include?('snappy')
145
+ skip "Snappy compression is not enabled"
146
+ end
147
+ end
148
+ end
149
+
150
+ def require_no_snappy_compression
151
+ before(:all) do
152
+ compressors = SpecConfig.instance.compressors
153
+ if compressors && compressors.include?('snappy')
154
+ skip "Snappy compression is enabled"
155
+ end
156
+ end
157
+ end
158
+
159
+ def require_zstd_compression
160
+ before(:all) do
161
+ compressors = SpecConfig.instance.compressors
162
+ unless compressors && compressors.include?('zstd')
163
+ skip "Zstd compression is not enabled"
164
+ end
165
+ end
166
+ end
167
+
168
+ def require_no_zstd_compression
169
+ before(:all) do
170
+ compressors = SpecConfig.instance.compressors
171
+ if compressors && compressors.include?('zstd')
172
+ skip "Zstd compression is enabled"
173
+ end
174
+ end
175
+ end
176
+
177
+ def require_no_compression
178
+ before(:all) do
179
+ if SpecConfig.instance.compressors
180
+ skip "Compression is enabled"
181
+ end
182
+ end
183
+ end
184
+
185
+ def ruby_version_gte(version)
186
+ before(:all) do
187
+ if RUBY_VERSION < version
188
+ skip "Ruby version #{version} or higher required"
189
+ end
190
+ end
191
+ end
192
+
193
+ def ruby_version_lt(version)
194
+ before(:all) do
195
+ if RUBY_VERSION >= version
196
+ skip "Ruby version less than #{version} required"
197
+ end
198
+ end
199
+ end
200
+
201
+ def require_auth(*values)
202
+ before(:all) do
203
+ if values.any?
204
+ unless values.include?(ENV['AUTH'])
205
+ msg = values.map { |v| "AUTH=#{v}" }.join(' or ')
206
+ skip "This test requires #{msg}"
207
+ end
208
+ else
209
+ unless ENV['AUTH'] == 'auth' || SpecConfig.instance.user || ClusterConfig.instance.auth_enabled?
210
+ skip "Auth required"
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ def require_no_auth
217
+ before(:all) do
218
+ auth = ENV.fetch('AUTH', '')
219
+ if (!auth.empty? && auth != 'noauth') || SpecConfig.instance.user || ClusterConfig.instance.auth_enabled?
220
+ skip "Auth not allowed"
221
+ end
222
+ end
223
+ end
224
+
225
+ def require_x509_auth
226
+ before(:all) do
227
+ unless SpecConfig.instance.x509_auth?
228
+ skip "X.509 auth required"
229
+ end
230
+ end
231
+ end
232
+
233
+ def require_no_external_user
234
+ before(:all) do
235
+ if SpecConfig.instance.external_user?
236
+ skip "External user configurations are not compatible with this test"
237
+ end
238
+ end
239
+ end
240
+
241
+ # Can the driver specify a write concern that won't be overridden?
242
+ # (mongos 4.0+ overrides the write concern)
243
+ def require_set_write_concern
244
+ before(:all) do
245
+ if %i(sharded load_balanced).include?(ClusterConfig.instance.topology) &&
246
+ ClusterConfig.instance.short_server_version >= '4.0'
247
+ then
248
+ skip "mongos 4.0+ overrides write concern"
249
+ end
250
+ end
251
+ end
252
+
253
+ def require_multi_mongos
254
+ before(:all) do
255
+ if ClusterConfig.instance.topology == :sharded && SpecConfig.instance.addresses.length == 1
256
+ skip 'Test requires a minimum of two mongoses if run in sharded topology'
257
+ end
258
+
259
+ if ClusterConfig.instance.topology == :load_balanced && SpecConfig.instance.single_mongos?
260
+ skip 'Test requires a minimum of two mongoses if run in load-balanced topology'
261
+ end
262
+ end
263
+ end
264
+
265
+ # In sharded topology operations are distributed to the mongoses.
266
+ # When we set fail points, the fail point may be set on one mongos and
267
+ # operation may be executed on another mongos, causing failures.
268
+ # Tests that are not setting targeted fail points should utilize this
269
+ # method to restrict themselves to single mongos.
270
+ #
271
+ # In load-balanced topology, the same problem can happen when there is
272
+ # more than one mongos behind the load balancer.
273
+ def require_no_multi_mongos
274
+ before(:all) do
275
+ if ClusterConfig.instance.topology == :sharded && SpecConfig.instance.addresses.length > 1
276
+ skip 'Test requires a single mongos if run in sharded topology'
277
+ end
278
+ if ClusterConfig.instance.topology == :load_balanced && !SpecConfig.instance.single_mongos?
279
+ skip 'Test requires a single mongos, as indicated by SINGLE_MONGOS=1 environment variable, if run in load-balanced topology'
280
+ end
281
+ end
282
+ end
283
+
284
+ alias :require_no_multi_shard :require_no_multi_mongos
285
+
286
+ def require_wired_tiger
287
+ before(:all) do
288
+ # Storage detection fails for serverless instances. However, it is safe to
289
+ # assume that a serverless instance uses WiredTiger Storage Engine.
290
+ if !SpecConfig.instance.serverless? && ClusterConfig.instance.storage_engine != :wired_tiger
291
+ skip 'Test requires WiredTiger storage engine'
292
+ end
293
+ end
294
+ end
295
+
296
+ def require_wired_tiger_on_36
297
+ before(:all) do
298
+ if ClusterConfig.instance.short_server_version >= '3.6'
299
+ # Storage detection fails for serverless instances. However, it is safe to
300
+ # assume that a serverless instance uses WiredTiger Storage Engine.
301
+ if !SpecConfig.instance.serverless? && ClusterConfig.instance.storage_engine != :wired_tiger
302
+ skip 'Test requires WiredTiger storage engine on 3.6+ servers'
303
+ end
304
+ end
305
+ end
306
+ end
307
+
308
+ def require_mmapv1
309
+ before(:all) do
310
+ if SpecConfig.instance.serverless? || ClusterConfig.instance.storage_engine != :mmapv1
311
+ skip 'Test requires MMAPv1 storage engine'
312
+ end
313
+ end
314
+ end
315
+
316
+ def require_enterprise
317
+ before(:all) do
318
+ unless ClusterConfig.instance.enterprise?
319
+ skip 'Test requires enterprise build of MongoDB'
320
+ end
321
+ end
322
+ end
323
+
324
+ # Integration tests for SRV polling require internet connectivity to
325
+ # look up SRV records and a sharded cluster configured on default port on
326
+ # localhost (localhost:27017, localhost:27018).
327
+ def require_default_port_deployment
328
+ # Because the DNS records at test1.test.build.10gen.cc point to
329
+ # localhost:27017 & localhost:27018, the test suite must have been
330
+ # configured to use these addresses
331
+ before(:all) do
332
+ have_default_port = SpecConfig.instance.addresses.any? do |address|
333
+ %w(127.0.0.1 127.0.0.1:27017 localhost localhost:27017).include?(address)
334
+ end
335
+ unless have_default_port
336
+ skip 'This test requires the test suite to be configured for localhost:27017'
337
+ end
338
+ end
339
+ end
340
+
341
+ # Some tests perform assertions on what the driver is logging.
342
+ # Some test configurations, for example OCSP with unknown response,
343
+ # produce warnings due to optional checks failing.
344
+ # This constraint skips tests that issue logging assertions on configurations
345
+ # that may produce non-test-originated log entries.
346
+ def require_warning_clean
347
+ before(:all) do
348
+ if ENV['OCSP_STATUS'] == 'unknown'
349
+ skip 'Unknown OCSP status is not global warning-clean'
350
+ end
351
+ end
352
+ end
353
+
354
+ def require_required_api_version
355
+ before(:all) do
356
+ unless ENV['API_VERSION_REQUIRED'] == '1'
357
+ skip 'Set API_VERSION_REQUIRED=1 to run this test'
358
+ end
359
+ end
360
+ end
361
+
362
+ def require_no_required_api_version
363
+ before(:all) do
364
+ if ENV['API_VERSION_REQUIRED'] == '1'
365
+ skip 'Cannot have API_VERSION_REQUIRED=1 to run this test'
366
+ end
367
+ end
368
+ end
369
+
370
+ def require_unix_socket
371
+ before(:all) do
372
+ if ENV['TOPOLOGY'] == 'load-balanced'
373
+ skip 'Load balancer does not listen on Unix sockets'
374
+ end
375
+ end
376
+ end
377
+ end
378
+ end