trinidad_init_services 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Trinidad
2
2
  module InitServices
3
- VERSION = '1.2.3'
3
+ VERSION = '1.3.0'
4
4
  end
5
5
  end
@@ -5,8 +5,9 @@ require 'fileutils'
5
5
  require 'java'
6
6
 
7
7
  describe Trinidad::InitServices::Configuration do
8
-
8
+
9
9
  before :each do
10
+ ENV['JAVA_HOME'] = nil # make sure it does not interfere
10
11
  Dir.mkdir(tmp_dir) unless File.exist?(tmp_dir)
11
12
  Dir.mkdir(init_dir)
12
13
  end
@@ -14,54 +15,137 @@ describe Trinidad::InitServices::Configuration do
14
15
  after :each do
15
16
  FileUtils.rm_r init_dir
16
17
  Dir.rmdir(tmp_dir) if Dir.entries(tmp_dir) == [ '.', '..' ]
18
+ ENV_JAVA.update @@env_java
17
19
  end
18
20
 
21
+ before(:all) { @@env_java = ENV_JAVA.dup }
22
+
19
23
  it "creates the init.d file" do
20
- subject.configure(config_defaults)
21
-
22
- File.exist?(init_file).should be_true
24
+ subject.configure config_defaults.merge 'java_home' => 'tmp/java', 'jruby_home' => 'tmp/jruby'
25
+
26
+ expect( File.exist?(init_file) ).to be true
27
+
28
+ init_file_content = File.read(init_file)
29
+
30
+ expect( init_file_content ).to match(/JSVC=tmp\/jsvc\/bin\/jsvc/)
31
+ expect( init_file_content ).to match(/JAVA_HOME="tmp\/java"/)
32
+ expect( init_file_content ).to match(/JRUBY_HOME="tmp\/jruby"/)
33
+ expect( init_file_content ).to match(/BASE_PATH="tmp\/app"/)
34
+
35
+ expect( init_file_content ).to match(/PID_FILE="tmp\/trinidad.pid"/)
36
+ expect( init_file_content ).to match(/OUT_FILE="tmp\/trinidad.out"/)
37
+
38
+ expect( init_file_content ).to match(/TRINIDAD_OPTS="--dir tmp\/app -e production"/)
39
+
40
+ expect( init_file_content ).to match(/RUN_USER=""/)
41
+ end
42
+
43
+ it "configures memory requirements using JAVA_OPTS (Java 6)" do
44
+ ENV_JAVA['java.version'] = '1.6.0_43'
45
+ ENV_JAVA['os.arch'] = 'x64'
46
+
47
+ defaults = config_defaults.merge 'configure_memory' => true, 'hot_deployment' => true
48
+ subject.configure(defaults)
49
+
50
+ init_file_content = File.read(init_file)
51
+
52
+ java_opts = init_file_content.match(/JAVA_OPTS="(.*?)"/m)
53
+ expect( java_opts ).to_not be nil
54
+ expect( java_opts = java_opts[1] ).to_not be nil
55
+ expect( java_opts ).to include '-XX:+UseCodeCacheFlushing'
56
+ expect( java_opts ).to include '-XX:ReservedCodeCacheSize='
57
+ expect( java_opts ).to include '-XX:MaxPermSize='
58
+ expect( java_opts ).to_not include '-XX:MaxMetaspaceSize='
59
+ expect( java_opts ).to include '-Xmx'
60
+
61
+ expect( java_opts ).to include '-XX:+UseConcMarkSweepGC'
62
+ expect( java_opts ).to include '-XX:+UseConcMarkSweepGC'
63
+ expect( java_opts ).to include '-XX:+UseCompressedOops'
64
+ end
65
+
66
+ it "configures memory requirements using JAVA_OPTS (Java 8)" do
67
+ ENV_JAVA['java.version'] = '1.8.0_05'
68
+ ENV_JAVA['os.arch'] = 'x64'
69
+
70
+ defaults = config_defaults.merge 'configure_memory' => true
71
+ subject.configure(defaults)
72
+
73
+ init_file_content = File.read(init_file)
74
+
75
+ java_opts = init_file_content.match(/JAVA_OPTS="(.*?)"/m)
76
+ expect( java_opts ).to_not be nil
77
+ expect( java_opts = java_opts[1] ).to_not be nil
78
+ expect( java_opts ).to include '-XX:+UseCodeCacheFlushing'
79
+ expect( java_opts ).to include '-XX:ReservedCodeCacheSize='
80
+ expect( java_opts ).to include '-XX:MaxMetaspaceSize='
81
+ expect( java_opts ).to include '-Xmx'
82
+
83
+ expect( java_opts ).to_not include '-XX:+UseConcMarkSweepGC'
84
+ expect( java_opts ).to_not include '-XX:+UseCompressedOops'
85
+ end
86
+
87
+ it "calculates memory requirements using JAVA_OPTS" do
88
+ defaults = config_defaults.merge 'configure_memory' => true
89
+ subject.configure(defaults)
90
+
91
+ init_file_content = File.read(init_file)
92
+
93
+ java_opts = init_file_content.match(/JAVA_OPTS="(.*?)"/m)[1]
94
+ expect( java_opts ).to_not be nil
95
+ expect( code_cache_size = java_opts.match(/-XX:ReservedCodeCacheSize=(.*)m/)[1] ).to_not be nil
96
+ if java_version =~ /^1\.(6|7)/
97
+ expect( max_perm_size = java_opts.match(/-XX:MaxPermSize=(.*)m/)[1] ).to_not be nil
98
+ else
99
+ expect( max_perm_size = java_opts.match(/-XX:MaxMetaspaceSize=(.*)m/)[1] ).to_not be nil
100
+ end
101
+ expect( max_heap_size = java_opts.match(/-Xmx(.*)m/)[1] ).to_not be nil
102
+
103
+ total = code_cache_size.to_i + max_perm_size.to_i + max_heap_size.to_i
104
+
105
+ expect( 720 - total ).to be >= 0
106
+ expect( 720 - total ).to be <= 10
107
+ end
108
+
109
+ it "configures -Xmx for custom JAVA_HOME" do
110
+ defaults = config_defaults.merge 'configure_memory' => true, 'java_home' => '/opt/ibm/jre-5'
111
+ subject.configure(defaults)
23
112
 
