1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# frozen_string_literal: true
# rubocop:todo all
module Unified
module DdlOperations
def list_databases(op)
list_dbs(op, name_only: false)
end
def list_database_names(op)
list_dbs(op, name_only: false)
end
def list_dbs(op, name_only: false)
client = entities.get(:client, op.use!('object'))
use_arguments(op) do |args|
opts = {}
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
if timeout_ms = args.use('timeoutMS')
opts[:timeout_ms] = timeout_ms
end
client.list_databases(args.use('filter') || {}, name_only, **opts)
end
end
def create_collection(op)
database = entities.get(:database, op.use!('object'))
use_arguments(op) do |args|
opts = {}
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
collection_opts = {}
if timeseries = args.use('timeseries')
collection_opts[:time_series] = timeseries
end
if expire_after_seconds = args.use('expireAfterSeconds')
collection_opts[:expire_after] = expire_after_seconds
end
if clustered_index = args.use('clusteredIndex')
collection_opts[:clustered_index] = clustered_index
end
if change_stream_pre_and_post_images = args.use('changeStreamPreAndPostImages')
collection_opts[:change_stream_pre_and_post_images] = change_stream_pre_and_post_images
end
if view_on = args.use('viewOn')
collection_opts[:view_on] = view_on
end
if pipeline = args.use('pipeline')
collection_opts[:pipeline] = pipeline
end
if capped = args.use('capped')
collection_opts[:capped] = capped
end
if size = args.use('size')
collection_opts[:size] = size
end
if max = args.use('max')
collection_opts[:max] = max
end
database[args.use!('collection'), collection_opts].create(**opts)
end
end
def list_collections(op)
list_colls(op, name_only: false)
end
def list_collection_names(op)
list_colls(op, name_only: true)
end
def list_colls(op, name_only: false)
database = entities.get(:database, op.use!('object'))
use_arguments(op) do |args|
opts = extract_options(args, 'filter', 'timeoutMode', allow_extra: true)
symbolize_options!(opts, :timeout_mode)
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
if timeout_ms = args.use('timeoutMS')
opts[:timeout_ms] = timeout_ms
end
database.list_collections(**opts.merge(name_only: name_only))
end
end
def drop_collection(op)
database = entities.get(:database, op.use!('object'))
use_arguments(op) do |args|
collection = database[args.use!('collection')]
collection.drop
end
end
def rename(op)
collection = entities.get(:collection, op.use!('object'))
use_arguments(op) do |args|
to = args.use!('to')
cmd = {
renameCollection: "#{collection.database.name}.#{collection.name}",
to: "#{collection.database.name}.#{to}"
}
if args.key?("dropTarget")
cmd[:dropTarget] = args.use("dropTarget")
end
collection.client.use(:admin).command(**cmd)
end
end
def assert_collection_exists(op, state = true)
consume_test_runner(op)
use_arguments(op) do |args|
client = ClientRegistry.instance.global_client('authorized')
database = client.use(args.use!('databaseName')).database
collection_name = args.use!('collectionName')
if state
unless database.collection_names.include?(collection_name)
raise Error::ResultMismatch, "Expected collection #{collection_name} to exist, but it does not"
end
else
if database.collection_names.include?(collection_name)
raise Error::ResultMismatch, "Expected collection #{collection_name} to not exist, but it does"
end
end
end
end
def assert_collection_not_exists(op)
assert_collection_exists(op, false)
end
def list_indexes(op)
collection = entities.get(:collection, op.use!('object'))
use_arguments(op) do |args|
opts = extract_options(args, 'timeoutMode', allow_extra: true)
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
if timeout_ms = args.use('timeoutMS')
opts[:timeout_ms] = timeout_ms
end
collection.indexes(**opts).to_a
end
end
def drop_indexes(op)
collection = entities.get(:collection, op.use!('object'))
use_arguments(op) do |args|
opts = extract_options(args, 'maxTimeMS', 'timeoutMS', allow_extra: true)
collection.indexes.drop_all(**opts)
end
end
def create_index(op)
collection = entities.get(:collection, op.use!('object'))
use_arguments(op) do |args|
opts = {}
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
if args.key?('unique')
opts[:unique] = args.use('unique')
end
if timeout_ms = args.use('timeoutMS')
opts[:timeout_ms] = timeout_ms
end
if max_time_ms = args.use('maxTimeMS')
opts[:max_time_ms] = max_time_ms
end
collection.indexes.create_one(
args.use!('keys'),
name: args.use('name'),
**opts,
)
end
end
def drop_index(op)
collection = entities.get(:collection, op.use!('object'))
use_arguments(op) do |args|
opts = extract_options(args, 'maxTimeMS', 'timeoutMS', allow_extra: true)
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
collection.indexes.drop_one(
args.use!('name'),
**opts,
)
end
end
def assert_index_exists(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = ClientRegistry.instance.global_client('authorized')
database = client.use(args.use!('databaseName'))
collection = database[args.use!('collectionName')]
index = collection.indexes.get(args.use!('indexName'))
end
end
def assert_index_not_exists(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = ClientRegistry.instance.global_client('authorized')
database = client.use(args.use!('databaseName'))
collection = database[args.use!('collectionName')]
begin
index = collection.indexes.get(args.use!('indexName'))
raise Error::ResultMismatch, "Index found"
rescue Mongo::Error::OperationFailure::Family => e
if e.code == 26
# OK
else
raise
end
end
end
end
def create_entities(op)
consume_test_runner(op)
use_arguments(op) do |args|
generate_entities(args.use!('entities'))
end
end
def record_topology_description(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = entities.get(:client, args.use!('client'))
entities.set(:topology, args.use!('id'), client.cluster.topology)
end
end
def assert_topology_type(op)
consume_test_runner(op)
use_arguments(op) do |args|
topology = entities.get(:topology, args.use!('topologyDescription'))
type = args.use!('topologyType')
unless topology.display_name == type
raise Error::ResultMismatch, "Expected topology type to be #{type}, but got #{topology.class}"
end
end
end
def retrieve_primary(topology)
topology.server_descriptions.detect { |k, desc| desc.primary? }&.first
end
def wait_for_primary_change(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = entities.get(:client, args.use!('client'))
topology = entities.get(:topology, args.use!('priorTopologyDescription'))
timeout_ms = args.use('timeoutMS') || 10000
old_primary = retrieve_primary(topology)
deadline = Mongo::Utils.monotonic_time + timeout_ms / 1000.0
loop do
client.cluster.scan!
new_primary = client.cluster.next_primary.address
if new_primary && old_primary != new_primary
break
end
if Mongo::Utils.monotonic_time >= deadline
raise "Did not receive a change in primary from #{old_primary} in 10 seconds"
else
sleep 0.1
end
end
end
end
end
end
|