Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pkg/plugins/golang/go_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,42 @@ limitations under the License.
package golang

import (
"errors"
"sort"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("GoVersion", func() {
Context("String", func() {
It("patch is not empty", func() {
v := GoVersion{major: 1, minor: 1, patch: 1}
Expect(v.String()).To(Equal("go1.1.1"))
})
It("preRelease is not empty", func() {
v := GoVersion{major: 1, minor: 1, prerelease: "-alpha"}
Expect(v.String()).To(Equal("go1.1-alpha"))
})
It("default", func() {
v := GoVersion{major: 1, minor: 1}
Expect(v.String()).To(Equal("go1.1"))
})
})

Context("MustParse", func() {
It("succeeds", func() {
v := GoVersion{major: 1, minor: 1, patch: 1}
Expect(MustParse("go1.1.1")).To(Equal(v))
})
It("panics", func() {
triggerPanic := func() {
MustParse("go1.a")
}
Expect(triggerPanic).To(PanicWith(errors.New("invalid version string")))
})
})

Context("parse", func() {
var v GoVersion

Expand Down Expand Up @@ -129,6 +158,24 @@ var _ = Describe("GoVersion", func() {
})
})

var _ = Describe("ValidateGoVersion", func() {
DescribeTable("should return no error for valid/supported go versions", func(minVersion, maxVersion GoVersion) {
Expect(ValidateGoVersion(minVersion, maxVersion)).To(Succeed())
},
Entry("for minVersion: 1.1.1 and maxVersion: 2000.1.1", GoVersion{major: 1, minor: 1, patch: 1},
GoVersion{major: 2000, minor: 1, patch: 1}),
Entry("for minVersion: 1.1.1 and maxVersion: 1.2000.2000", GoVersion{major: 1, minor: 1, patch: 1},
GoVersion{major: 1, minor: 2000, patch: 1}),
)

DescribeTable("should return error for invalid/unsupported go versions", func(minVersion, maxVersion GoVersion) {
Expect(ValidateGoVersion(minVersion, maxVersion)).NotTo(Succeed())
},
Entry("for invalid min and maxVersions", GoVersion{major: 2, minor: 2, patch: 2},
GoVersion{major: 1, minor: 1, patch: 1}),
)
})

var _ = Describe("checkGoVersion", func() {
var (
goVerMin GoVersion
Expand Down
11 changes: 11 additions & 0 deletions pkg/plugins/golang/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ var _ = Describe("Options", func() {
} else {
Expect(res.Path).To(Equal(path.Join(cfg.GetRepository(), "api", gvk.Version)))
}
} else if len(options.ExternalAPIPath) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to cover those options
To know more about see: https://kubebuilder.io/reference/using_an_external_resource

Also, you can check sample values in testdata samples like:
https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4/PROJECT#L66C1-L79C14

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can do that in follow ups :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @camilamacedo86 I will take look into this :-)

Expect(res.Path).To(Equal("testPath"))
} else {
// Core-resources have a path despite not having an API/Webhook but they are not tested here
Expect(res.Path).To(Equal(""))
Expand All @@ -104,6 +106,12 @@ var _ = Describe("Options", func() {
} else {
Expect(res.Webhooks.IsEmpty()).To(BeTrue())
}

if len(options.ExternalAPIPath) > 0 {
Expect(res.External).To(BeTrue())
Expect(res.Domain).To(Equal("test.io"))
}

Expect(res.QualifiedGroup()).To(Equal(gvk.Group + "." + gvk.Domain))
Expect(res.PackageName()).To(Equal(gvk.Group))
Expect(res.ImportAlias()).To(Equal(gvk.Group + gvk.Version))
Expand All @@ -112,6 +120,9 @@ var _ = Describe("Options", func() {
Entry("when updating nothing", Options{}),
Entry("when updating the plural", Options{Plural: "mates"}),
Entry("when updating the Controller", Options{DoController: true}),
Entry("when updating with External API Path", Options{ExternalAPIPath: "testPath", ExternalAPIDomain: "test.io"}),
Entry("when updating the API with setting webhooks params",
Options{DoAPI: true, DoDefaulting: true, DoValidation: true, DoConversion: true}),
)

DescribeTable("should use core apis",
Expand Down
111 changes: 111 additions & 0 deletions pkg/plugins/golang/repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2025 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 golang

import (
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("golang:repository", func() {
var (
tmpDir string
oldDir string
)

BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "repo-test")
Expect(err).NotTo(HaveOccurred())
oldDir, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(os.Chdir(tmpDir)).To(Succeed())
})

AfterEach(func() {
Expect(os.Chdir(oldDir)).To(Succeed())
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})

When("go.mod exists", func() {
BeforeEach(func() {
// Simulate `go mod edit -json` output by writing a go.mod file and using go commands
Expect(os.WriteFile("go.mod", []byte("module github.com/example/repo\n"), 0o644)).To(Succeed())
})

It("findGoModulePath returns the module path", func() {
path, err := findGoModulePath()
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal("github.com/example/repo"))
})

It("FindCurrentRepo returns the module path", func() {
path, err := FindCurrentRepo()
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal("github.com/example/repo"))
})
})

When("go.mod does not exist", func() {
It("findGoModulePath returns error", func() {
got, err := findGoModulePath()
Expect(err).To(HaveOccurred())
Expect(got).To(Equal(""))
})

It("FindCurrentRepo tries to init a module and returns the path or a helpful error", func() {
path, err := FindCurrentRepo()
if err != nil {
Expect(path).To(Equal(""))
Expect(err.Error()).To(ContainSubstring("could not determine repository path"))
} else {
Expect(path).NotTo(BeEmpty())
}
})
})

When("go mod command fails with exec.ExitError", func() {
var origPath string

BeforeEach(func() {
// Move go binary out of PATH to force exec error
origPath = os.Getenv("PATH")
// Set PATH to empty so "go" cannot be found
Expect(os.Setenv("PATH", "")).To(Succeed())
})

AfterEach(func() {
Expect(os.Setenv("PATH", origPath)).To(Succeed())
})

It("findGoModulePath returns error with stderr message", func() {
got, err := findGoModulePath()
Expect(err).To(HaveOccurred())
Expect(err.Error()).NotTo(BeEmpty())
Expect(got).To(Equal(""))
})

It("FindCurrentRepo returns error with stderr message", func() {
got, err := FindCurrentRepo()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("could not determine repository path"))
Expect(got).To(Equal(""))
})
})
})
Loading