44require 'ostruct'
55
66class HttpPullInputTest < Test ::Unit ::TestCase
7- TEST_INTERVAL_3_CONFIG = %[
8- tag test
9- url http://127.0.0.1
7+ setup do
8+ Fluent ::Test . setup
9+ end
10+
11+ sub_test_case "default value of each options" do
12+ TEST_DEFAULT_VALUE_CONFIG = %[
13+ tag test
14+ url http://127.0.0.1
15+ interval 3s
16+ ]
1017
11- interval 3
12- status_only true
13- ]
18+ test 'status_only' do
19+ d = create_driver TEST_DEFAULT_VALUE_CONFIG
20+ assert_equal ( "test" , d . instance . tag )
1421
15- TEST_INTERVAL_5_CONFIG = %[
16- tag test
17- url http://127.0.0.1
22+ assert_equal ( false , d . instance . status_only )
23+ end
1824
19- interval 5
20- ]
25+ test 'timeout' do
26+ d = create_driver TEST_DEFAULT_VALUE_CONFIG
27+ assert_equal ( "test" , d . instance . tag )
2128
22- setup do
23- Fluent :: Test . setup
29+ assert_equal ( 10 , d . instance . timeout )
30+ end
2431 end
2532
2633 sub_test_case "success case with status only" do
34+ TEST_INTERVAL_3_CONFIG = %[
35+ tag test
36+ url http://127.0.0.1
37+ timeout 10
38+
39+ interval 3s
40+ status_only true
41+ ]
42+
43+ TEST_INTERVAL_5_CONFIG = %[
44+ tag test
45+ url http://127.0.0.1
46+ timeout 10
47+
48+ interval 5s
49+ ]
50+
2751 setup do
28- mock ( RestClient ) . get ( "http://127.0.0.1" ) . times ( 2 ) do
29- OpenStruct . new ( { code : 200 , body : '{"status": "OK"}' } )
30- end
52+ mock ( RestClient ::Request ) .
53+ execute ( method : :get ,
54+ url : "http://127.0.0.1" ,
55+ timeout : 10 ) .
56+ times ( 2 ) do
57+ OpenStruct . new ( { code : 200 , body : '{"status": "OK"}' } )
58+ end
3159 end
3260
3361 test 'interval 3 with status_only' do
3462 d = create_driver TEST_INTERVAL_3_CONFIG
35- assert_equal "test" , d . instance . tag
63+ assert_equal ( "test" , d . instance . tag )
64+ assert_equal ( 3 , d . instance . interval )
3665
3766 d . run ( timeout : 5 ) do
3867 sleep 7
3968 end
40- assert_equal 2 , d . events . size
69+ assert_equal ( 2 , d . events . size )
4170
4271 d . events . each do |tag , time , record |
4372 assert_equal ( "test" , tag )
73+
4474 assert_equal ( { "url" => "http://127.0.0.1" , "status" => 200 } , record )
4575 assert ( time . is_a? ( Fluent ::EventTime ) )
4676 end
4777 end
4878
4979 test 'interval 5' do
5080 d = create_driver TEST_INTERVAL_5_CONFIG
51- assert_equal "test" , d . instance . tag
81+ assert_equal ( "test" , d . instance . tag )
82+ assert_equal ( 5 , d . instance . interval )
5283
5384 d . run ( timeout : 7 ) do
5485 sleep 11
5586 end
56- assert_equal 2 , d . events . size
87+ assert_equal ( 2 , d . events . size )
5788
5889 d . events . each do |tag , time , record |
5990 assert_equal ( "test" , tag )
91+
6092 assert_equal ( { "url" => "http://127.0.0.1" , "status" => 200 , "message" => { "status" => "OK" } } , record )
6193 assert ( time . is_a? ( Fluent ::EventTime ) )
6294 end
@@ -71,14 +103,62 @@ class HttpPullInputTest < Test::Unit::TestCase
71103 ]
72104 test "connection refused by remote" do
73105 d = create_driver TEST_REFUSED_CONFIG
74- d . run ( timeout : 2 ) { sleep 3 }
106+ assert_equal ( "test" , d . instance . tag )
75107
76- assert_equal 3 , d . events . size
108+ d . run ( timeout : 2 ) do
109+ sleep 3
110+ end
111+
112+ assert_equal ( 3 , d . events . size )
77113 d . events . each do |tag , time , record |
78114 assert_equal ( "test" , tag )
115+
79116 assert_equal ( "http://127.0.0.1:5927" , record [ "url" ] )
117+ assert ( time . is_a? ( Fluent ::EventTime ) )
118+
80119 assert_equal ( 0 , record [ "status" ] )
120+ assert_not_nil ( record [ "error" ] )
121+ end
122+ end
123+ end
124+
125+ sub_test_case "fail when remote timeout" do
126+ TEST_TIMEOUT_FAIL_CONFIG = %[
127+ tag test
128+ url http://127.0.0.1
129+ timeout 2
130+
131+ interval 3
132+ ]
133+
134+ setup do
135+ mock ( RestClient ::Request ) .
136+ execute ( method : :get ,
137+ url : "http://127.0.0.1" ,
138+ timeout : 2 ) .
139+ times ( 2 ) do
140+ sleep 2
141+ raise RestClient ::Exceptions ::Timeout . new
142+ end
143+ end
144+
145+ test "timeout" do
146+ d = create_driver TEST_TIMEOUT_FAIL_CONFIG
147+ assert_equal ( "test" , d . instance . tag )
148+
149+ d . run ( timeout : 5 ) 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" , record [ "url" ] )
81158 assert ( time . is_a? ( Fluent ::EventTime ) )
159+
160+ assert_equal ( 0 , record [ "status" ] )
161+ assert_not_nil ( record [ "error" ] )
82162 end
83163 end
84164 end
0 commit comments