-
Notifications
You must be signed in to change notification settings - Fork 1.6k
🌱 (cli; alpha commands): Add unit tests for alpha commands #4931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cmallikarjunah
wants to merge
1
commit into
kubernetes-sigs:master
Choose a base branch
from
cmallikarjunah:issue-4925-add-UTs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+789
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
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 update | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/common" | ||
"sigs.k8s.io/kubebuilder/v4/pkg/config" | ||
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml" | ||
v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3" | ||
) | ||
|
||
func TestCommand(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "update") | ||
} | ||
|
||
var _ = Describe("Prepare for internal update", func() { | ||
var ( | ||
tmpDir string | ||
workDir string | ||
projectFile string | ||
err error | ||
) | ||
|
||
BeforeEach(func() { | ||
workDir, err = os.Getwd() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
tmpDir, err = os.MkdirTemp("", "kubebuilder-prepare-test") | ||
Expect(err).ToNot(HaveOccurred()) | ||
err = os.Chdir(tmpDir) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
projectFile = filepath.Join(tmpDir, yaml.DefaultPath) | ||
|
||
config.Register(config.Version{Number: 3}, func() config.Config { | ||
return &v3.Cfg{Version: config.Version{Number: 3}, CliVersion: "1.0.0"} | ||
}) | ||
|
||
gock.New("https://api.github.com"). | ||
Get("/repos/kubernetes-sigs/kubebuilder/releases/latest"). | ||
Reply(200). | ||
JSON(map[string]string{ | ||
"tag_name": "v1.1.0", | ||
}) | ||
}) | ||
|
||
AfterEach(func() { | ||
err = os.Chdir(workDir) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
err = os.RemoveAll(tmpDir) | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer gock.Off() | ||
}) | ||
|
||
Context("Prepare", func() { | ||
DescribeTable("should succeed for valid options", | ||
func(options *Update) { | ||
|
||
const version = `version: "3"` | ||
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed()) | ||
|
||
result := options.Prepare() | ||
Expect(result).To(BeNil()) | ||
Expect(options.Prepare()).To(Succeed()) | ||
Expect(options.FromVersion).To(Equal("v1.0.0")) | ||
Expect(options.ToVersion).To(Equal("v1.1.0")) | ||
}, | ||
Entry("options", &Update{FromVersion: "v1.0.0", ToVersion: "v1.1.0", FromBranch: "test"}), | ||
Entry("options", &Update{FromVersion: "1.0.0", ToVersion: "1.1.0", FromBranch: "test"}), | ||
Entry("options", &Update{FromVersion: "v1.0.0", ToVersion: "v1.1.0"}), | ||
Entry("options", &Update{}), | ||
) | ||
|
||
DescribeTable("Should fail to prepare if project path is undetermined", | ||
func(options *Update) { | ||
err = options.Prepare() | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).Should(ContainSubstring("failed to determine project path")) | ||
}, | ||
Entry("options", &Update{FromVersion: "v1.0.0", ToVersion: "v1.1.0", FromBranch: "test"}), | ||
) | ||
|
||
DescribeTable("Should fail if PROJECT config could not be loaded", | ||
func(options *Update) { | ||
const version = "" | ||
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed()) | ||
|
||
err := options.Prepare() | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).Should(ContainSubstring("failed to load PROJECT config")) | ||
}, | ||
Entry("options", &Update{FromVersion: "v1.0.0", ToVersion: "v1.1.0", FromBranch: "test"}), | ||
) | ||
|
||
DescribeTable("Should fail if FromVersion cannot be determined", | ||
func(options *Update) { | ||
|
||
config.Register(config.Version{Number: 3}, func() config.Config { | ||
return &v3.Cfg{Version: config.Version{Number: 3}} | ||
}) | ||
|
||
const version = `version: "3"` | ||
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed()) | ||
|
||
// TODO: Update test. err is Nil as default branch in Prepare() is being hardcoded to "main". | ||
// Hence, the defineFromVersion condition will never fail. | ||
// err := options.Prepare() | ||
// Expect(err).ToNot(HaveOccurred()) | ||
// Expect(err.Error()).Should(ContainSubstring("failed to determine the version")) | ||
Comment on lines
+128
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure I understood this one. |
||
Expect(options.FromVersion).To(BeEquivalentTo("")) | ||
}, | ||
Entry("options", &Update{}), | ||
) | ||
}) | ||
|
||
Context("DefineFromVersion", func() { | ||
|
||
DescribeTable("Should succeed when branch or CliVersion in Project config is present", | ||
func(options *Update) { | ||
|
||
const version = `version: "3"` | ||
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed()) | ||
|
||
config, err := common.LoadProjectConfig(tmpDir) | ||
Expect(err).ToNot(HaveOccurred()) | ||
fromVersion, err := options.defineFromVersion(config) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(fromVersion).To(BeEquivalentTo("v1.0.0")) | ||
}, | ||
Entry("options", &Update{FromVersion: ""}), | ||
Entry("options", &Update{FromBranch: "test"}), | ||
Entry("options", &Update{FromVersion: "1.0.0", FromBranch: "test"}), | ||
) | ||
DescribeTable("Should fail when branch and CliVersion in Project config are absent", | ||
func(options *Update) { | ||
|
||
config.Register(config.Version{Number: 3}, func() config.Config { | ||
return &v3.Cfg{Version: config.Version{Number: 3}} | ||
}) | ||
|
||
const version = `version: "3"` | ||
Expect(os.WriteFile(projectFile, []byte(version), 0o644)).To(Succeed()) | ||
|
||
config, err := common.LoadProjectConfig(tmpDir) | ||
Expect(err).NotTo(HaveOccurred()) | ||
fromVersion, err := options.defineFromVersion(config) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("no version specified in PROJECT file")) | ||
Expect(fromVersion).To(Equal("")) | ||
}, | ||
Entry("options", &Update{FromVersion: ""}), | ||
Entry("options", &Update{FromVersion: "v1.0.0"}), | ||
) | ||
}) | ||
|
||
Context("DefineToVersion", func() { | ||
|
||
DescribeTable("Should succeed.", | ||
func(options *Update) { | ||
toVersion := options.defineToVersion() | ||
Expect(toVersion).To(BeEquivalentTo("v1.1.0")) | ||
}, | ||
Entry("options", &Update{ToVersion: "1.1.0"}), | ||
Entry("options", &Update{ToVersion: "v1.1.0"}), | ||
Entry("options", &Update{}), | ||
) | ||
|
||
}) | ||
|
||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.