24
113
  init_file_content = File.read(init_file)
25
114
 
26
- init_file_content.match(/JSVC=tmp\/jsvc/).should be_true
27
- init_file_content.match(/JAVA_HOME=tmp\/java/).should be_true
28
- init_file_content.match(/JRUBY_HOME=tmp\/jruby/).should be_true
29
- init_file_content.match(/APP_PATH=tmp\/app/).should be_true
30
- init_file_content.match(/TRINIDAD_OPTS="-d tmp\/app -e production"/).should be_true
31
-
32
- init_file_content.match(/RUN_USER=""/).should be_true
115
+ expect( init_file_content.match(/JAVA_OPTS=(.*)$/) ).to_not be nil
116
+ expect( init_file_content.match(/JAVA_OPTS=(.*)$/) ).to_not eql '-Xmx670m'
33
117
  end
34
-
118
+
35
119
  it "makes pid_file and log_file dirs" do
36
120
  pids_dir = File.join(tmp_dir, "pids")
37
121
  logs_dir = File.join(tmp_dir, "logs")
38
122
  begin
39
- subject.configure(
40
- config_defaults.merge 'pid_file' => "tmp/pids/trinidad.pid", 'log_file' => "tmp/logs/trinidad.log"
41
- )
42
-
43
- File.exist?(pids_dir).should be_true
44
- File.directory?(pids_dir).should be_true
45
- Dir.entries(pids_dir).should == ['.', '..']
46
-
47
- File.exist?(logs_dir).should be_true
48
- File.directory?(logs_dir).should be_true
49
- Dir.entries(logs_dir).should == ['.', '..']
123
+ config = config_defaults.dup; config.delete('out_file')
124
+ config.merge! 'pid_file' => "tmp/pids/trinidad.pid", 'log_file' => "tmp/logs/trinidad.out"
125
+ subject.configure(config)
126
+
127
+ expect( File.exist?(pids_dir) ).to be true
128
+ expect( File.directory?(pids_dir) ).to be true
129
+ expect( Dir.entries(pids_dir) ).to eql ['.', '..']
130
+
131
+ expect( File.exist?(logs_dir) ).to be true
132
+ expect( File.directory?(logs_dir) ).to be true
133
+ expect( Dir.entries(logs_dir) ).to eql ['.', '..']
50
134
  ensure
