jettywrapper 1.0.0 → 2.0.0

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.
@@ -1,24 +1,253 @@
1
- require File.join(File.dirname(__FILE__), "/../spec_helper")
2
- require File.join(File.dirname(__FILE__), "/../../lib/jettywrapper")
1
+ require 'spec_helper'
3
2
  require 'rubygems'
4
- require 'ruby-debug'
3
+ require 'fileutils'
5
4
 
6
- module Hydra
7
5
  describe Jettywrapper do
8
-
9
- # JETTY1 =
10
-
6
+
7
+ # JETTY1 =
11
8
  before(:all) do
12
9
  @jetty_params = {
13
10
  :quiet => false,
14
11
  :jetty_home => "/path/to/jetty",
15
- :jetty_port => 8888,
12
+ :jetty_port => TEST_JETTY_PORTS.first,
16
13
  :solr_home => "/path/to/solr",
17
14
  :startup_wait => 0,
18
- :java_opts => ["-Xmx256mb"]
15
+ :java_opts => ["-Xmx256m"],
16
+ :jetty_opts => ["/path/to/jetty_xml", "/path/to/other_jetty_xml"]
19
17
  }
18
+
19
+ Jettywrapper.logger.level=3
20
+ end
21
+
22
+ before do
23
+ Jettywrapper.reset_config
24
+ end
25
+
26
+ context "downloading" do
27
+ context "with default file" do
28
+ it "should download the zip file" do
29
+ FileUtils.rm "tmp/v8.1.1.zip", force: true
30
+ Jettywrapper.should_receive(:system).with('curl -L https://github.com/projecthydra/hydra-jetty/archive/v8.1.1.zip -o tmp/v8.1.1.zip').and_return(system ('true'))
31
+ Jettywrapper.download
32
+ end
33
+ end
34
+
35
+ context "specifying the file" do
36
+ it "should download the zip file" do
37
+ Jettywrapper.should_receive(:system).with('curl -L http://example.co/file.zip -o tmp/file.zip').and_return(system ('true'))
38
+ Jettywrapper.download('http://example.co/file.zip')
39
+ Jettywrapper.url.should == 'http://example.co/file.zip'
40
+ end
41
+ end
42
+ context "specifying the version" do
43
+ it "should download the zip file" do
44
+ Jettywrapper.should_receive(:system).with('curl -L https://github.com/projecthydra/hydra-jetty/archive/v9.9.9.zip -o tmp/v9.9.9.zip').and_return(system ('true'))
45
+ Jettywrapper.hydra_jetty_version = 'v9.9.9'
46
+ Jettywrapper.download
47
+ end
48
+ end
49
+ end
50
+
51
+ context "unzip" do
52
+ before do
53
+ Jettywrapper.url = nil
54
+ end
55
+ context "with default file" do
56
+ it "should download the zip file" do
57
+ File.should_receive(:exists?).and_return(true)
58
+ Jettywrapper.should_receive(:expanded_zip_dir).and_return('tmp/jetty_generator/hydra-jetty-v8.1.1')
59
+ Jettywrapper.should_receive(:system).with('unzip -d tmp/jetty_generator -qo tmp/v8.1.1.zip').and_return(system ('true'))
60
+ Jettywrapper.should_receive(:system).with('rm -r jetty').and_return(system ('true'))
61
+ Jettywrapper.should_receive(:system).with('mv tmp/jetty_generator/hydra-jetty-v8.1.1 jetty').and_return(system ('true'))
62
+ Jettywrapper.unzip
63
+ end
64
+ end
65
+
66
+ context "specifying the file" do
67
+ before do
68
+ Jettywrapper.url = 'http://example.co/file.zip'
69
+ end
70
+ it "should download the zip file" do
71
+ File.should_receive(:exists?).and_return(true)
72
+ Jettywrapper.should_receive(:expanded_zip_dir).and_return('tmp/jetty_generator/interal_dir')
73
+ Jettywrapper.should_receive(:system).with('unzip -d tmp/jetty_generator -qo tmp/file.zip').and_return(system ('true'))
74
+ Jettywrapper.should_receive(:system).with('rm -r jetty').and_return(system ('true'))
75
+ Jettywrapper.should_receive(:system).with('mv tmp/jetty_generator/interal_dir jetty').and_return(system ('true'))
76
+ Jettywrapper.unzip
77
+ end
78
+ end
20
79
  end
