Skip to content

Commit

Permalink
Add test checking all tctl commands match process
Browse files Browse the repository at this point in the history
  • Loading branch information
vapopov committed Dec 17, 2024
1 parent 8047189 commit 4b1e596
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tool/tctl/common/tctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package common

import (
"context"
"errors"
"os"
"testing"

Expand All @@ -31,6 +32,7 @@ import (
"github.com/gravitational/teleport/lib/config"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
"github.com/gravitational/teleport/tool/teleport/testenv"
)
Expand All @@ -40,6 +42,55 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

// TestCommandMatchBeforeAuthConnect verifies all defined `tctl` commands `TryRun`
// method, to ensure that auth client not initialized in matching process,
// so we don't require a client before command is executed.
func TestCommandMatchBeforeAuthConnect(t *testing.T) {
app := utils.InitCLIParser("tctl", GlobalHelpString)
cfg := servicecfg.MakeDefaultConfig()
cfg.CircuitBreakerConfig = breaker.NoopBreakerConfig()

var ccf tctlcfg.GlobalCLIFlags

commands := Commands()
for i := range commands {
commands[i].Initialize(app, &ccf, cfg)
}

testError := errors.New("auth client must not be initialized before match")

ctx := context.Background()
clientFunc := func(ctx context.Context) (client *authclient.Client, close func(context.Context), err error) {
return nil, nil, testError
}

var match bool
var err error

// We set the command which is not defined to go through
// all defined commands to ensure that auth client
// not initialised before command is matched.
for _, c := range commands {
match, err = c.TryRun(ctx, "non-existing-command", clientFunc)
if err != nil {
break
}
}
require.False(t, match)
require.NoError(t, err)

// Iterate and expect that `tokens ls` command going to be executed
// and auth client is requested.
for _, c := range commands {
match, err = c.TryRun(ctx, "tokens ls", clientFunc)
if err != nil {
break
}
}
require.False(t, match)
require.ErrorIs(t, err, testError)
}

// TestConnect tests client config and connection logic.
func TestConnect(t *testing.T) {
dynAddr := helpers.NewDynamicServiceAddr(t)
Expand Down
1 change: 1 addition & 0 deletions tool/tctl/common/version_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package common

import (
"context"

"github.com/alecthomas/kingpin/v2"
"github.com/gravitational/trace"

Expand Down

0 comments on commit 4b1e596

Please sign in to comment.