Skip to content

Commit fd79dc8

Browse files
Merge pull request #8 from HatsuneMiku3939/test/more-testcase
add more testcase
2 parents b1b77bb + 3be5e5d commit fd79dc8

File tree

5 files changed

+191
-34
lines changed

5 files changed

+191
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ TODO
44
Gemfile.lock
55
coverage/**/*
66
*.gem
7+
*.log

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ require "bundler"
22
Bundler::GemHelper.install_tasks
33

44
require "rake/testtask"
5+
require "fileutils"
56

67
Rake::TestTask.new(:test) do |t|
8+
if File.exists? "stub_server.log"
9+
puts "clear stub_server.log"
10+
FileUtils.rm "stub_server.log"
11+
end
12+
713
t.libs.push("lib", "test")
814
t.test_files = FileList["test/**/test_*.rb"]
915
t.verbose = true

test/helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121

2222
require "test/unit/rr"
2323

24+
# require stub_server
25+
require "test/helper/stub_server"
26+
2427
Test::Unit::TestCase.include(Fluent::Test::Helpers)
2528
Test::Unit::TestCase.extend(Fluent::Test::Helpers)

test/helper/stub_server.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'webrick'
2+
3+
class StubServer
4+
def initialize
5+
create_server
6+
7+
# mount handler
8+
@server.mount_proc '/', &method(:ok)
9+
@server.mount_proc '/not_exist', &method(:not_exist)
10+
@server.mount_proc '/timeout', &method(:timeout)
11+
@server.mount_proc '/internal_error', &method(:internal_error)
12+
@server.mount_proc '/redirect', &method(:redirect)
13+
end
14+
15+
def start
16+
@thread = Thread.new { @server.start }
17+
end
18+
19+
def shutdown
20+
@server.shutdown
21+
22+
# wait until webrick was shutting down
23+
while true
24+
break if @thread.status == false
25+
26+
# issue webrick shutdown once more
27+
@server.shutdown
28+
sleep 1
29+
end
30+
31+
# then exit thread
32+
@thread.exit
33+
end
34+
35+
private
36+
def create_server
37+
@log_file = File.open("stub_server.log", "a+")
38+
@log = WEBrick::Log.new @log_file
39+
@access_log = [
40+
[@log_file, WEBrick::AccessLog::COMBINED_LOG_FORMAT],
41+
]
42+
43+
@server = WEBrick::HTTPServer.new :Port => 3939, :Logger => @log, :AccessLog => @access_log
44+
end
45+
46+
def ok(req, res)
47+
res.status = 200
48+
res['Content-Type'] = 'application/json'
49+
res.body = '{ "status": "OK" }'
50+
end
51+
52+
def not_exist(req, res)
53+
res.status = 404
54+
res.body = ''
55+
end
56+
57+
def timeout(req, res)
58+
sleep 3
59+
60+
res.status = 200
61+
res['Content-Type'] = 'application/json'
62+
res.body = '{ "status": "OK" }'
63+
end
64+
65+
def internal_error(req, res)
66+
res.status = 500
67+
res.body = ''
68+
end
69+
70+
def redirect(req, res)
71+
res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect, "http://127.0.0.1:3939/"
72+
end
73+
end

test/plugin/test_in_http_pull.rb

Lines changed: 108 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
require 'ostruct'
55

66
class HttpPullInputTest < Test::Unit::TestCase
7+
@stub_server = nil
8+
79
setup do
810
Fluent::Test.setup
11+
12+
@stub_server = StubServer.new
13+
@stub_server.start
14+
end
15+
16+
teardown do
17+
@stub_server.shutdown
918
end
1019