21
-
80
+
81
+ context ".url" do
82
+ before do
83
+ subject.url = nil
84
+ end
85
+ subject {Jettywrapper}
86
+ context "When a constant is set" do
87
+ before do
88
+ ZIP_URL = 'http://example.com/foo.zip'
89
+ end
90
+ after do
91
+ Object.send(:remove_const, :ZIP_URL)
92
+ end
93
+ its(:url) {should == 'http://example.com/foo.zip'}
94
+ end
95
+ context "when a url is set" do
96
+ before do
97
+ subject.url = 'http://example.com/bar.zip'
98
+ end
99
+ its(:url) {should == 'http://example.com/bar.zip'}
100
+ end
101
+ context "when url is not set" do
102
+ its(:url) {should == 'https://github.com/projecthydra/hydra-jetty/archive/v8.1.1.zip'}
103
+ end
104
+ end
105
+
106
+ context ".tmp_dir" do
107
+ subject {Jettywrapper}
108
+ context "when a dir is set" do
109
+ before do
110
+ subject.tmp_dir = '/opt/tmp'
111
+ end
112
+ its(:tmp_dir) {should == '/opt/tmp'}
113
+ end
114
+ context "when dir is not set" do
115
+ before do
116
+ subject.tmp_dir = nil
117
+ end
118
+ its(:tmp_dir) {should == 'tmp'}
119
+ end
120
+
121
+ end
122
+
123
+ context ".jetty_dir" do
124
+ subject {Jettywrapper}
125
+ context "when a dir is set" do
126
+ before do
127
+ subject.jetty_dir = '/opt/jetty'
128
+ end
129
+ its(:jetty_dir) {should == '/opt/jetty'}
130
+ end
131
+ context "when dir is not set" do
132
+ before do
133
+ subject.jetty_dir = nil
134
+ end
135
+ its(:jetty_dir) {should == 'jetty'}
136
+ end
137
+ end
138
+
139
+ context "app_root" do
140
+ subject {Jettywrapper}
141
+ context "When rails is present" do
142
+ before do
143
+ class Rails
144
+ def self.root
145
+ 'rails_root'
146
+ end
147
+ end
148
+ end
149
+ after do
150
+ Object.send(:remove_const, :Rails)
151
+ end
152
+ its(:app_root) {should == 'rails_root'}
153
+ end
154
+ context "When APP_ROOT is set" do
155
+ before do
156
+ APP_ROOT = 'custom_root'
157
+ end
158
+ after do
159
+ Object.send(:remove_const, :APP_ROOT)
160
+ end
161
+ its(:app_root) {should == 'custom_root'}
162
+ end
163
+ context "otherwise" do
164
+ its(:app_root) {should == '.'}
165
+ end
166
+ end
167
+
168
+ describe "env" do
169
+ before do
170
+ ENV.delete('JETTYWRAPPER_ENV')
171
+ ENV.delete('RAILS_ENV')
172
+ ENV.delete('environment')
173
+ end
174
+
175
+ it "should have a setter" do
176
+ Jettywrapper.env = "abc"
177
+ expect(Jettywrapper.env).to eq "abc"
178
+ end
179
+
180
+ it "should load the ENV['JETTYWRAPPER_ENV']" do
181
+ ENV['JETTYWRAPPER_ENV'] = 'test'
182
+ ENV['RAILS_ENV'] = 'test2'
183
+ ENV['environment'] = 'test3'
184
+ expect(Jettywrapper.env).to eq "test"
185
+ end
186
+
187
+ it "should be the Rails environment" do
188
+ Rails = double(env: 'test')
189
+ expect(Jettywrapper.env).to eq "test"
190
+ end
191
+
192
+ it "should use the ENV['RAILS_ENV']" do
193
+ ENV['RAILS_ENV'] = 'test2'
194
+ ENV['environment'] = 'test3'
195
+ expect(Jettywrapper.env).to eq "test2"
196
+ end
197
+
198
+ it "should load the ENV['environment']" do
199
+ ENV['environment'] = 'test3'
200
+ expect(Jettywrapper.env).to eq "test3"
201
+ end
202
+
203
+ it "should default to 'development'" do
204
+ expect(Jettywrapper.env).to eq "development"
205
+ end
206
+
207
+ end
208
+
209
+ context "config" do
210
+ before do
211
+ end
212
+ it "loads the application jetty.yml first" do
213
+ IO.should_receive(:read).with('./config/jetty.yml').and_return("default:\n")
214
+ config = Jettywrapper.load_config
215
+ end
216
+
217
+ it "loads the application jetty.yml using erb parsing" do
218
+ IO.should_receive(:read).with('./config/jetty.yml').and_return("default:\n a: <%= 123 %>")
219
+ config = Jettywrapper.load_config
220
+ config[:a] == 123
221
+ end
222
+
223
+ it "falls back on the distributed jetty.yml" do
224
+ File.should_receive(:exists?).with('./config/jetty.yml').and_return(false)
225
+ IO.should_receive(:read).with { |value| value =~ /jetty.yml/ }.and_return("default:\n")
226
+ config = Jettywrapper.load_config
227
+ end
228
+
229
+ it "supports per-environment configuration" do
230
+ ENV['environment'] = 'test'
231
+ IO.should_receive(:read).with('./config/jetty.yml').and_return("default:\n a: 1\ntest:\n a: 2")
232
+ config = Jettywrapper.load_config
233
+ config[:a].should == 2
234
+ end
235
+
236
+ it "should take the env as an argument to load_config and the env should be sticky" do
237
+ IO.should_receive(:read).with('./config/jetty.yml').and_return("default:\n a: 1\nfoo:\n a: 2")
238
+ config = Jettywrapper.load_config('foo')
239
+ config[:a].should == 2
240
+ expect(Jettywrapper.env).to eq 'foo'
241
+ end
242
+
243
+ it "falls back on a 'default' environment configuration" do
244
+ ENV['environment'] = 'test'
245
+ IO.should_receive(:read).with('./config/jetty.yml').and_return("default:\n a: 1")
246
+ config = Jettywrapper.load_config
247
+ config[:a].should == 1
248
+ end
249
+ end
250
+
22
251
  context "instantiation" do
