fluent-plugin-aliyun-oss 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,166 @@
1
+ require 'fluent/test'
2
+ require 'fluent/test/helpers'
3
+ require 'fluent/test/log'
4
+ require 'fluent/test/driver/input'
5
+ require 'fluent/plugin/in_oss'
6
+ require 'test/unit/rr'
7
+ require 'uuidtools'
8
+
9
+ class OSSInputTest < Test::Unit::TestCase
10
+
11
+ include Fluent::Test::Helpers
12
+ include REXML
13
+
14
+ def setup
15
+ Fluent::Test.setup
16
+ @time = Time.now
17
+ stub(Fluent::EventTime).now { @time } if Fluent.const_defined?(:EventTime)
18
+ end
19
+
20
+ CONFIG = %(
21
+ endpoint #{ENV['OSS_ENDPOINT']}
22
+ bucket #{ENV['OSS_BUCKET']}
23
+ access_key_id #{ENV['ACCESS_KEY_ID']}
24
+ access_key_secret #{ENV['ACCESS_KEY_SECRET']}
25
+ oss_sdk_log_dir .
26
+ store_as #{ENV['STORE_AS']}
27
+ store_local #{ENV['STORE_LOCAL']}
28
+ <mns>
29
+ endpoint #{ENV['MNS_ENDPOINT']}
30
+ queue #{ENV['MNS_QUEUE']}
31
+ wait_seconds #{ENV['WAIT_SECONDS']}
32
+ poll_interval_seconds #{ENV['POLL_INTERVAL_SECONDS']}
33
+ </mns>
34
+ <parse>
35
+ @type json
36
+ </parse>
37
+ ).freeze
38
+
39
+ def create_driver
40
+ Fluent::Test::Driver::Input.new(Fluent::Plugin::OSSInput)
41
+ end
42
+
43
+ def test_configuration
44
+ driver = create_driver
45
+ driver.configure(CONFIG)
46
+
47
+ assert_equal(driver.instance.endpoint, ENV['OSS_ENDPOINT'])
48
+ assert_equal(driver.instance.bucket, ENV['OSS_BUCKET'])
49
+ assert_equal(driver.instance.access_key_id, ENV['ACCESS_KEY_ID'])
50
+ assert_equal(driver.instance.access_key_secret, ENV['ACCESS_KEY_SECRET'])
51
+ assert_equal(driver.instance.oss_sdk_log_dir, '.')
52
+ assert_equal(driver.instance.store_as, ENV['STORE_AS'])
53
+ assert_not_equal(driver.instance.store_as, ENV['STORE_AS'] + '-')
54
+
55
+ assert_equal(driver.instance.store_local, ENV['STORE_LOCAL'].to_bool) unless ENV['STORE_LOCAL'].nil?
56
+
57
+ assert_not_equal(driver.instance.store_local, !ENV['STORE_LOCAL'].to_bool) unless ENV['STORE_LOCAL'].nil?
58
+
59
+ assert_equal(driver.instance.mns.endpoint, ENV['MNS_ENDPOINT'])
60
+ assert_equal(driver.instance.mns.queue, ENV['MNS_QUEUE'])
61
+ assert_equal(driver.instance.mns.wait_seconds, ENV['WAIT_SECONDS'].to_i) unless ENV['WAIT_SECONDS'].nil?
62
+ assert_equal(driver.instance.mns.poll_interval_seconds, ENV['POLL_INTERVAL_SECONDS'].to_i) unless ENV['POLL_INTERVAL_SECONDS'].nil?
63
+
64
+ assert_equal(driver.instance.parser_configs[0]['@type'], 'json')
65
+
66
+ driver.instance.shutdown
67
+ puts driver.logs
68
+ end
69
+
70
+ def test_emit_events
71
+ driver = create_driver
72
+ driver.configure(CONFIG)
73
+
74
+ expect_records = 12_345
75
+ oss = create_oss(driver)
76
+
77
+ bucket = oss.get_bucket(driver.instance.bucket)
78
+
79
+ # create test object to bucket
80
+ content = get_random_content(expect_records)
81
+ object = get_test_object(content, bucket, driver.instance.store_as)
82
+
83
+ driver.run(expect_records: expect_records)
84
+
85
+ assert_equal(driver.events.size, expect_records)
86
+
87
+ result = ''
88
+ driver.events.each do |event|
89
+ result << event[2].to_s.gsub(/=>/, ':') << "\n"
90
+ end
91
+
92
+ assert_equal(result, content)
93
+
94
+ assert_false(driver.instance.instance_variable_get(:@running))
95
+ puts driver.logs
96
+
97
+ # delete test object
98
+ bucket.delete_object(object)
99
+ end
100
+
101
+ def test_no_events
102
+ driver = create_driver
103
+ driver.configure(CONFIG)
104
+
105
+ expect_records = 0
106
+ driver.run(expect_records: expect_records)
107
+
108
+ assert_equal(driver.events.size, expect_records)
109
+
110
+ assert_false(driver.instance.instance_variable_get(:@running))
111
+ puts driver.logs
112
+ end
113
+
114
+ def get_test_object(content, bucket, store_as)
115
+ object = 'fluentd-oss-test-' + ::UUIDTools::UUID.random_create.to_s
116
+ io = Tempfile.new(object)
117
+ io.binmode
118
+ io.write(content)
119
+ io.rewind
120
+
121
+ out = Tempfile.new('chunk-test-' + store_as + '-out-')
122
+ out.binmode
123
+
124
+ case store_as
125
+ when 'text'
126
+ object += '.txt'
127
+ bucket.put_object(object, file: io.path)
128
+ when 'json'
129
+ object += '.' + store_as
130
+ bucket.put_object(object, file: io.path)
131
+ when 'gzip_command', 'gzip'
132
+ object += '.gz'
133
+ system "gzip -c #{io.path} > #{out.path}"
134
+ bucket.put_object(object, file: out.path)
135
+ when 'lzo'
136
+ object += '.' + store_as
137
+ system "lzop -qf1 -c #{io.path} > #{out.path}"
138
+ bucket.put_object(object, file: out.path)
139
+ when 'lzma2'
140
+ object += '.xz'
141
+ system "xz -qf0 -c #{io.path} > #{out.path}"
142
+ bucket.put_object(object, file: out.path)
143
+ end
144
+
145
+ io.close(true) rescue nil
146
+ out.close(true) rescue nil
147
+ object
148
+ end
149
+
150
+ def get_random_content(lines)
151
+ content = []
152
+ lines.times do |i|
153
+ value = ::UUIDTools::UUID.random_create.to_s + '-' + i.to_s
154
+ content.append('{"message":"' + value + '"}' + "\n")
155
+ end
156
+ content.join
157
+ end
158
+
159
+ def create_oss(driver)
160
+ Aliyun::OSS::Client.new(
161
+ endpoint: driver.instance.endpoint,
162
+ access_key_id: driver.instance.access_key_id,
163
+ access_key_secret: driver.instance.access_key_secret
164
+ )
165
+ end
166
+ end
@@ -0,0 +1,175 @@
1
+ require 'fluent/test'
2
+ require 'fluent/test/helpers'
3
+ require 'fluent/test/log'
4
+ require 'fluent/test/driver/output'
5
+ require 'fluent/plugin/out_oss'
6
+ require 'test/unit/rr'
7
+ require 'uuidtools'
8
+
9
+ class OSSOutputTest < Test::Unit::TestCase
10
+
11
+ include Fluent::Test::Helpers
12
+
13
+ def setup
14
+ Fluent::Test.setup
15
+ @time = Time.now
16
+ stub(Fluent::EventTime).now { @time } if Fluent.const_defined?(:EventTime)
17
+ end
18
+
19
+ CONFIG = %(
20
+ endpoint #{ENV['OSS_ENDPOINT']}
21
+ bucket #{ENV['OSS_BUCKET']}
22
+ access_key_id #{ENV['ACCESS_KEY_ID']}
23
+ access_key_secret #{ENV['ACCESS_KEY_SECRET']}
24
+ oss_sdk_log_dir .
25
+ path #{ENV['OSS_PATH']}
26
+ store_as #{ENV['STORE_AS']}
27
+ <buffer tag,time>
28
+ @type memory
29
+ timekey 30
30
+ timekey_wait 1s
31
+ </buffer>
32
+ <format>
33
+ @type json
34
+ </format>
35
+ ).freeze
36
+
37
+ def create_driver
38
+ Fluent::Test::Driver::Output.new(Fluent::Plugin::OSSOutput)
39
+ end
40
+
41
+ def test_configuration
42
+ driver = create_driver
43
+ driver.configure(CONFIG)
44
+
45
+ assert_equal(driver.instance.endpoint, ENV['OSS_ENDPOINT'])
46
+ assert_equal(driver.instance.bucket, ENV['OSS_BUCKET'])
47
+ assert_equal(driver.instance.access_key_id, ENV['ACCESS_KEY_ID'])
48
+ assert_equal(driver.instance.access_key_secret, ENV['ACCESS_KEY_SECRET'])
49
+ assert_equal(driver.instance.oss_sdk_log_dir, '.')
50
+ assert_equal(driver.instance.path, ENV['OSS_PATH'])
51
+
52
+ assert_equal(driver.instance.store_as, ENV['STORE_AS'])
53
+ assert_not_equal(driver.instance.store_as, ENV['STORE_AS'] + '-')
54
+
55
+ assert_equal(driver.instance.buffer_config['@type'], 'memory')
56
+ assert_equal(driver.instance.buffer_config['timekey'], 30)
57
+ assert_equal(driver.instance.buffer_config['timekey_wait'], 1)
58
+ assert_equal(driver.instance.formatter_configs[0]['@type'], 'json')
59
+
60
+ driver.instance.shutdown
61
+ puts driver.logs
62
+ end
63
+
64
+ def test_write_lines
65
+ driver = create_driver
66
+ driver.configure(CONFIG)
67
+
68
+ time = event_time('2019-04-16 14:26:22 UTC')
69
+
70
+ expect_records = 12_345
71
+ content = get_random_content(expect_records)
72
+
73
+ expected_content = []
74
+ driver.run(default_tag: 'oss.output') do
75
+ content.each do |line|
76
+ driver.feed(time, 'message' => line)
77
+ expected_content.append("{\"message\":\"#{line}\"}")
78
+ end
79
+ end
80
+
81
+ oss = create_oss(driver)
82
+
83
+ bucket = oss.get_bucket(driver.instance.bucket)
84
+
85
+ verify_object_content(bucket, driver.instance.path,
86
+ driver.instance.store_as,
87
+ expected_content.join("\n") + "\n")
88
+ puts driver.logs
89
+ end
90
+
91
+ def test_no_writes
92
+ driver = create_driver
93
+ driver.configure(CONFIG)
94
+
95
+ driver.run(default_tag: 'oss.output') do
96
+ end
97
+
98
+ oss = create_oss(driver)
99
+
100
+ bucket = oss.get_bucket(driver.instance.bucket)
101
+
102
+ verify_object_content(bucket, driver.instance.path, driver.instance.store_as, nil)
103
+ puts driver.logs
104
+ end
105
+
106
+ def verify_object_content(bucket, prefix, store_as, expected_content)
107
+ objects = []
108
+ bucket.list_objects(prefix: prefix).each do |object|
109
+ objects.append(object.key)
110
+ end
111
+ assert_true(objects.size >= 1) unless expected_content.nil?
112
+ assert_equal(objects.size, 0) if expected_content.nil?
113
+
114
+ contents = []
115
+ unless expected_content.nil?
116
+ objects.each do |object|
117
+ contents.append(decompress_object(bucket, object, store_as))
118
+ bucket.delete_object(object)
119
+ end
120
+ assert_equal(contents.join, expected_content)
121
+ end
122
+ end
123
+
124
+ def create_oss(driver)
125
+ Aliyun::OSS::Client.new(
126
+ endpoint: driver.instance.endpoint,
127
+ access_key_id: driver.instance.access_key_id,
128
+ access_key_secret: driver.instance.access_key_secret
129
+ )
130
+ end
131
+
132
+ def get_random_content(lines)
133
+ content = []
134
+ lines.times do |i|
135
+ value = ::UUIDTools::UUID.random_create.to_s + '-' + i.to_s
136
+ content.append(value)
137
+ end
138
+ content
139
+ end
140
+
141
+ def decompress_object(bucket, key, store_as)
142
+ decompressed_content = ''
143
+
144
+ contents = []
145
+ bucket.get_object(key) do |content|
146
+ contents.append(content)
147
+ end
148
+
149
+ object = 'fluentd-oss-test-' + ::UUIDTools::UUID.random_create.to_s
150
+ io = Tempfile.new(object)
151
+ io.binmode
152
+ io.write(contents.join)
153
+ io.rewind
154
+
155
+ case store_as
156
+ when 'text', 'json'
157
+ decompressed_content = contents
158
+ when 'gzip_command', 'gzip'
159
+ stdout, succeeded = Open3.capture2("gzip -dc #{io.path}")
160
+ when 'lzo'
161
+ stdout, succeeded = Open3.capture2("lzop -qdc #{io.path}")
162
+ when 'lzma2'
163
+ stdout, succeeded = Open3.capture2("xz -qdc #{io.path}")
164
+ end
165
+
166
+ io.close(true) rescue nil
167
+
168
+ if succeeded
169
+ stdout.each_line do |line|
170
+ decompressed_content << line
171
+ end
172
+ end
173
+ decompressed_content
174
+ end
175
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-aliyun-oss
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jinhu Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aliyun-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: fluentd
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.22
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '2'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.14.22
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.9.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.9'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.9.2
67
+ - !ruby/object:Gem::Dependency
68
+ name: test-unit
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '3.0'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 3.0.8
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.0.8
87
+ - !ruby/object:Gem::Dependency
88
+ name: test-unit-rr
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.0'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.0.3
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 1.0.3
107
+ description: Aliyun OSS plugin for Fluentd event collector
108
+ email: jinhu.wu.nju@gmail.com
109
+ executables: []
110
+ extensions: []
111
+ extra_rdoc_files: []
112
+ files:
113
+ - ".gitignore"
114
+ - AUTHORS
115
+ - ChangeLog
116
+ - Gemfile
117
+ - README.md
118
+ - Rakefile
119
+ - VERSION
120
+ - fluent-plugin-oss.gemspec
121
+ - lib/fluent/plugin/in_oss.rb
122
+ - lib/fluent/plugin/mns/message.rb
123
+ - lib/fluent/plugin/mns/request.rb
124
+ - lib/fluent/plugin/oss_compressor_gzip_command.rb
125
+ - lib/fluent/plugin/oss_compressor_lzma2.rb
126
+ - lib/fluent/plugin/oss_compressor_lzo.rb
127
+ - lib/fluent/plugin/oss_decompressor_gzip_command.rb
128
+ - lib/fluent/plugin/oss_decompressor_lzma2.rb
129
+ - lib/fluent/plugin/oss_decompressor_lzo.rb
130
+ - lib/fluent/plugin/out_oss.rb
131
+ - test/plugin/test_in_oss.rb
132
+ - test/plugin/test_out_oss.rb
133
+ homepage: https://github.com/aliyun/fluent-plugin-oss
134
+ licenses:
135
+ - Apache-2.0
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.7.9
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Aliyun OSS plugin for Fluentd event collector
157
+ test_files:
158
+ - test/plugin/test_in_oss.rb
159
+ - test/plugin/test_out_oss.rb