51
135
  Dir.rmdir(pids_dir) if File.exist?(pids_dir)
52
136
  Dir.rmdir(logs_dir) if File.exist?(logs_dir)
53
137
  end
54
138
  end
55
-
139
+
56
140
  if java.lang.System.getProperty('os.name') !~ /windows/i
57
141
 
58
142
  it "fails for non-existing run user" do
59
143
  username = random_username
60
- lambda {
61
- subject.configure(config_defaults.merge 'run_user' => username)
144
+ lambda {
145
+ subject.configure(config_defaults.merge 'run_user' => username)
62
146
  }.should raise_error(ArgumentError)
63
147
  end
64
-
148
+
65
149
  it "sets valid run user" do
66
150
  username = `whoami`.chomp
67
151
  subject.configure(config_defaults.merge 'run_user' => username)
@@ -71,81 +155,90 @@ describe Trinidad::InitServices::Configuration do
71
155
  end
72
156
 
73
157
  unless (`which make` rescue '').chomp.empty?
74
-
158
+
75
159
  before(:all) do
76
160
  FileUtils.rm_r "/tmp/jsvc-unix-src" if File.exist? "/tmp/jsvc-unix-src"
77
161
  end
78
-
162
+
79
163
  it "configures and compiles jsvc" do
80
- config_options = config_defaults.merge 'jsvc_path' => nil
164
+ config_options = config_defaults.merge 'jruby_home' => '/opt/jruby'
165
+ config_options['jsvc_path'] = nil
81
166
  config_options['jsvc_unpack_dir'] = '/tmp'
82
-
167
+
83
168
  java_home = java.lang.System.get_property("java.home")
84
169
  java_home = java_home[0...-4] if java_home[-4..-1] == '/jre'
85
170
  config_options['java_home'] = java_home # need a JDK dir
86
-
171
+
87
172
  subject = Trinidad::InitServices::Configuration.new
88
173
  subject.instance_eval do # a bit of stubbing/mocking :
89
174
  def detect_jsvc_path; nil; end
90
- def ask_path(path, default = nil)
175
+ def ask_path(path, default = nil)
91
176
  raise path unless path =~ /path to jsvc .*/; default
92
177
  end
93
178
  end
94
-
179
+
95
180
  subject.configure(config_options)
96
181
 
97
182
  init_file_content = File.read(init_file) rescue ''
98
183
  init_file_content.should =~ /JSVC=\/tmp\/jsvc\-unix\-src\/jsvc/
99
- end
100
-
184
+ end unless ENV['SKIP_JSVC'] == 'true'
185
+
101
186
  end
102
-
187
+
103
188
  end
104
-
189
+
105
190
  it "resolves bundled bundled prunsrv.exe based on system arch" do
106
191
  trinidad_libs = File.expand_path('trinidad-libs', File.join(File.dirname(__FILE__), '../..'))
107
192
  subject = Trinidad::InitServices::Configuration.new
108
193
  subject.initialize_paths
109
-
194
+
110
195
  path = subject.send :bundled_prunsrv_path, "amd64"
111
196
  path.should == File.join(trinidad_libs, 'windows/amd64/prunsrv.exe')
112
197
 
113
198
  path = subject.send :bundled_prunsrv_path, "x86_64"
114
199
  path.should == File.join(trinidad_libs, 'windows/ia64/prunsrv.exe')
115
-
200
+
116
201
  path = subject.send :bundled_prunsrv_path, "i386"
117
202
  path.should == File.join(trinidad_libs, 'windows/prunsrv.exe')
118
-
203
+
119
204
  path = subject.send :bundled_prunsrv_path, "x86"
120
205
  path.should == File.join(trinidad_libs, 'windows/prunsrv.exe')
121
206
  end
122
-
123
- it "configures windows service" do
207
+
208
+ let(:windows_configuration) do
124
209
  subject = Trinidad::InitServices::Configuration.new
125
210
  subject.instance_eval do
126
- def windows?; true; end
127
- def macosx?; false; end
128
- def system(command); @system_command = command; end
129
- def system_command; @system_command; end
211
+ def windows?; true end
212
+ def macosx?; false end
213
+ def service_listed_windows?(service); false end
214
+ def system(command); @system_command = command end
215
+ def system_command; @system_command end
216
+ def log_command(command); end
130
217
  end
218
+ subject
219
+ end
220
+
221
+ it "configures windows service" do
222
+ subject = windows_configuration
131
223
  config_options = {
132
224
  'app_path' => "C:/MyApp",
133
225
  'ruby_compat_version' => "RUBY1_9",
134
- 'trinidad_name' => "Trinidad",
135
- 'trinidad_service_id' => "TrinidadService",
136
- 'trinidad_service_desc' => "Trinidad Service Description",
226
+ 'service_id' => "TrinidadService",
227
+ 'service_name' => "Trinidad",
228
+ 'service_desc' => "Trinidad Service Description",
137
229
  'trinidad_options' => "-e production -p 4242 ",
138
230
  'java_home' => "C:/Program Files (x86)/jdk-1.7.0",
139
231
  'jruby_home' => "C:/Program Files/jruby",
140
232
  }
141
233
  subject.configure(config_options)
142
- subject.system_command.should_not be nil
143
- subject.system_command.should =~ /\/\/IS\/\/TrinidadService/
144
- subject.system_command.should =~ /--DisplayName="Trinidad"/
145
- subject.system_command.should =~ /--Description="Trinidad Service Description"/
146
- subject.system_command.should =~ /--StartParams=".*?\\daemon.rb;-d;C:\\MyApp;-e;production;-p;4242"/
147
- subject.system_command.should =~ /--Classpath=\".*?\\jruby-jsvc.jar;.*?\\commons-daemon.jar;.*?\\jruby.jar/
148
- subject.system_command.should =~ %r{
234
+ ( system_command = subject.system_command ).should_not be nil
235
+ system_command.should =~ /\/\/IS\/\/TrinidadService/
236
+ system_command.should =~ /--DisplayName="Trinidad"/
237
+ system_command.should =~ /--Description="Trinidad Service Description"/
238
+ system_command.should =~ /--StartParams=".*?\\daemon.rb;--dir;C:\\MyApp;-e;production;-p;4242"/
239
+ system_command.should =~ /--Classpath=".*?\\jruby-jsvc.jar;.*?\\commons-daemon.jar;.*?\\jruby.jar/
240
+ system_command.should =~ /--JavaHome="C\:\\Program Files \(x86\)\\jdk-1\.7\.0"/
241
+ system_command.should =~ %r{
149
242
  \+\+JvmOptions="
150
243
  -Djruby.home=C:\\Program\ Files\\jruby;
151
244
  -Djruby.lib=C:\\Program\ Files\\jruby\\lib;
@@ -155,7 +248,43 @@ describe Trinidad::InitServices::Configuration do
155
248
  "
156
249
  }x
157
250
  end
158
-
251
+
252
+ it "configures windows service log out/err/pid" do
253
+ subject = windows_configuration
254
+ config_options = {
255
+ 'app_path' => "C:/MyApp",
256
+ 'log_path' => "C:/MyApp/log",
257
+ 'out_file' => "C:/MyApp/log/STD.out",
258
+ }
259
+ subject.configure(config_options)
260
+ ( system_command = subject.system_command ).should_not be nil
261
+ system_command.should =~ /\/\/IS\/\/Trinidad/
262
+ system_command.should =~ /--DisplayName="Trinidad"/
263
+ system_command.should =~ /--StdOutput="C\:\\MyApp\\log\\STD.out"/
264
+ system_command.should =~ /--StdError="C\:\\MyApp\\log\\STD.out"/
265
+ system_command.should =~ /--PidFile=Trinidad.pid/
266
+ end
267
+
268
+ it "updates windows service when installed" do
269
+ subject = windows_configuration
270
+ subject.instance_eval do
271
+ def service_listed_windows?(service); service == 'Trinidad' end
272
+ end
273
+ config_options = {
274
+ 'app_path' => "C:/MyApp",
275
+ }
276
+ subject.configure(config_options)
277
+ ( system_command = subject.system_command ).should_not be nil
278
+ expect( system_command ).to match /\/\/US\/\/Trinidad/
279
+ end
280
+
281
+ it "uninstalls windows service" do
282
+ subject = windows_configuration
283
+ subject.uninstall
284
+ ( system_command = subject.system_command ).should_not be nil
285
+ expect( system_command ).to match /\/\/DS\/\/Trinidad/
286
+ end
287
+
159
288
  it "ask_path works when non tty and default nil" do
160
289
  subject.ask = false
161
290
  stdin = mock('stdin')
@@ -173,7 +302,7 @@ describe Trinidad::InitServices::Configuration do
173
302
  subject.send(:ask_path, 'Home', false)
174
303
  } ).to raise_error RuntimeError
