Skip to content

Commit

Permalink
Merge pull request #5872 from mohamedawnallah/unitTestLogsKarmadactl
Browse files Browse the repository at this point in the history
pkg/karmadactl: unit test logs
  • Loading branch information
karmada-bot authored Nov 26, 2024
2 parents 6ceb3a9 + 543a23d commit 5c3594f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/karmadactl/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package logs

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
Expand Down
123 changes: 123 additions & 0 deletions pkg/karmadactl/logs/logs_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
}
}

0 comments on commit 5c3594f

Please sign in to comment.