Skip to content

Commit

Permalink
fix: validate plugins : allow many versions of the same plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila Macedo committed May 29, 2020
1 parent 587b7a3 commit dae83e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
9 changes: 5 additions & 4 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,13 @@ func validatePlugins(plugins ...plugin.Base) error {
pluginName, projectVersion, err)
}
}
// Check for duplicate plugin names. Names outside of a version can
// Check if has duplicate plugins. 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)
nameVersion := fmt.Sprintf("%v/%v", p.Name(), p.Version())
if _, seen := pluginNameSet[nameVersion]; seen {
return fmt.Errorf("two plugins have the same name and version: %q", nameVersion )
}
pluginNameSet[pluginName] = struct{}{}
pluginNameSet[nameVersion] = struct{}{}
}
return nil
}
Expand Down
29 changes: 0 additions & 29 deletions pkg/cli/cli_suite_test.go

This file was deleted.

28 changes: 28 additions & 0 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ package cli
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"

"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(
Expand Down Expand Up @@ -78,6 +84,28 @@ var _ = Describe("CLI", func() {
Expect(err).To(MatchError(`plugin key "foo" matches more than one known plugin`))
})
})

Describe("validate 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[0], plugins[1], plugins[2])
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[0], plugins[1])
Expect(err).To(HaveOccurred())
})
})
})

type mockPlugin struct {
Expand Down

0 comments on commit dae83e5

Please sign in to comment.