Skip to content

Commit

Permalink
add tests for --context param
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rk committed Nov 28, 2024
1 parent 4fe575c commit 76996bb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package cmd_test
import (
"fmt"
"os"
"strings"
"testing"

"github.com/deviceinsight/kafkactl/v5/internal/output"

"github.com/deviceinsight/kafkactl/v5/internal/global"

"github.com/deviceinsight/kafkactl/v5/internal/testutil"
Expand Down Expand Up @@ -125,3 +128,53 @@ func TestEnvironmentVariableLoadingAliases(t *testing.T) {
testutil.AssertEquals(t, "WaitForAll", viper.GetString("contexts.default.producer.requiredAcks"))
testutil.AssertEquals(t, "1234", viper.GetString("contexts.default.producer.maxMessageBytes"))
}

func TestContextFlag(t *testing.T) {

testutil.StartUnitTest(t)

kafkaCtl := testutil.CreateKafkaCtlCommand()

if _, err := kafkaCtl.Execute("version", "--context", "sasl-user"); err != nil {
t.Fatalf("failed to execute command: %v", err)
}

testutil.AssertEquals(t, "sasl-user", global.GetCurrentContext())
}

func TestContextFlagProvidesUnknownContext(t *testing.T) {

testutil.StartUnitTest(t)

kafkaCtl := testutil.CreateKafkaCtlCommand()

if _, err := kafkaCtl.Execute("version", "--context", "unknown-context"); err != nil {
t.Fatalf("failed to execute command: %v", err)
}

var failError error

output.Fail = func(err error) {
failError = err
}

global.GetCurrentContext()

testutil.AssertErrorContains(t, "not a valid context: unknown-context", failError)
}

func TestContextFlagAutoCompletion(t *testing.T) {

testutil.StartUnitTest(t)

kafkaCtl := testutil.CreateKafkaCtlCommand()
kafkaCtl.Verbose = false

if _, err := kafkaCtl.Execute("__complete", "--context", ""); err != nil {
t.Fatalf("failed to execute command: %v", err)
}

outputLines := strings.Split(strings.TrimSpace(kafkaCtl.GetStdOut()), "\n")

testutil.AssertArraysEquals(t, []string{"default", "k8s-mock", "no-avro", "sasl-admin", "sasl-user"}, outputLines[:len(outputLines)-1])
}

0 comments on commit 76996bb

Please sign in to comment.