From d660e1e4c0c6cabd200f84268e05d718e097c768 Mon Sep 17 00:00:00 2001 From: 1aal Date: Mon, 20 Nov 2023 15:59:23 +0800 Subject: [PATCH 1/5] add index UT --- pkg/cmd/addon/index_test.go | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 pkg/cmd/addon/index_test.go diff --git a/pkg/cmd/addon/index_test.go b/pkg/cmd/addon/index_test.go new file mode 100644 index 000000000..4547eac49 --- /dev/null +++ b/pkg/cmd/addon/index_test.go @@ -0,0 +1,80 @@ +package addon + +import ( + "bytes" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "k8s.io/cli-runtime/pkg/genericiooptions" +) + +var _ = Describe("index test", func() { + var streams genericiooptions.IOStreams + var out *bytes.Buffer + const ( + defaultIndexName = "kubeblocks" + testIndexName = "kb-other" + testIndexURL = "unknown" + ) + BeforeEach(func() { + streams, _, out, _ = genericiooptions.NewTestIOStreams() + }) + It("test index cmd", func() { + Expect(newIndexCmd(streams)).ShouldNot(BeNil()) + }) + + It("test index add cmd", func() { + cmd := newIndexAddCmd() + Expect(cmd).ShouldNot(BeNil()) + Expect(addIndex([]string{defaultIndexName, testIndexURL})).Should(HaveOccurred()) + Expect(addIndex([]string{testIndexName, testIndexURL})).Should(HaveOccurred()) + }) + + It("test index delete cmd", func() { + Expect(newIndexDeleteCmd()).ShouldNot(BeNil()) + Expect(deleteIndex(testIndexName)).Should(HaveOccurred()) + }) + + It("test index list cmd", func() { + Expect(newIndexListCmd(streams)).ShouldNot(BeNil()) + + Expect(listIndexes(out)).Should(Succeed()) + expect := `INDEX URL +kubeblocks https://github.com/apecloud/block-index.git +` + Expect(out.String()).Should(Equal(expect)) + }) + + It("test index update cmd", func() { + Expect(newIndexUpdateCmd(streams)).ShouldNot(BeNil()) + + o := &updateOption{ + names: make([]string, 0), + all: false, + IOStreams: streams, + } + Expect(o.validate([]string{defaultIndexName})).Should(Succeed()) + Expect(o.validate([]string{testIndexName})).Should(HaveOccurred()) + Expect(o.validate([]string{})).Should(HaveOccurred()) + o.all = true + Expect(o.validate([]string{})).Should(Succeed()) + Expect(o.run()).Should(Succeed()) + }) + + It("test index name", func() { + cases := []struct { + name string + success bool + }{ + {"kubeblocks", true}, {"KubeBlocks123", true}, {"Kube_Blocks", true}, {"kube-blocks", true}, {"12345", true}, + {"kube blocks", false}, {"kube@blocks", false}, {"", false}, {"kubekubekubeblocks", false}, + } + + for _, t := range cases { + if t.success { + Expect(IsValidIndexName(t.name)).Should(BeTrue()) + } else { + Expect(IsValidIndexName(t.name)).Should(BeFalse()) + } + } + }) +}) From 622b2ae7b697ebff4c761d83800f92ff88e1fe24 Mon Sep 17 00:00:00 2001 From: 1aal Date: Mon, 20 Nov 2023 15:59:57 +0800 Subject: [PATCH 2/5] adjust valid index name --- pkg/cmd/addon/index.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/addon/index.go b/pkg/cmd/addon/index.go index cec8cad38..434416b23 100644 --- a/pkg/cmd/addon/index.go +++ b/pkg/cmd/addon/index.go @@ -187,7 +187,7 @@ func newIndexUpdateCmd(streams genericiooptions.IOStreams) *cobra.Command { // IsValidIndexName validates if an index name contains invalid characters func IsValidIndexName(name string) bool { - var validNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) + var validNamePattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,16}$`) return validNamePattern.MatchString(name) } From cd21e4f86275918fc79cd00a7d08c8228f58ac7c Mon Sep 17 00:00:00 2001 From: 1aal Date: Mon, 20 Nov 2023 21:32:02 +0800 Subject: [PATCH 3/5] make lint --- pkg/cmd/addon/index_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/cmd/addon/index_test.go b/pkg/cmd/addon/index_test.go index 4547eac49..a5172c9de 100644 --- a/pkg/cmd/addon/index_test.go +++ b/pkg/cmd/addon/index_test.go @@ -2,8 +2,10 @@ package addon import ( "bytes" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "k8s.io/cli-runtime/pkg/genericiooptions" ) From 2dad5821e9d7f7ce733060e822b1e91a284609dd Mon Sep 17 00:00:00 2001 From: 1aal Date: Mon, 20 Nov 2023 21:43:15 +0800 Subject: [PATCH 4/5] fix workflows fail --- pkg/cmd/addon/index_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/cmd/addon/index_test.go b/pkg/cmd/addon/index_test.go index a5172c9de..e3e456ce5 100644 --- a/pkg/cmd/addon/index_test.go +++ b/pkg/cmd/addon/index_test.go @@ -19,7 +19,9 @@ var _ = Describe("index test", func() { ) BeforeEach(func() { streams, _, out, _ = genericiooptions.NewTestIOStreams() + Expect(addDefaultIndex()).Should(Succeed()) }) + It("test index cmd", func() { Expect(newIndexCmd(streams)).ShouldNot(BeNil()) }) @@ -37,6 +39,7 @@ var _ = Describe("index test", func() { }) It("test index list cmd", func() { + Expect(newIndexListCmd(streams)).ShouldNot(BeNil()) Expect(listIndexes(out)).Should(Succeed()) From bbb3b287093d996d7b3405138a56d8990da95b5f Mon Sep 17 00:00:00 2001 From: 1aal Date: Mon, 20 Nov 2023 21:44:08 +0800 Subject: [PATCH 5/5] fix lint --- pkg/cmd/addon/index_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/cmd/addon/index_test.go b/pkg/cmd/addon/index_test.go index e3e456ce5..d3045fc1c 100644 --- a/pkg/cmd/addon/index_test.go +++ b/pkg/cmd/addon/index_test.go @@ -39,9 +39,7 @@ var _ = Describe("index test", func() { }) It("test index list cmd", func() { - Expect(newIndexListCmd(streams)).ShouldNot(BeNil()) - Expect(listIndexes(out)).Should(Succeed()) expect := `INDEX URL kubeblocks https://github.com/apecloud/block-index.git