Add more tests for KafkaUtils
This commit is contained in:
@@ -5,9 +5,13 @@ defmodule KafkaexLagExporter.KafkaUtils.Test do
|
||||
alias KafkaexLagExporter.KafkaWrapper.Behaviour, as: KafkaWrapper
|
||||
alias KafkaexLagExporter.KafkaUtils
|
||||
|
||||
@test_endpoint "test_host:1234"
|
||||
@test_consumer_group "test-consumer-group"
|
||||
@test_host "test_host"
|
||||
@test_port "1234"
|
||||
@test_endpoint {@test_host, @test_port}
|
||||
@test_sock_opts [ssl: []]
|
||||
@test_group_name1 "test-consumer_group1"
|
||||
@test_group_name2 "test-consumer_group"
|
||||
@test_topic_name "test-topic_name"
|
||||
|
||||
setup do
|
||||
patch(
|
||||
@@ -19,60 +23,129 @@ defmodule KafkaexLagExporter.KafkaUtils.Test do
|
||||
:ok
|
||||
end
|
||||
|
||||
describe "resolve_offsets" do
|
||||
describe "get_consumer_group_names" do
|
||||
setup do
|
||||
client = :client2
|
||||
topic = "test-topic"
|
||||
consumer_info1 = {:a, @test_group_name1, "consumer"}
|
||||
consumer_info2 = {:b, @test_group_name2, "something-other"}
|
||||
|
||||
patch(
|
||||
KafkaWrapper,
|
||||
:get_partitions_count,
|
||||
fn _, _ -> {:ok, 3} end
|
||||
:list_all_groups,
|
||||
fn _, _ ->
|
||||
[
|
||||
{
|
||||
@test_endpoint,
|
||||
[consumer_info1, consumer_info2]
|
||||
}
|
||||
]
|
||||
end
|
||||
)
|
||||
|
||||
patch(
|
||||
KafkaWrapper,
|
||||
:resolve_offset,
|
||||
fn _, _, _, _, _ -> {:ok, 42} end
|
||||
)
|
||||
group_names = KafkaUtils.get_consumer_group_names(@test_endpoint)
|
||||
|
||||
offsets = KafkaUtils.resolve_offsets(topic, :earliest, client)
|
||||
|
||||
[client: client, offsets: offsets, topic: topic]
|
||||
[group_names: group_names]
|
||||
end
|
||||
|
||||
test "should get partition count", context do
|
||||
expected_topic = context[:topic]
|
||||
expected_client = context[:client]
|
||||
|
||||
assert_called(KafkaWrapper.get_partitions_count(expected_client, expected_topic))
|
||||
test "should list all groups" do
|
||||
assert_called(KafkaWrapper.list_all_groups([@test_endpoint], []))
|
||||
end
|
||||
|
||||
test "should resolve offset per partition", context do
|
||||
expected_topic = context[:topic]
|
||||
|
||||
assert_called(
|
||||
KafkaWrapper.resolve_offset(@test_endpoint, expected_topic, 0, :earliest, @test_sock_opts)
|
||||
)
|
||||
|
||||
assert_called(
|
||||
KafkaWrapper.resolve_offset(@test_endpoint, expected_topic, 1, :earliest, @test_sock_opts)
|
||||
)
|
||||
|
||||
assert_called(
|
||||
KafkaWrapper.resolve_offset(@test_endpoint, expected_topic, 2, :earliest, @test_sock_opts)
|
||||
)
|
||||
end
|
||||
|
||||
test "should return offsets", context do
|
||||
assert context[:offsets] == [{0, 42}, {1, 42}, {2, 42}]
|
||||
test "should filter for consumer groups and return names", context do
|
||||
assert context[:group_names] === [@test_group_name1]
|
||||
end
|
||||
end
|
||||
|
||||
describe "fetch_committed_offsets" do
|
||||
describe "get_consumer_group_info" do
|
||||
setup do
|
||||
partition_info1 = %{partition_index: 0, committed_offset: 23}
|
||||
partition_info2 = %{partition_index: 1, committed_offset: 42}
|
||||
consumer_group_name = "test-group-id1"
|
||||
member_assignment = "test_member_assignment"
|
||||
member_assignment = "test_member_assignment"
|
||||
consumer_id = "test_consumer_id"
|
||||
member_host = "test_member_host"
|
||||
|
||||
patch(
|
||||
KafkaWrapper,
|
||||
:describe_groups,
|
||||
fn _, _, _ ->
|
||||
{
|
||||
:ok,
|
||||
[
|
||||
%{
|
||||
group_id: consumer_group_name,
|
||||
members: [
|
||||
%{
|
||||
client_host: member_host,
|
||||
member_assignment: member_assignment,
|
||||
member_id: consumer_id
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
end
|
||||
)
|
||||
|
||||
patch(KafkaexLagExporter.TopicNameParser, :parse_topic_names, fn _ -> [@test_topic_name] end)
|
||||
|
||||
group_info =
|
||||
KafkaUtils.get_consumer_group_info(@test_endpoint, [], [
|
||||
@test_group_name1,
|
||||
@test_group_name2
|
||||
])
|
||||
|
||||
[
|
||||
member_host: member_host,
|
||||
group_info: group_info,
|
||||
consumer_group_name: consumer_group_name,
|
||||
member_assignment: member_assignment,
|
||||
consumer_id: consumer_id
|
||||
]
|
||||
end
|
||||
|
||||
test "should describe groups" do
|
||||
assert_called(
|
||||
KafkaWrapper.describe_groups(@test_endpoint, [], [@test_group_name1, @test_group_name2])
|
||||
)
|
||||
end
|
||||
|
||||
test "should parse topic names", context do
|
||||
expected_member_assignment = context[:expected_member_assignment]
|
||||
|
||||
assert_called(
|
||||
KafkaexLagExporter.TopicNameParser.parse_topic_names(expected_member_assignment)
|
||||
)
|
||||
end
|
||||
|
||||
test "should filter for consumer groups and return names", context do
|
||||
expected_consumer_group_name = context[:consumer_group_name]
|
||||
expected_consumer_id = context[:consumer_id]
|
||||
expected_member_host = context[:member_host]
|
||||
|
||||
assert context[:group_info] === [
|
||||
{
|
||||
expected_consumer_group_name,
|
||||
[@test_topic_name],
|
||||
expected_consumer_id,
|
||||
expected_member_host
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
describe "lag" do
|
||||
setup do
|
||||
consumer_group = "test-consumer-group"
|
||||
client = :client1
|
||||
committed_offset1 = 23
|
||||
committed_offset2 = 42
|
||||
resolved_offset = 50
|
||||
|
||||
partition_info1 = %{partition_index: 0, committed_offset: committed_offset1}
|
||||
partition_info2 = %{partition_index: 1, committed_offset: committed_offset2}
|
||||
|
||||
patch(KafkaWrapper, :get_partitions_count, fn _, _ -> {:ok, 2} end)
|
||||
|
||||
patch(KafkaWrapper, :resolve_offset, fn _, _, _, _, _ -> {:ok, resolved_offset} end)
|
||||
|
||||
patch(
|
||||
KafkaWrapper,
|
||||
@@ -82,23 +155,60 @@ defmodule KafkaexLagExporter.KafkaUtils.Test do
|
||||
end
|
||||
)
|
||||
|
||||
offsets = KafkaUtils.fetch_committed_offsets("test-topic", @test_consumer_group, :test_atom)
|
||||
lag = KafkaUtils.lag(@test_topic_name, consumer_group, client)
|
||||
|
||||
[offsets: offsets]
|
||||
[
|
||||
lag: lag,
|
||||
consumer_group: consumer_group,
|
||||
client: client,
|
||||
committed_offset1: committed_offset1,
|
||||
committed_offset2: committed_offset2,
|
||||
resolved_offset: resolved_offset
|
||||
]
|
||||
end
|
||||
|
||||
test "should get the committed offsets from KafkaWrapper" do
|
||||
test "should start connection", context do
|
||||
expected_client = context[:client]
|
||||
|
||||
assert_called(KafkaUtils.connection(expected_client))
|
||||
end
|
||||
|
||||
test "should get partition count", context do
|
||||
expected_client = context[:client]
|
||||
|
||||
assert_called(KafkaWrapper.get_partitions_count(expected_client, @test_topic_name))
|
||||
end
|
||||
|
||||
test "should resolve offsets for each partition" do
|
||||
assert_called(
|
||||
KafkaWrapper.resolve_offset(@test_endpoint, @test_topic_name, 0, :latest, @test_sock_opts)
|
||||
)
|
||||
|
||||
assert_called(
|
||||
KafkaWrapper.resolve_offset(@test_endpoint, @test_topic_name, 1, :latest, @test_sock_opts)
|
||||
)
|
||||
end
|
||||
|
||||
test "should fetch committed offsets", context do
|
||||
expected_consumer_group = context[:consumer_group]
|
||||
|
||||
assert_called(
|
||||
KafkaWrapper.fetch_committed_offsets(
|
||||
@test_endpoint,
|
||||
@test_sock_opts,
|
||||
@test_consumer_group
|
||||
expected_consumer_group
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
test "should return offsets", context do
|
||||
assert context[:offsets] == [{0, 23}, {1, 42}]
|
||||
test "should return calculated lag per partition", context do
|
||||
expected_lag_partition_1 = context[:resolved_offset] - context[:committed_offset1]
|
||||
expected_lag_partition_2 = context[:resolved_offset] - context[:committed_offset2]
|
||||
|
||||
assert context[:lag] === [
|
||||
{0, expected_lag_partition_1},
|
||||
{1, expected_lag_partition_2}
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user