summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShubham Singh <shubhammahar1306@gmail.com>2024-07-05 20:12:09 +0530
committerShubham Singh <shubhammahar1306@gmail.com>2024-07-05 20:12:09 +0530
commit08d2f4a7e5f01da6e76b2e0547d634ac2f14499b (patch)
treeb45cd4b2b63cfde2057d329da3d1c628cbe5ca8a
parentMerge pull request #5682 from 1Shubham7/cloudstream-test-1 (diff)
downloadkubeedge-08d2f4a7e5f01da6e76b2e0547d634ac2f14499b.tar.gz
added tests covering the debug package
Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/collect.go17
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/collect_test.go142
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/debug_test.go43
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/diagnose.go13
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/diagnose_test.go134
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/get_flags.go40
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/get_flags_test.go187
-rw-r--r--keadm/cmd/keadm/app/cmd/debug/get_test.go248
8 files changed, 793 insertions, 31 deletions
diff --git a/keadm/cmd/keadm/app/cmd/debug/collect.go b/keadm/cmd/keadm/app/cmd/debug/collect.go
index 65a56801c..d8b4485ba 100644
--- a/keadm/cmd/keadm/app/cmd/debug/collect.go
+++ b/keadm/cmd/keadm/app/cmd/debug/collect.go
@@ -1,3 +1,16 @@
+/*
+Copyright 2024 The KubeEdge 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 debug
import (
@@ -46,14 +59,12 @@ func NewCollect() *cobra.Command {
return cmd
}
-// dd flags
+// add flags
func addCollectOtherFlags(cmd *cobra.Command, collectOptions *common.CollectOptions) {
cmd.Flags().StringVarP(&collectOptions.Config, common.EdgecoreConfig, "c", collectOptions.Config,
fmt.Sprintf("Specify configuration file, default is %s", common.EdgecoreConfigPath))
cmd.Flags().BoolVarP(&collectOptions.Detail, "detail", "d", false,
"Whether to print internal log output")
- //cmd.Flags().StringVar(&collectOptions.OutputPath, "output-path", collectOptions.OutputPath,
- // "Cache data and store data compression packages in a directory that default to the current directory")
cmd.Flags().StringVarP(&collectOptions.OutputPath, "output-path", "o", collectOptions.OutputPath,
"Cache data and store data compression packages in a directory that default to the current directory")
cmd.Flags().StringVarP(&collectOptions.LogPath, "log-path", "l", util.KubeEdgeLogPath,
diff --git a/keadm/cmd/keadm/app/cmd/debug/collect_test.go b/keadm/cmd/keadm/app/cmd/debug/collect_test.go
new file mode 100644
index 000000000..38060f9cb
--- /dev/null
+++ b/keadm/cmd/keadm/app/cmd/debug/collect_test.go
@@ -0,0 +1,142 @@
+/*
+Copyright 2024 The KubeEdge 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 debug
+
+import (
+ "testing"
+
+ "github.com/spf13/cobra"
+ "github.com/stretchr/testify/assert"
+
+ "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common"
+ "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/util"
+)
+
+func TestCollect_NewCollect(t *testing.T) {
+ assert := assert.New(t)
+ cmd := NewCollect()
+
+ assert.NotNil(cmd)
+ assert.Equal("collect", cmd.Use)
+ assert.Equal("Obtain all the data of the current node", cmd.Short)
+ assert.Equal(edgecollectLongDescription, cmd.Long)
+ assert.Equal(edgecollectExample, cmd.Example)
+ assert.NotNil(cmd.Run)
+
+ subcommands := cmd.Commands()
+ assert.Empty(subcommands)
+
+ expectedFlags := []struct {
+ flagName string
+ shorthand string
+ defaultVal string
+ expectedVal string
+ }{
+ {
+ flagName: "config",
+ shorthand: "c",
+ defaultVal: common.EdgecoreConfigPath,
+ expectedVal: common.EdgecoreConfigPath,
+ },
+ {
+ flagName: "detail",
+ shorthand: "d",
+ defaultVal: "false",
+ expectedVal: "false",
+ },
+ {
+ flagName: "output-path",
+ shorthand: "o",
+ defaultVal: ".",
+ expectedVal: ".",
+ },
+ {
+ flagName: "log-path",
+ shorthand: "l",
+ defaultVal: util.KubeEdgeLogPath,
+ expectedVal: util.KubeEdgeLogPath,
+ },
+ }
+
+ for _, tt := range expectedFlags {
+ t.Run(tt.flagName, func(t *testing.T) {
+ flag := cmd.Flag(tt.flagName)
+ assert.Equal(tt.flagName, flag.Name)
+ assert.Equal(tt.defaultVal, flag.DefValue)
+ assert.Equal(tt.expectedVal, flag.Value.String())
+ assert.Equal(tt.shorthand, flag.Shorthand)
+ })
+ }
+}
+
+func TestCollect_AddCollectOtherFlags(t *testing.T) {
+ assert := assert.New(t)
+ cmd := &cobra.Command{}
+
+ co := newCollectOptions()
+ addCollectOtherFlags(cmd, co)
+
+ expectedFlags := []struct {
+ flagName string
+ shorthand string
+ defaultVal string
+ expectedVal string
+ }{
+ {
+ flagName: "config",
+ shorthand: "c",
+ defaultVal: common.EdgecoreConfigPath,
+ expectedVal: common.EdgecoreConfigPath,
+ },
+ {
+ flagName: "detail",
+ shorthand: "d",
+ defaultVal: "false",
+ expectedVal: "false",
+ },
+ {
+ flagName: "output-path",
+ shorthand: "o",
+ defaultVal: ".",
+ expectedVal: ".",
+ },
+ {
+ flagName: "log-path",
+ shorthand: "l",
+ defaultVal: util.KubeEdgeLogPath,
+ expectedVal: util.KubeEdgeLogPath,
+ },
+ }
+
+ for _, tt := range expectedFlags {
+ t.Run(tt.flagName, func(t *testing.T) {
+ flag := cmd.Flag(tt.flagName)
+ assert.Equal(tt.flagName, flag.Name)
+ assert.Equal(tt.defaultVal, flag.DefValue)
+ assert.Equal(tt.expectedVal, flag.Value.String())
+ assert.Equal(tt.shorthand, flag.Shorthand)
+ })
+ }
+}
+
+func TestCollect_NewCollectOptions(t *testing.T) {
+ assert := assert.New(t)
+
+ co := newCollectOptions()
+ assert.NotNil(co)
+
+ assert.Equal(common.EdgecoreConfigPath, co.Config)
+ assert.Equal(".", co.OutputPath)
+ assert.Equal(false, co.Detail)
+}
diff --git a/keadm/cmd/keadm/app/cmd/debug/debug_test.go b/keadm/cmd/keadm/app/cmd/debug/debug_test.go
new file mode 100644
index 000000000..81f54f1ad
--- /dev/null
+++ b/keadm/cmd/keadm/app/cmd/debug/debug_test.go
@@ -0,0 +1,43 @@
+/*
+Copyright 2024 The KubeEdge 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 debug
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestNewEdgeDebug(t *testing.T) {
+ assert := assert.New(t)
+
+ cmd := NewEdgeDebug()
+
+ assert.NotNil(cmd)
+ assert.Equal("debug", cmd.Use)
+ assert.Equal(edgeDebugShortDescription, cmd.Short)
+ assert.Equal(edgeDebugLongDescription, cmd.Long)
+
+ expectedSubCommands := []string{"get", "diagnose", "check", "collect"}
+ for _, subCmd := range expectedSubCommands {
+ found := false
+ for _, cmd := range cmd.Commands() {
+ if cmd.Use == subCmd {
+ found = true
+ break
+ }
+ }
+ assert.True(found)
+ }
+}
diff --git a/keadm/cmd/keadm/app/cmd/debug/diagnose.go b/keadm/cmd/keadm/app/cmd/debug/diagnose.go
index ce5cf2e56..982bbe1d9 100644
--- a/keadm/cmd/keadm/app/cmd/debug/diagnose.go
+++ b/keadm/cmd/keadm/app/cmd/debug/diagnose.go
@@ -1,3 +1,16 @@
+/*
+Copyright 2024 The KubeEdge 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 debug
import (
diff --git a/keadm/cmd/keadm/app/cmd/debug/diagnose_test.go b/keadm/cmd/keadm/app/cmd/debug/diagnose_test.go
new file mode 100644
index 000000000..e6b7b8845
--- /dev/null
+++ b/keadm/cmd/keadm/app/cmd/debug/diagnose_test.go
@@ -0,0 +1,134 @@
+/*
+Copyright 2024 The KubeEdge 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 debug
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common"
+)
+
+func TestNewDiagnose(t *testing.T) {
+ assert := assert.New(t)
+ cmd := NewDiagnose()
+
+ assert.NotNil(cmd)
+ assert.Equal("diagnose", cmd.Use)
+ assert.Equal(edgeDiagnoseShortDescription, cmd.Short)
+ assert.Equal(edgeDiagnoseLongDescription, cmd.Long)
+ assert.Equal(edgeDiagnoseExample, cmd.Example)
+
+ subcommands := cmd.Commands()
+ assert.NotNil(subcommands)
+}
+
+func TestNewSubDiagnose(t *testing.T) {
+ assert := assert.New(t)
+
+ cases := []struct {
+ use string
+ expectedDefValue map[string]string
+ expectedShorthand map[string]string
+ expectedUsage map[string]string
+ }{
+ {
+ use: common.ArgDiagnoseNode,
+ expectedDefValue: map[string]string{
+ common.EdgecoreConfig: common.EdgecoreConfigPath,
+ },
+ expectedShorthand: map[string]string{
+ common.EdgecoreConfig: "c",
+ },
+ expectedUsage: map[string]string{
+ common.EdgecoreConfig: fmt.Sprintf("Specify configuration file, default is %s", common.EdgecoreConfigPath),
+ },
+ },
+ {
+ use: common.ArgDiagnosePod,
+ expectedDefValue: map[string]string{
+ "namespace": "default",
+ },
+ expectedShorthand: map[string]string{
+ "namespace": "n",
+ },
+ expectedUsage: map[string]string{
+ "namespace": "specify namespace",
+ },
+ },
+ {
+ use: common.ArgDiagnoseInstall,
+ expectedDefValue: map[string]string{
+ "dns-ip": "",
+ "domain": "",
+ "ip": "",
+ "cloud-hub-server": "",
+ },
+ expectedShorthand: map[string]string{
+ "dns-ip": "D",
+ "domain": "d",
+ "ip": "i",
+ "cloud-hub-server": "s",
+ },
+ expectedUsage: map[string]string{
+ "dns-ip": "specify test dns server ip",
+ "domain": "specify test domain",
+ "ip": "specify test ip",
+ "cloud-hub-server": "specify cloudhub server",
+ },
+ },
+ }
+
+ for _, test := range cases {
+ t.Run(test.use, func(t *testing.T) {
+ diagnoseObj := Diagnose{
+ Use: test.use,
+ Desc: fmt.Sprintf("Diagnose %s", test.use),
+ }
+ cmd := NewSubDiagnose(diagnoseObj)
+
+ assert.NotNil(cmd)
+ assert.Equal(diagnoseObj.Use, cmd.Use)
+ assert.Equal(diagnoseObj.Desc, cmd.Short)
+
+ flags := cmd.Flags()
+ assert.NotNil(flags)
+
+ for flagName, expectedDefValue := range test.expectedDefValue {
+ t.Run(flagName, func(t *testing.T) {
+ flag := flags.Lookup(flagName)
+ assert.NotNil(flag)
+
+ assert.Equal(expectedDefValue, flag.DefValue)
+ assert.Equal(test.expectedShorthand[flagName], flag.Shorthand)
+ assert.Equal(test.expectedUsage[flagName], flag.Usage)
+ })
+ }
+ })
+ }
+}
+
+func TestNewDiagnoseOptions(t *testing.T) {
+ assert := assert.New(t)
+
+ do := NewDiagnoseOptions()
+ assert.NotNil(do)
+
+ assert.Equal("default", do.Namespace)
+ assert.Equal(common.EdgecoreConfigPath, do.Config)
+ assert.Equal("", do.CheckOptions.IP)
+ assert.Equal(3, do.CheckOptions.Timeout)
+}
diff --git a/keadm/cmd/keadm/app/cmd/debug/get_flags.go b/keadm/cmd/keadm/app/cmd/debug/get_flags.go
index 0ed31e2a9..fbfa5fbc9 100644
--- a/keadm/cmd/keadm/app/cmd/debug/get_flags.go
+++ b/keadm/cmd/keadm/app/cmd/debug/get_flags.go
@@ -104,6 +104,26 @@ func (f *HumanPrintFlags) AllowedFormats() []string {
return []string{"wide"}
}
+// NewHumanPrintFlags returns flags associated with
+// human-readable printing, with default values set.
+func NewHumanPrintFlags() *HumanPrintFlags {
+ showLabels := false
+ sortBy := ""
+ showKind := false
+ columnLabels := []string{}
+
+ return &HumanPrintFlags{
+ NoHeaders: false,
+ WithNamespace: false,
+ ColumnLabels: &columnLabels,
+
+ Kind: schema.GroupKind{},
+ ShowLabels: &showLabels,
+ SortBy: &sortBy,
+ ShowKind: &showKind,
+ }
+}
+
// ToPrinter receives an outputFormat and returns a printer capable of
// handling human-readable output.
func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
@@ -139,26 +159,6 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
return p, nil
}
-// NewHumanPrintFlags returns flags associated with
-// human-readable printing, with default values set.
-func NewHumanPrintFlags() *HumanPrintFlags {
- showLabels := false
- sortBy := ""
- showKind := false
- columnLabels := []string{}
-
- return &HumanPrintFlags{
- NoHeaders: false,
- WithNamespace: false,
- ColumnLabels: &columnLabels,
-
- Kind: schema.GroupKind{},
- ShowLabels: &showLabels,
- SortBy: &sortBy,
- ShowKind: &showKind,
- }
-}
-
// EnsureWithNamespace ensures that humanreadable flags return
// a printer capable of printing with a "namespace" column.
func (f *PrintFlags) EnsureWithNamespace() error {
diff --git a/keadm/cmd/keadm/app/cmd/debug/get_flags_test.go b/keadm/cmd/keadm/app/cmd/debug/get_flags_test.go
new file mode 100644
index 000000000..065eceec6
--- /dev/null
+++ b/keadm/cmd/keadm/app/cmd/debug/get_flags_test.go
@@ -0,0 +1,187 @@
+/*
+Copyright 2024 The Kubernetes 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.
+@CHANGELOG
+KubeEdge Authors: To create keadm debug get function like kubectl get,
+This file is derived from K8S Kubectl code with reduced set of methods
+Changes done are
+1. Package edged got some functions from "k8s.io/kubectl/pkg/cmd/get/get_flags.go"
+and made some variant
+*/
+
+package debug
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "k8s.io/apimachinery/pkg/runtime/schema"
+ "k8s.io/cli-runtime/pkg/genericclioptions"
+)
+
+func AllowedFormats(t *testing.T) {
+ assert := assert.New(t)
+ printFlags := &PrintFlags{
+ JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
+ NamePrintFlags: genericclioptions.NewNamePrintFlags(""),
+ TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(),
+ HumanReadableFlags: NewHumanPrintFlags(),
+ }
+
+ formats := printFlags.AllowedFormats()
+ expectedFormats := append(printFlags.JSONYamlPrintFlags.AllowedFormats(), printFlags.HumanReadableFlags.AllowedFormats()...)
+
+ assert.Equal(expectedFormats, formats)
+}
+
+func TestToPrinter(t *testing.T) {
+ assert := assert.New(t)
+
+ printFlags := &PrintFlags{
+ JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
+ NamePrintFlags: genericclioptions.NewNamePrintFlags(""),
+ TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(),
+ HumanReadableFlags: NewHumanPrintFlags(),
+ NoHeaders: new(bool),
+ OutputFormat: new(string),
+ }
+
+ *printFlags.OutputFormat = "json"
+ printer, err := printFlags.ToPrinter()
+ assert.NoError(err)
+ assert.NotNil(printer)
+
+ *printFlags.OutputFormat = "yaml"
+ printer, err = printFlags.ToPrinter()
+ assert.NoError(err)
+ assert.NotNil(printer)
+
+ *printFlags.OutputFormat = "wide"
+ printer, err = printFlags.ToPrinter()
+ assert.NoError(err)
+ assert.NotNil(printer)
+
+ *printFlags.OutputFormat = "unsupported"
+ printer, err = printFlags.ToPrinter()
+ assert.Error(err)
+ assert.Nil(printer)
+
+ *printFlags.OutputFormat = "wide"
+ *printFlags.NoHeaders = true
+ printer, err = printFlags.ToPrinter()
+ assert.NoError(err)
+ assert.NotNil(printer)
+
+ *printFlags.NoHeaders = false
+
+ *printFlags.OutputFormat = ""
+ printer, err = printFlags.ToPrinter()
+ assert.NoError(err)
+ assert.NotNil(printer)
+}
+
+func TestNewGetPrintFlags(t *testing.T) {
+ assert := assert.New(t)
+
+ printFlags := NewGetPrintFlags()
+ assert.NotNil(printFlags)
+
+ assert.IsType(&genericclioptions.JSONYamlPrintFlags{}, printFlags.JSONYamlPrintFlags)
+ assert.IsType(&genericclioptions.NamePrintFlags{}, printFlags.NamePrintFlags)
+ assert.IsType(&genericclioptions.KubeTemplatePrintFlags{}, printFlags.TemplateFlags)
+ assert.IsType(&HumanPrintFlags{}, printFlags.HumanReadableFlags)
+
+ assert.NotNil(printFlags.OutputFormat)
+ assert.Equal("", *printFlags.OutputFormat)
+
+ assert.NotNil(printFlags.NoHeaders)
+ assert.Equal(false, *printFlags.NoHeaders)
+
+ assert.Equal(printFlags.HumanReadableFlags.NoHeaders, false)
+ assert.Equal(printFlags.HumanReadableFlags.WithNamespace, false)
+ assert.Equal(*printFlags.HumanReadableFlags.ShowLabels, false)
+ assert.Equal(*printFlags.HumanReadableFlags.SortBy, "")
+ assert.Equal(*printFlags.HumanReadableFlags.ShowKind, false)
+ assert.Empty(*printFlags.HumanReadableFlags.ColumnLabels)
+}
+
+func TestHumanPrintFlags_AllowedFormats(t *testing.T) {
+ assert := assert.New(t)
+
+ humanPrintFlags := &HumanPrintFlags{}
+ formats := humanPrintFlags.AllowedFormats()
+
+ expectedFormats := []string{"wide"}
+ assert.Equal(expectedFormats, formats)
+}
+
+func TestNewHumanPrintFlags(t *testing.T) {
+ assert := assert.New(t)
+
+ humanPrintFlags := NewHumanPrintFlags()
+ assert.NotNil(humanPrintFlags)
+
+ assert.Equal(humanPrintFlags.NoHeaders, false)
+ assert.Equal(humanPrintFlags.WithNamespace, false)
+ assert.Equal(humanPrintFlags.ColumnLabels, &[]string{})
+ assert.Equal(humanPrintFlags.Kind, schema.GroupKind{})
+ assert.Equal(*humanPrintFlags.ShowLabels, false)
+ assert.Equal(*humanPrintFlags.SortBy, "")
+ assert.Equal(*humanPrintFlags.ShowKind, false)
+}
+
+func TestHumanPrintFlags_ToPrinter(t *testing.T) {
+ assert := assert.New(t)
+
+ humanPrintFlags := &HumanPrintFlags{
+ ShowKind: new(bool),
+ ShowLabels: new(bool),
+ SortBy: new(string),
+ ColumnLabels: new([]string),
+ NoHeaders: false,
+ Kind: schema.GroupKind{},
+ WithNamespace: false,
+ }
+
+ outputFormat := "wide"
+ printer, err := humanPrintFlags.ToPrinter(outputFormat)
+ assert.NoError(err)
+ assert.NotNil(printer)
+
+ outputFormat = ""
+ printer, err = humanPrintFlags.ToPrinter(outputFormat)
+ assert.NoError(err)
+ assert.NotNil(printer)
+
+ outputFormat = "unsupported"
+ printer, err = humanPrintFlags.ToPrinter(outputFormat)
+ assert.Error(err)
+ assert.Nil(printer)
+}
+
+func TestHumanPrintFlags_EnsureWithNamespace(t *testing.T) {
+ assert := assert.New(t)
+ humanPrintFlags := &HumanPrintFlags{}
+
+ err := humanPrintFlags.EnsureWithNamespace()
+
+ assert.NoError(err)
+ assert.Equal(humanPrintFlags.WithNamespace, true)
+}
+
+func Test_EnsureWithNamespace(t *testing.T) {
+ assert := assert.New(t)
+
+ printFlags := NewGetPrintFlags()
+ err := printFlags.EnsureWithNamespace()
+
+ assert.NoError(err)
+}
diff --git a/keadm/cmd/keadm/app/cmd/debug/get_test.go b/keadm/cmd/keadm/app/cmd/debug/get_test.go
index 31e01d264..9fa92e98d 100644
--- a/keadm/cmd/keadm/app/cmd/debug/get_test.go
+++ b/keadm/cmd/keadm/app/cmd/debug/get_test.go
@@ -1,5 +1,5 @@
/*
-Copyright 2020 The KubeEdge Authors.
+Copyright 2024 The KubeEdge 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
@@ -14,11 +14,177 @@ limitations under the License.
package debug
import (
- "reflect"
+ "errors"
"testing"
+
+ "github.com/spf13/cobra"
+ "github.com/stretchr/testify/assert"
+
+ edgecoreCfg "github.com/kubeedge/kubeedge/pkg/apis/componentconfig/edgecore/v1alpha2"
)
+func TestNewCmdDebugGet(t *testing.T) {
+ assert := assert.New(t)
+ cmd := NewCmdDebugGet()
+
+ assert.NotNil(cmd)
+ assert.Equal("get", cmd.Use)
+ assert.Equal("Display one or many resources", cmd.Short)
+ assert.Equal(debugGetLong, cmd.Long)
+ assert.Equal(debugGetExample, cmd.Example)
+
+ assert.NotNil(cmd.Run)
+
+ getOption := NewGetOptions()
+
+ flag := cmd.Flag("namespace")
+ assert.NotNil(flag)
+ assert.Equal(getOption.Namespace, flag.DefValue)
+ assert.Equal("namespace", flag.Name)
+ assert.Equal("n", flag.Shorthand)
+
+ flag = cmd.Flag("output")
+ assert.NotNil(flag)
+ assert.Equal(*getOption.PrintFlags.OutputFormat, flag.DefValue)
+ assert.Equal("output", flag.Name)
+ assert.Equal("o", flag.Shorthand)
+
+ flag = cmd.Flag("selector")
+ assert.NotNil(flag)
+ assert.Equal(getOption.LabelSelector, flag.DefValue)
+ assert.Equal("selector", flag.Name)
+ assert.Equal("l", flag.Shorthand)
+
+ flag = cmd.Flag("edgedb-path")
+ assert.NotNil(flag)
+ assert.Equal(getOption.DataPath, flag.DefValue)
+ assert.Equal("edgedb-path", flag.Name)
+ assert.Equal("p", flag.Shorthand)
+
+ flag = cmd.Flag("all-namespaces")
+ assert.NotNil(flag)
+ assert.Equal(getOption.AllNamespace, flag.DefValue == "true")
+ assert.Equal("all-namespaces", flag.Name)
+ assert.Equal("A", flag.Shorthand)
+}
+
+func TestCheckErr(t *testing.T) {
+ assert := assert.New(t)
+
+ mockHandler := func(msg string, exitCode int) {
+ t.Errorf("handleErr should not be called for nil error")
+ }
+ CheckErr(nil, mockHandler)
+
+ expectedMsg := "Test error"
+ expectedExitCode := DefaultErrorExitCode
+ expectedErr := errors.New(expectedMsg)
+
+ var handledMsg string
+ var handledExitCode int
+
+ mockHandler = func(msg string, exitCode int) {
+ handledMsg = msg
+ handledExitCode = exitCode
+ }
+
+ CheckErr(expectedErr, mockHandler)
+
+ assert.Equal(expectedMsg, handledMsg)
+ assert.Equal(expectedExitCode, handledExitCode)
+}
+
+func TestAddGetOtherFlags(t *testing.T) {
+ getOption := NewGetOptions()
+ cmd := &cobra.Command{}
+
+ addGetOtherFlags(cmd, getOption)
+
+ assert := assert.New(t)
+
+ flag := cmd.Flag("namespace")
+ assert.NotNil(flag)
+ assert.Equal(getOption.Namespace, flag.DefValue)
+ assert.Equal("namespace", flag.Name)
+ assert.Equal("n", flag.Shorthand)
+
+ flag = cmd.Flag("output")
+ assert.NotNil(flag)
+ assert.Equal(*getOption.PrintFlags.OutputFormat, flag.DefValue)
+ assert.Equal("output", flag.Name)
+ assert.Equal("o", flag.Shorthand)
+
+ flag = cmd.Flag("selector")
+ assert.NotNil(flag)
+ assert.Equal(getOption.LabelSelector, flag.DefValue)
+ assert.Equal("selector", flag.Name)
+ assert.Equal("l", flag.Shorthand)
+
+ flag = cmd.Flag("edgedb-path")
+ assert.NotNil(flag)
+ assert.Equal(getOption.DataPath, flag.DefValue)
+ assert.Equal("edgedb-path", flag.Name)
+ assert.Equal("p", flag.Shorthand)
+
+ flag = cmd.Flag("all-namespaces")
+ assert.NotNil(flag)
+ assert.Equal(getOption.AllNamespace, flag.DefValue == "true")
+ assert.Equal("all-namespaces", flag.Name)
+ assert.Equal("A", flag.Shorthand)
+}
+
+func TestNewGetOptions(t *testing.T) {
+ assert := assert.New(t)
+ opts := NewGetOptions()
+
+ assert.NotNil(opts)
+ assert.Equal(opts.Namespace, "default")
+ assert.Equal(opts.DataPath, edgecoreCfg.DataBaseDataSource)
+ assert.Equal(opts.PrintFlags, NewGetPrintFlags())
+}
+
+func TestIsAllowedFormat(t *testing.T) {
+ assert := assert.New(t)
+ getOptions := NewGetOptions()
+
+ tests := []struct {
+ format string
+ expected bool
+ }{
+ {
+ "yaml",
+ true,
+ },
+ {
+ "json",
+ true,
+ },
+ {
+ "wide",
+ true,
+ },
+ {
+ "xml",
+ false,
+ },
+ {
+ "",
+ false,
+ },
+ {
+ "plain",
+ false,
+ },
+ }
+
+ for _, test := range tests {
+ stdResult := getOptions.IsAllowedFormat(test.format)
+ assert.Equal(test.expected, stdResult)
+ }
+}
+
func TestSplitSelectorParameters(t *testing.T) {
+ assert := assert.New(t)
type args struct {
args string
}
@@ -37,22 +203,37 @@ func TestSplitSelectorParameters(t *testing.T) {
{Key: "key3", Value: "value3", Exist: true},
},
wantErr: false,
- }, {
+ },
+ {
name: "testWithoutLabel",
args: args{args: "key1"},
want: []Selector{},
wantErr: false,
- }, {
+ },
+ {
name: "testWithEmptyValue",
args: args{args: "key1!="},
want: []Selector{{Key: "key1", Value: "", Exist: false}},
wantErr: false,
- }, {
+ },
+ {
name: "testWithMoreThanOneLabel",
args: args{args: "key1=value1=,key2=value2"},
want: nil,
wantErr: true,
},
+ {
+ name: "testEmptyString",
+ args: args{args: ""},
+ want: []Selector{},
+ wantErr: false,
+ },
+ {
+ name: "testOnlyCommas",
+ args: args{args: ",,,"},
+ want: []Selector{},
+ wantErr: false,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -61,9 +242,60 @@ func TestSplitSelectorParameters(t *testing.T) {
t.Errorf("SplitSelectorParameters() error = %v, wantErr %v", err, tt.wantErr)
return
}
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("SplitSelectorParameters() = %v, want %v", got, tt.want)
- }
+ assert.Equal(tt.want, got)
+ })
+ }
+}
+
+func TestIsExistName(t *testing.T) {
+ tests := []struct {
+ name string
+ resNames []string
+ key string
+ expected bool
+ }{
+ {
+ name: "Key exists in resNames",
+ resNames: []string{"pod1", "pod2", "pod3"},
+ key: "pod1",
+ expected: true,
+ },
+ {
+ name: "Key does not exist in resNames",
+ resNames: []string{"pod1", "pod2", "pod3"},
+ key: "pod4",
+ expected: false,
+ },
+ {
+ name: "Empty resNames",
+ resNames: []string{},
+ key: "pod1",
+ expected: false,
+ },
+ {
+ name: "Empty key",
+ resNames: []string{"pod1", "pod2", "pod3"},
+ key: "",
+ expected: false,
+ },
+ {
+ name: "Key is substring of one element in resNames",
+ resNames: []string{"pod123", "pod2", "pod3"},
+ key: "pod1",
+ expected: false,
+ },
+ {
+ name: "ResNames contains special characters",
+ resNames: []string{"pod@123", "pod#2", "pod$3"},
+ key: "pod@123",
+ expected: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ result := isExistName(test.resNames, test.key)
+ assert.Equal(t, test.expected, result)
})
}
}