From ae9776f8f76a7607cbd1eb994dcccc755494f8b8 Mon Sep 17 00:00:00 2001 From: Papa Bakary Camara Date: Mon, 19 Feb 2024 10:01:37 +0100 Subject: [PATCH] UT for CheckConnectionToKafka with multiple broker addresses --- ccx_notification_writer_test.go | 31 ++++++++++++++++++++++++++++++- export_test.go | 3 +++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ccx_notification_writer_test.go b/ccx_notification_writer_test.go index 018b2ed..b15a057 100644 --- a/ccx_notification_writer_test.go +++ b/ccx_notification_writer_test.go @@ -241,7 +241,36 @@ func TestConvertLogLevel(t *testing.T) { for _, td := range testData { // perform conversion output := main.ConvertLogLevel(td.Input) - // check if converted value is eqaual to expected one + // check if converted value is equal to expected one assert.Equal(t, output, td.Output) } } + +// TestDoSelectedOperationCheckConnectionToKafka checks the function +// CheckConnectionToKafka called via doSelectedOperation function +func TestDoSelectedOperationCheckConnectionToKafka(t *testing.T) { + // fill in configuration structure + configuration := main.ConfigStruct{} + configuration.Broker = main.BrokerConfiguration{ + Addresses: "broker_address:9092, broker_address:9093", + Topic: "broker_topic", + } + cliFlags := main.CliFlags{ + CheckConnectionToKafka: true, + } + + // try to call the tested function and capture its output + output, err := capture.ErrorOutput(func() { + log.Logger = log.Output(zerolog.New(os.Stderr)) + code, err := main.DoSelectedOperation(&configuration, cliFlags) + assert.Equal(t, code, main.ExitStatusKafkaError) + assert.NoError(t, err) + }) + + // check the captured text + checkCapture(t, err) + + // expected content printed by tested function + assert.Contains(t, output, main.ConnectionToBrokerMessage) + assert.Contains(t, output, main.AllBrokerConnectionAttemptsMessage) +} diff --git a/export_test.go b/export_test.go index 3d6c33d..7037f02 100644 --- a/export_test.go +++ b/export_test.go @@ -45,6 +45,9 @@ var ( ShowConfiguration = showConfiguration DoSelectedOperation = doSelectedOperation + ConnectionToBrokerMessage = connectionToBrokerMessage + AllBrokerConnectionAttemptsMessage = allBrokerConnectionAttemptsMessage + // functions from consumer.go source file ParseMessage = parseMessage ShrinkMessage = shrinkMessage