fluent-plugin-mysql-2 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,360 @@
1
+ # coding: utf-8
2
+ require 'helper'
3
+ require 'mysql2-cs-bind'
4
+ require 'fluent/test/driver/output'
5
+ require 'fluent/plugin/buffer'
6
+ require 'fluent/config'
7
+ require 'time'
8
+ require 'timecop'
9
+
10
+ class MysqlBulkOutputTest < Test::Unit::TestCase
11
+ def setup
12
+ Fluent::Test.setup
13
+ end
14
+
15
+ def config_element(name = 'test', argument = '', params = {}, elements = [])
16
+ Fluent::Config::Element.new(name, argument, params, elements)
17
+ end
18
+
19
+ CONFIG = %[
20
+ database test_app_development
21
+ username root
22
+ password hogehoge
23
+ column_names id,user_name,created_at
24
+ key_names id,users,created_at
25
+ table users
26
+ ]
27
+
28
+ def create_driver(conf = CONFIG)
29
+ d = Fluent::Test::Driver::Output.new(Fluent::Plugin::MysqlBulkOutput).configure(conf)
30
+ d.instance.instance_eval {
31
+ def client
32
+ obj = Object.new
33
+ obj.instance_eval {
34
+ def xquery(*args); [1]; end
35
+ def close; true; end
36
+ }
37
+ obj
38
+ end
39
+ }
40
+ d
41
+ end
42
+
43
+ def create_metadata(timekey: nil, tag: nil, variables: nil)
44
+ Fluent::Plugin::Buffer::Metadata.new(timekey, tag, variables)
45
+ end
46
+
47
+ class TestExpandPlaceholders < self
48
+ data("table" => {"database" => "test_app_development",
49
+ "table" => "users_${tag}",
50
+ "extracted_database" => "test_app_development",
51
+ "extracted_table" => "users_input_mysql"
52
+ },
53
+ "database" => {"database" => "test_app_development_${tag}",
54
+ "table" => "users",
55
+ "extracted_database" => "test_app_development_input_mysql",
56
+ "extracted_table" => "users"
57
+ },
58
+ )
59
+ def test_expand_tag_placeholder(data)
60
+ config = config_element('ROOT', '', {
61
+ "@type" => "mysql_bulk",
62
+ "host" => "localhost",
63
+ "database" => data["database"],
64
+ "username" => "root",
65
+ "password" => "hogehoge",
66
+ "column_names" => "id,user_name,created_at",
67
+ "table" => data["table"],
68
+ }, [config_element('buffer', 'tag', {
69
+ "@type" => "memory",
70
+ "flush_interval" => "60s",
71
+ }, [])])
72
+ d = create_driver(config)
73
+ time = Time.now
74
+ metadata = create_metadata(timekey: time.to_i, tag: 'input.mysql')
75
+ database, table = d.instance.expand_placeholders(metadata)
76
+ assert_equal(data["extracted_database"], database)
77
+ assert_equal(data["extracted_table"], table)
78
+ end
79
+
80
+ def setup
81
+ Timecop.freeze(Time.parse("2016-09-26"))
82
+ end
83
+
84
+ data("table" => {"database" => "test_app_development",
85
+ "table" => "users_%Y%m%d",
86
+ "extracted_database" => "test_app_development",
87
+ "extracted_table" => "users_20160926"
88
+ },
89
+ "database" => {"database" => "test_app_development_%Y%m%d",
90
+ "table" => "users",
91
+ "extracted_database" => "test_app_development_20160926",
92
+ "extracted_table" => "users"
93
+ },
94
+ )
95
+ def test_expand_time_placeholder(data)
96
+ config = config_element('ROOT', '', {
97
+ "@type" => "mysql_bulk",
98
+ "host" => "localhost",
99
+ "database" => data["database"],
100
+ "username" => "root",
101
+ "password" => "hogehoge",
102
+ "column_names" => "id,user_name,created_at",
103
+ "table" => data["table"],
104
+ }, [config_element('buffer', 'time', {
105
+ "@type" => "memory",
106
+ "timekey" => "60s",
107
+ "timekey_wait" => "60s"
108
+ }, [])])
109
+ d = create_driver(config)
110
+ time = Time.now
111
+ metadata = create_metadata(timekey: time.to_i, tag: 'input.mysql')
112
+ database, table = d.instance.expand_placeholders(metadata)
113
+ assert_equal(data["extracted_database"], database)
114
+ assert_equal(data["extracted_table"], table)
115
+ end
116
+
117
+ def teardown
118
+ Timecop.return
119
+ end
120
+ end
121
+
122
+ def test_configure_error
123
+ assert_raise(Fluent::ConfigError) do
124
+ create_driver %[
125
+ host localhost
126
+ database test_app_development
127
+ username root
128
+ password hogehoge
129
+ table users
130
+ on_duplicate_key_update true
131
+ on_duplicate_update_keys user_name,updated_at
132
+ flush_interval 10s
133
+ ]
134
+ end
135
+
136
+ assert_raise(Fluent::ConfigError) do
137
+ create_driver %[
138
+ host localhost
139
+ database test_app_development
140
+ username root
141
+ password hogehoge
142
+ column_names id,user_name,created_at,updated_at
143
+ table users
144
+ on_duplicate_key_update true
145
+ flush_interval 10s
146
+ ]
147
+ end
148
+
149
+ assert_raise(Fluent::ConfigError) do
150
+ create_driver %[
151
+ host localhost
152
+ username root
153
+ password hogehoge
154
+ column_names id,user_name,created_at,updated_at
155
+ table users
156
+ on_duplicate_key_update true
157
+ on_duplicate_update_keys user_name,updated_at
158
+ flush_interval 10s
159
+ ]
160
+ end
161
+
162
+ assert_raise(Fluent::ConfigError) do
163
+ create_driver %[
164
+ host localhost
165
+ username root
166
+ password hogehoge
167
+ column_names id,user_name,login_count,created_at,updated_at
168
+ table users
169
+ on_duplicate_key_update true
170
+ on_duplicate_update_keys login_count,updated_at
171
+ on_duplicate_update_custom_values login_count
172
+ flush_interval 10s
173
+ ]
174
+ end
175
+ end
176
+
177
+ def test_configure
178
+ # not define format(default csv)
179
+ assert_nothing_raised(Fluent::ConfigError) do
180
+ create_driver %[
181
+ host localhost
182
+ database test_app_development
183
+ username root
184
+ password hogehoge
185
+ column_names id,user_name,created_at,updated_at
186
+ table users
187
+ on_duplicate_key_update true
188
+ on_duplicate_update_keys user_name,updated_at
189
+ flush_interval 10s
190
+ ]
191
+ end
192
+
193
+ assert_nothing_raised(Fluent::ConfigError) do
194
+ create_driver %[
195
+ database test_app_development
196
+ username root
197
+ password hogehoge
198
+ column_names id,user_name,created_at,updated_at
199
+ table users
200
+ ]
201
+ end
202
+
203
+ assert_nothing_raised(Fluent::ConfigError) do
204
+ create_driver %[
205
+ database test_app_development
206
+ username root
207
+ password hogehoge
208
+ column_names id,user_name,created_at,updated_at
209
+ table users
210
+ on_duplicate_key_update true
211
+ on_duplicate_update_keys user_name,updated_at
212
+ ]
213
+ end
214
+
215
+ assert_nothing_raised(Fluent::ConfigError) do
216
+ create_driver %[
217
+ database test_app_development
218
+ username root
219
+ password hogehoge
220
+ column_names id,user_name,created_at,updated_at
221
+ key_names id,user,created_date,updated_date
222
+ table users
223
+ on_duplicate_key_update true
224
+ on_duplicate_update_keys user_name,updated_at
225
+ ]
226
+ end
227
+
228
+ assert_nothing_raised(Fluent::ConfigError) do
229
+ create_driver %[
230
+ database test_app_development
231
+ username root
232
+ password hogehoge
233
+ key_names id,url,request_headers,params,created_at,updated_at
234
+ column_names id,url,request_headers_json,params_json,created_date,updated_date
235
+ json_key_names request_headers,params
236
+ table access
237
+ ]
238
+ end
239
+
240
+ assert_nothing_raised(Fluent::ConfigError) do
241
+ create_driver %[
242
+ database test_app_development
243
+ username root
244
+ password hogehoge
245
+ column_names id,user_name,login_count,created_at,updated_at
246
+ key_names id,user_name,login_count,created_date,updated_date
247
+ table users
248
+ on_duplicate_key_update true
249
+ on_duplicate_update_keys login_count,updated_at
250
+ on_duplicate_update_custom_values ${`login_count` + 1},updated_at
251
+ ]
252
+ end
253
+ end
254
+
255
+ def test_variables
256
+ d = create_driver %[
257
+ database test_app_development
258
+ username root
259
+ password hogehoge
260
+ column_names id,user_name,created_at,updated_at
261
+ table users
262
+ on_duplicate_key_update true
263
+ on_duplicate_update_keys user_name,updated_at
264
+ ]
265
+
266
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.key_names
267
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.column_names
268
+ assert_equal nil, d.instance.json_key_names
269
+ assert_equal nil, d.instance.unixtimestamp_key_names
270
+ assert_equal " ON DUPLICATE KEY UPDATE user_name = VALUES(user_name),updated_at = VALUES(updated_at)", d.instance.instance_variable_get(:@on_duplicate_key_update_sql)
271
+
272
+ d = create_driver %[
273
+ database test_app_development
274
+ username root
275
+ password hogehoge
276
+ column_names id,user_name,created_at,updated_at
277
+ table users
278
+ ]
279
+
280
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.key_names
281
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.column_names
282
+ assert_equal nil, d.instance.json_key_names
283
+ assert_equal nil, d.instance.unixtimestamp_key_names
284
+ assert_nil d.instance.instance_variable_get(:@on_duplicate_key_update_sql)
285
+
286
+ d = create_driver %[
287
+ database test_app_development
288
+ username root
289
+ password hogehoge
290
+ key_names id,user_name,created_at,updated_at
291
+ column_names id,user,created_date,updated_date
292
+ table users
293
+ ]
294
+
295
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.key_names
296
+ assert_equal ['id','user','created_date','updated_date'], d.instance.column_names
297
+ assert_equal nil, d.instance.json_key_names
298
+ assert_equal nil, d.instance.unixtimestamp_key_names
299
+ assert_nil d.instance.instance_variable_get(:@on_duplicate_key_update_sql)
300
+
301
+ d = create_driver %[
302
+ database test_app_development
303
+ username root
304
+ password hogehoge
305
+ key_names id,url,request_headers,params,created_at,updated_at
306
+ column_names id,url,request_headers_json,params_json,created_date,updated_date
307
+ unixtimestamp_key_names created_at,updated_at
308
+ json_key_names request_headers,params
309
+ table access
310
+ ]
311
+
312
+ assert_equal ['id','url','request_headers','params','created_at','updated_at'], d.instance.key_names
313
+ assert_equal ['id','url','request_headers_json','params_json','created_date','updated_date'], d.instance.column_names
314
+ assert_equal ['request_headers','params'], d.instance.json_key_names
315
+ assert_equal ['created_at', 'updated_at'], d.instance.unixtimestamp_key_names
316
+ assert_nil d.instance.instance_variable_get(:@on_duplicate_key_update_sql)
317
+
318
+ d = create_driver %[
319
+ database test_app_development
320
+ username root
321
+ password hogehoge
322
+ column_names id,user_name,login_count,created_at,updated_at
323
+ table users
324
+ on_duplicate_key_update true
325
+ on_duplicate_update_keys login_count,updated_at
326
+ on_duplicate_update_custom_values ${`login_count` + 1},updated_at
327
+ ]
328
+
329
+ assert_equal ['id','user_name','login_count','created_at','updated_at'], d.instance.key_names
330
+ assert_equal ['id','user_name','login_count','created_at','updated_at'], d.instance.column_names
331
+ assert_equal nil, d.instance.json_key_names
332
+ assert_equal nil, d.instance.unixtimestamp_key_names
333
+ assert_equal " ON DUPLICATE KEY UPDATE login_count = `login_count` + 1,updated_at = VALUES(updated_at)", d.instance.instance_variable_get(:@on_duplicate_key_update_sql)
334
+ end
335
+
336
+ def test_spaces_in_columns
337
+ d = create_driver %[
338
+ database test_app_development
339
+ username root
340
+ password hogehoge
341
+ column_names id, user_name, created_at, updated_at
342
+ table users
343
+ ]
344
+
345
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.key_names
346
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.column_names
347
+
348
+ d = create_driver %[
349
+ database test_app_development
350
+ username root
351
+ password hogehoge
352
+ key_names id, user_name, created_at, updated_at
353
+ column_names id, user_name, created_at, updated_at
354
+ table users
355
+ ]
356
+
357
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.key_names
358
+ assert_equal ['id','user_name','created_at','updated_at'], d.instance.column_names
359
+ end
360
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mysql-2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.7
5
+ platform: ruby
6
+ authors:
7
+ - TAGOMORI Satoshi
8
+ - Toyama Hiroshi
9
+ - Alex Scarborough
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2022-07-01 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fluentd
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 0.14.8
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '2'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.14.8
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '2'
35
+ - !ruby/object:Gem::Dependency
36
+ name: mysql2-cs-bind
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ - !ruby/object:Gem::Dependency
50
+ name: jsonpath
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: oj
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ - !ruby/object:Gem::Dependency
78
+ name: rake
79
+ requirement: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ - !ruby/object:Gem::Dependency
92
+ name: test-unit
93
+ requirement: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ type: :development
99
+ prerelease: false
100
+ version_requirements: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ - !ruby/object:Gem::Dependency
106
+ name: timecop
107
+ requirement: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 0.8.0
112
+ type: :development
113
+ prerelease: false
114
+ version_requirements: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: 0.8.0
119
+ description: fluent plugin to insert mysql as json(single column) or insert statement
120
+ email:
121
+ - tagomoris@gmail.com
122
+ - toyama0919@gmail.com
123
+ - alex@teak.io
124
+ executables: []
125
+ extensions: []
126
+ extra_rdoc_files: []
127
+ files:
128
+ - ".gitignore"
129
+ - ".travis.yml"
130
+ - Gemfile
131
+ - LICENSE.txt
132
+ - README.md
133
+ - README_mysql.md
134
+ - Rakefile
135
+ - fluent-plugin-mysql.gemspec
136
+ - lib/fluent/plugin/out_mysql.rb
137
+ - lib/fluent/plugin/out_mysql_bulk.rb
138
+ - test/helper.rb
139
+ - test/plugin/test_out_mysql.rb
140
+ - test/plugin/test_out_mysql_bulk.rb
141
+ homepage: https://github.com/GoCarrot/fluent-plugin-mysql
142
+ licenses:
143
+ - Apache-2.0
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubygems_version: 3.2.3
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: fluent plugin to insert mysql
164
+ test_files:
165
+ - test/helper.rb
166
+ - test/plugin/test_out_mysql.rb
167
+ - test/plugin/test_out_mysql_bulk.rb