fluent-plugin-scalyr 0.8.10 → 0.8.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Scalyr Output Plugin for Fluentd
3
5
  #
@@ -15,8 +17,6 @@
15
17
  # See the License for the specific language governing permissions and
16
18
  # limitations under the License.
17
19
 
18
-
19
-
20
20
  module Scalyr
21
21
  class ClientError < StandardError; end
22
22
  class Client4xxError < StandardError; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Scalyr Output Plugin for Fluentd
3
5
  #
@@ -15,9 +17,13 @@
15
17
  # See the License for the specific language governing permissions and
16
18
  # limitations under the License.
17
19
 
20
+ require "fluent/test"
21
+ require "fluent/test/helpers"
22
+ require "fluent/test/log"
23
+ require "fluent/test/driver/output"
24
+ require "fluent/plugin/out_scalyr"
18
25
 
19
- require 'fluent/test'
20
- require 'fluent/plugin/out_scalyr'
26
+ include Fluent::Test::Helpers # rubocop:disable Style/MixinUsage
21
27
 
22
28
  module Scalyr
23
29
  class ScalyrOutTest < Test::Unit::TestCase
@@ -25,13 +31,13 @@ module Scalyr
25
31
  Fluent::Test.setup
26
32
  end
27
33
 
28
- CONFIG = %[
34
+ CONFIG = %(
29
35
  api_write_token test_token
30
36
  ssl_ca_bundle_path /etc/ssl/certs/ca-certificates.crt
31
- ]
37
+ )
32
38
 
33
- def create_driver( conf = CONFIG )
34
- Fluent::Test::BufferedOutputTestDriver.new( Scalyr::ScalyrOut ).configure( conf )
39
+ def create_driver(conf=CONFIG)
40
+ Fluent::Test::Driver::Output.new(Scalyr::ScalyrOut).configure(conf)
35
41
  end
36
42
  end
37
43
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Scalyr Output Plugin for Fluentd
3
5
  #
@@ -15,58 +17,45 @@
15
17
  # See the License for the specific language governing permissions and
16
18
  # limitations under the License.
17
19
 
20
+ require "helper"
21
+ require "socket"
18
22
 
19
- require 'helper'
20
- require 'socket'
21
-
22
- class ConfigTest < Scalyr::ScalyrOutTest
23
-
24
- def test_default_params
25
- d = create_driver
26
- hostname = Socket.gethostname
27
- assert_not_nil( d.instance.server_attributes, "Default server_attributes should not be nil" )
28
- assert_equal( hostname, d.instance.server_attributes['serverHost'], "Default serverHost is not hostname" )
29
- assert( d.instance.ssl_verify_peer, "Default ssl_verify_peer should be true" )
30
-
31
- #check default buffer limits because they are set outside of the config_set_default
32
- assert_equal( 100*1024, d.instance.buffer.buffer_chunk_limit, "Buffer chunk limit should be 100k" )
33
- assert_equal( 1024, d.instance.buffer.buffer_queue_limit, "Buffer queue limit should be 1024" )
34
- end
35
-
23
+ # rubocop:disable Layout/LineLength
24
+ class ConfigTest < Scalyr::ScalyrOutTest
36
25
  def test_custom_serverhost_not_overwritten
37
26
  hostname = "customHost"
38
27
  d = create_driver CONFIG + "server_attributes { \"serverHost\":\"#{hostname}\" }\nuse_hostname_for_serverhost true"
39
- assert_equal( hostname, d.instance.server_attributes['serverHost'], "Custom serverHost should not be overwritten" )
28
+ assert_equal(hostname, d.instance.server_attributes["serverHost"], "Custom serverHost should not be overwritten")
40
29
  end
41
30
 
42
31
  def test_configure_use_hostname_for_serverhost
43
- d = create_driver CONFIG + 'use_hostname_for_serverhost false'
44
- assert_nil( d.instance.server_attributes, "Default server_attributes should be nil" )
32
+ d = create_driver CONFIG + "use_hostname_for_serverhost false"
33
+ assert_nil(d.instance.server_attributes, "Default server_attributes should be nil")
45
34
  end
46
35
 
47
36
  def test_configure_ssl_verify_peer
48
- d = create_driver CONFIG + 'ssl_verify_peer false'
49
- assert( !d.instance.ssl_verify_peer, "Config failed to set ssl_verify_peer" )
37
+ d = create_driver CONFIG + "ssl_verify_peer false"
38
+ assert(!d.instance.ssl_verify_peer, "Config failed to set ssl_verify_peer")
50
39
  end
