diff options
| author | KubeEdge Bot <48982446+kubeedge-bot@users.noreply.github.com> | 2024-07-16 15:18:58 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 15:18:58 +0800 |
| commit | 3142f6f084852f04d6f4450a6c9c87b66445f660 (patch) | |
| tree | 0240f7cec3171739b6926099ed9caa6a9c5964a2 | |
| parent | Merge pull request #5687 from 1Shubham7/helper-unit-tests (diff) | |
| parent | fixing errors (diff) | |
| download | kubeedge-3142f6f084852f04d6f4450a6c9c87b66445f660.tar.gz | |
Merge pull request #5708 from 1Shubham7/debug-tests
Test Coverage for `debug` package
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/collect.go | 4 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/collect_test.go | 145 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/debug_test.go | 46 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/diagnose_test.go | 137 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/get_flags.go | 6 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/get_flags_test.go | 187 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/debug/get_test.go | 246 |
7 files changed, 758 insertions, 13 deletions
diff --git a/keadm/cmd/keadm/app/cmd/debug/collect.go b/keadm/cmd/keadm/app/cmd/debug/collect.go index 1aee5f14a..74a2d6768 100644 --- a/keadm/cmd/keadm/app/cmd/debug/collect.go +++ b/keadm/cmd/keadm/app/cmd/debug/collect.go @@ -46,14 +46,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..028cbc303 --- /dev/null +++ b/keadm/cmd/keadm/app/cmd/debug/collect_test.go @@ -0,0 +1,145 @@ +/* +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..8709320d2 --- /dev/null +++ b/keadm/cmd/keadm/app/cmd/debug/debug_test.go @@ -0,0 +1,46 @@ +/* +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_test.go b/keadm/cmd/keadm/app/cmd/debug/diagnose_test.go new file mode 100644 index 000000000..d4832bfd7 --- /dev/null +++ b/keadm/cmd/keadm/app/cmd/debug/diagnose_test.go @@ -0,0 +1,137 @@ +/* +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..7051ba6eb 100644 --- a/keadm/cmd/keadm/app/cmd/debug/get_flags.go +++ b/keadm/cmd/keadm/app/cmd/debug/get_flags.go @@ -101,13 +101,13 @@ type HumanPrintFlags struct { // AllowedFormats returns more customized formating options func (f *HumanPrintFlags) AllowedFormats() []string { - return []string{"wide"} + return []string{FormatTypeWIDE} } // ToPrinter receives an outputFormat and returns a printer capable of // handling human-readable output. func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) { - if len(outputFormat) > 0 && outputFormat != "wide" { + if len(outputFormat) > 0 && outputFormat != FormatTypeWIDE { return nil, genericclioptions.NoCompatiblePrinterError{Options: f, AllowedFormats: f.AllowedFormats()} } @@ -130,7 +130,7 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint Kind: f.Kind, WithKind: showKind, NoHeaders: f.NoHeaders, - Wide: outputFormat == "wide", + Wide: outputFormat == FormatTypeWIDE, WithNamespace: f.WithNamespace, ColumnLabels: columnLabels, ShowLabels: showLabels, 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..b0fa7701b --- /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 = FormatTypeWIDE + 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 = FormatTypeWIDE + *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{FormatTypeWIDE} + 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 := FormatTypeWIDE + 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..e99668a41 100644 --- a/keadm/cmd/keadm/app/cmd/debug/get_test.go +++ b/keadm/cmd/keadm/app/cmd/debug/get_test.go @@ -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) }) } } |
