mongo_test_server 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module MongoTestServer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,5 +1,9 @@
1
1
  # coding: UTF-8
2
- require 'mongo'
2
+ #begin
3
+ # require 'mongo'
4
+ #rescue LoadError => e
5
+ # require 'moped'
6
+ #end
3
7
  require 'fileutils'
4
8
  require 'erb'
5
9
  require 'yaml'
@@ -7,9 +11,9 @@ require 'tempfile'
7
11
  require "mongo_test_server/version"
8
12
 
9
13
  module MongoTestServer
10
-
14
+
11
15
  class Mongod
12
-
16
+
13
17
  class << self
14
18
 
15
19
  def configure(options={}, &block)
@@ -22,7 +26,7 @@ module MongoTestServer
22
26
  def server
23
27
  @mongo_test_server ||= new
24
28
  end
25
-
29
+
26
30
  def start_server
27
31
  unless @mongo_test_server.nil?
28
32
  @mongo_test_server.start
@@ -30,20 +34,20 @@ module MongoTestServer
30
34
  puts "MongoTestServer not configured properly!"
31
35
  end
32
36
  end
33
-
37
+
34
38
  def stop_server
35
39
  unless @mongo_test_server.nil?
36
40
  @mongo_test_server.stop
37
41
  end
38
42
  end
39
-
43
+
40
44
  end
41
45
 
42
46
  attr_writer :port
43
47
  attr_writer :path
44
48
  attr_writer :name
45
49
  attr_reader :mongo_instance_id
46
-
50
+
47
51
  def initialize(port=nil, name=nil, path=nil)
48
52
  self.port = port
49
53
  self.path = path
@@ -78,7 +82,7 @@ module MongoTestServer
78
82
  def mongo_cmd_line
79
83
  "#{self.path} --port #{self.port} --dbpath #{self.mongo_dir} --noprealloc --nojournal --noauth --nohttpinterface --nssize 1 --oplogSize #{@oplog_size} --smallfiles --logpath #{self.mongo_log}"
80
84
  end
81
-
85
+
82
86
  def prepare
83
87
  FileUtils.rm_rf self.mongo_dir
84
88
  FileUtils.mkdir_p self.mongo_dir
@@ -88,7 +92,7 @@ module MongoTestServer
88
92
  pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.mongo_dir} | grep -v grep | awk '{print \$1}'`.chomp
89
93
  !pids.empty?
90
94
  end
91
-
95
+
92
96
  def started?
93
97
  File.directory?(self.mongo_dir) && File.exists?("#{self.mongo_dir}/started")
94
98
  end
@@ -112,11 +116,11 @@ module MongoTestServer
112
116
  def error?
113
117
  File.exists?("#{self.mongo_dir}/error")
114
118
  end
115
-
119
+
116
120
  def configured?
117
121
  @configured
118
122
  end
119
-
123
+
120
124
  def start
121
125
  unless started?
122
126
  prepare
@@ -140,8 +144,8 @@ module MongoTestServer
140
144
  unless killed? || $?.success?
141
145
  error_message = <<-ERROR
142
146
  <#{self.class.name}> Error executing command: #{command}
143
- <#{self.class.name}> Result is: #{IO.binread(self.mongo_log)}
144
- <#{self.class.name}> Error is: #{File.read(error_filepath)}
147
+ <#{self.class.name}> Result is: #{IO.binread(self.mongo_log) rescue "No mongo log on disk"}
148
+ <#{self.class.name}> Error is: #{File.read(error_filepath) rescue "No error file on disk"}
145
149
  ERROR
146
150
  File.open("#{self.mongo_dir}/error", 'w') do |f|
147
151
  f << error_message
@@ -150,13 +154,24 @@ module MongoTestServer
150
154
  end
151
155
  result
152
156
  end
153
-
157
+
158
+ def test_connection!
159
+ if defined?(Mongo)
160
+ c = Mongo::Connection.new("localhost", self.port)
161
+ c.close
162
+ elsif defined?(Moped)
163
+ session = Moped::Session.new(["localhost:#{self.port}"])
164
+ session.disconnect
165
+ else
166
+ raise Exeption.new "No mongo driver loaded! Only the official mongo driver and the moped driver are supported"
167
+ end
168
+ end
169
+
154
170
  def wait_until_ready
155
171
  retries = 10
156
172
  begin
157
173
  self.started = true
158
- c = Mongo::Connection.new("localhost", self.port)
159
- c.close
174
+ test_connection!
160
175
  rescue Exception => e
161
176
  if retries>0 && !killed? && !error?
162
177
  retries -= 1
@@ -167,20 +182,24 @@ module MongoTestServer
167
182
  error_lines = []
168
183
  error_lines << "<#{self.class.name}> cmd was: #{mongo_cmd_line}"
169
184
  error_lines << "<#{self.class.name}> ERROR: Failed to connect to mongo database: #{e.message}"
170
- IO.binread(self.mongo_log).split("\n").each do |line|
171
- error_lines << "<#{self.class.name}> #{line}"
185
+ begin
186
+ IO.binread(self.mongo_log).split("\n").each do |line|
187
+ error_lines << "<#{self.class.name}> #{line}"
188
+ end
189
+ rescue Exception => e
190
+ error_lines << "No mongo log on disk at #{self.mongo_log}"
172
191
  end
173
192
  stop
174
193
  raise Exception.new error_lines.join("\n")
175
194
  end
176
195
  end
177
196
  end
178
-
197
+
179
198
  def pids
180
199
  pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.mongo_dir} | grep -v grep | awk '{print \$1}'`.chomp
