From fbb4f90e77bac142eda90ae11ad45f93e1de086e Mon Sep 17 00:00:00 2001 From: su chen Date: Tue, 15 Aug 2023 15:54:49 +0800 Subject: [PATCH] add more test for option (#1060) --- pkg/option/option.go | 4 - pkg/option/option_test.go | 163 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 4 deletions(-) diff --git a/pkg/option/option.go b/pkg/option/option.go index 02172b4ded..cd12c22d77 100644 --- a/pkg/option/option.go +++ b/pkg/option/option.go @@ -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") diff --git a/pkg/option/option_test.go b/pkg/option/option_test.go index 8450074244..c0d9fbf95b 100644 --- a/pkg/option/option_test.go +++ b/pkg/option/option_test.go @@ -18,6 +18,7 @@ package option import ( + "github.com/megaease/easegress/v2/pkg/util/codectool" "github.com/stretchr/testify/assert" "os" @@ -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://localhost:2380", options.InitialClusterToString()) + assert.Equal([]string{"http://localhost:2380"}, options.GetPeerURLs()) + urls, err := options.GetFirstAdvertiseClientURL() + assert.Nil(err) + assert.Equal("http://localhost:2379", urls) + } +}