23
252
  it "can be instantiated" do
24
253
  ts = Jettywrapper.instance
@@ -26,17 +255,13 @@ module Hydra
26
255
  end
27
256
 
28
257
  it "can be configured with a params hash" do
29
- ts = Jettywrapper.configure(@jetty_params)
258
+ ts = Jettywrapper.configure(@jetty_params)
30
259
  ts.quiet.should == false
31
260
  ts.jetty_home.should == "/path/to/jetty"
32
- ts.port.should == 8888
261
+ ts.port.should == @jetty_params[:jetty_port]
33
262
  ts.solr_home.should == '/path/to/solr'
34
263
  ts.startup_wait.should == 0
35
- end
36
-
37
- # passing in a hash is no longer optional
38
- it "raises an error when called without a :jetty_home value" do
39
- lambda { ts = Jettywrapper.configure }.should raise_exception
264
+ ts.jetty_opts.should == @jetty_params[:jetty_opts]
40
265
  end
41
266
 
42
267
  it "should override nil params with defaults" do
@@ -45,60 +270,64 @@ module Hydra
45
270
  :jetty_home => '/path/to/jetty',
46
271
  :jetty_port => nil,
47
272
  :solr_home => nil,
48
- :startup_wait => nil
273
+ :startup_wait => nil,
274
+ :jetty_opts => nil
49
275
  }
50
276
 
51
- ts = Jettywrapper.configure(jetty_params)
277
+ ts = Jettywrapper.configure(jetty_params)
52
278
  ts.quiet.should == true
