racknga 0.9.1 → 0.9.2

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.
@@ -35,17 +35,17 @@ end
35
35
  end
36
36
 
37
37
  if @label
38
- parameter_prefix = /\Apassenger_(?:#{@label}_)?domain_/
38
+ parameter_prefix = /\Apassenger_(?:#{@label}_)?application_/
39
39
  else
40
- parameter_prefix = /\Apassenger_domain_/
40
+ parameter_prefix = /\Apassenger_application_/
41
41
  end
42
42
  parameter = File.basename($0).gsub(parameter_prefix, '')
43
43
  if /_(sessions|processed|uptime)\z/ =~ parameter
44
- @domain = $PREMATCH
44
+ @application = $PREMATCH
45
45
  @type = $1
46
- @domain = nil if @domain and @domain.empty?
46
+ @application = nil if @application and @application.empty?
47
47
  else
48
- @domain = @type = nil
48
+ @application = @type = nil
49
49
  end
50
50
 
51
51
  def passenger_status
@@ -72,7 +72,7 @@ def parse_uptime(uptime)
72
72
  uptime_in_minutes
73
73
  end
74
74
 
75
- def extract_domain(path)
75
+ def extract_application_name(path)
76
76
  components = path.split(/\//)
77
77
  ignore_components = ["current"]
78
78
  while ignore_components.include?(components.last)
@@ -84,24 +84,25 @@ end
84
84
  def parse_result(result)
85
85
  sections = {}
86
86
  section = nil
87
- domain = nil
87
+ application_name = nil
88
88
  result.each_line do |line|
89
89
  case section
90
- when "Domains"
90
+ when "Application groups"
91
91
  case line.chomp
92
92
  when /\A(.+):\s*\z/
93
93
  path = $1
94
- domain = extract_domain(path)
95
- sections[section] << [domain, []]
96
- when /\A\s+PID:\s+(\d+)\s+
97
- Sessions:\s+(\d+)\s+
98
- Processed:\s+(\d+)\s+
99
- Uptime:\s+(.+)\z/x
94
+ application_name = extract_application_name(path)
95
+ sections[section] << [application_name, []]
96
+ when /\A\s+\*\s+
97
+ PID:\s+(\d+)\s+
98
+ Sessions:\s+(\d+)\s+
99
+ Processed:\s+(\d+)\s+
100
+ Uptime:\s+(.+)\z/x
100
101
  pid = $1.to_i
101
102
  sessions = $2.to_i
102
103
  processed = $3.to_i
103
104
  uptime = parse_uptime($4)
104
- _domain, processes = sections[section].last
105
+ _application_name, processes = sections[section].last
105
106
  processes << {
106
107
  :pid => pid,
107
108
  :sessions => sessions,
@@ -119,6 +120,16 @@ def parse_result(result)
119
120
  sections
120
121
  end
121
122
 
123
+ def extract_applications(result)
124
+ parse_result(result)["Application groups"] || []
125
+ end
126
+
127
+ def extract_application(result)
128
+ extract_applications(result).find do |name, processes|
129
+ name == @application
130
+ end
131
+ end
132
+
122
133
  def vlabel
123
134
  case @type
124
135
  when "sessions"
@@ -139,26 +150,30 @@ def config
139
150
  exit(false)
140
151
  end
141
152
 
153
+ application = extract_application(result)
154
+ if application.nil?
155
+ puts("application doesn't exist: #{@application}: <\n#{result}\n>")
156
+ exit(false)
157
+ end
158
+
142
159
  if @label
143
- title = "Passenger: #{@label}: #{@type}: #{@domain}"
160
+ title = "Passenger: #{@label}: #{@type}: #{@application}"
144
161
  else
145
- title = "Passenger: #{@type}: #{@domain}"
162
+ title = "Passenger: #{@type}: #{@application}"
146
163
  end
147
- sections = parse_result(result)
164
+
148
165
  puts(<<-EOC)
149
166
  graph_title #{title}
150
167
  graph_category passenger
151
- graph_info Passenger #{@domain} #{@type}
168
+ graph_info Passenger #{@application} #{@type}
152
169
  graph_vlabel #{vlabel}
153
170
 
154
171
  EOC
155
- sections["Domains"].each do |domain, processes|
156
- next if domain != @domain
157
- processes.sort_by do |attributes|
158
- attributes[:pid]
159
- end.each_with_index do |attributes, i|
160
- puts("#{@type}#{i}.label #{i} (PID #{attributes[:pid]})")
161
- end
172
+ name, processes = application
173
+ processes.sort_by do |attributes|
174
+ attributes[:pid]
175
+ end.each_with_index do |attributes, i|
176
+ puts("#{@type}#{i}.label #{i} (PID #{attributes[:pid]})")
162
177
  end
163
178
  end
164
179
 
@@ -168,15 +183,17 @@ def report
168
183
  puts result
169
184
  exit(false)
170
185
  end
186
+ application = extract_application(result)
187
+ if application.nil?
188
+ puts("application doesn't exist: #{@application}: <\n#{result}\n>")
189
+ exit(false)
190
+ end
171
191
 
172
- sections = parse_result(result)
173
- sections["Domains"].each do |domain, processes|
174
- next if domain != @domain
175
- processes.sort_by do |attributes|
176
- attributes[:pid]
177
- end.each_with_index do |attributes, i|
178
- puts("#{@type}#{i}.value #{attributes[@type.to_sym]}")
179
- end
192
+ name, processes = application
193
+ processes.sort_by do |attributes|
194
+ attributes[:pid]
195
+ end.each_with_index do |attributes, i|
196
+ puts("#{@type}#{i}.value #{attributes[@type.to_sym]}")
180
197
  end
181
198
  end
182
199
 
@@ -193,30 +210,29 @@ when "auto", "autoconf", "detect"
193
210
  when "suggest"
194
211
  success, result = passenger_status
195
212
  if success
196
- sections = parse_result(result)
197
- domains = sections["Domains"]
198
- if domains
199
- domains.each do |domain, processes|
200
- puts "#{domain}_sessions"
201
- puts "#{domain}_processed"
202
- puts "#{domain}_uptime"
213
+ applications = extract_applications(result)
214
+ if applications.empty?
215
+ puts("no application: <\n#{result}\n>")
216
+ exit(false)
217
+ else
218
+ applications.each do |application, processes|
219
+ puts "#{application}_sessions"
220
+ puts "#{application}_processed"
221
+ puts "#{application}_uptime"
203
222
  end
204
223
  exit(true)
205
- else
206
- puts "no domain: #{result.inspect}"
207
- exit(false)
208
224
  end
209
225
  else
210
226
  puts result
211
227
  exit(false)
212
228
  end
213
229
  else
214
- if @domain.nil?
215
- puts "no domain"
230
+ if @application.nil?
231
+ puts "no application"
216
232
  exit(false)
217
233
  end
218
234
  if @type.nil?
219
- puts "no type: #{@domain}"
235
+ puts "no type: #{@application}"
220
236
  exit(false)
221
237
  end
222
238
  case mode
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2010 Kouhei Sutou <kou@clear-code.com>
1
+ # Copyright (C) 2010-2011 Kouhei Sutou <kou@clear-code.com>
2
2
  #
3
3
  # This library is free software; you can redistribute it and/or
4
4
  # modify it under the terms of the GNU Lesser General Public
@@ -14,29 +14,26 @@
14
14
  # License along with this library; if not, write to the Free Software
15
15
  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
16
 
17
- require 'rubygems'
18
-
19
- require 'rack/test'
20
- require 'webrat'
17
+ require "test/unit/capybara"
21
18
  require 'json'
22
19
 
23
20
  require 'racknga'
24
21
  require 'racknga/middleware/cache'
22
+ require 'racknga/middleware/instance_name'
25
23
 
26
- Webrat.configure do |config|
27
- config.mode = :rack
24
+ Capybara.configure do |config|
25
+ config.default_driver = nil
26
+ config.current_driver = nil
28
27
  end
29
28
 
30
29
  module RackngaTestUtils
31
- include Rack::Test::Methods
32
- include Webrat::Methods
33
- include Webrat::Matchers
30
+ include Capybara::DSL
34
31
 
35
32
  def fixtures_dir
36
33
  Pathname(__FILE__).dirname + "fixtures"
37
34
  end
38
35
 
39
- def response
40
- webrat_session.response
36
+ def get(*args)
37
+ page.driver.get(*args)
41
38
  end
42
39
  end
data/test/run-test.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # Copyright (C) 2010 Kouhei Sutou <kou@clear-code.com>
3
+ # Copyright (C) 2010-2011 Kouhei Sutou <kou@clear-code.com>
4
4
  #
5
5
  # This library is free software; you can redistribute it and/or
6
6
  # modify it under the terms of the GNU Lesser General Public
@@ -23,24 +23,19 @@ $KCODE = "u" if RUBY_VERSION < "1.9"
23
23
  require 'pathname'
24
24
 
25
25
  base_dir = Pathname(__FILE__).dirname.parent.expand_path
26
- test_unit_dir = base_dir + "test-unit"
27
- test_unit_lib_dir = test_unit_dir + "lib"
28
26
  rroonga_dir = base_dir.parent + "rroonga"
29
27
  rroonga_ext_dir = rroonga_dir + "ext" + "groonga"
30
28
  rroonga_lib_dir = rroonga_dir + "lib"
31
29
  lib_dir = base_dir + "lib"
32
30
  test_dir = base_dir + "test"
33
31
 
34
- unless File.exist?(test_unit_dir)
35
- test_unit_repository = "http://test-unit.rubyforge.org/svn/trunk/"
36
- system("svn co #{test_unit_repository} #{test_unit_dir}") or exit(false)
37
- end
38
-
39
- $LOAD_PATH.unshift(test_unit_lib_dir.to_s)
32
+ require "rubygems"
33
+ require "bundler/setup"
40
34
 
41
35
  require 'test/unit'
36
+ require 'test/unit/notify'
42
37
 
43
- # ARGV.unshift("--priority-mode")
38
+ Test::Unit::Priority.enable
44
39
 
45
40
  $LOAD_PATH.unshift(rroonga_ext_dir.to_s)
46
41
  $LOAD_PATH.unshift(rroonga_lib_dir.to_s)
@@ -0,0 +1,77 @@
1
+ # Copyright (C) 2011 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
+
17
+ class MiddlewareCacheTest < Test::Unit::TestCase
18
+ include RackngaTestUtils
19
+
20
+ def app
21
+ application = Proc.new do |environment|
22
+ @environment = environment
23
+ [200,
24
+ {"Content-Type" => "application/json"},
25
+ [@body]]
26
+ end
27
+ cache_options = {
28
+ :database_path => @database_path.to_s,
29
+ }
30
+ @cache = Racknga::Middleware::Cache.new(application, cache_options)
31
+ Proc.new do |environment|
32
+ environment = @update_environment.call(environment) if @update_environment
33
+ @cache.call(environment)
34
+ end
35
+ end
36
+
37
+ def setup
38
+ @cache_key_key = "racknga.cache.key"
39
+ @environment = nil
40
+ @body = "{}"
41
+ @update_environment = nil
42
+ setup_temporary_directory
43
+ @database_path = @temporary_directory + "cache/db"
44
+ Capybara.app = app
45
+ end
46
+
47
+ def setup_temporary_directory
48
+ @temporary_directory = Pathname.new(__FILE__).dirname + "tmp"
49
+ FileUtils.rm_rf(@temporary_directory)
50
+ FileUtils.mkdir_p(@temporary_directory)
51
+ end
52
+
53
+ def teardown
54
+ teardown_temporary_directory
55
+ end
56
+
57
+ def teardown_temporary_directory
58
+ FileUtils.rm_rf(@temporary_directory)
59
+ end
60
+
61
+ def test_4096_length_path
62
+ responses = @cache.database.responses
63
+ assert_equal([], responses.to_a.collect(&:key))
64
+ path = "/" + "x" * 4095
65
+ visit(path)
66
+ assert_equal([path], responses.to_a.collect(&:key))
67
+ end
68
+
69
+ def test_4096_length_over_path
70
+ responses = @cache.database.responses
71
+ assert_equal([], responses.to_a.collect(&:key))
72
+ path = "/" + "x" * 4096
73
+ visit(path)
74
+ assert_equal([Digest::SHA1.hexdigest(path)],
75
+ responses.to_a.collect(&:key))
76
+ end
77
+ end
@@ -0,0 +1,98 @@
1
+ # Copyright (C) 2011 Ryo Onodera <onodera@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
+
17
+ class InstanceNameTest < Test::Unit::TestCase
18
+ include RackngaTestUtils
19
+
20
+ def test_no_option
21
+ server = default_instance_name.server
22
+ user = default_instance_name.user
23
+ revision = default_instance_name.revision
24
+
25
+ instance_name_options({}) do
26
+ request
27
+ assert_header("Proc (at #{revision}) on #{server} by #{user}")
28
+ end
29
+ end
30
+
31
+ def test_application_name
32
+ application_name = "HelloWorld"
33
+ server = default_instance_name.server
34
+ user = default_instance_name.user
35
+ revision = default_instance_name.revision
36
+
37
+ instance_name_options(:application_name => application_name) do
38
+ request
39
+ assert_header("#{application_name} (at #{revision}) on #{server} by #{user}")
40
+ end
41
+ end
42
+
43
+ def test_both_application_name_and_version
44
+ application_name = "HelloWorld"
45
+ version = 1
46
+ server = default_instance_name.server
47
+ user = default_instance_name.user
48
+ revision = default_instance_name.revision
49
+
50
+ instance_name_options(:application_name => application_name,
51
+ :version => version) do
52
+ request
53
+ assert_header("#{application_name} v#{version} (at #{revision}) on #{server} by #{user}")
54
+ end
55
+ end
56
+
57
+ private
58
+ def prepare_rack_stack(options)
59
+ application = create_minimal_application
60
+ instance_name = create_instance_name(application, options)
61
+ outermost_wrapper_middleware(instance_name)
62
+ end
63
+
64
+ def outermost_wrapper_middleware(application)
65
+ Proc.new do |environment|
66
+ application.call(environment)
67
+ end
68
+ end
69
+
70
+ def create_instance_name(*arguments)
71
+ Racknga::Middleware::InstanceName.new(*arguments)
72
+ end
73
+
74
+ def default_instance_name
75
+ @default_instance_name ||= create_instance_name(create_minimal_application).freeze
76
+ end
77
+
78
+ def create_minimal_application
79
+ Proc.new do |environment|
80
+ [200,
81
+ {"Content-Type" => "text/plain"},
82
+ ["Hello world."]]
83
+ end
84
+ end
85
+
86
+ def request
87
+ get("/")
88
+ end
89
+
90
+ def instance_name_options(options)
91
+ Capybara.app = prepare_rack_stack(options)
92
+ yield
93
+ end
94
+
95
+ def assert_header(expected_header)
96
+ assert_equal(expected_header, page.response_headers["X-Responsed-By"])
97
+ end
98
+ end
@@ -36,32 +36,32 @@ class MiddlewareJSONPTest < Test::Unit::TestCase
36
36
  @environment = nil
37
37
  @body = "{}"
38
38
  @update_environment = nil
39
+ Capybara.app = app
39
40
  end
40
41
 
41
42
  def test_no_jsonp
42
- get "/"
43
- assert_equal(@body, webrat_session.response.body)
43
+ visit("/")
44
+ assert_not_jsonp_response(@body)
44
45
  end
45
46
 
46
47
  def test_jsonp
47
- get "/?callback=jsonp_callback"
48
- assert_equal("jsonp_callback(#{@body});",
49
- webrat_session.response.body)
48
+ visit("/?callback=jsonp_callback")
49
+ assert_jsonp_response("jsonp_callback(#{@body});")
50
50
  end
51
51
 
52
52
  def test_no_jsonp_cache_key
53
- get "/"
53
+ visit("/")
54
54
  assert_nil(@environment[@cache_key_key])
55
55
  end
56
56
 
57
57
  def test_jsonp_cache_key
58
- get "/?callback=jsonp_callback"
58
+ visit("/?callback=jsonp_callback")
59
59
  assert_equal("/",
60
60
  @environment[@cache_key_key])
61
61
  end
62
62
 
63
63
  def test_jsonp_cache_key_with_parameters
64
- get "/?query=ruby&callback=jsonp_callback&_=1279288762"
64
+ visit("/?query=ruby&callback=jsonp_callback&_=1279288762")
65
65
  assert_equal("/?query=ruby",
66
66
  @environment[@cache_key_key])
67
67
  end
@@ -71,8 +71,31 @@ class MiddlewareJSONPTest < Test::Unit::TestCase
71
71
  environment[@cache_key_key] = "pc"
72
72
  environment
73
73
  end
74
- get "/?callback=jsonp_callback"
74
+ visit("/?callback=jsonp_callback")
75
75
  assert_equal("pc:/",
76
76
  @environment[@cache_key_key])
77
77
  end
78
+
79
+ private
80
+ def assert_jsonp_response(body)
81
+ assert_equal({
82
+ :content_type => "text/javascript",
83
+ :body => body,
84
+ },
85
+ {
86
+ :content_type => page.response_headers["Content-Type"],
87
+ :body => page.source,
88
+ })
89
+ end
90
+
91
+ def assert_not_jsonp_response(body)
92
+ assert_equal({
93
+ :content_type => "application/json",
94
+ :body => body,
95
+ },
96
+ {
97
+ :content_type => page.response_headers["Content-Type"],
98
+ :body => page.source,
99
+ })
100
+ end
78
101
  end
@@ -39,6 +39,7 @@ module MiddlewareRangeTests
39
39
  end
40
40
  stat = @ogv.stat
41
41
  @etag = "%x-%x-%x" % [stat.ino, stat.size, stat.mtime.to_i * 1_000_000]
42
+ Capybara.app = app
42
43
  end
43
44
 
44
45
  def test_both
@@ -135,10 +136,10 @@ module MiddlewareRangeTests
135
136
  :body => body,
136
137
  },
137
138
  {
138
- :status => response.status,
139
- :content_length => response.headers["Content-Length"],
140
- :content_range => response.headers["Content-Range"],
141
- :body => response.body,
139
+ :status => page.status_code,
140
+ :content_length => page.response_headers["Content-Length"],
141
+ :content_range => page.response_headers["Content-Range"],
142
+ :body => page.source,
142
143
  })
143
144
  end
144
145
  end