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
9 changes: 7 additions & 2 deletions cmd/nodepool/agent/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func NewAgentPlatformCreateOptions(_ *cobra.Command) *AgentPlatformCreateOptions
}

func NewCreateCommand(coreOpts *core.CreateNodePoolOptions) *cobra.Command {
cmd, platformOpts := newCreateCommandWithOpts()
cmd.RunE = coreOpts.CreateRunFunc(platformOpts)
return cmd
}

func newCreateCommandWithOpts() (*cobra.Command, *AgentPlatformCreateOptions) {

Copy link
Copy Markdown
Member

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 direct TestUpdateNodePool already gives. If you consolidate the tests as suggested, TestNewCreateCommand only needs to cover command wiring (Use/RunE/flag registration), so this split stays justified and minimal.

cmd := &cobra.Command{
Use: "agent",
Short: "Creates basic functional NodePool resources for Agent platform",
Expand All @@ -35,9 +41,8 @@ func NewCreateCommand(coreOpts *core.CreateNodePoolOptions) *cobra.Command {

platformOpts := NewAgentPlatformCreateOptions(cmd)
cmd.Flags().StringVar(&platformOpts.AgentLabelSelector, "agentLabelSelector", platformOpts.AgentLabelSelector, "A LabelSelector for selecting Agents according to their labels, e.g., 'size=large,zone notin (az1,az2)'")
cmd.RunE = coreOpts.CreateRunFunc(platformOpts)

return cmd
return cmd, platformOpts
}

func (o *AgentPlatformCreateOptions) UpdateNodePool(_ context.Context, nodePool *hyperv1.NodePool, _ *hyperv1.HostedCluster, _ crclient.Client) error {
Expand Down
122 changes: 122 additions & 0 deletions cmd/nodepool/agent/create_test.go
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)
}
})
Comment thread
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())
}
}
Comment thread
mehabhalodiya marked this conversation as resolved.
Loading