51
40
 
52
41
  def test_scalyr_server_adding_trailing_slash
53
- d = create_driver CONFIG + 'scalyr_server http://www.example.com'
54
- assert_equal( "http://www.example.com/", d.instance.scalyr_server, "Missing trailing slash for scalyr_server" )
42
+ d = create_driver CONFIG + "scalyr_server http://www.example.com"
43
+ assert_equal("http://www.example.com/", d.instance.scalyr_server, "Missing trailing slash for scalyr_server")
55
44
  end
56
45
 
57
46
  def test_configure_ssl_ca_bundle_path
58
- d = create_driver CONFIG + 'ssl_ca_bundle_path /test/ca-bundle.crt'
59
- assert_equal( "/test/ca-bundle.crt", d.instance.ssl_ca_bundle_path, "Config failed to set ssl_ca_bundle_path" )
47
+ d = create_driver CONFIG + "ssl_ca_bundle_path /test/ca-bundle.crt"
48
+ assert_equal("/test/ca-bundle.crt", d.instance.ssl_ca_bundle_path, "Config failed to set ssl_ca_bundle_path")
60
49
  end
61
50
 
62
51
  def test_configure_ssl_verify_depth
63
- d = create_driver CONFIG + 'ssl_verify_depth 10'
64
- assert_equal( 10, d.instance.ssl_verify_depth, "Config failed to set ssl_verify_depth" )
52
+ d = create_driver CONFIG + "ssl_verify_depth 10"
53
+ assert_equal(10, d.instance.ssl_verify_depth, "Config failed to set ssl_verify_depth")
65
54
  end
66
55
 
67
56
  def test_configure_server_attributes
68
57
  d = create_driver CONFIG + 'server_attributes { "test":"value" }'
69
- assert_equal( "value", d.instance.server_attributes["test"], "Config failed to set server_attributes" )
58
+ assert_equal("value", d.instance.server_attributes["test"], "Config failed to set server_attributes")
70
59
  end
71
60
  end
72
-
61
+ # rubocop:enable Layout/LineLength
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Scalyr Output Plugin for Fluentd
3
5
  #
@@ -15,234 +17,234 @@
15
17
  # See the License for the specific language governing permissions and
16
18
  # limitations under the License.
17
19
 
18
-
19
- require 'helper'
20
- require 'fluent/event'
20
+ require "helper"
21
+ require "flexmock/test_unit"
22
+ require "fluent/event"
21
23
 
22
24
  class EventsTest < Scalyr::ScalyrOutTest
23
-
24
25
  def test_format
25
26
  d = create_driver
26
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
27
- mock = flexmock( d.instance )
28
- mock.should_receive( :post_request ).with_any_args.and_return( response )
29
-
30
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
31
- d.emit( { "a" => 1 }, time )
32
- d.expect_format [ "test", time, { "a" => 1 } ].to_msgpack
33
- d.run
27
+
28
+ time = event_time("2015-04-01 10:00:00 UTC")
29
+
30
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
31
+ mock = flexmock(d.instance)
32
+ mock.should_receive(:post_request).with_any_args.and_return(response)
33
+
34
+ d.run(default_tag: "test") do
35
+ d.feed(time, {"a" => 1})
36
+ end
37
+
38
+ expected = [
39
+ ["test", time.sec, time.nsec, {"a" => 1}].to_msgpack
40
+ ]
41
+
42
+ assert_equal(expected, d.formatted)
34
43
  end
35
44
 
36
45
  def test_build_add_events_body_basic_values
37
46
  d = create_driver
38
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
39
- mock = flexmock( d.instance )
40
47
 
41
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
42
- attrs = { "a" => 1 }
43
- d.emit( attrs, time )
48
+ time = event_time("2015-04-01 10:00:00 UTC")
49
+ attrs = {"a" => 1}
50
+ attrs["logfile"] = "/fluentd/test"
44
51
 
45
- attrs["logfile"] = "/fluentd/test";
52
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
53
+ mock = flexmock(d.instance)
46
54
 
