|
| 1 | +require 'helper' |
| 2 | +require 'fluent/test/driver/input' |
| 3 | +require 'securerandom' |
| 4 | + |
| 5 | +class RdkafkaGroupInputTest < Test::Unit::TestCase |
| 6 | + |
| 7 | + def have_rdkafka |
| 8 | + begin |
| 9 | + require 'fluent/plugin/in_rdkafka_group' |
| 10 | + true |
| 11 | + rescue LoadError |
| 12 | + false |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + def setup |
| 17 | + omit_unless(have_rdkafka, "rdkafka isn't installed") |
| 18 | + Fluent::Test.setup |
| 19 | + end |
| 20 | + |
| 21 | + TOPIC_NAME = "kafka-input-#{SecureRandom.uuid}" |
| 22 | + |
| 23 | + CONFIG = %[ |
| 24 | + topics #{TOPIC_NAME} |
| 25 | + kafka_configs {"bootstrap.servers": "localhost:9092", "group.id": "test_group"} |
| 26 | + <parse> |
| 27 | + @type none |
| 28 | + </parse> |
| 29 | + ] |
| 30 | + |
| 31 | + def create_driver(conf = CONFIG) |
| 32 | + Fluent::Test::Driver::Input.new(Fluent::Plugin::RdKafkaGroupInput).configure(conf) |
| 33 | + end |
| 34 | + |
| 35 | + |
| 36 | + def test_configure |
| 37 | + d = create_driver |
| 38 | + assert_equal [TOPIC_NAME], d.instance.topics |
| 39 | + assert_equal 'localhost:9092', d.instance.kafka_configs['bootstrap.servers'] |
| 40 | + end |
| 41 | + |
| 42 | + def test_multi_worker_support |
| 43 | + d = create_driver |
| 44 | + assert_true d.instance.multi_workers_ready? |
| 45 | + end |
| 46 | + |
| 47 | + class ConsumeTest < self |
| 48 | + TOPIC_NAME = "kafka-input-#{SecureRandom.uuid}" |
| 49 | + |
| 50 | + def setup |
| 51 | + @kafka = Kafka.new(["localhost:9092"], client_id: 'kafka') |
| 52 | + @producer = @kafka.producer |
| 53 | + @kafka.create_topic(TOPIC_NAME) |
| 54 | + end |
| 55 | + |
| 56 | + def teardown |
| 57 | + @kafka.delete_topic(TOPIC_NAME) |
| 58 | + @kafka.close |
| 59 | + end |
| 60 | + |
| 61 | + def test_consume |
| 62 | + conf = %[ |
| 63 | + topics #{TOPIC_NAME} |
| 64 | + kafka_configs {"bootstrap.servers": "localhost:9092", "group.id": "test_group"} |
| 65 | + <parse> |
| 66 | + @type none |
| 67 | + </parse> |
| 68 | + ] |
| 69 | + |
| 70 | + d = create_driver(conf) |
| 71 | + |
| 72 | + d.run(expect_records: 1, timeout: 10) do |
| 73 | + sleep 0.1 |
| 74 | + @producer.produce("Hello, fluent-plugin-kafka!", topic: TOPIC_NAME) |
| 75 | + @producer.deliver_messages |
| 76 | + end |
| 77 | + |
| 78 | + expected = {'message' => 'Hello, fluent-plugin-kafka!'} |
| 79 | + assert_equal expected, d.events[0][2] |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + class ConsumeTopicWithRegexpTest < self |
| 84 | + TOPIC_NAME1 = "kafka-input-1-#{SecureRandom.uuid}" |
| 85 | + TOPIC_NAME2 = "kafka-input-2-#{SecureRandom.uuid}" |
| 86 | + TOPIC_NAME_REGEXP = "/kafka-input-(1|2)-.*/" |
| 87 | + |
| 88 | + def setup |
| 89 | + @kafka = Kafka.new(["localhost:9092"], client_id: 'kafka') |
| 90 | + @producer = @kafka.producer |
| 91 | + @kafka.create_topic(TOPIC_NAME1) |
| 92 | + @kafka.create_topic(TOPIC_NAME2) |
| 93 | + end |
| 94 | + |
| 95 | + def teardown |
| 96 | + @kafka.delete_topic(TOPIC_NAME1) |
| 97 | + @kafka.delete_topic(TOPIC_NAME2) |
| 98 | + @kafka.close |
| 99 | + end |
| 100 | + |
| 101 | + def test_consume_with_regexp |
| 102 | + conf = %[ |
| 103 | + topics #{TOPIC_NAME_REGEXP} |
| 104 | + kafka_configs {"bootstrap.servers": "localhost:9092", "group.id": "test_group"} |
| 105 | + <parse> |
| 106 | + @type none |
| 107 | + </parse> |
| 108 | + ] |
| 109 | + d = create_driver(conf) |
| 110 | + |
| 111 | + d.run(expect_records: 2, timeout: 10) do |
| 112 | + sleep 0.1 |
| 113 | + @producer.produce("Hello, fluent-plugin-kafka! in topic 1", topic: TOPIC_NAME1) |
| 114 | + @producer.produce("Hello, fluent-plugin-kafka! in topic 2", topic: TOPIC_NAME2) |
| 115 | + @producer.deliver_messages |
| 116 | + end |
| 117 | + expected_message_pattern = /Hello, fluent-plugin-kafka! in topic [12]/ |
| 118 | + assert_equal 2, d.events.size |
| 119 | + assert_match(expected_message_pattern, d.events[0][2]['message']) |
| 120 | + assert_match(expected_message_pattern, d.events[1][2]['message']) |
| 121 | + end |
| 122 | + end |
| 123 | +end |
0 commit comments