-
Notifications
You must be signed in to change notification settings - Fork 567
CNTRLPLANE-3899: Add shared cmd/ unit tests for create nodepool agent #9162
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:shared_nodepool
Aug 20, 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
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,122 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1" | ||
| "github.com/openshift/hypershift/cmd/nodepool/core" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| // newAgentNodePool builds the NodePool fixture shared by the UpdateNodePool test cases. | ||
| func newAgentNodePool() *hyperv1.NodePool { | ||
| return &hyperv1.NodePool{ | ||
| Spec: hyperv1.NodePoolSpec{ | ||
| Arch: string(hyperv1.ArchitectureAMD64), | ||
| Platform: hyperv1.NodePoolPlatform{ | ||
| Type: hyperv1.AgentPlatform, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestNewCreateCommand(t *testing.T) { | ||
| coreOpts := &core.CreateNodePoolOptions{} | ||
| cmd := NewCreateCommand(coreOpts) | ||
|
|
||
| if cmd.Use != "agent" { | ||
| t.Errorf("expected Use to be %q, got %q", "agent", cmd.Use) | ||
| } | ||
| if cmd.RunE == nil { | ||
| t.Error("expected RunE to be set") | ||
| } | ||
| flag := cmd.Flag("agentLabelSelector") | ||
| if flag == nil { | ||
| t.Fatal("expected agentLabelSelector flag to be registered") | ||
| } | ||
| if flag.DefValue != "" { | ||
| t.Errorf("expected agentLabelSelector default to be empty, got %q", flag.DefValue) | ||
| } | ||
| } | ||
|
|
||
| func TestUpdateNodePool(t *testing.T) { | ||
| for _, testCase := range []struct { | ||
| name string | ||
| labelSelector string | ||
| expectedSelector *metav1.LabelSelector | ||
| expectPanic bool | ||
| }{ | ||
| { | ||
| name: "When an empty selector is provided, it should produce an empty label selector", | ||
| labelSelector: "", | ||
| expectedSelector: &metav1.LabelSelector{ | ||
| MatchLabels: map[string]string{}, | ||
| MatchExpressions: []metav1.LabelSelectorRequirement{}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "When an equality-based selector is provided, it should set matchLabels", | ||
| labelSelector: "size=large", | ||
| expectedSelector: &metav1.LabelSelector{ | ||
| MatchLabels: map[string]string{"size": "large"}, | ||
| MatchExpressions: []metav1.LabelSelectorRequirement{}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "When a set-based selector is provided, it should set matchLabels and matchExpressions", | ||
| labelSelector: "size=large,zone notin (az1,az2)", | ||
| expectedSelector: &metav1.LabelSelector{ | ||
| MatchLabels: map[string]string{"size": "large"}, | ||
| MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
| { | ||
| Key: "zone", | ||
| Operator: metav1.LabelSelectorOpNotIn, | ||
| Values: []string{"az1", "az2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "When an invalid label selector is provided, it should panic", | ||
| labelSelector: "!!!invalid!!!", | ||
| expectPanic: true, | ||
| }, | ||
| } { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| ctx := t.Context() | ||
|
|
||
| platformOpts := &AgentPlatformCreateOptions{ | ||
| AgentLabelSelector: testCase.labelSelector, | ||
| } | ||
| nodePool := newAgentNodePool() | ||
|
|
||
| if testCase.expectPanic { | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Fatal("expected panic for invalid label selector, got none") | ||
| } | ||
| }() | ||
| _ = platformOpts.UpdateNodePool(ctx, nodePool, nil, nil) | ||
| return | ||
| } | ||
|
|
||
| if err := platformOpts.UpdateNodePool(ctx, nodePool, nil, nil); err != nil { | ||
| t.Fatalf("failed to update nodepool: %v", err) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(testCase.expectedSelector, nodePool.Spec.Platform.Agent.AgentLabelSelector); diff != "" { | ||
| t.Errorf("unexpected label selector (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
|
mehabhalodiya marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| func TestType(t *testing.T) { | ||
| opts := &AgentPlatformCreateOptions{} | ||
| if opts.Type() != hyperv1.AgentPlatform { | ||
| t.Errorf("expected Type() to return %q, got %q", hyperv1.AgentPlatform, opts.Type()) | ||
| } | ||
| } | ||
|
mehabhalodiya marked this conversation as resolved.
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reasonable seam to make the command testable — no change requested. One note: the
UpdateNodePool-driven tests reach through this helper mainly to re-assert coverage that a directTestUpdateNodePoolalready gives. If you consolidate the tests as suggested,TestNewCreateCommandonly needs to cover command wiring (Use/RunE/flag registration), so this split stays justified and minimal.