Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <[email protected]>
  • Loading branch information
qiujian16 committed Jul 24, 2023
1 parent 9542855 commit 0b7a4fa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
7 changes: 7 additions & 0 deletions pkg/common/options/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions pkg/common/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion pkg/common/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 0b7a4fa

Please sign in to comment.