53
279
  ts.jetty_home.should == "/path/to/jetty"
54
280
  ts.port.should == 8888
55
281
  ts.solr_home.should == File.join(ts.jetty_home, "solr")
56
282
  ts.startup_wait.should == 5
283
+ ts.jetty_opts.should == []
57
284
  end
58
-
285
+
59
286
  it "passes all the expected values to jetty during startup" do
60
- ts = Jettywrapper.configure(@jetty_params)
287
+ ts = Jettywrapper.configure(@jetty_params)
61
288
  command = ts.jetty_command
62
289
  command.should include("-Dsolr.solr.home=#{@jetty_params[:solr_home]}")
63
290
  command.should include("-Djetty.port=#{@jetty_params[:jetty_port]}")
64
- command.should include("-Xmx256mb")
65
-
291
+ command.should include("-Xmx256m")
292
+ command.should include("start.jar")
293
+ command.slice(command.index('start.jar')+1, 2).should == @jetty_params[:jetty_opts]
66
294
  end
67
-
295
+
296
+ it "escapes the :solr_home parameter" do
297
+ ts = Jettywrapper.configure(@jetty_params.merge(:solr_home => '/path with spaces/to/solr'))
298
+ command = ts.jetty_command
299
+ command.should include("-Dsolr.solr.home=/path\\ with\\ spaces/to/solr")
300
+ end
301
+
68
302
  it "has a pid if it has been started" do
69
303
  jetty_params = {
70
304
  :jetty_home => '/tmp'
71
305
  }
72
- ts = Jettywrapper.configure(jetty_params)
73
- Jettywrapper.any_instance.stubs(:build_process).returns(stub('proc', :pid=>5454))
306
+ ts = Jettywrapper.configure(jetty_params)
307
+ Jettywrapper.any_instance.stub(:process).and_return(double('proc', :start => nil, :pid=>5454))
74
308
  ts.stop
75
309
  ts.start
76
310
  ts.pid.should eql(5454)
77
311
  ts.stop
78
312
  end
79
-
313
+
80
314
  it "can pass params to a start method" do
81
315
  jetty_params = {
82
316
  :jetty_home => '/tmp', :jetty_port => 8777
83
317
  }
84
- ts = Jettywrapper.configure(jetty_params)
318
+ ts = Jettywrapper.configure(jetty_params)
85
319
  ts.stop
86
- Jettywrapper.any_instance.stubs(:build_process).returns(stub('proc', :pid=>2323))
320
+ Jettywrapper.any_instance.stub(:process).and_return(double('proc', :start => nil, :pid=>2323))
87
321
  swp = Jettywrapper.start(jetty_params)
88
322
  swp.pid.should eql(2323)
89
- swp.pid_file.should eql("_tmp.pid")
323
+ swp.pid_file.should eql("_tmp_test.pid")
90
324
  swp.stop
91
325
  end
92
-
93
- it "checks to see if its pid files are stale" do
94
- @pending
95
- end
96
-
97
- # return true if it's running, otherwise return false
326
+
98
327
  it "can get the status for a given jetty instance" do
99
328
  # Don't actually start jetty, just fake it
100
- Jettywrapper.any_instance.stubs(:build_process).returns(stub('proc', :pid=>12345))
101
-
329
+ Jettywrapper.any_instance.stub(:process).and_return(double('proc', :start => nil, :pid=>12345))
330
+
102
331
  jetty_params = {
103
332
  :jetty_home => File.expand_path("#{File.dirname(__FILE__)}/../../jetty")
104
333
  }
@@ -108,10 +337,10 @@ module Hydra
108
337
  Jettywrapper.is_jetty_running?(jetty_params).should eql(true)
109
338
  Jettywrapper.stop(jetty_params)
110
339
  end
111
-
340
+
112
341
  it "can get the pid for a given jetty instance" do
113
342
  # Don't actually start jetty, just fake it