1120
sub_test_case "default value of each options" do
1221
TEST_DEFAULT_VALUE_CONFIG = %[
1322
tag test
14-
url http://127.0.0.1
23+
url http://127.0.0.1:3939
1524
1625
interval 3s
1726
format json
@@ -32,11 +41,10 @@ class HttpPullInputTest < Test::Unit::TestCase
3241
end
3342
end
3443

35-
sub_test_case "success case with status only" do
44+
sub_test_case "success case" do
3645
TEST_INTERVAL_3_CONFIG = %[
3746
tag test
38-
url http://127.0.0.1
39-
timeout 10
47+
url http://127.0.0.1:3939
4048
4149
interval 3s
4250
format none
@@ -45,37 +53,34 @@ class HttpPullInputTest < Test::Unit::TestCase
4553

4654
TEST_INTERVAL_5_CONFIG = %[
4755
tag test
48-
url http://127.0.0.1
49-
timeout 10
56+
url http://127.0.0.1:3939
5057
5158
interval 5s
5259
format json
5360
]
5461

55-
setup do
56-
mock(RestClient::Request).
57-
execute(method: :get,
58-
url: "http://127.0.0.1",
59-
timeout: 10).
60-
times(2) do
61-
OpenStruct.new({code: 200, body: '{"status": "OK"}'})
62-
end
63-
end
62+
TEST_INTERVAL_3_REDIRECT_CONFIG = %[
63+
tag test
64+
url http://127.0.0.1:3939/redirect
65+
66+
interval 3s
67+
format json
68+
]
6469

6570
test 'interval 3 with status_only' do
6671
d = create_driver TEST_INTERVAL_3_CONFIG
6772
assert_equal("test", d.instance.tag)
6873
assert_equal(3, d.instance.interval)
6974

70-
d.run(timeout: 5) do
75+
d.run(timeout: 8) do
7176
sleep 7
7277
end
7378
assert_equal(2, d.events.size)
7479

7580
d.events.each do |tag, time, record|
7681
assert_equal("test", tag)
7782

78-
assert_equal({"url"=>"http://127.0.0.1","status"=>200}, record)
83+
assert_equal({"url"=>"http://127.0.0.1:3939","status"=>200}, record)
7984
assert(time.is_a?(Fluent::EventTime))
8085
end
8186
end
@@ -85,20 +90,100 @@ class HttpPullInputTest < Test::Unit::TestCase
8590
assert_equal("test", d.instance.tag)
8691
assert_equal(5, d.instance.interval)
8792

88-
d.run(timeout: 7) do
93+
d.run(timeout: 12) do
8994
sleep 11
9095
end
9196
assert_equal(2, d.events.size)
9297

9398
d.events.each do |tag, time, record|
9499
assert_equal("test", tag)
95100

96-
assert_equal({"url"=>"http://127.0.0.1","status"=>200, "message"=>{"status"=>"OK"}}, record)
101+
assert_equal({"url"=>"http://127.0.0.1:3939","status"=>200, "message"=>{"status"=>"OK"}}, record)
102+
assert(time.is_a?(Fluent::EventTime))
103+
end
104+
end
105+
106+
test 'interval 3 with redirect' do
107+
d = create_driver TEST_INTERVAL_3_REDIRECT_CONFIG
108+
assert_equal("test", d.instance.tag)
109+
assert_equal(3, d.instance.interval)
110+
111+
d.run(timeout: 8) do
112+
sleep 7
113+
end
114+
assert_equal(2, d.events.size)
115+
116+
d.events.each do |tag, time, record|
117+
assert_equal("test", tag)
118+
119+
assert_equal({"url"=>"http://127.0.0.1:3939/redirect","status"=>200, "message"=>{"status"=>"OK"}}, record)
97120
assert(time.is_a?(Fluent::EventTime))
98121
end
99122
end
100123
end
101124

125+
sub_test_case "fail when not 200 OK" do
126+
TEST_404_INTERVAL_3_CONFIG = %[
127+
tag test
128+
url http://127.0.0.1:3939/not_exist
129+
130+
interval 3s
131+
format none
132+
status_only true
133+
]
134+
135+
TEST_500_INTERVAL_3_CONFIG = %[
136+
tag test
137+
url http://127.0.0.1:3939/internal_error
138+
139+
interval 3s
140+
format none
141+
status_only true
142+
]
143+
144+
test '404' do
145+
d = create_driver TEST_404_INTERVAL_3_CONFIG
146+
assert_equal("test", d.instance.tag)
147+
assert_equal(3, d.instance.interval)
148+
149+
d.run(timeout: 8) do
150+
sleep 7
151+
end
152+
assert_equal(2, d.events.size)
153+
154+
d.events.each do |tag, time, record|
155+
assert_equal("test", tag)
156+
157+
assert_equal("http://127.0.0.1:3939/not_exist", record["url"])
158+
assert(time.is_a?(Fluent::EventTime))
159+
160+
assert_equal(404, record["status"])
161+
assert_not_nil(record["error"])
162+
end
163+
end
164+
165+
test '500' do
166+
d = create_driver TEST_500_INTERVAL_3_CONFIG
167+
assert_equal("test", d.instance.tag)
168+
assert_equal(3, d.instance.interval)
169+
170+
d.run(timeout: 8) do
171+
sleep 7
172+
end
173+
assert_equal(2, d.events.size)
174+
175+
d.events.each do |tag, time, record|
176+
assert_equal("test", tag)
177+
178+
assert_equal("http://127.0.0.1:3939/internal_error", record["url"])
179+
assert(time.is_a?(Fluent::EventTime))
180+
181+
assert_equal(500, record["status"])
182+
assert_not_nil(record["error"])
183+
end
184+
end
185+
end
186+
102187
sub_test_case "fail when remote down" do
103188
TEST_REFUSED_CONFIG = %[
104189
tag test
@@ -111,7 +196,7 @@ class HttpPullInputTest < Test::Unit::TestCase
111196
d = create_driver TEST_REFUSED_CONFIG
112197
assert_equal("test", d.instance.tag)
113198

114-
d.run(timeout: 2) do
199+
d.run(timeout: 4) do
115200
sleep 3
116201
end
117202

@@ -131,38 +216,27 @@ class HttpPullInputTest < Test::Unit::TestCase
131216
sub_test_case "fail when remote timeout" do
132217
TEST_TIMEOUT_FAIL_CONFIG = %[
133218
tag test
134-
url http://127.0.0.1
219+
url http://127.0.0.1:3939/timeout
135220
timeout 2s
136221
137222
interval 3s
138223
format json
139224
]
140225

141-
setup do
142-
mock(RestClient::Request).
143-
execute(method: :get,
144-
url: "http://127.0.0.1",
145-
timeout: 2).
146-
times(2) do
147-
sleep 2
148-
raise RestClient::Exceptions::Timeout.new
149-
end
150-
end
151-
152226
test "timeout" do
153227
d = create_driver TEST_TIMEOUT_FAIL_CONFIG
154228
assert_equal("test", d.instance.tag)
155229
assert_equal(2, d.instance.timeout)
156230

157-
d.run(timeout: 5) do
231+
d.run(timeout: 8) do
158232
sleep 7
159233
end
160234
assert_equal(2, d.events.size)
161235

162236
d.events.each do |tag, time, record|
163237
assert_equal("test", tag)
164238

165-
assert_equal("http://127.0.0.1", record["url"])
239+
assert_equal("http://127.0.0.1:3939/timeout", record["url"])
166240
assert(time.is_a?(Fluent::EventTime))
167241

168242
assert_equal(0, record["status"])

0 commit comments

Comments
 (0)