PageRenderTime 44ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/support/connection_string.rb

http://github.com/mongodb/mongo-ruby-driver
Ruby | 228 lines | 160 code | 46 blank | 22 comment | 16 complexity | 1ccfa5283a41c711594e0561c5ed7e5e MD5 | raw file
Possible License(s): Apache-2.0
  1. # Copyright (C) 2014-2016 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. RSpec::Matchers.define :have_hosts do |test|
  15. match do |client|
  16. def find_server(client, host)
  17. client.cluster.instance_variable_get(:@servers).detect do |s|
  18. s.address.host == host.host
  19. end
  20. end
  21. def match_host?(server, host)
  22. server.address.host == host.host
  23. end
  24. def match_port?(server, host)
  25. server.address.port == host.port || !host.port
  26. end
  27. def match_address_family?(server, host)
  28. address_family(server) == host.address_family
  29. end
  30. def address_family(server)
  31. server.address.socket(2)
  32. server.address.instance_variable_get(:@resolver).class
  33. end
  34. test.hosts.all? do |host|
  35. server = find_server(client, host)
  36. match_host?(server, host) &&
  37. match_port?(server, host) if server #&&
  38. #match_address_family?(server, host) if server
  39. end
  40. failure_message do |client|
  41. "With URI: #{test.uri_string}\n" +
  42. "Expected that test hosts: #{test.hosts} would match " +
  43. "client hosts: #{client.cluster.instance_variable_get(:@servers)}"
  44. end
  45. end
  46. end
  47. RSpec::Matchers.define :match_auth do |test|
  48. def match_database?(client, auth)
  49. client.options[:database] == auth.database || !auth.database
  50. end
  51. def match_password?(client, auth)
  52. client.options[:password] == auth.password ||
  53. client.options[:password].nil? && auth.password == ''
  54. end
  55. match do |client|
  56. auth = test.auth
  57. return true unless auth
  58. client.options[:user] == auth.username &&
  59. match_password?(client, auth) &&
  60. match_database?(client, auth)
  61. end
  62. failure_message do |client|
  63. "With URI: #{test.uri_string}\n" +
  64. "Expected that test auth: #{test.auth} would match client auth: #{client.options}"
  65. end
  66. end
  67. RSpec::Matchers.define :match_options do |test|
  68. match do |client|
  69. options = test.options
  70. return true unless options
  71. options.match?(client.options)
  72. end
  73. failure_message do |client|
  74. "With URI: #{test.uri_string}\n" +
  75. "Expected that test options: #{test.options.options} would match client options: #{client.options}"
  76. end
  77. end
  78. module Mongo
  79. module ConnectionString
  80. class Spec
  81. attr_reader :description
  82. # Instantiate the new spec.
  83. #
  84. # @example Create the spec.
  85. # Spec.new(file)
  86. #
  87. # @param [ String ] file The name of the file.
  88. #
  89. # @since 2.0.0
  90. def initialize(file)
  91. file = File.new(file)
  92. @spec = YAML.load(ERB.new(file.read).result)
  93. file.close
  94. @description = File.basename(file)
  95. end
  96. def tests
  97. @tests ||= @spec['tests'].collect do |spec|
  98. Test.new(spec)
  99. end
  100. end
  101. end
  102. class Test
  103. attr_reader :description
  104. attr_reader :uri_string
  105. def initialize(spec)
  106. @spec = spec
  107. @description = @spec['description']
  108. @uri_string = @spec['uri']
  109. end
  110. def valid?
  111. @spec['valid']
  112. end
  113. def warn?
  114. @spec['warning']
  115. end
  116. def hosts
  117. @hosts ||= @spec['hosts'].collect do |host|
  118. Host.new(host)
  119. end
  120. end
  121. def options
  122. @options ||= Options.new(@spec['options']) if @spec['options']
  123. end
  124. def client
  125. @client ||= Mongo::Client.new(@spec['uri'])
  126. end
  127. def uri
  128. @uri ||= Mongo::URI.new(@spec['uri'])
  129. end
  130. def auth
  131. @auth ||= Auth.new(@spec['auth']) if @spec['auth']
  132. end
  133. end
  134. class Host
  135. MAPPING = {
  136. 'ipv4' => Mongo::Address::IPv4,
  137. 'ipv6' => Mongo::Address::IPv6,
  138. 'unix' => Mongo::Address::Unix
  139. }
  140. attr_reader :host
  141. attr_reader :port
  142. def initialize(spec)
  143. @spec = spec
  144. @host = @spec['host']
  145. @port = @spec['port']
  146. end
  147. def address_family
  148. MAPPING[@spec['type']]
  149. end
  150. end
  151. class Auth
  152. attr_reader :username
  153. attr_reader :password
  154. attr_reader :database
  155. def initialize(spec)
  156. @spec = spec
  157. @username = @spec['username']
  158. @password = @spec['password']
  159. @database = @spec['db']
  160. end
  161. def to_s
  162. "username: #{username}, password: #{password}, database: #{database}"
  163. end
  164. end
  165. class Options
  166. MAPPINGS = {
  167. 'replicaset' => :replica_set,
  168. 'authmechanism' => :auth_mech
  169. }
  170. attr_reader :options
  171. def initialize(options)
  172. @options = options
  173. end
  174. def match?(opts)
  175. @options.keys.all? do |k|
  176. opts[MAPPINGS[k]] == @options[k] ||
  177. Mongo::URI::AUTH_MECH_MAP[@options[k]] == opts[MAPPINGS[k]]
  178. end
  179. end
  180. end
  181. end
  182. end