Files
k3k/cli/cmds/cluster_delete_test.go
T
Enrico CandinoandGitHub eb0955f666 Add cluster delete autocompletion (#1080)
* Add cluster deletion and completion enhancements

- Implemented `--all` flag for deleting all clusters in a namespace.
- Updated argument parsing to allow for maximum one cluster name.
- Added completion functions for cluster names with namespace filtering.
- Enhanced tests for cluster argument resolution and completion functions.

* fix docs

* Add log message for empty cluster deletion in specified namespace

* Refactor context handling in command functions to use `cmd.Context()`
2026-07-28 18:22:25 +02:00

84 lines
2.0 KiB
Go

package cmds
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/rancher/k3k/pkg/apis/k3k.io/v1beta1"
)
func TestDeleteMissingCluster(t *testing.T) {
scheme := runtime.NewScheme()
require.NoError(t, v1beta1.AddToScheme(scheme))
appCtx := &AppContext{Client: fake.NewClientBuilder().WithScheme(scheme).Build()}
err := delete(appCtx)(&cobra.Command{}, []string{"missing"})
require.EqualError(t, err, `cluster "missing" not found in namespace "k3k-missing"`)
}
func Test_resolveClusterArg(t *testing.T) {
tests := []struct {
name string
flagNamespace string
arg string
wantNamespace string
wantName string
wantErr bool
}{
{
name: "bare name defaults to k3k-<name>",
arg: "mycluster",
wantNamespace: "k3k-mycluster",
wantName: "mycluster",
},
{
name: "bare name respects the namespace flag",
flagNamespace: "custom",
arg: "mycluster",
wantNamespace: "custom",
wantName: "mycluster",
},
{
name: "namespace/name form is split",
arg: "k3k-foo/mycluster",
wantNamespace: "k3k-foo",
wantName: "mycluster",
},
{
name: "namespace/name matching the flag is accepted",
flagNamespace: "k3k-foo",
arg: "k3k-foo/mycluster",
wantNamespace: "k3k-foo",
wantName: "mycluster",
},
{
name: "namespace/name conflicting with the flag errors",
flagNamespace: "bar",
arg: "k3k-foo/mycluster",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
appCtx := &AppContext{namespace: tt.flagNamespace}
namespace, name, err := resolveClusterArg(appCtx, tt.arg)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantNamespace, namespace)
assert.Equal(t, tt.wantName, name)
})
}
}