nginx_test_helper 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ require "nginx_test_helper/env_methods"
1
2
  require "erb"
2
3
 
3
4
  module NginxTestHelper
@@ -33,7 +34,11 @@ module NginxTestHelper
33
34
  def write_directive(name, value, comment=nil)
34
35
  directive = []
35
36
  directive << %(##{comment}) unless comment.nil?
36
- directive << %(#{"#" if value.nil?}#{name} "#{value}";)
37
+ if (value.is_a?(Array))
38
+ directive << %(#{"#" if value.empty?}#{name} #{value.join(" ")};)
39
+ else
40
+ directive << %(#{"#" if value.nil?}#{name} "#{value}";)
41
+ end
37
42
  directive.join("\n")
38
43
  end
39
44
 
@@ -24,6 +24,18 @@ module NginxTestHelper
24
24
  def nginx_tests_tmp_dir
25
25
  ENV['NGINX_TESTS_TMP_DIR'].nil? ? "/tmp/nginx_tests" : ENV['NGINX_TESTS_TMP_DIR']
26
26
  end
27
+
28
+ def nginx_tests_cores_dir
29
+ File.join(nginx_tests_tmp_dir, "cores")
30
+ end
31
+
32
+ def nginx_tests_core_dir(current_test)
33
+ File.join(nginx_tests_cores_dir, current_test)
34
+ end
35
+
36
+ def nginx_event_type
37
+ (RUBY_PLATFORM =~ /darwin/) ? 'kqueue' : 'epoll'
38
+ end
27
39
  end
28
40
 
29
41
  include ClassMethods
@@ -0,0 +1,75 @@
1
+ module NginxTestHelper
2
+ module HttpMatchers
3
+ class BeHttpStatus
4
+ def initialize(expected)
5
+ @expected = expected
6
+ @should_has_content = nil
7
+ end
8
+
9
+ def matches?(target)
10
+ @target = target
11
+ ret = @target.response_header.status.eql?(@expected)
12
+ ret = @should_has_content ? has_content? : !has_content? unless (@should_has_content.nil? || !ret)
13
+ ret = @target.response_header.content_length == @length unless (@length.nil? || !ret)
14
+ ret = @target.response == @body unless (@body.nil? || !ret)
15
+ ret
16
+ end
17
+ alias == matches?
18
+
19
+ def without_body
20
+ @should_has_content = false
21
+ self
22
+ end
23
+
24
+ def with_body(body=nil)
25
+ @body = body
26
+ @should_has_content = true
27
+ self
28
+ end
29
+
30
+ def with_body_length(length)
31
+ @length = length
32
+ self
33
+ end
34
+
35
+ def failure_message_for_should
36
+ "expected that the '#{request.method}' to '#{request.uri}' to #{description}"
37
+ end
38
+
39
+ def failure_message_for_should_not
40
+ "expected that the '#{request.method}' to '#{request.uri}' not to #{description}"
41
+ end
42
+
43
+ def description
44
+ about_content = ""
45
+ returned_values = " but returned with status '#{@target.response_header.status}'"
46
+ if @body.nil? && @length.nil? && @should_has_content.nil?
47
+ returned_values += " and content_length equals to #{@target.response_header.content_length.to_i}"
48
+ elsif @body.nil? && @length.nil?
49
+ about_content += " and #{@should_has_content ? "with body" : "without body"}"
50
+ returned_values += " and #{@should_has_content ? "without body" : "with body"}"
51
+ elsif @body.nil?
52
+ about_content += " and content length #{@length}"
53
+ returned_values += " and #{@target.response_header.content_length} of length"
54
+ else
55
+ about_content += " and body '#{@target.response}'"
56
+ returned_values += " and #{@body} as content"
57
+ end
58
+ "be returned with status '#{@expected}'#{about_content}#{returned_values}"
59
+ end
60
+
61
+ private
62
+ def has_content?
63
+ @target.response_header.content_length.to_i > 0
64
+ end
65
+
66
+ def request
67
+ @target.respond_to?(:req) ? @target.req : @target
68
+ end
69
+ end
70
+
71
+ def be_http_status(expected)
72
+ BeHttpStatus.new(expected)
73
+ end
74
+ end
75
+ end
@@ -1,6 +1,25 @@
1
1
  if defined?(RSpec)
2
2
  RSpec.configure do |config|
3
3
  config.include NginxTestHelper
4
+ config.include NginxTestHelper::HttpMatchers
5
+
6
+ config.before(:suite) do
7
+ FileUtils.rm_rf Dir[File.join(NginxTestHelper.nginx_tests_cores_dir, "**")]
8
+ end
9
+
10
+ config.around(:each) do |ex|
11
+ logs = Dir[File.join(NginxTestHelper.nginx_tests_tmp_dir, "logs", "**")]
12
+ error_log_pre = logs.map{|log| File.readlines(log)}.flatten
13
+
14
+ ex.run
15
+
16
+ logs = Dir[File.join(NginxTestHelper.nginx_tests_tmp_dir, "logs", "**")]
17
+ error_log_pos = logs.map{|log| File.readlines(log)}.flatten
18
+ raise StandardError.new("\n\n#{config_id} let open sockets\n\n") if (error_log_pos - error_log_pre).join.include?("open socket")
19
+
20
+ cores = Dir[File.join(NginxTestHelper.nginx_tests_core_dir(config_id), "*core*")]
21
+ raise StandardError.new("Generated core dump(s) at:\n#{cores.join("\n")}\n\n") unless cores.empty?
22
+ end
4
23
  end
5
24
 
6
25
  RSpec::Matchers.define :be_in_the_interval do |min, max|
@@ -1,3 +1,3 @@
1
1
  module NginxTestHelper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "nginx_test_helper/version"
2
2
  require "nginx_test_helper/env_methods"
3
3
  require "nginx_test_helper/config"
4
+ require "nginx_test_helper/http_matchers"
4
5
  require "nginx_test_helper/rspec_utils"
5
6
  require "nginx_test_helper/command_line_tool"
6
7
  require "popen4"
@@ -75,11 +76,15 @@ module NginxTestHelper
75
76
  def start_server(config)
76
77
  error_message = ""
77
78
  unless config.configuration[:disable_start_stop_server]
78
- status = POpen4::popen4("#{ config.nginx_executable } -c #{ config.configuration_filename }") do |stdout, stderr, stdin, pid|
79
- error_message = stderr.read.strip unless stderr.eof
80
- return error_message unless error_message.nil?
79
+ working_dir = nginx_tests_core_dir(config_id)
80
+ FileUtils.mkdir_p working_dir
81
+ Dir.chdir working_dir do
82
+ status = POpen4::popen4("#{ config.nginx_executable } -c #{ config.configuration_filename }") do |stdout, stderr, stdin, pid|
83
+ error_message = stderr.read.strip unless stderr.eof
84
+ return error_message unless error_message.nil?
85
+ end
86
+ fail("Server doesn't started - #{error_message}") unless status.exitstatus == 0
81
87
  end
82
- fail("Server doesn't started - #{error_message}") unless status.exitstatus == 0
83
88
  end
84
89
  error_message
85
90
  end
@@ -84,5 +84,15 @@ describe NginxTestHelper::Config do
84
84
  conf.should include(%(\n#comment foo\nname "bar";))
85
85
  conf.should include(%(\nwithout_comment "bar";))
86
86
  end
87
+
88
+ context "when value is an Array" do
89
+ let(:configuration) { {:foo => ["bar", "zzz"], :xyz => nil, :configuration_template => %(\n<%= write_directive('name', foo, 'comment foo') %>\n<%= write_directive('without_comment', foo) %>)} }
90
+
91
+ it "should write multiple itens without quotes" do
92
+ conf = File.read(subject.configuration[:configuration_filename])
93
+ conf.should include(%(\n#comment foo\nname bar zzz;))
94
+ conf.should include(%(\nwithout_comment bar zzz;))
95
+ end
96
+ end
87
97
  end
88
98
  end
@@ -26,6 +26,14 @@ describe NginxTestHelper::EnvMethods do
26
26
  it "should return nginx tests tmp dir" do
27
27
  subject.nginx_tests_tmp_dir.should eql("/tmp/nginx_tests")
28
28
  end
29
+
30
+ it "should return nginx tests cores dir based on tmp dir" do
31
+ subject.nginx_tests_cores_dir.should eql("/tmp/nginx_tests/cores")
32
+ end
33
+
34
+ it "should return nginx tests core dir based on tmp dir" do
35
+ subject.nginx_tests_core_dir("test_id").should eql("/tmp/nginx_tests/cores/test_id")
36
+ end
29
37
  end
30
38
 
31
39
  context "with environment values" do
@@ -53,6 +61,16 @@ describe NginxTestHelper::EnvMethods do
53
61
  ENV['NGINX_TESTS_TMP_DIR'] = "/path/to/tests/tmp/dir"
54
62
  subject.nginx_tests_tmp_dir.should eql("/path/to/tests/tmp/dir")
55
63
  end
64
+
65
+ it "should return nginx tests cores dir based on tmp dir" do
66
+ ENV['NGINX_TESTS_TMP_DIR'] = "/path/to/tests/tmp/dir"
67
+ subject.nginx_tests_cores_dir.should eql("/path/to/tests/tmp/dir/cores")
68
+ end
69
+
70
+ it "should return nginx tests core dir based on tmp dir" do
71
+ ENV['NGINX_TESTS_TMP_DIR'] = "/path/to/tests/tmp/dir"
72
+ subject.nginx_tests_core_dir("test_id").should eql("/path/to/tests/tmp/dir/cores/test_id")
73
+ end
56
74
  end
57
75
 
58
76
  it "should use nginx host and port to compose nginx address" do
@@ -61,6 +79,22 @@ describe NginxTestHelper::EnvMethods do
61
79
 
62
80
  subject.nginx_address.should eql("http://some_host:some_port")
63
81
  end
82
+
83
+ context "when on a Darwin ruby platform" do
84
+ it "should return kqueue as event type" do
85
+ with_constants({"RUBY_PLATFORM" => "x86_64-darwin13.0"}) do
86
+ subject.nginx_event_type.should eql("kqueue")
87
+ end
88
+ end
89
+ end
90
+
91
+ context "when not on a Darwin ruby platform" do
92
+ it "should return epoll as event type" do
93
+ with_constants({"RUBY_PLATFORM" => "Any other platform"}) do
94
+ subject.nginx_event_type.should eql("epoll")
95
+ end
96
+ end
97
+ end
64
98
  end
65
99
 
66
100
  context "when a class include the NginxTestHelper::EnvMethods module" do
data/spec/spec_helper.rb CHANGED
@@ -30,6 +30,24 @@ end
30
30
  # Requires lib.
31
31
  Dir[File.expand_path('../lib/**/*.rb', File.dirname(__FILE__))].each { |f| require f }
32
32
 
33
+ def with_constants(constants, &block)
34
+ old_verbose, $VERBOSE = $VERBOSE, nil
35
+ saved_constants = {}
36
+ constants.each do |constant, val|
37
+ saved_constants[ constant ] = Object.const_get( constant )
38
+ Object.const_set( constant, val )
39
+ end
40
+
41
+ begin
42
+ block.call
43
+ ensure
44
+ constants.each do |constant, val|
45
+ Object.const_set( constant, saved_constants[ constant ] )
46
+ end
47
+ $VERBOSE = old_verbose
48
+ end
49
+ end
50
+
33
51
  module NginxConfiguration
34
52
  def self.default_configuration
35
53
  {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nginx_test_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-14 00:00:00.000000000 Z
12
+ date: 2014-03-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: popen4
@@ -94,6 +94,7 @@ files:
94
94
  - lib/nginx_test_helper/command_line_tool.rb
95
95
  - lib/nginx_test_helper/config.rb
96
96
  - lib/nginx_test_helper/env_methods.rb
97
+ - lib/nginx_test_helper/http_matchers.rb
97
98
  - lib/nginx_test_helper/rspec_utils.rb
98
99
  - lib/nginx_test_helper/version.rb
99
100
  - nginx_test_helper.gemspec