114
- Jettywrapper.any_instance.stubs(:build_process).returns(stub('proc', :pid=>54321))
343
+ Jettywrapper.any_instance.stub(:process).and_return(double('proc', :start => nil, :pid=>54321))
115
344
  jetty_params = {
116
345
  :jetty_home => File.expand_path("#{File.dirname(__FILE__)}/../../jetty")
117
346
  }
@@ -121,35 +350,46 @@ module Hydra
121
350
  Jettywrapper.pid(jetty_params).should eql(54321)
122
351
  Jettywrapper.stop(jetty_params)
123
352
  end
124
-
353
+
125
354
  it "can pass params to a stop method" do
126
355
  jetty_params = {
127
356
  :jetty_home => '/tmp', :jetty_port => 8777
128
357
  }
129
- Jettywrapper.any_instance.stubs(:build_process).returns(stub('proc', :pid=>2323))
358
+ Jettywrapper.any_instance.stub(:process).and_return(double('proc', :start => nil, :pid=>2323))
130
359
  swp = Jettywrapper.start(jetty_params)
131
360
  (File.file? swp.pid_path).should eql(true)
132
-
361
+
133
362
  swp = Jettywrapper.stop(jetty_params)
134
363
  (File.file? swp.pid_path).should eql(false)
135
364
  end
136
-
137
- it "knows what its pid file should be called" do
138
- ts = Jettywrapper.configure(@jetty_params)
139
- ts.pid_file.should eql("_path_to_jetty.pid")
365
+
366
+ describe "creates a pid file" do
367
+ let(:ts) { Jettywrapper.configure(@jetty_params) }
368
+ describe "when the environment isn't set" do
369
+ before { ENV['environment'] = nil }
370
+ it "should have the path and env in the name" do
371
+ ts.pid_file.should eql("_path_to_jetty_development.pid")
372
+ end
373
+ end
374
+ describe "when the environment is set" do
375
+ before { ENV['environment'] = 'test' }
376
+ it "should have the path and env in the name" do
377
+ ts.pid_file.should eql("_path_to_jetty_test.pid")
378
+ end
379
+ end
140
380
  end
141
-
381
+
142
382
  it "knows where its pid file should be written" do
143
- ts = Jettywrapper.configure(@jetty_params)
383
+ ts = Jettywrapper.configure(@jetty_params)
144
384
  ts.pid_dir.should eql(File.expand_path("#{ts.base_path}/tmp/pids"))
145
385
  end
146
-
386
+
147
387
  it "writes a pid to a file when it is started" do
148
388
  jetty_params = {
149
389
  :jetty_home => '/tmp'
150
390
  }
151
- ts = Jettywrapper.configure(jetty_params)
152
- Jettywrapper.any_instance.stubs(:build_process).returns(stub('proc', :pid=>2222))
391
+ ts = Jettywrapper.configure(jetty_params)
392
+ Jettywrapper.any_instance.stub(:process).and_return(double('proc', :start => nil, :pid=>2222))
153
393
  ts.stop
154
394
  ts.pid_file?.should eql(false)
155
395
  ts.start
@@ -158,50 +398,64 @@ module Hydra
158
398
  pid_from_file = File.open( ts.pid_path ) { |f| f.gets.to_i }
159
399
  pid_from_file.should eql(2222)
160
400
  end
161
-
401
+
162
402
  end # end of instantiation context
163
-
403
+
164
404
  context "logging" do
165
405
  it "has a logger" do
166
- ts = Jettywrapper.configure(@jetty_params)
406
+ ts = Jettywrapper.configure(@jetty_params)
167
407
  ts.logger.should be_kind_of(Logger)
168
408
  end
169
-
170
- end # end of logging context
171
-
409
+
410
+ end # end of logging context
411
+
172
412
  context "wrapping a task" do
173
413
  it "wraps another method" do
174
- Jettywrapper.any_instance.stubs(:start).returns(true)
175
- Jettywrapper.any_instance.stubs(:stop).returns(true)
176
- error = Jettywrapper.wrap(@jetty_params) do
414
+ Jettywrapper.any_instance.stub(:start).and_return(true)
415
+ Jettywrapper.any_instance.stub(:stop).and_return(true)
416
+ error = Jettywrapper.wrap(@jetty_params) do
177
417
  end
