Skip to content

Commit 50596b0

Browse files
committed
Add ci setting & spec
1 parent 3f257c1 commit 50596b0

File tree

7 files changed

+311
-104
lines changed

7 files changed

+311
-104
lines changed

.circleci/config.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: 2.1
2+
3+
orbs:
4+
ruby-orbs: sue445/ruby-orbs@volatile
5+
6+
jobs:
7+
test:
8+
docker:
9+
- image: cimg/ruby:3.0
10+
environment:
11+
AWS_REGION: ap-northeast-1
12+
AWS_ACCESS_KEY_ID: dummy
13+
AWS_SECRET_ACCESS_KEY: dummy
14+
- image: amazon/dynamodb-local
15+
environment:
16+
AWS_REGION: ap-northeast-1
17+
steps:
18+
- checkout
19+
- ruby-orbs/bundle-install:
20+
gemspec_name: fluent-plugin-dynamodb-add
21+
with_gemfile_lock: false
22+
23+
- run: bundle exec rake test
24+
25+
workflows:
26+
test:
27+
jobs:
28+
- test

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
source 'https://rubygems.org'
22

33
gemspec
4+
5+
gem 'rexml'

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require "bundler/gem_tasks"
22
require 'rake/testtask'
33

4+
Rake::Task[:release].clear
5+
46
Rake::TestTask.new(:test) do |test|
57
test.libs << 'lib' << 'test'
6-
test.test_files = FileList['test/test_*.rb']
8+
test.pattern = 'test/**/test_*.rb'
79
test.verbose = true
810
end
911

fluent-plugin-dynamodb-add.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
1919

2020
spec.add_development_dependency "bundler"
2121
spec.add_development_dependency "rake"
22+
spec.add_development_dependency "test-unit"
2223

2324
spec.add_dependency "fluentd", ">= 1", "< 2"
2425
spec.add_dependency "aws-sdk-dynamodb"

test/helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$LOAD_PATH.unshift(File.expand_path("../../", __FILE__))
2+
require "test-unit"
3+
require "fluent/test"
4+
require "fluent/test/driver/output"
5+
require "fluent/test/helpers"
6+
7+
Test::Unit::TestCase.include(Fluent::Test::Helpers)
8+
Test::Unit::TestCase.extend(Fluent::Test::Helpers)

