Skip to content

Commit

Permalink
🐛 regression - extracommands require a valid PROJECT file again
Browse files Browse the repository at this point in the history
* Handle failing on 3-alpha configs
* Set project version to stable version if the it is registered
* Added IsRegistered command to registry
* Add unit test for buildCmd
* Add unit test for IsRegistered
* Fix typo in test name: resgitry_test -> registry_test

Signed-off-by: jesus m. rodriguez <jesusr@redhat.com>
  • Loading branch information
jmrodri committed Apr 11, 2021
1 parent 84f357b commit 2442aa7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand Down Expand Up @@ -144,8 +145,21 @@ func newCLI(options ...Option) (*CLI, error) {
func (c *CLI) buildCmd() error {
c.cmd = c.newRootCmd()

var uve config.UnsupportedVersionError

// Get project version and plugin keys.
if err := c.getInfo(); err != nil {
switch err := c.getInfo(); {
case err == nil:
case errors.As(err, &uve) && uve.Version.Compare(config.Version{Number: 3, Stage: stage.Alpha}) == 0:
// Check if the corresponding stable version exists, set c.projectVersion and break
stableVersion := config.Version{
Number: uve.Version.Number,
}
if config.IsRegistered(stableVersion) {
// Use the stableVersion
c.projectVersion = stableVersion
}
default:
return err
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
goPluginV3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
)
Expand Down Expand Up @@ -91,6 +92,40 @@ var _ = Describe("CLI", func() {
}
})

Context("buildCmd", func() {
BeforeEach(func() {
projectFile := `domain: zeusville.com
layout: go.kubebuilder.io/v3
projectName: demo-zeus-operator
repo: github.com/jmrodri/demo-zeus-operator
resources:
- crdVersion: v1
group: test
kind: Test
version: v1
version: 3-alpha
plugins:
manifests.sdk.operatorframework.io/v2: {}
`
f, err := c.fs.FS.Create("PROJECT")
Expect(err).To(Not(HaveOccurred()))

_, err = f.WriteString(projectFile)
Expect(err).To(Not(HaveOccurred()))
})

When("reading a 3-alpha config", func() {
It("should succeed and set the projectVersion", func() {
err := c.buildCmd()
Expect(err).To(Not(HaveOccurred()))
Expect(c.projectVersion.Compare(
config.Version{
Number: 3,
Stage: stage.Stable})).To(Equal(0))
})
})
})

// TODO: test CLI.getInfoFromConfigFile using a mock filesystem

Context("getInfoFromConfig", func() {
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func Register(version Version, constructor func() Config) {
registry[version] = constructor
}

// IsRegistered returns true if the given version has been registered through Register
func IsRegistered(version Version) bool {
_, ok := registry[version]
return ok
}

// New creates Config instances from the previously registered implementations through Register
func New(version Version) (Config, error) {
if constructor, exists := registry[version]; exists {
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/resgistry_test.go → pkg/config/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ var _ = Describe("registry", func() {
})
})

Context("IsRegistered", func() {
It("should return true for registered constructors", func() {
Register(version, f)
Expect(IsRegistered(version)).To(BeTrue())
})
It("should fail for unregistered constructors", func() {
Expect(IsRegistered(version)).To(BeFalse())
})
})

Context("New", func() {
It("should use the registered constructors", func() {
registry[version] = f
Expand Down

0 comments on commit 2442aa7

Please sign in to comment.