Skip to content

Commit e4b4585

Browse files
committed
implement uri option spec tests
1 parent 90340ab commit e4b4585

11 files changed

+532
-6
lines changed

lib/mongo/uri.rb

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,19 @@ def self.uri_option(uri_key, name, extra = {})
430430
uri_option 'sockettimeoutms', :socket_timeout, :type => :ms_convert
431431
uri_option 'serverselectiontimeoutms', :server_selection_timeout, :type => :ms_convert
432432
uri_option 'localthresholdms', :local_threshold, :type => :ms_convert
433+
uri_option 'heartbeatfrequencyms', :heartbeat_frequency, :type => :ms_convert
434+
uri_option 'maxidletimems', :max_idle_time, :type => :ms_convert
433435

434436
# Write Options
435437
uri_option 'w', :w, :group => :write
436-
uri_option 'journal', :j, :group => :write
438+
uri_option 'journal', :j, :group => :write, :type => :bool
437439
uri_option 'fsync', :fsync, :group => :write
438-
uri_option 'wtimeoutms', :timeout, :group => :write
440+
uri_option 'wtimeoutms', :timeout, :group => :write, :type => :unsigned_int
439441

440442
# Read Options
441443
uri_option 'readpreference', :mode, :group => :read, :type => :read_mode
442444
uri_option 'readpreferencetags', :tag_sets, :group => :read, :type => :read_tags
443-
uri_option 'maxstalenessseconds', :max_staleness, :group => :read
445+
uri_option 'maxstalenessseconds', :max_staleness, :group => :read, :type => :max_staleness
444446

445447
# Pool options
446448
uri_option 'minpoolsize', :min_pool_size
@@ -449,6 +451,13 @@ def self.uri_option(uri_key, name, extra = {})
449451

450452
# Security Options
451453
uri_option 'ssl', :ssl
454+
uri_option 'tls', :ssl
455+
uri_option 'tlsallowinvalidcertificates', :ssl_verify, :type => :inverse_bool
456+
uri_option 'tlscafilepath', :ssl_ca_cert
457+
uri_option 'tlsclientcertfilepath', :ssl_cert
458+
uri_option 'tlsclientkeyfilepath', :ssl_key
459+
uri_option 'tlsclientkeypassword', :ssl_key_pass_phrase
460+
452461

453462
# Topology options
454463
uri_option 'connect', :connect
@@ -461,7 +470,9 @@ def self.uri_option(uri_key, name, extra = {})
461470
# Client Options
462471
uri_option 'appname', :app_name
463472
uri_option 'compressors', :compressors, :type => :array
464-
uri_option 'zlibcompressionlevel', :zlib_compression_level
473+
uri_option 'readconcernlevel', :read_concern
474+
uri_option 'retrywrites', :retry_writes, :type => :bool
475+
uri_option 'zlibcompressionlevel', :zlib_compression_level, :type => :zlib_compression_level
465476

466477
# Casts option values that do not have a specifically provided
467478
# transformation to the appropriate type.
@@ -571,7 +582,7 @@ def auth_source(value)
571582
#
572583
# @return [Symbol] The transformed authentication mechanism.
573584
def auth_mech(value)
574-
AUTH_MECH_MAP[value.upcase]
585+
AUTH_MECH_MAP[value.upcase] || log_warn("#{value} is not a valid auth mechanism")
575586
end
576587

577588
# Read preference mode transformation.
@@ -615,6 +626,60 @@ def auth_mech_props(value)
615626
properties
616627
end
617628

629+
def zlib_compression_level(value)
630+
if /^-?\d+$/ =~ value
631+
i = value.to_i
632+
633+
if i >= -1 && i <= 9
634+
return i
635+
end
636+
end
637+
638+
log_warn("#{value} is not valid zlibCompressionLevel")
639+
nil
640+
end
641+
642+
def bool(value)
643+
case value
644+
when "true"
645+
true
646+
when "false"
647+
false
648+
else
649+
log_warn("invalid boolean: #{value}")
650+
nil
651+
end
652+
end
653+
654+
def inverse_bool(value)
655+
!bool(value)
656+
end
657+
658+
def max_staleness(value)
659+
if /^\d+$/ =~ value
660+
int = value.to_i
661+
662+
if int >= 0 && int < 90
663+
log_warn("max staleness must be either 0 or greater than 90: #{value}")
664+
end
665+
666+
return int
667+
end
668+
669+
log_warn("Invalid max staleness value: #{value}")
670+
nil
671+
end
672+
673+
# Ensures that a connection string value
674+
def unsigned_int(value)
675+
unless /^\d+$/ =~ value
676+
log_warn("Invalid unsigned int value: #{value}")
677+
return nil
678+
end
679+
680+
value.to_i
681+
end
682+
618683
# Ruby's convention is to provide timeouts in seconds, not milliseconds and
619684
# to use fractions where more precision is necessary. The connection string
620685
# options are always in MS so we provide an easy conversion type.
@@ -625,6 +690,11 @@ def auth_mech_props(value)
625690
#
626691
# @since 2.0.0
627692
def ms_convert(value)
693+
unless /^-?\d+(\.\d+)?$/ =~ value
694+
log_warn("Invalid ms value: #{value}")
695+
return nil
696+
end
697+
628698
value.to_f / 1000
629699
end
630700

