From 3d4ae11b4e5bb816b7e23d82683553cc5de9cb5c Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sat, 30 May 2020 00:57:57 +0100 Subject: [PATCH] fix: validate plugins : allow many versions of the same plugin --- pkg/cli/cli.go | 10 +++++----- pkg/cli/cli_suite_test.go | 29 ----------------------------- pkg/cli/cli_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 34 deletions(-) delete mode 100644 pkg/cli/cli_suite_test.go diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 1d9dacfd567..610f5bf6e24 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -351,12 +351,12 @@ func validatePlugins(plugins ...plugin.Base) error { pluginName, projectVersion, err) } } - // Check for duplicate plugin names. Names outside of a version can - // conflict because multiple project versions of a plugin may exist. - if _, seen := pluginNameSet[pluginName]; seen { - return fmt.Errorf("two plugins have the same name: %q", pluginName) + // Check for duplicate plugin keys. + pluginKey := plugin.KeyFor(p) + if _, seen := pluginNameSet[pluginKey]; seen { + return fmt.Errorf("two plugins have the same key: %q", pluginKey) } - pluginNameSet[pluginName] = struct{}{} + pluginNameSet[pluginKey] = struct{}{} } return nil } diff --git a/pkg/cli/cli_suite_test.go b/pkg/cli/cli_suite_test.go deleted file mode 100644 index 90b9178ce67..00000000000 --- a/pkg/cli/cli_suite_test.go +++ /dev/null @@ -1,29 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package cli - -import ( - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -func TestCLI(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "CLI Suite") -} diff --git a/pkg/cli/cli_test.go b/pkg/cli/cli_test.go index ef4fd86b923..72defc69895 100644 --- a/pkg/cli/cli_test.go +++ b/pkg/cli/cli_test.go @@ -17,12 +17,19 @@ limitations under the License. package cli import ( + "testing" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "sigs.k8s.io/kubebuilder/pkg/plugin" ) +func TestCLI(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "CLI Suite") +} + var _ = Describe("CLI", func() { Describe("resolvePluginsByKey", func() { plugins := makePluginsForKeys( @@ -78,6 +85,28 @@ var _ = Describe("CLI", func() { Expect(err).To(MatchError(`plugin key "foo" matches more than one known plugin`)) }) }) + + Describe("Check if has duplicate plugins", func() { + It("should work successfully when a plugin has many versions", func() { + plugins := makePluginsForKeys( + "foo.example.com/v1.0.0", + "foo.example.com/v2.0.0", + "foo.example.com/v3.0.0", + ) + + err := validatePlugins(plugins...) + Expect(err).NotTo(HaveOccurred()) + }) + It("should fail when found more than one plugin with the same name and version", func() { + plugins := makePluginsForKeys( + "foo.example.com/v1.0.0", + "foo.example.com/v1.0.0", + ) + + err := validatePlugins(plugins...) + Expect(err).To(HaveOccurred()) + }) + }) }) type mockPlugin struct {