From 543a23dbd087991ab777c740124697609a3ca3c2 Mon Sep 17 00:00:00 2001 From: Mohamed Awnallah Date: Sat, 23 Nov 2024 16:49:00 +0200 Subject: [PATCH] pkg/karmadactl: unit test logs In this commit, we unit test logs on completion for pods/containers, selectors, and member factory builder. Signed-off-by: Mohamed Awnallah --- pkg/karmadactl/logs/logs.go | 3 +- pkg/karmadactl/logs/logs_test.go | 123 +++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 pkg/karmadactl/logs/logs_test.go diff --git a/pkg/karmadactl/logs/logs.go b/pkg/karmadactl/logs/logs.go index 07ce22349..91e12a3d7 100644 --- a/pkg/karmadactl/logs/logs.go +++ b/pkg/karmadactl/logs/logs.go @@ -17,6 +17,7 @@ limitations under the License. package logs import ( + "errors" "fmt" "github.com/spf13/cobra" @@ -120,7 +121,7 @@ type CommandLogsOptions struct { // Complete ensures that options are valid and marshals them if necessary func (o *CommandLogsOptions) Complete(cmd *cobra.Command, args []string, f util.Factory) error { if o.Cluster == "" { - return fmt.Errorf("must specify a cluster") + return errors.New("must specify a cluster") } // print correct usage message when the given arguments are invalid diff --git a/pkg/karmadactl/logs/logs_test.go b/pkg/karmadactl/logs/logs_test.go new file mode 100644 index 000000000..e5e3ffd81 --- /dev/null +++ b/pkg/karmadactl/logs/logs_test.go @@ -0,0 +1,123 @@ +/* +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 logs + +import ( + "errors" + "strings" + "testing" + + "github.com/spf13/cobra" + kubectllogs "k8s.io/kubectl/pkg/cmd/logs" + cmdutil "k8s.io/kubectl/pkg/cmd/util" + + "github.com/karmada-io/karmada/pkg/karmadactl/util" +) + +type testFactory struct { + util.Factory +} + +func (t *testFactory) FactoryForMemberCluster(string) (cmdutil.Factory, error) { + return nil, errors.New("failed to create factory for member cluster") +} + +func TestCompleteLogOptions(t *testing.T) { + tests := []struct { + name string + cobraCmd *cobra.Command + args []string + f util.Factory + logsOpts *CommandLogsOptions + wantErr bool + errMsg string + }{ + { + name: "CompleteLogOptions_WithoutCluster_ClusterMustBeSpecified", + logsOpts: &CommandLogsOptions{Cluster: ""}, + wantErr: true, + errMsg: "must specify a cluster", + }, + { + name: "CompleteLogOptions_WithoutArgumentsAndSelector_GotLogsUsage", + cobraCmd: &cobra.Command{}, + logsOpts: &CommandLogsOptions{ + Cluster: "test-cluster", + KubectlLogsOptions: &kubectllogs.LogsOptions{ + Selector: "", + }, + }, + args: make([]string, 0), + wantErr: true, + errMsg: logsUsageErrStr, + }, + { + name: "CompleteLogOptions_WithArgumentsAndSelector_GotLogsUsage", + cobraCmd: &cobra.Command{}, + logsOpts: &CommandLogsOptions{ + Cluster: "test-cluster", + KubectlLogsOptions: &kubectllogs.LogsOptions{ + Selector: "env=test", + }, + }, + args: []string{"nginx"}, + wantErr: true, + errMsg: "only a selector (-l) or a POD name is allowed", + }, + { + name: "CompleteLogOptions_WithTwoOrMoreArgs_GotLogsUsage", + cobraCmd: &cobra.Command{}, + logsOpts: &CommandLogsOptions{ + Cluster: "test-cluster", + KubectlLogsOptions: &kubectllogs.LogsOptions{ + Selector: "env=test", + }, + }, + args: []string{"nginx-container", "nginx-container-2", "nginx-pod"}, + wantErr: true, + errMsg: logsUsageErrStr, + }, + { + name: "CompleteLogOptions_ReturnMemberClusterFactory_FailedToReturnMemberClusterFactory", + cobraCmd: &cobra.Command{}, + f: &testFactory{}, + logsOpts: &CommandLogsOptions{ + Cluster: "test-cluster", + KubectlLogsOptions: &kubectllogs.LogsOptions{ + Selector: "env=test", + }, + }, + args: []string{"nginx-container", "nginx-pod"}, + wantErr: true, + errMsg: "failed to create factory for member cluster", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := test.logsOpts.Complete(test.cobraCmd, test.args, test.f) + 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()) + } + }) + } +}