@@ -636,6 +706,11 @@ def ms_convert(value)
636706
def hash_extractor(value)
637707
value.split(',').reduce({}) do |set, tag|
638708
k, v = tag.split(':')
709+
if v.nil?
710+
log_warn("#{value} is not a valid URI hash")
711+
return nil
712+
end
713+
639714
set.merge(decode(k).downcase.to_sym => decode(v))
640715
end
641716
end

spec/lite_spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
RETRYABLE_WRITES_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/retryable_writes/**/*.yml")
1010
COMMAND_MONITORING_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/command_monitoring/**/*.yml")
1111
CONNECTION_STRING_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/connection_string/*.yml")
12+
URI_OPTIONS_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/uri_options/*.yml")
1213
DNS_SEEDLIST_DISCOVERY_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/dns_seedlist_discovery/*.yml")
1314
GRIDFS_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/gridfs/*.yml")
1415
TRANSACTIONS_TESTS = Dir.glob("#{CURRENT_PATH}/spec_tests/data/transactions/*.yml")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests:
2+
-
3+
description: "Valid auth options are parsed correctly"
4+
uri: "mongodb://example.com/?authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:other,CANONICALIZE_HOST_NAME:true&authSource=$external"
5+
valid: true
6+
warning: false
7+
hosts: ~
8+
auth:
9+
username: ~
10+
password: ~
11+
db: ~
12+
options:
13+
authmechanism: "GSSAPI"
14+
authMechanismProperties:
15+
SERVICE_NAME: "other"
16+
CANONICALIZE_HOST_NAME: "true"
17+
authSource: "$external"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
tests:
2+
-
3+
description: "Valid compression options are parsed correctly"
4+
uri: "mongodb://example.com/?compressors=zlib&zlibCompressionLevel=9"
5+
valid: true
6+
warning: false
7+
hosts: ~
8+
auth:
9+
username: ~
10+
password: ~
11+
db: ~
12+
options:
13+
compressors: "zlib"
14+
zlibCompressionLevel: "9"
15+
-
16+
description: "Invalid compressors cause a warning"
17+
uri: "mongodb://example.com/?compressors=jpeg"
18+
valid: true
19+
warning: true
20+
hosts: ~
21+
auth:
22+
username: ~
23+
password: ~
24+
db: ~
25+
options: {}
26+
-
27+
description: "Invalid zlibCompressionLevel causes a warning"
28+
uri: "mongodb://example.com/?compressors=zlib&zlibCompressionLevel=invalid"
29+
valid: true
30+
warning: true
31+
hosts: ~
32+
auth:
33+
username: ~
34+
password: ~
35+
db: ~
36+
options: {}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
tests:
2+
-
3+
description: "Valid read and write concern are parsed correctly"
4+
uri: "mongodb://example.com/?readConcernLevel=majority&w=5&wTimeoutMS=30000&journal=false"
5+
valid: true
6+
warning: false
7+
hosts: ~
8+
auth:
9+
username: ~
10+
password: ~
11+
db: ~
12+
options:
13+
readConcernLevel: "majority"
14+
w: "5"
15+
wTimeoutMS: "30000"
16+
journal: "false"
17+
-
18+
description: "Arbitrary string readConcernLevel does not cause a warning"
19+
uri: "mongodb://example.com/?readConcernLevel=arbitraryButStillValid"
20+
valid: true
21+
warning: false
22+
hosts: ~
23+
auth:
24+
username: ~
25+
password: ~
26+
db: ~
27+
options:
28+
readConcernLevel: "arbitraryButStillValid"
29+
-
30+
description: "Arbitrary string w doesn't cause a warning"
31+
uri: "mongodb://example.com/?w=arbitraryButStillValid"
32+
valid: true
33+
warning: false
34+
hosts: ~
35+
auth:
36+
username: ~
37+
password: ~
38+
db: ~
39+
options:
40+
w: "arbitraryButStillValid"
41+
-
42+
description: "Invalid wTimeoutMS causes a warning"
43+
uri: "mongodb://example.com/?wTimeoutMS=invalid"
44+
valid: true
45+
warning: true
46+
hosts: ~
47+
auth:
48+
username: ~
49+
password: ~
50+
db: ~
51+
options: {}
52+
-
53+
description: "Invalid journal causes a warning"
54+
uri: "mongodb://example.com/?journal=invalid"
55+
valid: true
56+
warning: true
57+
hosts: ~
58+
auth:
59+
username: ~
60+
password: ~
61+
db: ~
62+
options: {}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
tests:
2+
-
3+
description: "Valid connection and timeout options are parsed correctly"
4+
uri: "mongodb://example.com/?appname=URI-OPTIONS-SPEC-TEST&connectTimeoutMS=20000&heartbeatFrequencyMS=5000&localThresholdMS=3000&replicaSet=uri-options-spec&retryWrites=true&serverSelectionTimeoutMS=15000&socketTimeoutMS=7500"
5+
valid: true
6+
warning: false
7+
hosts: ~
8+
auth:
9+
username: ~
10+
password: ~
11+
db: ~
12+
options:
13+
appname: "URI-OPTIONS-SPEC-TEST"
14+
connectTimeoutMS: "20000"
15+
heartbeatFrequencyMS: "5000"
16+
localThresholdMS: "3000"
17+
maxIdleTimeMS: "50000"
18+
replicaSet: "uri-options-spec"
19+
retryWrites: "true"
20+
serverSelectionTimeoutMS: "15000"
21+
socketTimeoutMS: "7500"
22+
-
23+
description: "Invalid connectTimeoutMS causes a warning"
24+
uri: "mongodb://example.com/?connectTimeoutMS=invalid"
25+
valid: true
26+
warning: true
27+
hosts: ~
28+
auth:
29+
username: ~
30+
password: ~
31+
db: ~
32+
options: {}
33+
-
34+
description: "Invalid heartbeatFrequencyMS causes a warning"
35+
uri: "mongodb://example.com/?heartbeatFrequencyMS=invalid"
36+
valid: true
37+
warning: true
38+
hosts: ~
39+
auth:
40+
username: ~
41+
password: ~
42+
db: ~
43+
options: {}
44+
-
45+
description: "Invalid localThresholdMS causes a warning"
46+
uri: "mongodb://example.com/?localThresholdMS=invalid"
47+
valid: true
48+
warning: true
49+
hosts: ~
50+
auth:
51+
username: ~
52+
password: ~
53+
db: ~
54+
options: {}
55+
-
56+
description: "Invalid serverSelectionTimeoutMS causes a warning"
57+
uri: "mongodb://example.com/?serverSelectionTimeoutMS=invalid"
58+
valid: true
59+
warning: true
60+
hosts: ~
61+
auth:
62+
username: ~
63+
password: ~
64+
db: ~
65+
options: {}
66+
-
67+
description: "Invalid socketTimeoutMS causes a warning"
68+
uri: "mongodb://example.com/?socketTimeoutMS=invalid"
69+
valid: true
70+
warning: true
71+
hosts: ~
72+
auth:
73+
username: ~
74+
password: ~
75+
db: ~
76+
options: {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
tests:
2+
-
3+
description: "Valid connection pool options are parsed correctly"
4+
uri: "mongodb://example.com/?maxIdleTimeMS=50000"
5+
valid: true
6+
warning: false
7+
hosts: ~
8+
auth:
9+
username: ~
10+
password: ~
11+
db: ~
12+
options:
13+
appname: "URI-OPTIONS-SPEC-TEST"
14+
connectTimeoutMS: "20000"
15+
heartbeatFrequencyMS: "5000"
16+
localThresholdMS: "3000"
17+
maxIdleTimeMS: "50000"
18+
replicaSet: "uri-options-spec"
19+
retryWrites: "true"
20+
serverSelectionTimeoutMS: "15000"
21+
socketTimeoutMS: "7500"
22+
-
23+
description: "Invalid maxIdleTimeMS causes a warning"
24+
uri: "mongodb://example.com/?maxIdleTimeMS=invalid"
25+
valid: true
26+
warning: true
27+
hosts: ~
28+
auth:
29+
username: ~
30+
password: ~
31+
db: ~
32+
options: {}

0 commit comments

Comments
 (0)