From dc0279ab48f7891e4f78d8f51ae7bcfd2fc350e2 Mon Sep 17 00:00:00 2001 From: Mohamed Awnallah Date: Sat, 23 Nov 2024 00:48:02 +0200 Subject: [PATCH] pkg/karmadactl: unit test exec In this commit, we unit test exec on validating the execute options on control plane and member clusters. Signed-off-by: Mohamed Awnallah --- pkg/karmadactl/exec/exec.go | 3 +- pkg/karmadactl/exec/exec_test.go | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 pkg/karmadactl/exec/exec_test.go diff --git a/pkg/karmadactl/exec/exec.go b/pkg/karmadactl/exec/exec.go index 3cd9624b8..9eb317a8b 100644 --- a/pkg/karmadactl/exec/exec.go +++ b/pkg/karmadactl/exec/exec.go @@ -17,6 +17,7 @@ limitations under the License. package exec import ( + "errors" "fmt" "time" @@ -151,7 +152,7 @@ func (o *CommandExecOptions) Validate() error { return err } if o.OperationScope == options.Members && len(o.Cluster) == 0 { - return fmt.Errorf("must specify a member cluster") + return errors.New("must specify a member cluster") } return o.KubectlExecOptions.Validate() } diff --git a/pkg/karmadactl/exec/exec_test.go b/pkg/karmadactl/exec/exec_test.go new file mode 100644 index 000000000..673e2686a --- /dev/null +++ b/pkg/karmadactl/exec/exec_test.go @@ -0,0 +1,79 @@ +/* +Copyright 2024 The Karmada Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package exec + +import ( + "bytes" + "strings" + "testing" + + "k8s.io/cli-runtime/pkg/genericiooptions" + "k8s.io/cli-runtime/pkg/resource" + kubectlexec "k8s.io/kubectl/pkg/cmd/exec" + + "github.com/karmada-io/karmada/pkg/karmadactl/options" +) + +func TestValidateExecute(t *testing.T) { + tests := []struct { + name string + execOpts *CommandExecOptions + wantErr bool + errMsg string + }{ + { + name: "Validate_WithMemberOperationScopeAndWithoutMemberCluster_MemberClusterOptMustBeSpecified", + execOpts: &CommandExecOptions{OperationScope: options.Members}, + wantErr: true, + errMsg: "must specify a member cluster", + }, + { + name: "Validate_WithMemberOperationScopeAndWithMemberCluster_Validated", + execOpts: &CommandExecOptions{ + OperationScope: options.Members, + Cluster: "test-cluster", + KubectlExecOptions: &kubectlexec.ExecOptions{ + FilenameOptions: resource.FilenameOptions{}, + ResourceName: "test-pod", + Command: []string{"date"}, + StreamOptions: kubectlexec.StreamOptions{ + IOStreams: genericiooptions.IOStreams{ + Out: &bytes.Buffer{}, + ErrOut: &bytes.Buffer{}, + }, + }, + }, + }, + + wantErr: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := test.execOpts.Validate() + if err == nil && test.wantErr { + t.Fatal("expected an error, but got none") + } + if err != nil && !test.wantErr { + t.Errorf("unexpected error, got: %v", err) + } + if err != nil && test.wantErr && !strings.Contains(err.Error(), test.errMsg) { + t.Errorf("expected error message %s to be in %s", test.errMsg, err.Error()) + } + }) + } +}