-
Notifications
You must be signed in to change notification settings - Fork 567
CNTRLPLANE-3890: Add product-cli unit tests for HCP create cluster #9107
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
Merged
openshift-merge-bot
merged 1 commit into
openshift:main
from
mehabhalodiya:unittest_create
Jul 31, 2026
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/gomega" | ||
|
|
||
| "github.com/openshift/hypershift/cmd/cluster/agent" | ||
| "github.com/openshift/hypershift/cmd/cluster/core" | ||
| "github.com/openshift/hypershift/support/certs" | ||
| "github.com/openshift/hypershift/support/testutil" | ||
| "github.com/openshift/hypershift/test/integration/framework" | ||
|
|
||
| utilrand "k8s.io/apimachinery/pkg/util/rand" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| func TestNewCreateCommand(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| test func(t *testing.T) | ||
| }{ | ||
| { | ||
| name: "When Agent create command is created, it should have 'agent' as use", | ||
| test: func(t *testing.T) { | ||
| g := NewWithT(t) | ||
| opts := core.DefaultOptions() | ||
| cmd := NewCreateCommand(opts) | ||
| g.Expect(cmd.Use).To(Equal("agent")) | ||
| }, | ||
| }, | ||
| { | ||
| name: "When Agent create command is created, it should mark agent-namespace as required", | ||
| test: func(t *testing.T) { | ||
| g := NewWithT(t) | ||
| opts := core.DefaultOptions() | ||
| cmd := NewCreateCommand(opts) | ||
|
|
||
| agentNSFlag := cmd.Flag("agent-namespace") | ||
| g.Expect(agentNSFlag).ToNot(BeNil()) | ||
| g.Expect(agentNSFlag.Annotations).To(HaveKey(cobra.BashCompOneRequiredFlag)) | ||
| g.Expect(agentNSFlag.Annotations[cobra.BashCompOneRequiredFlag]).To(ContainElement("true")) | ||
| }, | ||
| }, | ||
| { | ||
| name: "When Agent create command is added to a parent, pull-secret should be inherited", | ||
| test: func(t *testing.T) { | ||
| g := NewWithT(t) | ||
| opts := core.DefaultOptions() | ||
|
|
||
| parent := &cobra.Command{Use: "cluster"} | ||
| core.BindOptions(opts, parent.PersistentFlags()) | ||
|
|
||
| cmd := NewCreateCommand(opts) | ||
| parent.AddCommand(cmd) | ||
|
|
||
| pullSecretFlag := cmd.InheritedFlags().Lookup("pull-secret") | ||
| g.Expect(pullSecretFlag).ToNot(BeNil(), "pull-secret should be inherited from parent command") | ||
| }, | ||
| }, | ||
| { | ||
| name: "When Agent create command is created, it should register exactly the expected flags", | ||
| test: func(t *testing.T) { | ||
| g := NewWithT(t) | ||
| opts := core.DefaultOptions() | ||
| cmd := NewCreateCommand(opts) | ||
|
|
||
| expectedFlags := []string{ | ||
| "agent-namespace", | ||
| "agentLabelSelector", | ||
| "api-server-address", | ||
| } | ||
| var actualFlags []string | ||
| cmd.Flags().VisitAll(func(f *pflag.Flag) { | ||
| actualFlags = append(actualFlags, f.Name) | ||
| }) | ||
| sort.Strings(actualFlags) | ||
| g.Expect(actualFlags).To(Equal(expectedFlags)) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| tt.test(t) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCreateCluster(t *testing.T) { | ||
| utilrand.Seed(1234567890) | ||
| certs.UnsafeSeed(1234567890) | ||
| ctx := framework.InterruptableContext(t.Context()) | ||
|
|
||
| tempDir := t.TempDir() | ||
|
|
||
| pullSecretFile := filepath.Join(tempDir, "pull-secret.json") | ||
| if err := os.WriteFile(pullSecretFile, []byte(`fake`), 0600); err != nil { | ||
| t.Fatalf("failed to write pullSecret: %v", err) | ||
| } | ||
|
|
||
| for _, testCase := range []struct { | ||
| name string | ||
| args []string | ||
| }{ | ||
| { | ||
| name: "When minimal flags are provided, it should render correctly", | ||
| args: []string{ | ||
| "--api-server-address=fakeAddress", | ||
| "--render-sensitive", | ||
| "--name=example", | ||
| "--pull-secret=" + pullSecretFile, | ||
| }, | ||
| }, | ||
| } { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| flags := pflag.NewFlagSet(testCase.name, pflag.ContinueOnError) | ||
| coreOpts := core.DefaultOptions() | ||
| core.BindOptions(coreOpts, flags) | ||
| agentOpts := agent.DefaultOptions() | ||
| agent.BindOptions(agentOpts, flags) | ||
| if err := flags.Parse(testCase.args); err != nil { | ||
| t.Fatalf("failed to parse flags: %v", err) | ||
| } | ||
|
|
||
| tempDir := t.TempDir() | ||
| manifestsFile := filepath.Join(tempDir, "manifests.yaml") | ||
| coreOpts.Render = true | ||
| coreOpts.RenderInto = manifestsFile | ||
|
|
||
| if err := core.CreateCluster(ctx, coreOpts, agentOpts); err != nil { | ||
| t.Fatalf("failed to create cluster: %v", err) | ||
| } | ||
|
|
||
| manifests, err := os.ReadFile(manifestsFile) | ||
| if err != nil { | ||
| t.Fatalf("failed to read manifests file: %v", err) | ||
| } | ||
| testutil.CompareWithFixture(t, manifests) | ||
| }) | ||
| } | ||
| } | ||
135 changes: 135 additions & 0 deletions
135
...ixture_TestCreateCluster_When_minimal_flags_are_provided__it_should_render_correctly.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.