47
- mock.should_receive( :post_request ).with(
55
+ mock_called = false
56
+
57
+ mock.should_receive(:post_request).with(
48
58
  URI,
49
- on { |request_body|
50
- body = JSON.parse( request_body )
51
- assert( body.key?( "token" ), "Missing token field" )
52
- assert( body.key?( "client_timestamp" ), "Missing client_timestamp field" )
53
- assert( body.key?( "session" ), "Missing session field" )
54
- assert( !body.key?( "sessionInfo"), "sessionInfo field set, but no sessionInfo" )
55
- assert( body.key?( "events" ), "missing events field" )
56
- assert( body.key?( "threads" ), "missing threads field" )
57
- assert_equal( 1, body['events'].length, "Only expecting 1 event" )
58
- assert_equal( d.instance.to_nanos( time ), body['events'][0]['ts'].to_i, "Event timestamp differs" )
59
- assert_equal( attrs, body['events'][0]['attrs'], "Value of attrs differs from log" )
60
- true
59
+ on {|request_body|
60
+ body = JSON.parse(request_body)
61
+ assert(body.key?("token"), "Missing token field")
62
+ assert(body.key?("client_timestamp"), "Missing client_timestamp field")
63
+ assert(body.key?("sessionInfo"), "sessionInfo field set, but no sessionInfo")
64
+ assert(body.key?("events"), "missing events field")
65
+ assert(body.key?("threads"), "missing threads field")
66
+ assert_equal(1, body["events"].length, "Only expecting 1 event")
67
+ assert_equal(time.sec * 1_000_000_000, body["events"][0]["ts"].to_i,
68
+ "Event timestamp differs")
69
+ assert_equal(attrs, body["events"][0]["attrs"], "Value of attrs differs from log")
70
+ mock_called = true
61
71
  }
62
- ).and_return( response )
72
+ ).once.and_return(response)
63
73
 
64
- d.run
74
+ d.run(default_tag: "test") do
75
+ d.feed(time, attrs)
76
+ end
77
+
78
+ assert_equal(mock_called, true, "mock method was never called!")
65
79
  end
66
80
 
67
81
  def test_build_add_events_body_dont_override_logfile_field
68
82
  d = create_driver
69
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
70
- mock = flexmock( d.instance )
71
83
 
72
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
73
- attrs = { "a" => 1 }
74
- attrs["logfile"] = "/some/log/file";
75
- d.emit( attrs, time )
84
+ time = event_time("2015-04-01 10:00:00 UTC")
85
+ attrs = {"a" => 1}
86
+ attrs["logfile"] = "/some/log/file"
87
+
88
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
89
+ mock = flexmock(d.instance)
90
+
91
+ mock_called = false
76
92
 
77
- mock.should_receive( :post_request ).with(
93
+ mock.should_receive(:post_request).with(
78
94
  URI,
79
- on { |request_body|
80
- body = JSON.parse( request_body )
81
- assert_equal( attrs, body['events'][0]['attrs'], "Value of attrs differs from log" )
95
+ on {|request_body|
96
+ body = JSON.parse(request_body)
97
+ assert_equal(attrs, body["events"][0]["attrs"], "Value of attrs differs from log")
98
+ mock_called = true
82
99
  true
83
100
  }
84
- ).and_return( response )
101
+ ).once.and_return(response)
85
102
 
86
- d.run
103
+ d.run(default_tag: "test") do
104
+ d.feed(time, attrs)
105
+ end
106
+
107
+ assert_equal(mock_called, true, "mock method was never called!")
87
108
  end
88
109
 
89
110
  def test_build_add_events_body_with_server_attributes
90
111
  d = create_driver CONFIG + 'server_attributes { "test":"value" }'
91
112
 
92
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
93
- mock = flexmock( d.instance )
94
-
95
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
96
- attrs = { "a" => 1 }
97
- d.emit( attrs, time )
98
-
99
- mock.should_receive( :post_request ).with(
100
- URI,
101
- on { |request_body|
102
- body = JSON.parse( request_body )
103
- assert( body.key?( "sessionInfo"), "sessionInfo field set, but no sessionInfo" )
104
- assert_equal( "value", body["sessionInfo"]["test"] )
105
- true
106
- }
107
- ).and_return( response )
108
-
109
- d.run
110
- end
111
-
112
- def test_build_add_events_body_incrementing_timestamps
113
- d = create_driver
114
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
115
- mock = flexmock( d.instance )
113
+ time = event_time("2015-04-01 10:00:00 UTC")
114
+ attrs = {"a" => 1}
116
115
 
117
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
118
- d.emit( { "a" => 1 }, time )
119
- d.emit( { "a" => 2 }, time )
116
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
117
+ mock = flexmock(d.instance)
120
118
 
121
- time = Time.parse("2015-04-01 09:59:00 UTC").to_i
122
- d.emit( { "a" => 3 }, time )
119
+ mock_called = false
123
120
 
124
- mock.should_receive( :post_request ).with(
121
+ mock.should_receive(:post_request).with(
125
122
  URI,
126
- on { |request_body|
127
- body = JSON.parse( request_body )
128
- events = body['events']
129
- assert_equal( 3, events.length, "Expecting 3 events" )
130
- #test equal timestamps are increased
131
- assert events[1]['ts'].to_i > events[0]['ts'].to_i, "Event timestamps must increase"
132
-
133
- #test earlier timestamps are increased
134
- assert events[2]['ts'].to_i > events[1]['ts'].to_i, "Event timestamps must increase"
135
-
123
+ on {|request_body|
124
+ body = JSON.parse(request_body)
125
+ assert(body.key?("sessionInfo"), "sessionInfo field set, but no sessionInfo")
126
+ assert_equal("value", body["sessionInfo"]["test"])
127
+ mock_called = true
136
128
  true
137
129
  }
138
- ).and_return( response )
130
+ ).once.and_return(response)
131
+
132
+ d.run(default_tag: "test") do
133
+ d.feed(time, attrs)
134
+ end
139
135
 
140
- d.run
136
+ assert_equal(mock_called, true, "mock method was never called!")
141
137
  end
142
138
 
143
- def test_build_add_events_body_thread_ids
139
+ def test_build_add_events_body_incrementing_timestamps
144
140
  d = create_driver
145
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
146
- mock = flexmock( d.instance )
147
-
148
- entries = []
149
141
 
150
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
151
- entries << [time, { "a" => 1 }]
142
+ time1 = event_time("2015-04-01 10:00:00 UTC")
143
+ time2 = event_time("2015-04-01 09:59:00 UTC")
152
144
 
153
- es = Fluent::ArrayEventStream.new(entries)
154
- buffer = d.instance.format_stream("test1", es)
145
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
146
+ mock = flexmock(d.instance)
155
147
 
156
- chunk = d.instance.buffer.new_chunk('')
157
- chunk << buffer
148
+ mock_called = false
158
149
 
159
- buffer = d.instance.format_stream("test2", es)
160
- chunk << buffer
161
-
162
- mock.should_receive( :post_request ).with(
150
+ mock.should_receive(:post_request).with(
163
151
  URI,
164
- on { |request_body|
165
- body = JSON.parse( request_body )
166
- events = body['events']
167
- threads = body['threads']
168
-
169
- assert_equal( 2, threads.length, "Expecting 2 threads, #{threads.length} found" )
170
- assert_equal( 2, events.length, "Expecting 2 events, #{events.length} found" )
171
- assert_equal( events[0]['thread'], threads[0]['id'].to_s, "thread id should match event thread id" )
172
- assert_equal( events[1]['thread'], threads[1]['id'].to_s, "thread id should match event thread id" )
152
+ on {|request_body|
153
+ body = JSON.parse(request_body)
154
+ events = body["events"]
155
+ assert_equal(3, events.length, "Expecting 3 events")
156
+ # Since 0.8.10 timestamps dont need to increase anymore
157
+ assert events[1]["ts"].to_i == events[0]["ts"].to_i, "Event timestamps must be the same"
158
+ assert events[2]["ts"].to_i < events[0]["ts"].to_i, "Event timestamps must be less"
159
+ mock_called = true
173
160
  true
174
161
  }
175
- ).at_least.once.and_return( response )
162
+ ).once.and_return(response)
163
+
164
+ d.run(default_tag: "test") do
165
+ d.feed(time1, {"a" => 1})
166
+ d.feed(time1, {"a" => 2})
167
+ d.feed(time2, {"a" => 3})
168
+ end
176
169
 
177
- d.instance.start
178
- d.instance.write( chunk )
179
- d.instance.shutdown
170
+ assert_equal(mock_called, true, "mock method was never called!")
180
171
  end
181
172
 
182
173
  def test_default_message_field
183
174
  d = create_driver CONFIG
184
175
 
185
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
186
- mock = flexmock( d.instance )
176
+ time = event_time("2015-04-01 10:00:00 UTC")
177
+ attrs = {"log" => "this is a test", "logfile" => "/fluentd/test"}
178
+
179
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
180
+ mock = flexmock(d.instance)
187
181
 
188
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
189
- attrs = { "log" => "this is a test", "logfile" => "/fluentd/test" }
190
- d.emit( attrs, time )
182
+ mock_called = false
191
183
 
192
- mock.should_receive( :post_request ).with(
184
+ mock.should_receive(:post_request).with(
193
185
  URI,
194
- on { |request_body|
195
- body = JSON.parse( request_body )
196
- events = body['events']
197
- assert_equal( attrs, body['events'][0]['attrs'], "Value of attrs differs from log" )
186
+ on {|request_body|
187
+ body = JSON.parse(request_body)
188
+ assert_equal(attrs, body["events"][0]["attrs"], "Value of attrs differs from log")
189
+ mock_called = true
198
190
  true
199
191
  }
200
- ).and_return( response )
192
+ ).once.and_return(response)
201
193
 
202
- d.run
194
+ d.run(default_tag: "test") do
195
+ d.feed(time, attrs)
196
+ end
197
+
198
+ assert_equal(mock_called, true, "mock method was never called!")
203
199
  end
204
200
 
205
201
  def test_different_message_field
206
- d = create_driver CONFIG + 'message_field log'
202
+ d = create_driver CONFIG + "message_field log"
203
+
204
+ time = event_time("2015-04-01 10:00:00 UTC")
205
+ attrs = {"log" => "this is a test"}
207
206
 
208
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
209
- mock = flexmock( d.instance )
207
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
208
+ mock = flexmock(d.instance)
210
209
 
211
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
212
- attrs = { "log" => "this is a test" }
213
- d.emit( attrs, time )
210
+ mock_called = false
214
211
 
215
- mock.should_receive( :post_request ).with(
212
+ mock.should_receive(:post_request).with(
216
213
  URI,
217
- on { |request_body|
218
- body = JSON.parse( request_body )
219
- events = body['events']
220
- assert( events[0]['attrs'].key?( 'message'), "'message' field not found in event" )
221
- assert_equal( "this is a test", events[0]['attrs']['message'], "'message' field incorrect" )
222
- assert( !events[0]['attrs'].key?( 'log' ), "'log' field should no longer exist in event" )
214
+ on {|request_body|
215
+ body = JSON.parse(request_body)
216
+ events = body["events"]
217
+ assert(events[0]["attrs"].key?("message"), "'message' field not found in event")
218
+ assert_equal("this is a test", events[0]["attrs"]["message"], "'message' field incorrect")
219
+ assert(!events[0]["attrs"].key?("log"), "'log' field should no longer exist in event")
220
+ mock_called = true
223
221
  true
224
222
  }
225
- ).and_return( response )
223
+ ).once.and_return(response)
226
224
 
227
- d.run
225
+ d.run(default_tag: "test") do
226
+ d.feed(time, attrs)
227
+ end
228
+
229
+ assert_equal(mock_called, true, "mock method was never called!")
228
230
  end
229
231
 
230
232
  def test_different_message_field_message_already_exists
231
- d = create_driver CONFIG + 'message_field log'
233
+ d = create_driver CONFIG + "message_field log"
232
234
 
233
- response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
234
- mock = flexmock( d.instance )
235
- mock.should_receive( :post_request ).and_return( response )
235
+ time = event_time("2015-04-01 10:00:00 UTC")
236
+ attrs = {"log" => "this is a test", "message" => "uh oh"}
236
237
 
237
- time = Time.parse("2015-04-01 10:00:00 UTC").to_i
238
- attrs = { "log" => "this is a test", "message" => "uh oh" }
239
- d.emit( attrs, time )
238
+ response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
239
+ mock = flexmock(d.instance)
240
240
 
241
- logger = flexmock( $log )
242
- logger.should_receive( :warn ).with( /overwriting log record field 'message'/i ).at_least().once()
241
+ mock.should_receive(:post_request).once.and_return(response)
243
242
 
244
- d.run
245
- end
243
+ logger = flexmock($log)
244
+ logger.should_receive(:warn).with(/overwriting log record field 'message'/i).at_least.once
246
245
 
246
+ d.run(default_tag: "test") do
247
+ d.feed(time, attrs)
248
+ end
249
+ end
247
250
  end
248
-