fluent-plugin-scalyr 0.8.9 → 0.8.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Scalyr Output Plugin for Fluentd
5
+ #
6
+ # Copyright (C) 2020 Scalyr, Inc.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ module Scalyr
21
+ def sanitize_and_reencode_value(value)
22
+ # Method which recursively sanitizes the provided value and tries to re-encode all the strings as
23
+ # UTF-8 ignoring any bad unicode sequences
24
+ case value
25
+ when Hash
26
+ return sanitize_and_reencode_hash(value)
27
+ when Array
28
+ return sanitize_and_reencode_array(value)
29
+ when String
30
+ value = sanitize_and_reencode_string(value)
31
+ return value
32
+ end
33
+
34
+ # We only need to re-encode strings, for other value types (ints, nils,
35
+ # etc. no reencoding is needed)
36
+ value
37
+ end
38
+
39
+ def sanitize_and_reencode_array(array)
40
+ array.each_with_index do |value, index|
41
+ value = sanitize_and_reencode_value(value)
42
+ array[index] = value
43
+ end
44
+
45
+ array
46
+ end
47
+
48
+ def sanitize_and_reencode_hash(hash)
49
+ hash.each do |key, value|
50
+ hash[key] = sanitize_and_reencode_value(value)
51
+ end
52
+
53
+ hash
54
+ end
55
+
56
+ def sanitize_and_reencode_string(value)
57
+ # Function which sanitized the provided string value and tries to re-encode it as UTF-8
58
+ # ignoring any encoding error which could arise due to bad or partial unicode sequence
59
+ begin # rubocop:disable Style/RedundantBegin
60
+ value.encode("UTF-8", invalid: :replace, undef: :replace, replace: "<?>").force_encoding("UTF-8") # rubocop:disable Layout/LineLength, Lint/RedundantCopDisableDirective
61
+ rescue # rubocop:disable Style/RescueStandardError
62
+ "failed-to-reencode-as-utf8"
63
+ end
64
+ end
65
+ end
data/test/helper.rb CHANGED
@@ -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
data/test/test_config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Scalyr Output Plugin for Fluentd
3
5
  #
@@ -15,58 +17,50 @@
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
- end
72
60
 
61
+ def test_configure_parser
62
+ d = create_driver CONFIG + "parser access_log"
63
+ assert_equal("access_log", d.instance.parser, "Config failed to set parser")
64
+ end
65
+ end
66
+ # rubocop:enable Layout/LineLength