From 0b7a4fa4e1722fadddc6edbb2af36f65fa1ceefc Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Mon, 24 Jul 2023 13:20:29 +0800 Subject: [PATCH] Add unit tests Signed-off-by: Jian Qiu --- pkg/common/options/agent.go | 7 ++++++ pkg/common/options/options.go | 7 ------ pkg/common/options/options_test.go | 36 +++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/pkg/common/options/agent.go b/pkg/common/options/agent.go index 6131ec813..63ee288f5 100644 --- a/pkg/common/options/agent.go +++ b/pkg/common/options/agent.go @@ -18,6 +18,13 @@ import ( "open-cluster-management.io/ocm/pkg/registration/spoke/registration" ) +const ( + // spokeAgentNameLength is the length of the spoke agent name which is generated automatically + spokeAgentNameLength = 5 + // defaultSpokeComponentNamespace is the default namespace in which the spoke agent is deployed + defaultSpokeComponentNamespace = "open-cluster-management-agent" +) + // AgentOptions is the common agent options type AgentOptions struct { CommoOpts *Options diff --git a/pkg/common/options/options.go b/pkg/common/options/options.go index 09dbf6811..a985ed9dd 100644 --- a/pkg/common/options/options.go +++ b/pkg/common/options/options.go @@ -8,13 +8,6 @@ import ( "k8s.io/apimachinery/pkg/version" ) -const ( - // spokeAgentNameLength is the length of the spoke agent name which is generated automatically - spokeAgentNameLength = 5 - // defaultSpokeComponentNamespace is the default namespace in which the spoke agent is deployed - defaultSpokeComponentNamespace = "open-cluster-management-agent" -) - type Options struct { CmdConfig *controllercmd.ControllerCommandConfig Burst int diff --git a/pkg/common/options/options_test.go b/pkg/common/options/options_test.go index cb75f89ec..7eb503f29 100644 --- a/pkg/common/options/options_test.go +++ b/pkg/common/options/options_test.go @@ -2,6 +2,9 @@ package options import ( "context" + "github.com/openshift/library-go/pkg/controller/controllercmd" + "github.com/spf13/cobra" + "open-cluster-management.io/ocm/pkg/version" "os" "path" "testing" @@ -92,7 +95,7 @@ func TestComplete(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { // setup kube client - objects := []runtime.Object{} + var objects []runtime.Object if c.secret != nil { objects = append(objects, c.secret) } @@ -219,3 +222,34 @@ func TestGetOrGenerateClusterAgentNames(t *testing.T) { }) } } + +func TestNewOptions(t *testing.T) { + opts := NewOptions() + cmd := &cobra.Command{ + Use: "test", + Short: "test Controller", + Run: func(cmd *cobra.Command, args []string) { + _ = cmd.Help() + os.Exit(1) + }, + } + + opts.NewControllerCommandConfig("test", version.Get(), func(ctx context.Context, controllerCtx *controllercmd.ControllerContext) error { + return nil + }) + + opts.AddFlags(cmd.Flags()) + if err := cmd.Flags().Set("kube-api-qps", "10"); err != nil { + t.Fatal(err) + } + if err := cmd.Flags().Set("kube-api-burst", "20"); err != nil { + t.Fatal(err) + + } + if err := cmd.Flags().Set("disable-leader-election", "true"); err != nil { + t.Fatal(err) + } + if err := cmd.Flags().Set("unsupported-flag", "true"); err == nil { + t.Errorf("Should return err") + } +}