181
200
  pids.split("\n").map {|p| (p.nil? || p=='') ? nil : p.to_i }
182
201
  end
183
-
202
+
184
203
  def stop
185
204
  mongo_pids = pids
186
205
  self.killed = true
@@ -193,7 +212,11 @@ module MongoTestServer
193
212
  def mongoid_options(options={})
194
213
  options = {host: "localhost", port: self.port, database: "#{self.name}_test_db", use_utc: false, use_activesupport_time_zone: true}.merge(options)
195
214
  end
196
-
215
+
216
+ def mongoid3_options(options={})
217
+ options = {hosts: ["localhost:#{self.port}"], database: "#{self.name}_test_db", use_utc: false, use_activesupport_time_zone: true}.merge(options)
218
+ end
219
+
197
220
  def mongoid_yml(options={})
198
221
  options = mongoid_options(options)
199
222
  mongo_conf_yaml = <<EOY
@@ -204,6 +227,19 @@ use_utc: #{options[:use_utc]}
204
227
  use_activesupport_time_zone: #{options[:use_activesupport_time_zone]}
205
228
  EOY
206
229
  end
207
-
230
+
231
+ def mongoid3_yml(options={})
232
+ options = mongoid3_options(options)
233
+ mongo_conf_yaml = <<EOY
234
+ sessions:
235
+ default:
236
+ hosts:
237
+ - #{options[:hosts].first}
238
+ database : #{options[:database]}
239
+ use_utc: #{options[:use_utc]}
240
+ use_activesupport_time_zone: #{options[:use_activesupport_time_zone]}
241
+ EOY
242
+ end
243
+
208
244
  end
209
245
  end
@@ -15,7 +15,8 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = MongoTestServer::VERSION
17
17
 
18
- gem.add_dependency('mongo', '>=1.6.0')
18
+ gem.add_development_dependency('mongo', '>=1.6.0')
19
+ gem.add_development_dependency('moped', '>=1.3.0')
19
20
  unless RUBY_PLATFORM == 'java'
20
21
  gem.add_development_dependency('bson_ext', '>=1.3.0')
21
22
  end
data/spec/spec_helper.rb CHANGED
@@ -18,7 +18,7 @@ RSpec.configure do |config|
18
18
  # config.mock_with :flexmock
19
19
  # config.mock_with :rr
20
20
  config.mock_with :rspec
21
-
21
+
22
22
  ## perhaps this should be removed as well
23
23
  ## and done in Rakefile?
24
24
  config.color_enabled = true
metadata CHANGED
@@ -1,39 +1,82 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_test_server
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.2
4
+ version: 0.0.3
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - John Axel Eriksson
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-16 00:00:00.000000000Z
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo
16
- version_requirements: &2056 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
17
18
  requirements:
18
19
  - - ! '>='
19
20
  - !ruby/object:Gem::Version
20
21
  version: 1.6.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
21
25
  none: false
22
- requirement: *2056
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: moped
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.0
38
+ type: :development
23
39
  prerelease: false
24
- type: :runtime
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: bson_ext
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.0
25
62
  - !ruby/object:Gem::Dependency
26
63
  name: rspec
27
- version_requirements: &2074 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
28
66
  requirements:
29
67
  - - ! '>='
30
68
  - !ruby/object:Gem::Version
31
69
  version: 2.6.0
32
- none: false
33
- requirement: *2074
34
- prerelease: false
35
70
  type: :development
36
- description: Standalone mongo test server for use with rspec or other unit testing framework
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.6.0
78
+ description: Standalone mongo test server for use with rspec or other unit testing
79
+ framework
37
80
  email:
38
81
  - john@insane.se
39
82
  executables: []
@@ -53,29 +96,28 @@ files:
53
96
  - test.rb
54
97
  homepage: ''
55
98
  licenses: []
56
- post_install_message:
99
+ post_install_message:
57
100
  rdoc_options: []
58
101
  require_paths:
59
102
  - lib
60
103
  required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
61
105
  requirements:
62
106
  - - ! '>='
63
107
  - !ruby/object:Gem::Version
64
108
  version: '0'
65
- none: false
66
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
67
111
  requirements:
68
112
  - - ! '>='
69
113
  - !ruby/object:Gem::Version
70
114
  version: '0'
71
- none: false
72
115
  requirements: []
73
- rubyforge_project:
74
- rubygems_version: 1.8.17
75
- signing_key:
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.23
118
+ signing_key:
76
119
  specification_version: 3
77
120
  summary: Standalone mongo test server for use with rspec or other unit testing framework
78
121
  test_files:
79
122
  - spec/mongo_test_server/mongo_test_server_spec.rb
80
123
  - spec/spec_helper.rb
81
- ...