-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor kafka exporter script and add ut
- Loading branch information
Showing
6 changed files
with
174 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# shellcheck shell=bash | ||
# shellcheck disable=SC2034 | ||
|
||
# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run. | ||
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then | ||
echo "kafka_exporter_setup_spec.sh skip cases because dependency bash version 4 or higher is not installed." | ||
exit 0 | ||
fi | ||
|
||
Describe "Kafka Exporter Setup Script Tests" | ||
# Load the scripts to be tested and dependencies | ||
Include ../scripts/kafka-exporter-setup.sh | ||
|
||
init() { | ||
ut_mode="true" | ||
} | ||
BeforeAll "init" | ||
|
||
un_setup() { | ||
# Reset environment variables before each test | ||
unset BROKER_POD_FQDN_LIST | ||
unset COMBINE_POD_FQDN_LIST | ||
unset TLS_ENABLED | ||
} | ||
|
||
Describe "generate_kafka_servers()" | ||
It "returns an error if both BROKER_POD_FQDN_LIST and COMBINE_POD_FQDN_LIST are unset" | ||
un_setup | ||
When run generate_kafka_servers | ||
The stderr should include "Error: BROKER_POD_FQDN_LIST and COMBINE_POD_FQDN_LIST environment variable is not set" | ||
The status should be failure | ||
End | ||
|
||
It "generates server list from COMBINE_POD_FQDN_LIST" | ||
un_setup | ||
COMBINE_POD_FQDN_LIST="kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local,kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local" | ||
When run generate_kafka_servers | ||
The output should equal " --kafka.server=kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local:9094 --kafka.server=kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local:9094" | ||
The status should be success | ||
End | ||
|
||
It "generates server list from BROKER_POD_FQDN_LIST if COMBINE_POD_FQDN_LIST is unset" | ||
un_setup | ||
BROKER_POD_FQDN_LIST="kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local,kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local" | ||
When run generate_kafka_servers | ||
The output should equal " --kafka.server=kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local:9094 --kafka.server=kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local:9094" | ||
The status should be success | ||
End | ||
End | ||
|
||
Describe "get_start_kafka_exporter_cmd()" | ||
It "returns failure if generate_kafka_servers fails" | ||
generate_kafka_servers() { | ||
return 1 | ||
} | ||
When run get_start_kafka_exporter_cmd | ||
The stderr should include "failed to generate kafka servers. Exiting." | ||
The status should be failure | ||
End | ||
|
||
It "returns the correct command with TLS enabled" | ||
un_setup | ||
COMBINE_POD_FQDN_LIST="combine1.example.com" | ||
TLS_ENABLED="true" | ||
When run get_start_kafka_exporter_cmd | ||
The output should include "kafka_exporter --web.listen-address=:9308 --tls.enabled --kafka.server=combine1.example.com:9094" | ||
The stderr should include "TLS_ENABLED is set to true, start kafka_exporter with tls enabled." | ||
The status should be success | ||
End | ||
|
||
It "returns the correct command with TLS disabled" | ||
un_setup | ||
BROKER_POD_FQDN_LIST="broker1.example.com" | ||
TLS_ENABLED="" | ||
When run get_start_kafka_exporter_cmd | ||
The output should include "kafka_exporter --web.listen-address=:9308 --kafka.server=broker1.example.com:9094" | ||
The stderr should include "TLS_ENABLED is not set, start kafka_exporter with tls disabled." | ||
The status should be success | ||
End | ||
End | ||
End |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/bin/bash | ||
|
||
# shellcheck disable=SC2034 | ||
ut_mode="false" | ||
test || __() { | ||
# when running in non-unit test mode, set the options "set -ex". | ||
set -ex; | ||
} | ||
|
||
generate_kafka_servers() { | ||
local servers="" | ||
|
||
if [[ -z "$BROKER_POD_FQDN_LIST" ]] && [[ -z "$COMBINE_POD_FQDN_LIST" ]]; then | ||
echo "Error: BROKER_POD_FQDN_LIST and COMBINE_POD_FQDN_LIST environment variable is not set, Please check and try again." >&2 | ||
return 1 | ||
fi | ||
|
||
## try to use COMBINE_POD_FQDN_LIST first | ||
if [[ -n "$COMBINE_POD_FQDN_LIST" ]]; then | ||
IFS=',' read -r -a combine_pod_fqdn_list <<< "$COMBINE_POD_FQDN_LIST" | ||
for pod_fqdn in "${combine_pod_fqdn_list[@]}"; do | ||
servers="${servers} --kafka.server=${pod_fqdn}:9094" | ||
done | ||
echo "$servers" | ||
return 0 | ||
fi | ||
|
||
# if COMBINE_POD_FQDN_LIST is not set, use BROKER_POD_FQDN_LIST | ||
IFS=',' read -r -a broker_pod_fqdn_list <<< "$BROKER_POD_FQDN_LIST" | ||
for pod_fqdn in "${broker_pod_fqdn_list[@]}"; do | ||
servers="${servers} --kafka.server=${pod_fqdn}:9094" | ||
done | ||
echo "$servers" | ||
return 0 | ||
} | ||
|
||
get_start_kafka_exporter_cmd() { | ||
local servers | ||
local status | ||
servers=$(generate_kafka_servers) | ||
status=$? | ||
if [[ $status -ne 0 ]]; then | ||
echo "failed to generate kafka servers. Exiting." >&2 | ||
return 1 | ||
fi | ||
|
||
if [[ -n "$TLS_ENABLED" ]]; then | ||
echo "TLS_ENABLED is set to true, start kafka_exporter with tls enabled." >&2 | ||
echo "kafka_exporter --web.listen-address=:9308 --tls.enabled ${servers}" | ||
else | ||
echo "TLS_ENABLED is not set, start kafka_exporter with tls disabled." >&2 | ||
echo "kafka_exporter --web.listen-address=:9308 ${servers}" | ||
fi | ||
return 0 | ||
} | ||
|
||
start_kafka_exporter() { | ||
local cmd | ||
cmd=$(get_start_kafka_exporter_cmd) | ||
status=$? | ||
if [[ $status -ne 0 ]]; then | ||
ehco "failed to get start kafka_exporter command. Exiting." >&2 | ||
exit 1 | ||
fi | ||
$cmd | ||
} | ||
|
||
# This is magic for shellspec ut framework. | ||
# Sometime, functions are defined in a single shell script. | ||
# You will want to test it. but you do not want to run the script. | ||
# When included from shellspec, __SOURCED__ variable defined and script | ||
# end here. The script path is assigned to the __SOURCED__ variable. | ||
${__SOURCED__:+false} : || return 0 | ||
|
||
# main | ||
start_kafka_exporter |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters