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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
# frozen_string_literal: true
# rubocop:todo all
module Unified
module SupportOperations
def run_command(op)
database = entities.get(:database, op.use!('object'))
use_arguments(op) do |args|
args.use!('commandName')
cmd = args.use!('command')
opts = {}
if session = args.use('session')
opts[:session] = entities.get(:session, session)
end
if read_preference = args.use('readPreference')
opts[:read] = ::Utils.snakeize_hash(read_preference)
end
if timeout_ms = args.use('timeoutMS')
opts[:timeout_ms] = timeout_ms
end
database.command(cmd, **opts)
end
end
def fail_point(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = entities.get(:client, args.use!('client'))
client.command(fp = args.use('failPoint'))
$disable_fail_points ||= []
$disable_fail_points << [
fp,
ClusterConfig.instance.primary_address,
]
end
end
def targeted_fail_point(op)
consume_test_runner(op)
use_arguments(op) do |args|
session = args.use!('session')
session = entities.get(:session, session)
unless session.pinned_server
raise ArgumentError, 'Targeted fail point requires session to be pinned to a server'
end
client = ClusterTools.instance.direct_client(session.pinned_server.address,
database: 'admin')
client.command(fp = args.use!('failPoint'))
args.clear
$disable_fail_points ||= []
$disable_fail_points << [
fp,
session.pinned_server.address,
]
end
end
def end_session(op)
session = entities.get(:session, op.use!('object'))
session.end_session
end
def assert_session_dirty(op)
consume_test_runner(op)
use_arguments(op) do |args|
session = entities.get(:session, args.use!('session'))
session.dirty? || raise(Error::ResultMismatch, 'expected session to be dirty')
end
end
def assert_session_not_dirty(op)
consume_test_runner(op)
use_arguments(op) do |args|
session = entities.get(:session, args.use!('session'))
session.dirty? && raise(Error::ResultMismatch, 'expected session to be not dirty')
end
end
def assert_same_lsid_on_last_two_commands(op, expected: true)
consume_test_runner(op)
use_arguments(op) do |args|
client = entities.get(:client, args.use!('client'))
subscriber = @subscribers.fetch(client)
unless subscriber.started_events.length >= 2
raise Error::ResultMismatch, "Must have at least 2 events, have #{subscriber.started_events.length}"
end
lsids = subscriber.started_events[-2..-1].map do |cmd|
cmd.command.fetch('lsid')
end
if expected
unless lsids.first == lsids.last
raise Error::ResultMismatch, "lsids differ but they were expected to be the same"
end
else
if lsids.first == lsids.last
raise Error::ResultMismatch, "lsids are the same but they were expected to be different"
end
end
end
end
def assert_different_lsid_on_last_two_commands(op)
assert_same_lsid_on_last_two_commands(op, expected: false)
end
def start_transaction(op)
$klil_transactions = true
session = entities.get(:session, op.use!('object'))
assert_no_arguments(op)
session.start_transaction
end
def assert_session_transaction_state(op)
consume_test_runner(op)
use_arguments(op) do |args|
session = entities.get(:session, args.use!('session'))
state = args.use!('state')
unless session.send("#{state}_transaction?")
raise Error::ResultMismatch, "Expected session to have state #{state}"
end
end
end
def commit_transaction(op)
session = entities.get(:session, op.use!('object'))
opts = {}
use_arguments(op) do |args|
opts[:timeout_ms] = args.use('timeoutMS')
end
session.commit_transaction(opts.compact)
end
def abort_transaction(op)
session = entities.get(:session, op.use!('object'))
opts = {}
use_arguments(op) do |args|
opts[:timeout_ms] = args.use('timeoutMS')
end
session.abort_transaction(opts.compact)
end
def with_transaction(op)
$kill_transactions = true
session = entities.get(:session, op.use!('object'))
use_arguments(op) do |args|
ops = args.use!('callback')
if args.empty?
opts = {}
else
opts = ::Utils.underscore_hash(args)
if value = opts[:read_concern]&.[](:level)
opts[:read_concern][:level] = value.to_sym
end
args.clear
end
session.with_transaction(**opts) do
execute_operations(ops)
end
end
end
def assert_session_pinned(op, state = true)
consume_test_runner(op)
use_arguments(op) do |args|
session = entities.get(:session, args.use!('session'))
if state
unless session.pinned_server
raise Error::ResultMismatch, 'Expected session to be pinned but it is not'
end
else
if session.pinned_server
raise Error::ResultMismatch, 'Expected session to be not pinned but it is'
end
end
end
end
def assert_session_unpinned(op)
assert_session_pinned(op, false)
end
def _loop(op)
consume_test_runner(op)
use_arguments(op) do |args|
ops = args.use!('operations')
if store_errors = args.use('storeErrorsAsEntity')
entities.set(:error_list, store_errors, [])
end
if store_failures = args.use('storeFailuresAsEntity')
entities.set(:failure_list, store_failures, [])
end
store_iterations = args.use('storeIterationsAsEntity')
iterations = 0
store_successes = args.use('storeSuccessesAsEntity')
successes = 0
loop do
break if stop?
begin
ops.map(&:dup).each do |op|
execute_operation(op)
successes += 1
end
rescue Unified::Error::ResultMismatch => e
if store_failures
STDERR.puts "Failure: #{e.class}: #{e}"
entities.get(:failure_list, store_failures) << {
error: "#{e.class}: #{e}",
time: Time.now.to_f,
}
elsif store_errors
STDERR.puts "Failure: #{e.class}: #{e} (reporting as error)"
entities.get(:error_list, store_errors) << {
error: "#{e.class}: #{e}",
time: Time.now.to_f,
}
else
raise
end
rescue Interrupt
raise
rescue => e
if store_failures
STDERR.puts "Error: #{e.class}: #{e} (reporting as failure)"
entities.get(:failure_list, store_failures) << {
error: "#{e.class}: #{e}",
time: Time.now.to_f,
}
elsif store_errors
STDERR.puts "Error: #{e.class}: #{e}"
entities.get(:error_list, store_errors) << {
error: "#{e.class}: #{e}",
time: Time.now.to_f,
}
else
raise
end
end
iterations += 1
end
if store_iterations
entities.set(:iteration_count, store_iterations, iterations)
end
if store_successes
entities.set(:success_count, store_successes, successes)
end
end
end
def assert_event_count(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = entities.get(:client, args.use!('client'))
subscriber = @subscribers.fetch(client)
event = args.use!('event')
assert_eq(event.keys.length, 1, "Expected event must have one key: #{event}")
count = args.use!('count')
events = select_events(subscriber, event)
if %w(serverDescriptionChangedEvent poolClearedEvent).include?(event.keys.first)
# We publish SDAM events from both regular and push monitors.
# This means sometimes there are two ServerMarkedUnknownEvent
# events published for the same server transition.
# Allow actual event count to be at least the expected event count
# in case there are multiple transitions in a single test.
assert_gte(events.length, count, "Expected event #{event} to occur #{count} times but received it #{events.length} times.")
else
assert_eq(events.length, count, "Expected event #{event} to occur #{count} times but received it #{events.length} times.")
end
end
end
def select_events(subscriber, event)
expected_name, opts = event.first
expected_name = expected_name.sub(/Event$/, '').sub(/^(.)/) { $1.upcase }
subscriber.wanted_events.select do |wevent|
if wevent.class.name.sub(/.*::/, '') == expected_name
spec = UsingHash[opts]
result = true
if new_desc = spec.use('newDescription')
if type = new_desc.use('type')
result &&= wevent.new_description.server_type == type.downcase.to_sym
end
end
unless spec.empty?
raise NotImplementedError, "Unhandled keys: #{spec}"
end
result
end
end
end
def assert_number_connections_checked_out(op)
consume_test_runner(op)
use_arguments(op) do |args|
client = entities.get(:client, args.use!('client'))
connections = args.use!('connections')
actual_c = client.cluster.servers.map(&:pool_internal).compact.sum do |p|
p.instance_variable_get(:@checked_out_connections).length
end
assert_eq(actual_c, connections, "Expected client #{client} to have #{connections} checked out connections but there are #{actual_c}.")
end
end
private
# @param [ UsingHash ] args the arguments to extract options from
# @param [ Array<String | Hash> ] keys an array of strings and Hashes,
# where Hashes represent a mapping from the MDB key to the correspoding
# Ruby key. For Strings, the Ruby key is assumed to be a simple conversion
# of the MDB key, from camel-case to snake-case.
# @param [ true | false ] allow_extra whether or not extra keys are allowed
# to exist in the args hash, beyond those listed.
def extract_options(args, *keys, allow_extra: false)
{}.tap do |opts|
keys.each do |key|
Array(key).each do |mdb_key, ruby_key|
value = args.use(mdb_key)
opts[ruby_key || mdb_name_to_ruby(mdb_key)] = value unless value.nil?
end
end
raise NotImplementedError, "unhandled keys: #{args}" if !allow_extra && !args.empty?
end
end
def symbolize_options!(opts, *keys)
keys.each do |key|
opts[key] = mdb_name_to_ruby(opts[key]) if opts[key]
end
end
def mdb_name_to_ruby(name)
name.to_s.gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2}" }.downcase.to_sym
end
def assert_no_arguments(op)
if op.key?('arguments')
raise NotimplementedError, "Arguments are not allowed"
end
end
def consume_test_runner(op)
v = op.use!('object')
unless v == 'testRunner'
raise NotImplementedError, 'Expected object to be testRunner'
end
end
def decode_hex_bytes(value)
value.scan(/../).map { |hex| hex.to_i(16).chr }.join
end
end
end
|