178
418
  error.should eql(false)
179
419
  end
180
-
420
+
181
421
  it "configures itself correctly when invoked via the wrap method" do
182
- Jettywrapper.any_instance.stubs(:start).returns(true)
183
- Jettywrapper.any_instance.stubs(:stop).returns(true)
184
- error = Jettywrapper.wrap(@jetty_params) do
185
- ts = Jettywrapper.instance
186
- ts.quiet.should == true
422
+ Jettywrapper.any_instance.stub(:start).and_return(true)
423
+ Jettywrapper.any_instance.stub(:stop).and_return(true)
424
+ error = Jettywrapper.wrap(@jetty_params) do
425
+ ts = Jettywrapper.instance
426
+ ts.quiet.should == @jetty_params[:quiet]
187
427
  ts.jetty_home.should == "/path/to/jetty"
188
- ts.port.should == 8888
428
+ ts.port.should == @jetty_params[:jetty_port]
189
429
  ts.solr_home.should == "/path/to/solr"
190
- ts.startup_wait.should == 0
430
+ ts.startup_wait.should == 0
191
431
  end
192
432
  error.should eql(false)
193
433
  end
194
-
434
+
195
435
  it "captures any errors produced" do
196
- Jettywrapper.any_instance.stubs(:start).returns(true)
197
- Jettywrapper.any_instance.stubs(:stop).returns(true)
198
- error = Jettywrapper.wrap(@jetty_params) do
436
+ Jettywrapper.any_instance.stub(:start).and_return(true)
437
+ Jettywrapper.any_instance.stub(:stop).and_return(true)
438
+ Jettywrapper.instance.logger.should_receive(:error).with("*** Error starting jetty: this is an expected error message")
439
+ expect { error = Jettywrapper.wrap(@jetty_params) do
199
440
  raise "this is an expected error message"
200
- end
201
- error.class.should eql(RuntimeError)
202
- error.message.should eql("this is an expected error message")
441
+ end }.to raise_error "this is an expected error message"
203
442
  end
204
-
443
+
205
444
  end # end of wrapping context
445
+
446
+ context "quiet mode", :quiet => true do
447
+ it "inherits the current stderr/stdout in 'loud' mode" do
448
+ ts = Jettywrapper.configure(@jetty_params.merge(:quiet => false))
449
+ process = ts.process
450
+ process.io.stderr.should == $stderr
451
+ process.io.stdout.should == $stdout
452
+ end
453
+
454
+ it "redirect stderr/stdout to a log file in quiet mode" do
455
+ ts = Jettywrapper.configure(@jetty_params.merge(:quiet => true))
456
+ process = ts.process
457
+ process.io.stderr.should_not == $stderr
458
+ process.io.stdout.should_not == $stdout
459
+ end
460
+ end
206
461
  end
207
- end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,21 @@
1
1
  $: << File.join(File.dirname(__FILE__), "/../../lib")
2
- require 'spec/autorun'
3
- # require 'spec/rails'
4
2
 
5
- Spec::Runner.configure do |config|
6
- config.mock_with :mocha
7
- end
3
+ if ENV['COVERAGE'] and RUBY_VERSION =~ /^1.9/
4
+ require 'simplecov'
5
+ require 'simplecov-rcov'
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
8
+ SimpleCov.start
9
+ end
10
+
11
+ require 'rspec/its'
12
+ require 'jettywrapper'
13
+
14
+ RSpec.configure do |config|
15
+ end
16
+
17
+ unless ENV.select { |k,v| k =~ /TEST_JETTY_PORT/ }.empty?
18
+ TEST_JETTY_PORTS = ENV.select { |k,v| k =~ /TEST_JETTY_PORT/ }.sort_by { |k,v| k }.map { |k,v| v }
19
+ else
20
+ TEST_JETTY_PORTS = [8983, 8984,9999,8888]
21
+ end