175
304
  end
176
-
305
+
177
306
  it "ask= forces trinidad to not ask on tty" do
178
307
  subject.ask = false
179
308
  outcome = subject.send :ask, 'hello?', :there
@@ -182,53 +311,49 @@ describe Trinidad::InitServices::Configuration do
182
311
  outcome = subject.send :ask, 'de-ja-vu?', nil
183
312
  outcome.should be nil
184
313
  end
185
-
314
+
186
315
  it "say= silences standard output" do
187
316
  def subject.puts(msg)
188
317
  raise msg
189
318
  end
190
319
  lambda { subject.send :say, 'hello' }.should raise_error
191
-
320
+
192
321
  subject.say = false
193
322
  lambda { subject.send :say, 'hello' }.should_not raise_error
194
323
  end
195
-
324
+
196
325
  private
197
-
198
- def config_defaults
199
- YAML::load %Q{
200
- app_path: "tmp/app"
201
- trinidad_options: "-e production"
202
- jruby_home: "tmp/jruby"
203
- ruby_compat_version: RUBY1_8
204
- trinidad_name: Trinidad
205
- jsvc_path: "tmp/jsvc"
206
- java_home: "tmp/java"
207
- output_path: "tmp/etc_init.d"
208
- pid_file: "tmp/trinidad.pid"
209
- log_file: "tmp/trinidad.log"
210
- run_user: ""
211
- }
212
- end
213
-
214
- def init_file
215
- File.join init_dir, 'trinidad'
216
- end
217
326
 
