mongo_test_server 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
module MongoTestServer
|
4
|
+
|
5
|
+
class RamDiskStorage
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def supported?
|
9
|
+
`which hdiutil`!=''
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(name)
|
14
|
+
@name = "#{name}-mongo-ram-disk"
|
15
|
+
@path = "/Volumes/#{@name}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def create
|
19
|
+
@ram_disk_device ||= lambda {
|
20
|
+
device = `hdiutil attach -nomount ram://1000000`.chomp
|
21
|
+
`diskutil erasevolume HFS+ #{@name} #{device}`
|
22
|
+
device
|
23
|
+
}.call
|
24
|
+
path
|
25
|
+
end
|
26
|
+
|
27
|
+
def path
|
28
|
+
@path
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete
|
32
|
+
`umount #{path} 2> /dev/null`
|
33
|
+
`hdiutil detach #{@ram_disk_device} 2> /dev/null`
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
module MongoTestServer
|
4
|
+
|
5
|
+
class TmpStorage
|
6
|
+
|
7
|
+
require 'tmpdir'
|
8
|
+
require 'fileutils'
|
9
|
+
|
10
|
+
def initialize(name)
|
11
|
+
@name = "#{name}-mongo-tmp"
|
12
|
+
@tmp_dir = "#{Dir.tmpdir}/#{@name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def create
|
16
|
+
FileUtils.mkdir_p @tmp_dir
|
17
|
+
path
|
18
|
+
end
|
19
|
+
|
20
|
+
def path
|
21
|
+
@tmp_dir
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete
|
25
|
+
FileUtils.rm_rf @tmp_dir
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/lib/mongo_test_server.rb
CHANGED
@@ -9,6 +9,8 @@ require 'erb'
|
|
9
9
|
require 'yaml'
|
10
10
|
require 'tempfile'
|
11
11
|
require "mongo_test_server/version"
|
12
|
+
require "mongo_test_server/tmp_storage"
|
13
|
+
require "mongo_test_server/ram_disk_storage"
|
12
14
|
|
13
15
|
if defined?(Rails)
|
14
16
|
require 'mongo_test_server/railtie'
|
@@ -62,43 +64,32 @@ module MongoTestServer
|
|
62
64
|
@mongo_instance_id = "#{Time.now.to_i}_#{Random.new.rand(100000..900000)}"
|
63
65
|
@oplog_size = 200
|
64
66
|
@configured = true
|
65
|
-
self.started = false
|
66
67
|
end
|
67
68
|
|
68
69
|
def use_ram_disk=(bool)
|
69
|
-
|
70
|
-
@use_ram_disk = true
|
71
|
-
else
|
72
|
-
$stderr.puts "MongoTestServer: can't use a ram disk on this system"
|
73
|
-
@use_ram_disk = false
|
74
|
-
end
|
70
|
+
@use_ram_disk = bool && RamDiskStorage.supported?
|
75
71
|
end
|
76
72
|
|
77
73
|
def use_ram_disk?
|
78
74
|
@use_ram_disk
|
79
75
|
end
|
80
76
|
|
81
|
-
def
|
82
|
-
@
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
def ram_disk_mount
|
92
|
-
"/Volumes/#{ram_disk_name}"
|
77
|
+
def storage
|
78
|
+
@storage ||= if use_ram_disk?
|
79
|
+
$stderr.puts "MongoTestServer: using ram disk storage"
|
80
|
+
RamDiskStorage.new(@name)
|
81
|
+
else
|
82
|
+
$stderr.puts "MongoTestServer: using tmp disk storage"
|
83
|
+
TmpStorage.new(@name)
|
84
|
+
end
|
93
85
|
end
|
94
86
|
|
95
|
-
def
|
96
|
-
|
97
|
-
`hdiutil detach #{@ram_disk_device} 2> /dev/null`
|
87
|
+
def mongo_storage
|
88
|
+
storage.path
|
98
89
|
end
|
99
90
|
|
100
91
|
def mongo_log
|
101
|
-
"#{
|
92
|
+
"#{storage.path}/mongo_log"
|
102
93
|
end
|
103
94
|
|
104
95
|
def port
|
@@ -113,61 +104,45 @@ module MongoTestServer
|
|
113
104
|
@name ||= "#{Random.new.rand(100000..900000)}"
|
114
105
|
end
|
115
106
|
|
116
|
-
def
|
117
|
-
@
|
118
|
-
if self.use_ram_disk?
|
119
|
-
$stderr.puts "MongoTestServer: using ramdisk"
|
120
|
-
setup_ram_disk
|
121
|
-
else
|
122
|
-
"/tmp/#{self.name}_mongo_testserver_#{@mongo_instance_id}"
|
123
|
-
end
|
124
|
-
}.call
|
125
|
-
end
|
126
|
-
|
127
|
-
def remove_mongo_dir
|
128
|
-
if self.use_ram_disk?
|
129
|
-
teardown_ram_disk
|
130
|
-
else
|
131
|
-
FileUtils.rm_rf self.mongo_dir
|
132
|
-
end
|
107
|
+
def mongo_cmd_line
|
108
|
+
"#{self.path} --port #{self.port} --profile 2 --dbpath #{self.mongo_storage} --syncdelay 0 --nojournal --noauth --nohttpinterface --nssize 1 --oplogSize #{@oplog_size} --smallfiles --logpath #{self.mongo_log}"
|
133
109
|
end
|
134
110
|
|
135
|
-
def
|
136
|
-
|
111
|
+
def before_start
|
112
|
+
storage.create
|
137
113
|
end
|
138
114
|
|
139
|
-
def
|
140
|
-
|
141
|
-
FileUtils.mkdir_p self.mongo_dir
|
115
|
+
def after_stop
|
116
|
+
storage.delete
|
142
117
|
end
|
143
118
|
|
144
119
|
def running?
|
145
|
-
pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.
|
120
|
+
pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.mongo_storage} | grep -v grep | awk '{print \$1}'`.chomp
|
146
121
|
!pids.empty?
|
147
122
|
end
|
148
123
|
|
149
124
|
def started?
|
150
|
-
File.directory?(self.
|
125
|
+
File.directory?(self.mongo_storage) && File.exists?("#{self.mongo_storage}/started")
|
151
126
|
end
|
152
127
|
|
153
128
|
def killed?
|
154
|
-
!File.directory?(self.
|
129
|
+
!File.directory?(self.mongo_storage) || File.exists?("#{self.mongo_storage}/killed")
|
155
130
|
end
|
156
131
|
|
157
132
|
def started=(running)
|
158
|
-
if File.directory?(self.
|
159
|
-
running ? FileUtils.touch("#{self.
|
133
|
+
if File.directory?(self.mongo_storage)
|
134
|
+
running ? FileUtils.touch("#{self.mongo_storage}/started") : FileUtils.rm_f("#{self.mongo_storage}/started")
|
160
135
|
end
|
161
136
|
end
|
162
137
|
|
163
138
|
def killed=(killing)
|
164
|
-
if File.directory?(self.
|
165
|
-
killing ? FileUtils.touch("#{self.
|
139
|
+
if File.directory?(self.mongo_storage)
|
140
|
+
killing ? FileUtils.touch("#{self.mongo_storage}/killed") : FileUtils.rm_f("#{self.mongo_storage}/killed")
|
166
141
|
end
|
167
142
|
end
|
168
143
|
|
169
144
|
def error?
|
170
|
-
File.exists?("#{self.
|
145
|
+
File.exists?("#{self.mongo_storage}/error")
|
171
146
|
end
|
172
147
|
|
173
148
|
def configured?
|
@@ -176,7 +151,7 @@ module MongoTestServer
|
|
176
151
|
|
177
152
|
def start
|
178
153
|
unless started?
|
179
|
-
|
154
|
+
before_start
|
180
155
|
if RUBY_PLATFORM=='java'
|
181
156
|
@mongo_process_or_thread = Thread.new { run(mongo_cmd_line) }
|
182
157
|
else
|
@@ -200,7 +175,7 @@ module MongoTestServer
|
|
200
175
|
<#{self.class.name}> Result is: #{IO.binread(self.mongo_log) rescue "No mongo log on disk"}
|
201
176
|
<#{self.class.name}> Error is: #{File.read(error_filepath) rescue "No error file on disk"}
|
202
177
|
ERROR
|
203
|
-
File.open("#{self.
|
178
|
+
File.open("#{self.mongo_storage}/error", 'w') do |f|
|
204
179
|
f << error_message
|
205
180
|
end
|
206
181
|
self.killed=true
|
@@ -249,7 +224,7 @@ module MongoTestServer
|
|
249
224
|
end
|
250
225
|
|
251
226
|
def pids
|
252
|
-
pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.
|
227
|
+
pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.mongo_storage} | grep -v grep | awk '{print \$1}'`.chomp
|
253
228
|
pids.split("\n").map {|p| (p.nil? || p=='') ? nil : p.to_i }
|
254
229
|
end
|
255
230
|
|
@@ -258,7 +233,7 @@ module MongoTestServer
|
|
258
233
|
self.killed = true
|
259
234
|
self.started = false
|
260
235
|
mongo_pids.each { |ppid| `kill -9 #{ppid} 2> /dev/null` }
|
261
|
-
|
236
|
+
after_stop
|
262
237
|
self
|
263
238
|
end
|
264
239
|
|
@@ -86,9 +86,9 @@ describe MongoTestServer::Mongod do
|
|
86
86
|
|
87
87
|
it "should cleanup after itself" do
|
88
88
|
subject.started?.should be_true
|
89
|
-
File.directory?(subject.
|
89
|
+
File.directory?(subject.mongo_storage).should be_true
|
90
90
|
subject.stop
|
91
|
-
File.exists?(subject.
|
91
|
+
File.exists?(subject.mongo_storage).should be_false
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_test_server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -91,6 +91,8 @@ files:
|
|
91
91
|
- lib/mongo_test_server.rb
|
92
92
|
- lib/mongo_test_server/engine.rb
|
93
93
|
- lib/mongo_test_server/railtie.rb
|
94
|
+
- lib/mongo_test_server/ram_disk_storage.rb
|
95
|
+
- lib/mongo_test_server/tmp_storage.rb
|
94
96
|
- lib/mongo_test_server/version.rb
|
95
97
|
- mongo_test_server.gemspec
|
96
98
|
- spec/mongo_test_server/mongo_test_server_spec.rb
|