test/plugin/test_out_dynamodb_add.rb

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
require 'helper'
2+
3+
require 'fluent/plugin/out_dynamodb_add'
4+
require 'aws-sdk-dynamodb'
5+
6+
class DynamodbAddTest < Test::Unit::TestCase
7+
include Fluent::Test::Helpers
8+
9+
def setup
10+
Fluent::Test.setup
11+
end
12+
13+
CONFIG = %[
14+
count_key test_count_key
15+
dynamo_count_key test_dynamo_count_key
16+
table_name test_table_name
17+
use_iam_role false
18+
aws_key_id test_aws_key_id
19+
aws_sec_key test_aws_sec_key
20+
endpoint https://test_endpoint
21+
hash_key test_hash_key1,test_hash_key2
22+
hash_key_delimiter :
23+
add_hash_key_prefix 3
24+
range_key test_range_key
25+
]
26+
27+
def create_driver(conf = CONFIG)
28+
Fluent::Test::Driver::Output.new(Fluent::Plugin::DynamodbAdd).configure(conf)
29+
end
30+
31+
sub_test_case 'confguration' do
32+
def test_configure_not_use_iam_role
33+
d = create_driver
34+
assert_equal 'test_count_key', d.instance.count_key
35+
assert_equal 'test_dynamo_count_key', d.instance.dynamo_count_key
36+
assert_equal 'test_table_name', d.instance.table_name
37+
assert_equal false, d.instance.use_iam_role
38+
assert_equal 'test_aws_key_id', d.instance.aws_key_id
39+
assert_equal 'test_aws_sec_key', d.instance.aws_sec_key
40+
assert_equal 'https://test_endpoint', d.instance.endpoint
41+
assert_equal ['test_hash_key1','test_hash_key2'], d.instance.hash_key
42+
assert_equal ':', d.instance.hash_key_delimiter
43+
assert_equal '3', d.instance.add_hash_key_prefix
44+
assert_equal 'test_range_key', d.instance.range_key
45+
end
46+
47+
def test_configure_use_iam_role
48+
conf = CONFIG.clone
49+
conf.gsub!(/use_iam_role\sfalse/, "use_iam_role true")
50+
conf.gsub!(/aws_key_id\stest_aws_key_id/, "")
51+
conf.gsub!(/aws_sec_key\stest_aws_sec_key/, "")
52+
53+
d = create_driver(conf)
54+
assert_equal 'test_count_key', d.instance.count_key
55+
assert_equal 'test_dynamo_count_key', d.instance.dynamo_count_key
56+
assert_equal 'test_table_name', d.instance.table_name
57+
assert_equal true, d.instance.use_iam_role
58+
assert_equal nil, d.instance.aws_key_id
59+
assert_equal nil, d.instance.aws_sec_key
60+
assert_equal 'https://test_endpoint', d.instance.endpoint
61+
assert_equal ['test_hash_key1','test_hash_key2'], d.instance.hash_key
62+
assert_equal ':', d.instance.hash_key_delimiter
63+
assert_equal '3', d.instance.add_hash_key_prefix
64+
assert_equal 'test_range_key', d.instance.range_key
65+
end
66+
67+
def test_configure_not_use_iam_role_and_not_set_range_key
68+
conf = CONFIG.clone
69+
conf.gsub!(/range_key\stest_range_key/, "")
70+
71+
d = create_driver(conf)
72+
assert_equal 'test_count_key', d.instance.count_key
73+
assert_equal 'test_dynamo_count_key', d.instance.dynamo_count_key
74+
assert_equal 'test_table_name', d.instance.table_name
75+
assert_equal false, d.instance.use_iam_role
76+
assert_equal 'test_aws_key_id', d.instance.aws_key_id
77+
assert_equal 'test_aws_sec_key', d.instance.aws_sec_key
78+
assert_equal 'https://test_endpoint', d.instance.endpoint
79+
assert_equal ['test_hash_key1','test_hash_key2'], d.instance.hash_key
80+
assert_equal ':', d.instance.hash_key_delimiter
81+
assert_equal '3', d.instance.add_hash_key_prefix
82+
assert_equal nil, d.instance.range_key
83+
end
84+
85+
def test_configure_set_timestamp
86+
conf = CONFIG.clone
87+
conf << " set_timestamp last_updated_at"
88+
89+
d = create_driver(conf)
90+
assert_equal 'test_count_key', d.instance.count_key
91+
assert_equal 'test_dynamo_count_key', d.instance.dynamo_count_key
92+
assert_equal 'test_table_name', d.instance.table_name
93+
assert_equal false, d.instance.use_iam_role
94+
assert_equal 'test_aws_key_id', d.instance.aws_key_id
95+
assert_equal 'test_aws_sec_key', d.instance.aws_sec_key
96+
assert_equal 'https://test_endpoint', d.instance.endpoint
97+
assert_equal ['test_hash_key1','test_hash_key2'], d.instance.hash_key
98+
assert_equal ':', d.instance.hash_key_delimiter
99+
assert_equal '3', d.instance.add_hash_key_prefix
100+
assert_equal 'test_range_key', d.instance.range_key
101+
assert_equal 'last_updated_at', d.instance.set_timestamp
102+
end
103+
end
104+
105+
106+
def test_count_with_range_key_table
107+
table_name = 'sample_table'
108+
109+
create_table(dynamodb_client, table_with_range_key(table_name))
110+
111+
d = create_driver(
112+
<<~EOS
113+
count_key count
114+
hash_key project_id
115+
116+
table_name #{table_name}
117+
range_key time
118+
dynamo_count_key count
119+
120+
endpoint http://localhost:8000
121+
region ap-northeast-1
122+
aws_key_id dummy
123+
aws_sec_key dummy
124+
EOS
125+
)
126+
127+
resource = Aws::DynamoDB::Resource.new(client: dynamodb_client)
128+
@table = resource.table(table_name)
129+
130+
time = 1000
131+
132+
d.run(default_tag: 'test') do
133+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 1, 'project_id' => 1, 'time' => time})
134+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 2, 'project_id' => 1, 'time' => time})
135+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 4, 'project_id' => 1, 'time' => 2000})
136+
end
137+
138+
item = @table.get_item(key: { 'Id': '1', 'ViewTimestamp': time.to_i}).item
139+
140+
assert_equal 3, item['count']
141+
142+
d.run(default_tag: 'test') do
143+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 8, 'project_id' => 1, 'time' => time})
144+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 16, 'project_id' => 1, 'time' => time})
145+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 32, 'project_id' => 1, 'time' => 2000})
146+
end
147+
148+
item = @table.get_item(key: { 'Id': '1', 'ViewTimestamp': time.to_i}).item
149+
150+
assert_equal 27, item['count']
151+
152+
delete_table(dynamodb_client, table_name)
153+
end
154+
155+
def test_count_with_no_range_key_table
156+
table_name = 'sample_table'
157+
158+
create_table(dynamodb_client, table_without_range_key(table_name))
159+
160+
d = create_driver(
161+
<<~EOS
162+
count_key count
163+
hash_key project_id
164+
165+
table_name #{table_name}
166+
dynamo_count_key count
167+
168+
endpoint http://localhost:8000
169+
region ap-northeast-1
170+
aws_key_id dummy
171+
aws_sec_key dummy
172+
EOS
173+
)
174+
175+
resource = Aws::DynamoDB::Resource.new(client: dynamodb_client)
176+
@table = resource.table(table_name)
177+
178+
d.run(default_tag: 'test') do
179+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 1, 'project_id' => 1})
180+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 2, 'project_id' => 1})
181+
end
182+
183+
item = @table.get_item(key: { 'Id': '1'}).item
184+
185+
assert_equal 3, item['count']
186+
187+
d.run(default_tag: 'test') do
188+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 4, 'project_id' => 1})
189+
d.feed(event_time('2022-09-01 10:00:00 UTC'), {'count' => 8, 'project_id' => 1})
190+
end
191+
192+
item = @table.get_item(key: { 'Id': '1'}).item
193+
194+
assert_equal 15, item['count']
195+
196+
delete_table(dynamodb_client, table_name)
197+
end
198+
199+
private
200+
201+
def create_table(dynamodb_client, table_definition)
202+
response = dynamodb_client.create_table(table_definition)
203+
response.table_description.table_status
204+
rescue StandardError => e
205+
binding.irb
206+
end
207+
208+
def delete_table(dynamodb_client, table_name)
209+
dynamodb_client.delete_table(
210+
table_name: table_name
211+
)
212+
end
213+
214+
def table_without_range_key(table_name)
215+
table_definition = {
216+
table_name: table_name,
217+
key_schema: [
218+
{
219+
attribute_name: 'Id',
220+
key_type: 'HASH' # Partition key.
221+
},
222+
],
223+
attribute_definitions: [
224+
{
225+
attribute_name: 'Id',
226+
attribute_type: 'S'
227+
},
228+
],
229+
billing_mode: "PAY_PER_REQUEST",
230+
}
231+
end
232+
233+
def table_with_range_key(table_name)
234+
table_definition = {
235+
table_name: table_name,
236+
key_schema: [
237+
{
238+
attribute_name: 'Id',
239+
key_type: 'HASH' # Partition key.
240+
},
241+
{
242+
attribute_name: 'ViewTimestamp',
243+
key_type: 'RANGE' # Sort key.
244+
}
245+
],
246+
attribute_definitions: [
247+
{
248+
attribute_name: 'Id',
249+
attribute_type: 'S'
250+
},
251+
{
252+
attribute_name: 'ViewTimestamp',
253+
attribute_type: 'N'
254+
}
255+
],
256+
billing_mode: "PAY_PER_REQUEST",
257+
}
258+
end
259+
260+
261+
def dynamodb_client
262+
@client ||= Aws::DynamoDB::Client.new({
263+
access_key_id: 'dummy',
264+
secret_access_key: 'dummy',
265+
endpoint: 'http://localhost:8000',
266+
region: 'ap-northeast-1'
267+
})
268+
end
269+
end

0 commit comments

Comments
 (0)