218
- def init_dir
219
- File.join tmp_dir, 'etc_init.d'
220
- end
327
+ def config_defaults
328
+ YAML::load File.read(config_file_path)
329
+ end
221
330
 
222
- def tmp_dir
223
- File.join root_dir, 'tmp'
224
- end
331
+ def config_file_path
332
+ File.expand_path('init_service_config.yml', File.dirname(__FILE__))
333
+ end
334
+
335
+ def init_file
336
+ File.join init_dir, 'trinidad'
337
+ end
338
+
339
+ def init_dir
340
+ File.join tmp_dir, 'etc_init.d'
341
+ end
342
+
343
+ def tmp_dir
344
+ File.join root_dir, 'tmp'
345
+ end
346
+
347
+ def root_dir
348
+ File.join File.dirname(__FILE__), "/../../"
349
+ end
350
+
351
+ def random_username(len = 8)
352
+ (0...len).map{ ( 65 + rand(25) ).chr }.join.downcase
353
+ end
354
+
355
+ def java_version
356
+ Java::JavaLang::System.getProperty("java.version")
357
+ end
225
358
 
226
- def root_dir
227
- File.join File.dirname(__FILE__), "/../../"
228
- end
229
-
230
- def random_username(len = 8)
231
- (0...len).map{ ( 65 + rand(25) ).chr }.join.downcase
232
- end
233
-
234
359
  end
@@ -0,0 +1,11 @@
1
+ app_path: "tmp/app"
2
+ trinidad_options: "-e production"
3
+ #java_home: "tmp/java"
4
+ #jruby_home: "tmp/jruby"
5
+ ruby_compat_version: RUBY1_8
6
+ trinidad_name: Trinidad
7
+ jsvc_path: tmp/jsvc/bin/jsvc
8
+ output_path: tmp/etc_init.d
9
+ pid_file: tmp/trinidad.pid
10
+ out_file: tmp/trinidad.out
11
+ run_user: ""