|
| 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