Skip to content

Commit

Permalink
add more test for option (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci committed Aug 15, 2023
1 parent bee4509 commit fbb4f90
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 4 deletions.
4 changes: 0 additions & 4 deletions pkg/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,6 @@ func (opt *Options) validate() error {
return fmt.Errorf("invalid api-addr: %v", err)
}

if err != nil {
return fmt.Errorf("invalid api-url: %v", err)
}

// dirs
if opt.HomeDir == "" {
return fmt.Errorf("empty home-dir")
Expand Down
163 changes: 163 additions & 0 deletions pkg/option/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package option

import (
"github.com/megaease/easegress/v2/pkg/util/codectool"
"github.com/stretchr/testify/assert"

"os"
Expand All @@ -40,3 +41,165 @@ func TestLogOptions(t *testing.T) {
at.Equal(abs, options.AbsLogDir)
at.False(options.DisableAccessLog)
}

func TestOption(t *testing.T) {
assert := assert.New(t)
options := New()
assert.NoError(options.Parse())

// test YAML, and FlagUsages method
{
yamlStr := options.YAML()
assert.NotEmpty(yamlStr)

data, err := codectool.MarshalYAML(options)
assert.NoError(err)
assert.NotEmpty(data)
assert.Equal(yamlStr, string(data))

flagUsages := options.FlagUsages()
assert.NotEmpty(flagUsages)
assert.Equal(options.flags.FlagUsages(), flagUsages)
}

// check rename legacy cluster roles
{
options.flags.Parse([]string{"--cluster-role=writer"})
assert.Equal("writer", options.ClusterRole)
options.renameLegacyClusterRoles()
assert.Equal("primary", options.ClusterRole)

options.flags.Parse([]string{"--cluster-role=reader"})
assert.Equal("reader", options.ClusterRole)
options.renameLegacyClusterRoles()
assert.Equal("secondary", options.ClusterRole)
}

// test show version, always success parse
{
options.ShowVersion = true
assert.NoError(options.Parse())
options.ShowVersion = false
}

// test config file not exist, return error
{
options.ConfigFile = "not-exist"
assert.Error(options.Parse())
}

// check validate
{
assert.Nil(options.validate())

// invalid cluster name
func() {
name := options.ClusterName
defer func() {
options.ClusterName = name
}()

options.ClusterName = ""
assert.Error(options.validate())
options.ClusterName = "!!!not-valid-name"
assert.Error(options.validate())
}()

// invalid cluster role, secondary can not force new cluster
func() {
role := options.ClusterRole
force := options.ForceNewCluster
defer func() {
options.ClusterRole = role
options.ForceNewCluster = force
}()

options.ClusterRole = "secondary"
options.ForceNewCluster = true
assert.Error(options.validate())
}()

// invalid cluster role, secondary need primary listen peer urls
func() {
role := options.ClusterRole
urls := options.Cluster.PrimaryListenPeerURLs
defer func() {
options.ClusterRole = role
options.Cluster.PrimaryListenPeerURLs = urls
}()

options.ClusterRole = "secondary"
options.Cluster.PrimaryListenPeerURLs = []string{}
assert.Error(options.validate())
}()

// invalid cluster role
func() {
role := options.ClusterRole
defer func() {
options.ClusterRole = role
}()

options.ClusterRole = "not-exist"
assert.Error(options.validate())
}()

// invalid home dir
func() {
dir := options.HomeDir
defer func() {
options.HomeDir = dir
}()

options.HomeDir = ""
assert.Error(options.validate())
}()

// invalid data dir
func() {
dir := options.DataDir
defer func() {
options.DataDir = dir
}()

options.DataDir = ""
assert.Error(options.validate())
}()

// invalid tls cert file
func() {
tlsFlag := options.TLS
defer func() {
options.TLS = tlsFlag
}()

options.TLS = true
assert.Error(options.validate())
}()

// invalid name
func() {
name := options.Name
apiAddr := options.APIAddr
defer func() {
options.Name = name
options.APIAddr = apiAddr
}()

options.Name = ""
options.APIAddr = ""
assert.Error(options.validate())
}()

assert.Nil(options.validate())
}

// other methods
{
assert.Equal("eg-default-name=http:https://localhost:2380", options.InitialClusterToString())
assert.Equal([]string{"http:https://localhost:2380"}, options.GetPeerURLs())
urls, err := options.GetFirstAdvertiseClientURL()
assert.Nil(err)
assert.Equal("http:https://localhost:2379", urls)
}
}

0 comments on commit fbb4f90

Please sign in to comment.