mirror of
https://github.com/rancher/k3k.git
synced 2026-08-18 20:07:06 +00:00
* 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()`
84 lines
2.0 KiB
Go
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)
|
|
})
|
|
}
|
|
}
|